assets and tier plans revamp

Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
2026-08-01 17:44:25 +08:00
parent 39c3c4566b
commit cae958b5d5
41 changed files with 1271 additions and 165 deletions
+21 -5
View File
@@ -2,10 +2,22 @@
const { DataTypes } = require('sequelize');
const sequelize = require('../../config/db.config');
const { Course } = require('./courses.mdl');
const Unit = require('./units.mdl');
const Lesson = require('./lessons.mdl');
// Polymorphic target — a product is sold against exactly one Course, Unit, or
// Lesson (purchasable_type + purchasable_id), not just courses. 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 },
course_id: { type: DataTypes.BIGINT, allowNull: false },
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 },
@@ -18,8 +30,12 @@ const mdl_Product = sequelize.define('Product', {
paranoid: true,
});
// A course has one product listing; a product belongs to one course
mdl_Product.belongsTo(Course, { foreignKey: 'course_id', as: 'course' });
Course.hasOne(mdl_Product, { foreignKey: 'course_id', as: 'product' });
// Scoped hasOne per target type — 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' });
Unit.hasOne(mdl_Product, { foreignKey: 'purchasable_id', constraints: false, scope: { purchasable_type: 'unit' }, as: 'product' });
Lesson.hasOne(mdl_Product, { foreignKey: 'purchasable_id', constraints: false, scope: { purchasable_type: 'lesson' }, as: 'product' });
module.exports = mdl_Product;