mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
39 lines
2.0 KiB
JavaScript
39 lines
2.0 KiB
JavaScript
'use strict';
|
|
const { DataTypes } = require('sequelize');
|
|
const sequelize = require('../../config/db.config');
|
|
const { Course } = require('./courses.mdl');
|
|
|
|
// Polymorphic target — a product is sold against a Course (purchasable_type +
|
|
// purchasable_id). Unit/Lesson individual purchase was removed; the schema
|
|
// stays polymorphic (purchasable_type/purchasable_id, not a course_id FK)
|
|
// since existing rows and course_purchases still key off it. See
|
|
// utils/purchasable.util.js for the type -> model/checkout-path resolver used
|
|
// by every consumer (access checks, admin CRUD, checkout).
|
|
//
|
|
// constraints: false below because purchasable_id doesn't point at a single
|
|
// table — referential integrity across the three possible targets is
|
|
// enforced at the application layer only (see
|
|
// 20270101000078-generalize-products-purchasable.js).
|
|
const mdl_Product = sequelize.define('Product', {
|
|
id: { type: DataTypes.BIGINT, primaryKey: true, autoIncrement: true },
|
|
purchasable_type: { type: DataTypes.STRING(10), allowNull: false, defaultValue: 'course' },
|
|
purchasable_id: { type: DataTypes.BIGINT, allowNull: false },
|
|
name: { type: DataTypes.STRING(200), allowNull: false },
|
|
description: { type: DataTypes.TEXT, allowNull: true },
|
|
price: { type: DataTypes.DECIMAL(10, 2), allowNull: false },
|
|
currency: { type: DataTypes.STRING(10), allowNull: false, defaultValue: 'USD' },
|
|
access_days: { type: DataTypes.INTEGER, allowNull: true },
|
|
is_active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true },
|
|
}, {
|
|
tableName: 'products',
|
|
timestamps: true,
|
|
paranoid: true,
|
|
});
|
|
|
|
// Scoped hasOne — Sequelize automatically adds the matching purchasable_type
|
|
// filter to the join, so existing `include: [{ model: mdl_Product, as:
|
|
// 'product' }]` call sites on Course keep working unchanged.
|
|
Course.hasOne(mdl_Product, { foreignKey: 'purchasable_id', constraints: false, scope: { purchasable_type: 'course' }, as: 'product' });
|
|
|
|
module.exports = mdl_Product;
|