mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
92 lines
4.3 KiB
JavaScript
92 lines
4.3 KiB
JavaScript
'use strict';
|
|
const mdl_Product = require('../../models/courses/products.mdl');
|
|
const mdl_Category = require('../../models/courses/categories.mdl');
|
|
const { Course, CourseProductCategory: mdl_CourseProductCategory } = require('../../models/courses/courses.mdl');
|
|
const R = require('../../utils/response.util');
|
|
const logActivity = require('../../utils/logActivity.util');
|
|
|
|
// ─── PRODUCT (per course) ─────────────────────────────────────────────────────
|
|
|
|
exports.getCourseProduct = async (req, res) => {
|
|
try {
|
|
const product = await mdl_Product.findOne({ where: { course_id: req.params.courseId }, paranoid: false });
|
|
return R.success(res, 'Product retrieved.', product ?? null);
|
|
} catch (err) {
|
|
console.error('[ADMIN][PRODUCTS][GET]', err);
|
|
return R.error(res, 'Could not retrieve product.', 500);
|
|
}
|
|
};
|
|
|
|
exports.upsertCourseProduct = async (req, res) => {
|
|
try {
|
|
const { courseId } = req.params;
|
|
const { name, description, price, currency, access_days, is_active } = req.body;
|
|
if (!name || price == null) return R.error(res, 'name and price are required.', 400);
|
|
|
|
const existing = await mdl_Product.findOne({ where: { course_id: courseId }, paranoid: false });
|
|
|
|
if (existing) {
|
|
if (existing.deletedAt) await existing.restore();
|
|
await existing.update({ name, description: description ?? null, price, currency: currency ?? 'USD', access_days: access_days ?? null, is_active: is_active ?? true });
|
|
logActivity(req.user?.user_id, 'upsert_course_product', { entityType: 'product', details: { course_id: courseId, name } });
|
|
return R.success(res, 'Product updated.', existing);
|
|
}
|
|
|
|
const product = await mdl_Product.create({ course_id: courseId, name, description: description ?? null, price, currency: currency ?? 'USD', access_days: access_days ?? null, is_active: is_active ?? true });
|
|
logActivity(req.user?.user_id, 'upsert_course_product', { entityType: 'product', entityId: product.product_id, details: { course_id: courseId, name } });
|
|
return R.success(res, 'Product created.', product, 201);
|
|
} catch (err) {
|
|
console.error('[ADMIN][PRODUCTS][UPSERT]', err);
|
|
return R.error(res, 'Could not save product.', 500);
|
|
}
|
|
};
|
|
|
|
exports.removeCourseProduct = async (req, res) => {
|
|
try {
|
|
const product = await mdl_Product.findOne({ where: { course_id: req.params.courseId } });
|
|
if (!product) return R.error(res, 'Product not found.', 404);
|
|
await product.destroy();
|
|
logActivity(req.user?.user_id, 'remove_course_product', { entityType: 'product', details: { course_id: req.params.courseId } });
|
|
return R.success(res, 'Product removed.');
|
|
} catch (err) {
|
|
console.error('[ADMIN][PRODUCTS][REMOVE]', err);
|
|
return R.error(res, 'Could not remove product.', 500);
|
|
}
|
|
};
|
|
|
|
// ─── CATEGORIES (per course) ──────────────────────────────────────────────────
|
|
|
|
exports.getCourseCategories = async (req, res) => {
|
|
try {
|
|
const course = await Course.findByPk(req.params.courseId, {
|
|
include: [{ model: mdl_Category, as: 'categories', through: { attributes: [] } }],
|
|
});
|
|
if (!course) return R.error(res, 'Course not found.', 404);
|
|
return R.success(res, 'Course categories retrieved.', course.categories ?? []);
|
|
} catch (err) {
|
|
console.error('[ADMIN][PRODUCTS][GET CATEGORIES]', err);
|
|
return R.error(res, 'Could not retrieve course categories.', 500);
|
|
}
|
|
};
|
|
|
|
exports.syncCourseCategories = async (req, res) => {
|
|
try {
|
|
const { courseId } = req.params;
|
|
const { category_ids = [] } = req.body;
|
|
|
|
await mdl_CourseProductCategory.destroy({ where: { course_id: courseId } });
|
|
|
|
if (category_ids.length > 0) {
|
|
await mdl_CourseProductCategory.bulkCreate(
|
|
category_ids.map((id) => ({ course_id: courseId, category_id: id }))
|
|
);
|
|
}
|
|
|
|
logActivity(req.user?.user_id, 'sync_course_categories', { entityType: 'product', details: { course_id: courseId, category_ids, count: category_ids.length } });
|
|
return R.success(res, 'Course categories updated.');
|
|
} catch (err) {
|
|
console.error('[ADMIN][PRODUCTS][SYNC CATEGORIES]', err);
|
|
return R.error(res, 'Could not sync course categories.', 500);
|
|
}
|
|
};
|