/*********************************************************************************************************************************************************************** * File Name: purchasable.util.js * Type of Program: Utility * Description: Resolves a Product's polymorphic target (purchasable_type + * purchasable_id → Course | Unit | Lesson) and the checkout * route for it. Products.mdl.js can't express a single * Sequelize association across three different target models, * so this is the shared lookup every consumer (access checks, * admin product CRUD, client checkout) goes through instead. * * Author: Kenneth Obsequio (@lash0000) * Date Created: Aug. 1, 2026 ***********************************************************************************************************************************************************************/ 'use strict'; const { Course } = require('../models/courses/courses.mdl'); const Unit = require('../models/courses/units.mdl'); const Lesson = require('../models/courses/lessons.mdl'); const TYPE_MODELS = { course: Course, unit: Unit, lesson: Lesson }; const TYPE_PK = { course: 'course_id', unit: 'unit_id', lesson: 'lesson_id' }; function getPurchasableModel(type) { return TYPE_MODELS[type] ?? null; } // Returns the target row (needs at least uuid/course_id + subscription for // callers), or null if the type is unknown or the row doesn't exist. async function resolvePurchasable(type, id) { const Model = TYPE_MODELS[type]; if (!Model) return null; return Model.findOne({ where: { [TYPE_PK[type]]: id, deletedAt: null } }); } // Course checkout stays keyed by numeric course_id (existing convention); // Unit/Lesson checkout is keyed by uuid, matching how /units/:uuid and // /lessons/:uuid already work everywhere else in the standalone consumption paths. function checkoutPath(type, record) { if (type === 'course') return `/course/${record.course_id}/checkout`; if (type === 'unit') return `/units/${record.uuid}/checkout`; if (type === 'lesson') return `/lessons/${record.uuid}/checkout`; return null; } module.exports = { getPurchasableModel, resolvePurchasable, checkoutPath };