Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/helpers/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 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;
if (query.startDate) {
Expand All @@ -2352,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' } })
Expand Down Expand Up @@ -2395,16 +2399,20 @@ 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 (NumbersHelper.isEqualTo(invoicedAmount, 0)) status = NOT_INVOICED;
if (isArchivedBeforePaymentsStart) 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)) {
status = PAID;
}

trainerCourses.push({
...omit(course, ['billingPurchaseList']),
...omit(course, ['billingPurchaseList', 'archivedAt']),
...(plannedSalary !== undefined && { plannedSalary }),
invoicedAmount,
paidAmount,
Expand Down
93 changes: 87 additions & 6 deletions tests/unit/helpers/courses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11525,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' }] },
Expand Down Expand Up @@ -11556,6 +11556,85 @@ describe('listTrainersBilling', () => {
sinon.assert.notCalled(courseSlotFind);
});

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: '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 = [
{
course: courses[1]._id,
trainer: trainerId,
amount: 40,
payment: { status: 'pending' },
submittedAt: '2024-01-01T08:00:00.000Z',
},
];

courseFind.returns(SinonMongoose.stubChainedQueries(courses, ['populate', 'sort', 'lean']));
trainerBillFind.returns(SinonMongoose.stubChainedQueries(trainerBills, ['populate', 'lean']));

const result = await CourseHelper.listTrainersBilling({ trainer: trainerId });

expect(result).toEqual([{
...trainer,
courses: [
{
...omit(courses[0], ['billingPurchaseList', 'archivedAt']),
plannedSalary: 100,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pourquoi il a un plannedSalary ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pcq il y a const billingPurchaseList = [{ billingItem: { type: TRAINER_SALARY }, price: 100, count: 1 }]; dans courses[0], rien n'empeche d'avoir d'anciennes formations avec trainer_salary non ?

invoicedAmount: 0,
paidAmount: 0,
status: 'paid',
},
{
...omit(courses[1], ['billingPurchaseList', 'archivedAt']),
plannedSalary: 100,
invoicedAmount: 40,
paidAmount: 0,
status: 'partially_invoiced',
},
{
...omit(courses[2], ['billingPurchaseList', 'archivedAt']),
plannedSalary: 100,
invoicedAmount: 0,
paidAmount: 0,
status: 'not_invoiced',
},
{
...omit(courses[3], ['billingPurchaseList']),
plannedSalary: 100,
invoicedAmount: 0,
paidAmount: 0,
status: 'not_invoiced',
},
],
}]);
sinon.assert.notCalled(courseSlotFind);
});

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' } };
Expand All @@ -11582,7 +11661,7 @@ 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([]));
Expand All @@ -11602,7 +11681,7 @@ describe('listTrainersBilling', () => {
{ ...courses[1], invoicedAmount: 0, paidAmount: 0, status: 'not_invoiced' },
],
}]);
SinonMongoose.calledOnceWithExactly(
SinonMongoose.calledWithExactly(
courseSlotFind,
[
{
Expand All @@ -11613,7 +11692,8 @@ describe('listTrainersBilling', () => {
],
},
{ query: 'lean' },
]
],
0
);
SinonMongoose.calledWithExactly(
trainerBillFind,
Expand All @@ -11640,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' }] },
Expand Down Expand Up @@ -11716,6 +11796,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({});

Expand Down Expand Up @@ -11754,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' }] },
Expand Down
Loading