mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user