From 15910320d0a99f345c5cb2e0f4bb8b203b7c1859 Mon Sep 17 00:00:00 2001 From: Manon Palin Date: Mon, 16 Nov 2026 15:13:14 +0100 Subject: [PATCH 1/2] COM-4329 - flag old courses to paid --- src/helpers/courses.js | 15 +++- tests/unit/helpers/courses.test.js | 116 +++++++++++++++++++++++++++-- 2 files changed, 125 insertions(+), 6 deletions(-) diff --git a/src/helpers/courses.js b/src/helpers/courses.js index 6d428c5496..7f6787cea5 100644 --- a/src/helpers/courses.js +++ b/src/helpers/courses.js @@ -2330,6 +2330,10 @@ exports.getCourseTrainerSalary = (course) => { return salary ? NumbersHelper.toNumber(NumbersHelper.multiply(salary.price, salary.count)) : undefined; }; +// Trainer billing was not tracked in the app before October 1st, 2026: +// courses ended before this date are considered as paid +const TRAINER_PAYMENTS_START_DATE = '2026-09-30T22:00:00.000Z'; + exports.listTrainersBilling = async (query) => { let courseIdsWithSlotsOrBillsInPeriod; if (query.startDate) { @@ -2371,6 +2375,14 @@ exports.listTrainersBilling = async (query) => { .lean(); const billsByTrainerAndCourse = groupBy(trainerBills, bill => `${bill.trainer}-${bill.course}`); + const courseSlots = await CourseSlot + .find({ course: { $in: courses.map(c => c._id) } }, { course: 1, endDate: 1 }) + .lean(); + const isCourseEndedBeforePaymentsStart = mapValues( + groupBy(courseSlots, 'course'), + slots => slots.every(slot => slot.endDate && CompaniDate(slot.endDate).isBefore(TRAINER_PAYMENTS_START_DATE)) + ); + const trainers = query.trainer ? [courses[0].trainers.find(t => UtilsHelper.areObjectIdsEquals(t._id, query.trainer))] : uniqBy(courses.flatMap(c => c.trainers), t => t._id.toHexString()); @@ -2396,7 +2408,8 @@ exports.listTrainersBilling = async (query) => { paidAmount = NumbersHelper.toNumber(paidAmount); let status = INVOICED; - if (NumbersHelper.isEqualTo(invoicedAmount, 0)) status = NOT_INVOICED; + if (isCourseEndedBeforePaymentsStart[course._id]) status = PAID; + else if (NumbersHelper.isEqualTo(invoicedAmount, 0)) status = NOT_INVOICED; else if (plannedSalary !== undefined && NumbersHelper.isLessThan(invoicedAmount, plannedSalary)) { status = PARTIALLY_INVOICED; } else if (NumbersHelper.isEqualTo(invoicedAmount, paidAmount)) { diff --git a/tests/unit/helpers/courses.test.js b/tests/unit/helpers/courses.test.js index 0ce3540e33..4b7669a984 100644 --- a/tests/unit/helpers/courses.test.js +++ b/tests/unit/helpers/courses.test.js @@ -11487,6 +11487,7 @@ describe('listTrainersBilling', () => { courseFind.returns(SinonMongoose.stubChainedQueries(courses, ['populate', 'sort', 'lean'])); trainerBillFind.returns(SinonMongoose.stubChainedQueries(trainerBills, ['populate', 'lean'])); + courseSlotFind.returns(SinonMongoose.stubChainedQueries([], ['lean'])); const result = await CourseHelper.listTrainersBilling({ trainer: trainerId }); @@ -11553,7 +11554,86 @@ describe('listTrainersBilling', () => { { query: 'lean' }, ] ); - sinon.assert.notCalled(courseSlotFind); + SinonMongoose.calledOnceWithExactly( + courseSlotFind, + [ + { + query: 'find', + args: [{ course: { $in: courses.map(c => c._id) } }, { course: 1, endDate: 1 }], + }, + { query: 'lean' }, + ] + ); + }); + + it('should consider courses ended before trainer payments start as paid', async () => { + const trainerId = new ObjectId(); + const trainer = { _id: trainerId, identity: { firstname: 'Jean', lastname: 'Dupont' } }; + const billingPurchaseList = [{ billingItem: { type: TRAINER_SALARY }, price: 100, count: 1 }]; + const courses = [ + { _id: new ObjectId(), trainers: [trainer], tradeName: 'ended before', billingPurchaseList }, + { _id: new ObjectId(), trainers: [trainer], tradeName: 'ended after with bill', billingPurchaseList }, + { _id: new ObjectId(), trainers: [trainer], tradeName: 'ended after', billingPurchaseList }, + { _id: new ObjectId(), trainers: [trainer], tradeName: 'with slot to plan', billingPurchaseList }, + ]; + const trainerBills = [ + { + course: courses[1]._id, + trainer: trainerId, + amount: 40, + payment: { status: 'pending' }, + submittedAt: '2024-01-01T08:00:00.000Z', + }, + ]; + const courseSlots = [ + { course: courses[0]._id, endDate: '2026-09-01T10:00:00.000Z' }, + { course: courses[0]._id, endDate: '2026-09-30T21:00:00.000Z' }, + { course: courses[1]._id, endDate: '2026-11-01T10:00:00.000Z' }, + { course: courses[2]._id, endDate: '2026-09-01T10:00:00.000Z' }, + { course: courses[2]._id, endDate: '2026-10-01T08:00:00.000Z' }, + { course: courses[3]._id, endDate: '2026-09-01T10:00:00.000Z' }, + { course: courses[3]._id }, + ]; + + courseFind.returns(SinonMongoose.stubChainedQueries(courses, ['populate', 'sort', 'lean'])); + trainerBillFind.returns(SinonMongoose.stubChainedQueries(trainerBills, ['populate', 'lean'])); + courseSlotFind.returns(SinonMongoose.stubChainedQueries(courseSlots, ['lean'])); + + const result = await CourseHelper.listTrainersBilling({ trainer: trainerId }); + + expect(result).toEqual([{ + ...trainer, + courses: [ + { + ...omit(courses[0], ['billingPurchaseList']), + plannedSalary: 100, + invoicedAmount: 0, + paidAmount: 0, + status: 'paid', + }, + { + ...omit(courses[1], ['billingPurchaseList']), + plannedSalary: 100, + invoicedAmount: 40, + paidAmount: 0, + status: 'partially_invoiced', + }, + { + ...omit(courses[2], ['billingPurchaseList']), + plannedSalary: 100, + invoicedAmount: 0, + paidAmount: 0, + status: 'not_invoiced', + }, + { + ...omit(courses[3], ['billingPurchaseList']), + plannedSalary: 100, + invoicedAmount: 0, + paidAmount: 0, + status: 'not_invoiced', + }, + ], + }]); }); it('should only return courses with slots or trainer bills in period', async () => { @@ -11582,10 +11662,11 @@ describe('listTrainersBilling', () => { { course: courses[1]._id, submittedAt: '2024-02-15T08:00:00.000Z', trainer: trainerId, amount: 100 }, ]; - courseSlotFind.returns(SinonMongoose.stubChainedQueries(courseSlotsInPeriod, ['lean'])); + courseSlotFind.onCall(0).returns(SinonMongoose.stubChainedQueries(courseSlotsInPeriod, ['lean'])); trainerBillFind.onCall(0).returns(SinonMongoose.stubChainedQueries(trainerBillsInPeriod, ['lean'])); courseFind.returns(SinonMongoose.stubChainedQueries(courses, ['populate', 'sort', 'lean'])); trainerBillFind.onCall(1).returns(SinonMongoose.stubChainedQueries([])); + courseSlotFind.onCall(1).returns(SinonMongoose.stubChainedQueries([], ['lean'])); const result = await CourseHelper.listTrainersBilling(query); @@ -11602,7 +11683,7 @@ describe('listTrainersBilling', () => { { ...courses[1], invoicedAmount: 0, paidAmount: 0, status: 'not_invoiced' }, ], }]); - SinonMongoose.calledOnceWithExactly( + SinonMongoose.calledWithExactly( courseSlotFind, [ { @@ -11613,7 +11694,22 @@ describe('listTrainersBilling', () => { ], }, { query: 'lean' }, - ] + ], + 0 + ); + SinonMongoose.calledWithExactly( + courseSlotFind, + [ + { + query: 'find', + args: [ + { course: { $in: [courses[0]._id, courses[1]._id] } }, + { course: 1, endDate: 1 }, + ], + }, + { query: 'lean' }, + ], + 1 ); SinonMongoose.calledWithExactly( trainerBillFind, @@ -11716,6 +11812,7 @@ describe('listTrainersBilling', () => { courseFind.returns(SinonMongoose.stubChainedQueries(courses, ['populate', 'sort', 'lean'])); trainerBillFind.returns(SinonMongoose.stubChainedQueries(trainerBills, ['populate', 'lean'])); + courseSlotFind.returns(SinonMongoose.stubChainedQueries([], ['lean'])); const result = await CourseHelper.listTrainersBilling({}); @@ -11779,6 +11876,15 @@ describe('listTrainersBilling', () => { { query: 'lean' }, ] ); - sinon.assert.notCalled(courseSlotFind); + SinonMongoose.calledOnceWithExactly( + courseSlotFind, + [ + { + query: 'find', + args: [{ course: { $in: courses.map(c => c._id) } }, { course: 1, endDate: 1 }], + }, + { query: 'lean' }, + ] + ); }); }); From 1f05524f265ed4a934af88ba88212bd645ad1d07 Mon Sep 17 00:00:00 2001 From: Manon Palin Date: Thu, 19 Nov 2026 11:52:20 +0100 Subject: [PATCH 2/2] COM-4329 - flag old archived courses to paid --- src/helpers/courses.js | 23 +++----- tests/unit/helpers/courses.test.js | 89 +++++++++++------------------- 2 files changed, 41 insertions(+), 71 deletions(-) diff --git a/src/helpers/courses.js b/src/helpers/courses.js index 7f6787cea5..2230f6611c 100644 --- a/src/helpers/courses.js +++ b/src/helpers/courses.js @@ -2330,9 +2330,9 @@ exports.getCourseTrainerSalary = (course) => { return salary ? NumbersHelper.toNumber(NumbersHelper.multiply(salary.price, salary.count)) : undefined; }; -// Trainer billing was not tracked in the app before October 1st, 2026: -// courses ended before this date are considered as paid -const TRAINER_PAYMENTS_START_DATE = '2026-09-30T22:00:00.000Z'; +// Trainer billing was not tracked in the app before September 1st, 2026: +// courses archived before this date are considered as paid +const TRAINER_PAYMENTS_START_DATE = '2026-08-31T22:00:00.000Z'; exports.listTrainersBilling = async (query) => { let courseIdsWithSlotsOrBillsInPeriod; @@ -2356,7 +2356,7 @@ exports.listTrainersBilling = async (query) => { ...(query.trainer ? { trainers: query.trainer } : { 'trainers.0': { $exists: true } }), type: { $nin: [SINGLE, INTER_B2C] }, }, - { trainers: 1, createdAt: 1, billingPurchaseList: 1, type: 1, misc: 1, tradeName: 1 } + { trainers: 1, createdAt: 1, billingPurchaseList: 1, type: 1, misc: 1, tradeName: 1, archivedAt: 1 } ) .populate({ path: 'companies', select: 'name' }) .populate({ path: 'subProgram', select: 'program', populate: { path: 'program', select: 'name' } }) @@ -2375,14 +2375,6 @@ exports.listTrainersBilling = async (query) => { .lean(); const billsByTrainerAndCourse = groupBy(trainerBills, bill => `${bill.trainer}-${bill.course}`); - const courseSlots = await CourseSlot - .find({ course: { $in: courses.map(c => c._id) } }, { course: 1, endDate: 1 }) - .lean(); - const isCourseEndedBeforePaymentsStart = mapValues( - groupBy(courseSlots, 'course'), - slots => slots.every(slot => slot.endDate && CompaniDate(slot.endDate).isBefore(TRAINER_PAYMENTS_START_DATE)) - ); - const trainers = query.trainer ? [courses[0].trainers.find(t => UtilsHelper.areObjectIdsEquals(t._id, query.trainer))] : uniqBy(courses.flatMap(c => c.trainers), t => t._id.toHexString()); @@ -2407,8 +2399,11 @@ exports.listTrainersBilling = async (query) => { invoicedAmount = NumbersHelper.toNumber(invoicedAmount); paidAmount = NumbersHelper.toNumber(paidAmount); + const isArchivedBeforePaymentsStart = !!course.archivedAt && + CompaniDate(course.archivedAt).isBefore(TRAINER_PAYMENTS_START_DATE); + let status = INVOICED; - if (isCourseEndedBeforePaymentsStart[course._id]) status = PAID; + if (isArchivedBeforePaymentsStart) status = PAID; else if (NumbersHelper.isEqualTo(invoicedAmount, 0)) status = NOT_INVOICED; else if (plannedSalary !== undefined && NumbersHelper.isLessThan(invoicedAmount, plannedSalary)) { status = PARTIALLY_INVOICED; @@ -2417,7 +2412,7 @@ exports.listTrainersBilling = async (query) => { } trainerCourses.push({ - ...omit(course, ['billingPurchaseList']), + ...omit(course, ['billingPurchaseList', 'archivedAt']), ...(plannedSalary !== undefined && { plannedSalary }), invoicedAmount, paidAmount, diff --git a/tests/unit/helpers/courses.test.js b/tests/unit/helpers/courses.test.js index 4b7669a984..300d1d57e2 100644 --- a/tests/unit/helpers/courses.test.js +++ b/tests/unit/helpers/courses.test.js @@ -11487,7 +11487,6 @@ describe('listTrainersBilling', () => { courseFind.returns(SinonMongoose.stubChainedQueries(courses, ['populate', 'sort', 'lean'])); trainerBillFind.returns(SinonMongoose.stubChainedQueries(trainerBills, ['populate', 'lean'])); - courseSlotFind.returns(SinonMongoose.stubChainedQueries([], ['lean'])); const result = await CourseHelper.listTrainersBilling({ trainer: trainerId }); @@ -11526,7 +11525,7 @@ describe('listTrainersBilling', () => { query: 'find', args: [ { trainers: trainerId, type: { $nin: ['single', 'inter_b2c'] } }, - { trainers: 1, createdAt: 1, billingPurchaseList: 1, type: 1, misc: 1, tradeName: 1 }, + { trainers: 1, createdAt: 1, billingPurchaseList: 1, type: 1, misc: 1, tradeName: 1, archivedAt: 1 }, ], }, { query: 'populate', args: [{ path: 'companies', select: 'name' }] }, @@ -11554,27 +11553,36 @@ describe('listTrainersBilling', () => { { query: 'lean' }, ] ); - SinonMongoose.calledOnceWithExactly( - courseSlotFind, - [ - { - query: 'find', - args: [{ course: { $in: courses.map(c => c._id) } }, { course: 1, endDate: 1 }], - }, - { query: 'lean' }, - ] - ); + sinon.assert.notCalled(courseSlotFind); }); - it('should consider courses ended before trainer payments start as paid', async () => { + it('should consider courses archived before trainer payments start as paid', async () => { const trainerId = new ObjectId(); const trainer = { _id: trainerId, identity: { firstname: 'Jean', lastname: 'Dupont' } }; const billingPurchaseList = [{ billingItem: { type: TRAINER_SALARY }, price: 100, count: 1 }]; const courses = [ - { _id: new ObjectId(), trainers: [trainer], tradeName: 'ended before', billingPurchaseList }, - { _id: new ObjectId(), trainers: [trainer], tradeName: 'ended after with bill', billingPurchaseList }, - { _id: new ObjectId(), trainers: [trainer], tradeName: 'ended after', billingPurchaseList }, - { _id: new ObjectId(), trainers: [trainer], tradeName: 'with slot to plan', billingPurchaseList }, + { + _id: new ObjectId(), + trainers: [trainer], + tradeName: 'archived before', + billingPurchaseList, + archivedAt: '2026-08-31T21:00:00.000Z', + }, + { + _id: new ObjectId(), + trainers: [trainer], + tradeName: 'archived after with bill', + billingPurchaseList, + archivedAt: '2026-09-01T08:00:00.000Z', + }, + { + _id: new ObjectId(), + trainers: [trainer], + tradeName: 'archived on september 1st', + billingPurchaseList, + archivedAt: '2026-08-31T22:00:00.000Z', + }, + { _id: new ObjectId(), trainers: [trainer], tradeName: 'not archived', billingPurchaseList }, ]; const trainerBills = [ { @@ -11585,19 +11593,9 @@ describe('listTrainersBilling', () => { submittedAt: '2024-01-01T08:00:00.000Z', }, ]; - const courseSlots = [ - { course: courses[0]._id, endDate: '2026-09-01T10:00:00.000Z' }, - { course: courses[0]._id, endDate: '2026-09-30T21:00:00.000Z' }, - { course: courses[1]._id, endDate: '2026-11-01T10:00:00.000Z' }, - { course: courses[2]._id, endDate: '2026-09-01T10:00:00.000Z' }, - { course: courses[2]._id, endDate: '2026-10-01T08:00:00.000Z' }, - { course: courses[3]._id, endDate: '2026-09-01T10:00:00.000Z' }, - { course: courses[3]._id }, - ]; courseFind.returns(SinonMongoose.stubChainedQueries(courses, ['populate', 'sort', 'lean'])); trainerBillFind.returns(SinonMongoose.stubChainedQueries(trainerBills, ['populate', 'lean'])); - courseSlotFind.returns(SinonMongoose.stubChainedQueries(courseSlots, ['lean'])); const result = await CourseHelper.listTrainersBilling({ trainer: trainerId }); @@ -11605,21 +11603,21 @@ describe('listTrainersBilling', () => { ...trainer, courses: [ { - ...omit(courses[0], ['billingPurchaseList']), + ...omit(courses[0], ['billingPurchaseList', 'archivedAt']), plannedSalary: 100, invoicedAmount: 0, paidAmount: 0, status: 'paid', }, { - ...omit(courses[1], ['billingPurchaseList']), + ...omit(courses[1], ['billingPurchaseList', 'archivedAt']), plannedSalary: 100, invoicedAmount: 40, paidAmount: 0, status: 'partially_invoiced', }, { - ...omit(courses[2], ['billingPurchaseList']), + ...omit(courses[2], ['billingPurchaseList', 'archivedAt']), plannedSalary: 100, invoicedAmount: 0, paidAmount: 0, @@ -11634,6 +11632,7 @@ describe('listTrainersBilling', () => { }, ], }]); + sinon.assert.notCalled(courseSlotFind); }); it('should only return courses with slots or trainer bills in period', async () => { @@ -11666,7 +11665,6 @@ describe('listTrainersBilling', () => { trainerBillFind.onCall(0).returns(SinonMongoose.stubChainedQueries(trainerBillsInPeriod, ['lean'])); courseFind.returns(SinonMongoose.stubChainedQueries(courses, ['populate', 'sort', 'lean'])); trainerBillFind.onCall(1).returns(SinonMongoose.stubChainedQueries([])); - courseSlotFind.onCall(1).returns(SinonMongoose.stubChainedQueries([], ['lean'])); const result = await CourseHelper.listTrainersBilling(query); @@ -11697,20 +11695,6 @@ describe('listTrainersBilling', () => { ], 0 ); - SinonMongoose.calledWithExactly( - courseSlotFind, - [ - { - query: 'find', - args: [ - { course: { $in: [courses[0]._id, courses[1]._id] } }, - { course: 1, endDate: 1 }, - ], - }, - { query: 'lean' }, - ], - 1 - ); SinonMongoose.calledWithExactly( trainerBillFind, [ @@ -11736,7 +11720,7 @@ describe('listTrainersBilling', () => { trainers: trainerId, type: { $nin: ['single', 'inter_b2c'] }, }, - { trainers: 1, createdAt: 1, billingPurchaseList: 1, type: 1, misc: 1, tradeName: 1 }, + { trainers: 1, createdAt: 1, billingPurchaseList: 1, type: 1, misc: 1, tradeName: 1, archivedAt: 1 }, ], }, { query: 'populate', args: [{ path: 'companies', select: 'name' }] }, @@ -11851,7 +11835,7 @@ describe('listTrainersBilling', () => { query: 'find', args: [ { 'trainers.0': { $exists: true }, type: { $nin: ['single', 'inter_b2c'] } }, - { trainers: 1, createdAt: 1, billingPurchaseList: 1, type: 1, misc: 1, tradeName: 1 }, + { trainers: 1, createdAt: 1, billingPurchaseList: 1, type: 1, misc: 1, tradeName: 1, archivedAt: 1 }, ], }, { query: 'populate', args: [{ path: 'companies', select: 'name' }] }, @@ -11876,15 +11860,6 @@ describe('listTrainersBilling', () => { { query: 'lean' }, ] ); - SinonMongoose.calledOnceWithExactly( - courseSlotFind, - [ - { - query: 'find', - args: [{ course: { $in: courses.map(c => c._id) } }, { course: 1, endDate: 1 }], - }, - { query: 'lean' }, - ] - ); + sinon.assert.notCalled(courseSlotFind); }); });