fix(payments): enhance duplicate payment check query and auto-populate course
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// /components/payments/paymentService.js
|
||||
'use strict';
|
||||
|
||||
const mongoose = require('mongoose');
|
||||
const Payment = require('./paymentModel');
|
||||
const Transaction = require('./transactionModel');
|
||||
const AppError = require('../../utils/AppError');
|
||||
@@ -310,6 +311,16 @@ const createPayment = async (body, actorId = null) => {
|
||||
const incomingTransactions = Array.isArray(payload.transactions) ? payload.transactions : null;
|
||||
delete payload.transactions;
|
||||
|
||||
if (!payload.course && payload.classes && payload.classes.length) {
|
||||
const firstClassId = Array.isArray(payload.classes) ? payload.classes[0] : payload.classes;
|
||||
if (firstClassId) {
|
||||
const firstClass = await Class.findById(firstClassId).select('course').lean();
|
||||
if (firstClass?.course) {
|
||||
payload.course = firstClass.course;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const payment = await Payment.create({
|
||||
...payload,
|
||||
discount: normalizeDiscount(payload.discount, payload.amount),
|
||||
@@ -585,11 +596,11 @@ const checkDuplicatePayment = async (query = {}) => {
|
||||
isDeleted: { $ne: true }
|
||||
};
|
||||
|
||||
const userConditions = [userId];
|
||||
if (mongoose.isValidObjectId(userId)) {
|
||||
filter.user = new mongoose.Types.ObjectId(String(userId));
|
||||
} else {
|
||||
filter.user = userId;
|
||||
userConditions.push(new mongoose.Types.ObjectId(String(userId)));
|
||||
}
|
||||
filter.user = { $in: userConditions };
|
||||
|
||||
const rawClasses = [];
|
||||
if (classId) rawClasses.push(classId);
|
||||
@@ -599,16 +610,27 @@ const checkDuplicatePayment = async (query = {}) => {
|
||||
}
|
||||
|
||||
if (rawClasses.length) {
|
||||
const classObjectIds = rawClasses.map((id) => {
|
||||
return mongoose.isValidObjectId(id) ? new mongoose.Types.ObjectId(String(id)) : id;
|
||||
const classObjectIds = [];
|
||||
rawClasses.forEach((id) => {
|
||||
classObjectIds.push(id);
|
||||
if (mongoose.isValidObjectId(id)) {
|
||||
classObjectIds.push(new mongoose.Types.ObjectId(String(id)));
|
||||
}
|
||||
});
|
||||
|
||||
const classDocs = await Class.find({ _id: { $in: classObjectIds } }).select('course').lean();
|
||||
const courseIds = classDocs.map((c) => c.course).filter(Boolean);
|
||||
const courseObjectIds = [];
|
||||
courseIds.forEach((id) => {
|
||||
courseObjectIds.push(id);
|
||||
if (mongoose.isValidObjectId(id)) {
|
||||
courseObjectIds.push(new mongoose.Types.ObjectId(String(id)));
|
||||
}
|
||||
});
|
||||
|
||||
const orConditions = [{ classes: { $in: classObjectIds } }];
|
||||
if (courseIds.length) {
|
||||
orConditions.push({ course: { $in: courseIds } });
|
||||
if (courseObjectIds.length) {
|
||||
orConditions.push({ course: { $in: courseObjectIds } });
|
||||
}
|
||||
filter.$or = orConditions;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user