Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
2026-08-13 19:58:52 +08:00
parent 9018d6d158
commit 2e9c2ad43f
23 changed files with 954 additions and 287 deletions
+7 -10
View File
@@ -2,11 +2,11 @@
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
// 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).
//
@@ -30,12 +30,9 @@ const mdl_Product = sequelize.define('Product', {
paranoid: true,
});
// 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.
// 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' });
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;