diff --git a/src/helpers/courses.js b/src/helpers/courses.js index 4634c71f31..6d428c5496 100644 --- a/src/helpers/courses.js +++ b/src/helpers/courses.js @@ -2331,18 +2331,24 @@ exports.getCourseTrainerSalary = (course) => { }; exports.listTrainersBilling = async (query) => { - let courseIdsWithSlotsInPeriod; + let courseIdsWithSlotsOrBillsInPeriod; if (query.startDate) { const slotsInPeriod = await CourseSlot .find({ startDate: { $gte: query.startDate }, endDate: { $lte: query.endDate } }, { course: 1 }) .lean(); - courseIdsWithSlotsInPeriod = uniqBy(slotsInPeriod.map(slot => slot.course), id => id.toHexString()); + const billsInPeriod = await TrainerBill + .find({ submittedAt: { $gte: query.startDate, $lte: query.endDate }, course: { $exists: true } }, { course: 1 }) + .lean(); + courseIdsWithSlotsOrBillsInPeriod = uniqBy( + [...slotsInPeriod.map(slot => slot.course), ...billsInPeriod.map(bill => bill.course)], + id => id.toHexString() + ); } const courses = await Course .find( { - ...(courseIdsWithSlotsInPeriod && { _id: { $in: courseIdsWithSlotsInPeriod } }), + ...(courseIdsWithSlotsOrBillsInPeriod && { _id: { $in: courseIdsWithSlotsOrBillsInPeriod } }), ...(query.trainer ? { trainers: query.trainer } : { 'trainers.0': { $exists: true } }), type: { $nin: [SINGLE, INTER_B2C] }, }, @@ -2359,7 +2365,7 @@ exports.listTrainersBilling = async (query) => { const trainerBills = await TrainerBill .find( { course: { $in: courses.map(c => c._id) }, ...(query.trainer && { trainer: query.trainer }) }, - { course: 1, amount: 1, trainer: 1 } + { course: 1, amount: 1, trainer: 1, submittedAt: 1 } ) .populate({ path: 'payment', select: 'status' }) .lean(); @@ -2380,9 +2386,11 @@ exports.listTrainersBilling = async (query) => { const courseBills = billsByTrainerAndCourse[`${trainer._id}-${course._id}`] || []; let invoicedAmount = 0; let paidAmount = 0; + const billsSubmissionDates = []; for (const bill of courseBills) { invoicedAmount = NumbersHelper.add(invoicedAmount, bill.amount); if (bill.payment?.status === PAID) paidAmount = NumbersHelper.add(paidAmount, bill.amount); + billsSubmissionDates.push(bill.submittedAt); } invoicedAmount = NumbersHelper.toNumber(invoicedAmount); paidAmount = NumbersHelper.toNumber(paidAmount); @@ -2401,6 +2409,7 @@ exports.listTrainersBilling = async (query) => { invoicedAmount, paidAmount, status, + ...(status === INVOICED && { billsSubmissionDates }), }); } diff --git a/tests/integration/courses.test.js b/tests/integration/courses.test.js index 1e236e07be..29c24e7d43 100644 --- a/tests/integration/courses.test.js +++ b/tests/integration/courses.test.js @@ -10128,7 +10128,7 @@ describe('COURSES ROUTES - GET /courses/trainers-billing', () => { expect(trainers[0].courses.length).toEqual(15); }); - it('should return all trainer\'s courses with slots in period', async () => { + it('should return all trainer\'s courses with slots or trainer bills in period', async () => { const startDate = '2020-03-01T00:00:00.000Z'; const endDate = '2020-03-05T23:59:59.999Z'; const response = await app.inject({ @@ -10140,8 +10140,15 @@ describe('COURSES ROUTES - GET /courses/trainers-billing', () => { expect(response.statusCode).toBe(200); const { trainers } = response.result.data; expect(trainers.length).toEqual(2); - expect(trainers.find(t => UtilsHelper.areObjectIdsEquals(t._id, trainer._id)).courses.length).toEqual(2); - expect(trainers.find(t => UtilsHelper.areObjectIdsEquals(t._id, trainerAndCoach._id)).courses.length).toEqual(4); + const trainerCourses = trainers.find(t => UtilsHelper.areObjectIdsEquals(t._id, trainer._id)).courses; + const trainerAndCoachCourses = trainers + .find(t => UtilsHelper.areObjectIdsEquals(t._id, trainerAndCoach._id)).courses; + expect(trainerCourses.length).toEqual(3); + expect(trainerAndCoachCourses.length).toEqual(4); + const trainerCourseBilledInPeriod = trainerCourses + .find(c => UtilsHelper.areObjectIdsEquals(c._id, coursesList[7]._id)); + expect(CompaniDate(trainerCourseBilledInPeriod.billsSubmissionDates[0]).toISO()) + .toEqual('2020-03-02T10:00:00.000Z'); }); it('should return 400 if endDate without startDate', async () => { @@ -10202,7 +10209,8 @@ describe('COURSES ROUTES - GET /courses/trainers-billing', () => { const { courses } = response.result.data.trainers[0]; expect(courses.length).toEqual(15); expect(courses.every(c => c.type !== SINGLE)).toBeTruthy(); - expect(courses[0]).toEqual(expect.objectContaining({ + const notInvoicedCourse = courses.find(c => UtilsHelper.areObjectIdsEquals(c._id, coursesList[4]._id)); + expect(notInvoicedCourse).toEqual(expect.objectContaining({ invoicedAmount: 0, paidAmount: 0, status: 'not_invoiced', diff --git a/tests/integration/seed/coursesSeed.js b/tests/integration/seed/coursesSeed.js index 836bbf72b6..c6ba003827 100644 --- a/tests/integration/seed/coursesSeed.js +++ b/tests/integration/seed/coursesSeed.js @@ -1548,6 +1548,16 @@ const trainerBillList = [ submittedAt: '2025-01-01T10:00:00.000Z', file: { publicId: 'publicId', link: 'link' }, }, + { + _id: new ObjectId(), + trainer: trainer._id, + number: 'FACT_0002', + course: coursesList[7]._id, + amount: 50, + submittedAt: '2020-03-02T10:00:00.000Z', + createdAt: '2020-03-02T10:00:00.000Z', + file: { publicId: 'publicId2', link: 'link2' }, + }, ]; const populateDB = async () => { diff --git a/tests/unit/helpers/courses.test.js b/tests/unit/helpers/courses.test.js index b6f95af63f..0ce3540e33 100644 --- a/tests/unit/helpers/courses.test.js +++ b/tests/unit/helpers/courses.test.js @@ -11455,10 +11455,34 @@ describe('listTrainersBilling', () => { }, ]; const trainerBills = [ - { course: courses[1]._id, trainer: trainerId, amount: 40, payment: { status: 'pending' } }, - { course: courses[2]._id, trainer: trainerId, amount: 100, payment: { status: 'pending' } }, - { course: courses[3]._id, trainer: trainerId, amount: 60, payment: { status: 'paid' } }, - { course: courses[3]._id, trainer: trainerId, amount: 40, payment: { status: 'paid' } }, + { + course: courses[1]._id, + trainer: trainerId, + amount: 40, + payment: { status: 'pending' }, + submittedAt: '2024-01-01T08:00:00.000Z', + }, + { + course: courses[2]._id, + trainer: trainerId, + amount: 100, + payment: { status: 'pending' }, + submittedAt: '2024-01-01T08:00:00.000Z', + }, + { + course: courses[3]._id, + trainer: trainerId, + amount: 60, + payment: { status: 'paid' }, + submittedAt: '2024-01-01T08:00:00.000Z', + }, + { + course: courses[3]._id, + trainer: trainerId, + amount: 40, + payment: { status: 'paid' }, + submittedAt: '2024-01-01T08:00:00.000Z', + }, ]; courseFind.returns(SinonMongoose.stubChainedQueries(courses, ['populate', 'sort', 'lean'])); @@ -11483,6 +11507,7 @@ describe('listTrainersBilling', () => { invoicedAmount: 100, paidAmount: 0, status: 'invoiced', + billsSubmissionDates: ['2024-01-01T08:00:00.000Z'], }, { ...omit(courses[3], ['billingPurchaseList']), @@ -11521,7 +11546,7 @@ describe('listTrainersBilling', () => { query: 'find', args: [ { course: { $in: courses.map(c => c._id) }, trainer: trainerId }, - { course: 1, amount: 1, trainer: 1 }, + { course: 1, amount: 1, trainer: 1, submittedAt: 1 }, ], }, { query: 'populate', args: [{ path: 'payment', select: 'status' }] }, @@ -11531,7 +11556,7 @@ describe('listTrainersBilling', () => { sinon.assert.notCalled(courseSlotFind); }); - it('should only return courses with slots in period', async () => { + it('should only return courses with slots or trainer bills in period', async () => { const trainerId = new ObjectId(); const trainer = { _id: trainerId, identity: { firstname: 'Jean', lastname: 'Dupont' } }; const query = { trainer: trainerId, startDate: '2024-02-01T00:00:00.000Z', endDate: '2024-02-29T23:59:59.999Z' }; @@ -11551,17 +11576,16 @@ describe('listTrainersBilling', () => { endDate: '2024-02-01T10:00:00.000Z', trainers: [trainerId], }, - { - course: courses[1]._id, - startDate: '2024-02-06T08:00:00.000Z', - endDate: '2024-02-06T10:00:00.000Z', - trainers: [trainerId], - }, + { course: courses[0]._id, startDate: '2024-02-06T08:00:00.000Z', endDate: '2024-02-06T10:00:00.000Z' }, + ]; + const trainerBillsInPeriod = [ + { course: courses[1]._id, submittedAt: '2024-02-15T08:00:00.000Z', trainer: trainerId, amount: 100 }, ]; courseSlotFind.returns(SinonMongoose.stubChainedQueries(courseSlotsInPeriod, ['lean'])); + trainerBillFind.onCall(0).returns(SinonMongoose.stubChainedQueries(trainerBillsInPeriod, ['lean'])); courseFind.returns(SinonMongoose.stubChainedQueries(courses, ['populate', 'sort', 'lean'])); - trainerBillFind.returns(SinonMongoose.stubChainedQueries([], ['populate', 'lean'])); + trainerBillFind.onCall(1).returns(SinonMongoose.stubChainedQueries([])); const result = await CourseHelper.listTrainersBilling(query); @@ -11591,6 +11615,20 @@ describe('listTrainersBilling', () => { { query: 'lean' }, ] ); + SinonMongoose.calledWithExactly( + trainerBillFind, + [ + { + query: 'find', + args: [ + { submittedAt: { $gte: query.startDate, $lte: query.endDate }, course: { $exists: true } }, + { course: 1 }, + ], + }, + { query: 'lean' }, + ], + 0 + ); SinonMongoose.calledOnceWithExactly( courseFind, [ @@ -11616,6 +11654,21 @@ describe('listTrainersBilling', () => { { query: 'lean' }, ] ); + SinonMongoose.calledWithExactly( + trainerBillFind, + [ + { + query: 'find', + args: [ + { course: { $in: [courses[0]._id, courses[1]._id] }, trainer: trainerId }, + { course: 1, amount: 1, trainer: 1, submittedAt: 1 }, + ], + }, + { query: 'populate', args: [{ path: 'payment', select: 'status' }] }, + { query: 'lean' }, + ], + 1 + ); }); it('should return all trainers without planned salary for courses with several trainers', async () => { @@ -11638,9 +11691,27 @@ describe('listTrainersBilling', () => { }, ]; const trainerBills = [ - { course: courses[0]._id, trainer: firstTrainerId, amount: 100, payment: { status: 'paid' } }, - { course: courses[0]._id, trainer: secondTrainerId, amount: 40, payment: { status: 'pending' } }, - { course: courses[1]._id, trainer: secondTrainerId, amount: 100, payment: { status: 'pending' } }, + { + course: courses[0]._id, + trainer: firstTrainerId, + amount: 100, + payment: { status: 'paid' }, + submittedAt: '2024-01-01T08:00:00.000Z', + }, + { + course: courses[0]._id, + trainer: secondTrainerId, + amount: 40, + payment: { status: 'pending' }, + submittedAt: '2024-01-01T08:00:00.000Z', + }, + { + course: courses[1]._id, + trainer: secondTrainerId, + amount: 100, + payment: { status: 'pending' }, + submittedAt: '2024-01-01T08:00:00.000Z', + }, ]; courseFind.returns(SinonMongoose.stubChainedQueries(courses, ['populate', 'sort', 'lean'])); @@ -11658,13 +11729,20 @@ describe('listTrainersBilling', () => { { ...secondTrainer, courses: [ - { ...omit(courses[0], ['billingPurchaseList']), invoicedAmount: 40, paidAmount: 0, status: 'invoiced' }, + { + ...omit(courses[0], ['billingPurchaseList']), + invoicedAmount: 40, + paidAmount: 0, + status: 'invoiced', + billsSubmissionDates: ['2024-01-01T08:00:00.000Z'], + }, { ...omit(courses[1], ['billingPurchaseList']), plannedSalary: 100, invoicedAmount: 100, paidAmount: 0, status: 'invoiced', + billsSubmissionDates: ['2024-01-01T08:00:00.000Z'], }, ], }, @@ -11693,7 +11771,10 @@ describe('listTrainersBilling', () => { SinonMongoose.calledOnceWithExactly( trainerBillFind, [ - { query: 'find', args: [{ course: { $in: courses.map(c => c._id) } }, { course: 1, amount: 1, trainer: 1 }] }, + { + query: 'find', + args: [{ course: { $in: courses.map(c => c._id) } }, { course: 1, amount: 1, trainer: 1, submittedAt: 1 }], + }, { query: 'populate', args: [{ path: 'payment', select: 'status' }] }, { query: 'lean' }, ]