feat: add soft delete for classes with cascade to sessions and linked records

This commit is contained in:
2026-08-21 13:19:50 +03:30
parent 5b56171328
commit e34fd25af7
15 changed files with 184 additions and 33 deletions
@@ -33,7 +33,7 @@ const normalizeId = (value) => {
/** Revenue collected/owed for a set of classIds, derived from Payment records (one payment ~ one class enrollment). */
const getRevenueByClass = async (classIds) => {
const payments = await Payment.find({ classes: { $in: classIds } })
const payments = await Payment.find({ classes: { $in: classIds }, isDeleted: { $ne: true } })
.select('classes amount discount paidAmount')
.lean();
@@ -53,7 +53,7 @@ const getRevenueByClass = async (classIds) => {
};
const getSessionCountsByClass = async (classIds) => {
const sessions = await Session.find({ class: { $in: classIds } }).select('class status').lean();
const sessions = await Session.find({ class: { $in: classIds }, isDeleted: { $ne: true } }).select('class status').lean();
const byClass = new Map(classIds.map((id) => [String(id), { total: 0, held: 0, scheduled: 0, cancelled: 0 }]));
for (const session of sessions) {
const key = String(session.class);
@@ -224,7 +224,7 @@ const getSessionReport = async (sessionId) => {
const getRangeReport = async (query = {}) => {
const { start, end } = resolveDateRange(query);
const classFilter = { isActive: { $ne: false } };
const classFilter = { isActive: { $ne: false }, isDeleted: { $ne: true } };
if (query.classId) classFilter._id = query.classId;
const classes = await Class.find(classFilter)
@@ -248,7 +248,7 @@ const getRangeReport = async (query = {}) => {
};
}
const sessions = await Session.find({ class: { $in: classIds }, day: { $gte: start, $lte: end } })
const sessions = await Session.find({ class: { $in: classIds }, day: { $gte: start, $lte: end }, isDeleted: { $ne: true } })
.select('class status day')
.lean();
@@ -259,7 +259,7 @@ const getRangeReport = async (query = {}) => {
heldByClass.set(key, (heldByClass.get(key) || 0) + 1);
}
const payments = await Payment.find({ classes: { $in: classIds } }).select('_id classes').lean();
const payments = await Payment.find({ classes: { $in: classIds }, isDeleted: { $ne: true } }).select('_id classes').lean();
const paymentClassByPaymentId = new Map();
const paymentIds = [];
for (const payment of payments) {