From 222261efc13b749c448c032b4d559bd74686030c Mon Sep 17 00:00:00 2001 From: Manon Palin Date: Fri, 20 Nov 2026 12:09:52 +0100 Subject: [PATCH 1/2] COM-4332 - display generic contact infos if contact is operationRepresentative --- src/helpers/courses.js | 30 +++- tests/unit/helpers/courses.test.js | 253 ++++++++++++++++++++++++++++- 2 files changed, 276 insertions(+), 7 deletions(-) diff --git a/src/helpers/courses.js b/src/helpers/courses.js index c760e90b4c..d5b807c32f 100644 --- a/src/helpers/courses.js +++ b/src/helpers/courses.js @@ -565,6 +565,21 @@ exports.formatCourseWithProgress = (course, userId, shouldComputePresence = fals }; }; +const _formatUserWithGenericContactInfos = user => ({ + ...omit(user, 'contact'), + local: { email: process.env.MANAGEMENT_COMPANI_EMAIL }, +}); + +const _formatSingleCourseInterlocutors = (course) => { + const isOperationsRepresentativeContact = UtilsHelper + .areObjectIdsEquals(get(course, 'contact._id'), get(course, 'operationsRepresentative._id')); + + return { + operationsRepresentative: _formatUserWithGenericContactInfos(course.operationsRepresentative), + ...(isOperationsRepresentativeContact && { contact: _formatUserWithGenericContactInfos(course.contact) }), + }; +}; + const getCourseForOperations = async (courseId, credentials, origin) => { const isRofOrAdmin = [VENDOR_ADMIN, TRAINING_ORGANISATION_MANAGER].includes(get(credentials, 'role.vendor.name')); @@ -700,6 +715,7 @@ const getCourseForOperations = async (courseId, credentials, origin) => { totalTheoreticalDuration: exports.getTotalTheoreticalDuration(fetchedCourse), trainees, ...isBlended && { slots }, + ...(origin === WEBAPP && fetchedCourse.type === SINGLE && _formatSingleCourseInterlocutors(fetchedCourse)), }; }; @@ -865,8 +881,17 @@ exports.getTraineesWithElearningProgress = (trainees, steps) => { return trainees.map(t => ({ ...t, ..._computeTraineeElearningProgress(t._id, stepsWithGroupedHistories) })); }; +const _formatCourseForPedagogy = (course) => { + const isOperationsRepresentativeContact = course.type === SINGLE && + UtilsHelper.areObjectIdsEquals(get(course, 'contact._id'), course.operationsRepresentative); + const formattedCourse = omit(course, 'operationsRepresentative'); + if (!isOperationsRepresentativeContact) return formattedCourse; + + return { ...formattedCourse, contact: _formatUserWithGenericContactInfos(course.contact) }; +}; + const _getCourseForPedagogy = async (courseId, credentials) => { - const course = await Course.findOne({ _id: courseId }) + const fetchedCourse = await Course.findOne({ _id: courseId }) .populate({ path: 'subProgram', select: 'program steps', @@ -903,9 +928,10 @@ const _getCourseForPedagogy = async (courseId, credentials) => { populate: [{ path: 'slots.slotId', select: 'startDate endDate step' }, { path: 'trainer', select: 'identity' }], }) .populate({ path: 'questionnaires' }) - .select('_id misc format type trainees gSheetId certificateGenerationMode tradeName') + .select('_id misc format type trainees gSheetId certificateGenerationMode tradeName operationsRepresentative') .lean({ autopopulate: true, virtuals: true }); + const course = _formatCourseForPedagogy(fetchedCourse); const courseTrainerIds = course.trainers ? course.trainers.map(trainer => trainer._id) : []; const isTrainer = UtilsHelper.doesArrayIncludeId(courseTrainerIds, credentials._id); const courseTutorIds = course.tutors ? course.tutors.map(tutor => tutor._id) : []; diff --git a/tests/unit/helpers/courses.test.js b/tests/unit/helpers/courses.test.js index 4d2aba2cfd..25cf31bc8d 100644 --- a/tests/unit/helpers/courses.test.js +++ b/tests/unit/helpers/courses.test.js @@ -2954,6 +2954,92 @@ describe('getCourse', () => { sinon.assert.notCalled(attendanceCountDocuments); }); + it('should return single course with generic operations representative infos (webapp)', async () => { + process.env.MANAGEMENT_COMPANI_EMAIL = 'test@compani.fr'; + const operationsRepresentative = { + _id: new ObjectId(), + identity: { firstname: 'Jean', lastname: 'Opération' }, + local: { email: 'jean.operation@compani.fr' }, + contact: { phone: '0123456789', countryCode: '+33' }, + picture: { link: 'link' }, + }; + const course = { + _id: new ObjectId(), + type: SINGLE, + format: BLENDED, + trainees: [{ _id: traineeIds[0] }], + subProgram: { steps: [{ theoreticalDuration: 'PT3600S' }] }, + slots: [{ step: new ObjectId() }], + operationsRepresentative, + contact: { + _id: operationsRepresentative._id, + identity: operationsRepresentative.identity, + contact: operationsRepresentative.contact, + }, + }; + findOne.returns(SinonMongoose.stubChainedQueries(course)); + getCompanyAtCourseRegistrationList.returns([{ trainee: traineeIds[0], company: authCompanyId }]); + + const result = await CourseHelper.getCourse( + { action: OPERATIONS, origin: WEBAPP }, + { _id: course._id }, + { role: { vendor: { name: 'vendor_admin' } }, company: { _id: new ObjectId() } } + ); + + expect(result.operationsRepresentative).toEqual({ + _id: operationsRepresentative._id, + identity: { firstname: 'Jean', lastname: 'Opération' }, + local: { email: 'test@compani.fr' }, + picture: { link: 'link' }, + }); + expect(result.contact).toEqual({ + _id: operationsRepresentative._id, + identity: { firstname: 'Jean', lastname: 'Opération' }, + local: { email: 'test@compani.fr' }, + }); + process.env.MANAGEMENT_COMPANI_EMAIL = ''; + }); + + it('should return single course with contact infos if contact is not operations representative', async () => { + process.env.MANAGEMENT_COMPANI_EMAIL = 'test@compani.fr'; + const contact = { + _id: new ObjectId(), + identity: { firstname: 'Paul', lastname: 'Formateur' }, + contact: { phone: '0123456789', countryCode: '+33' }, + }; + const course = { + _id: new ObjectId(), + type: SINGLE, + format: BLENDED, + trainees: [{ _id: traineeIds[0] }], + subProgram: { steps: [{ theoreticalDuration: 'PT3600S' }] }, + slots: [{ step: new ObjectId() }], + operationsRepresentative: { + _id: new ObjectId(), + identity: { firstname: 'Jean', lastname: 'Opération' }, + local: { email: 'jean.operation@compani.fr' }, + contact: { phone: '0987654321', countryCode: '+33' }, + }, + contact, + }; + findOne.returns(SinonMongoose.stubChainedQueries(course)); + getCompanyAtCourseRegistrationList.returns([{ trainee: traineeIds[0], company: authCompanyId }]); + + const result = await CourseHelper.getCourse( + { action: OPERATIONS, origin: WEBAPP }, + { _id: course._id }, + { role: { vendor: { name: 'vendor_admin' } }, company: { _id: new ObjectId() } } + ); + + expect(result.operationsRepresentative).toEqual({ + _id: course.operationsRepresentative._id, + identity: { firstname: 'Jean', lastname: 'Opération' }, + local: { email: 'test@compani.fr' }, + }); + expect(result.contact).toEqual(contact); + process.env.MANAGEMENT_COMPANI_EMAIL = ''; + }); + it('should return inter b2b course with trainees filtering on company (webapp)', async () => { const course = { _id: new ObjectId(), @@ -3466,7 +3552,12 @@ describe('getCourse', () => { query: 'populate', args: [{ path: 'questionnaires' }], }, - { query: 'select', args: ['_id misc format type trainees gSheetId certificateGenerationMode tradeName'] }, + { + query: 'select', + args: [ + '_id misc format type trainees gSheetId certificateGenerationMode tradeName operationsRepresentative', + ], + }, { query: 'lean', args: [{ virtuals: true, autopopulate: true }] }, ] ); @@ -3632,7 +3723,12 @@ describe('getCourse', () => { query: 'populate', args: [{ path: 'questionnaires' }], }, - { query: 'select', args: ['_id misc format type trainees gSheetId certificateGenerationMode tradeName'] }, + { + query: 'select', + args: [ + '_id misc format type trainees gSheetId certificateGenerationMode tradeName operationsRepresentative', + ], + }, { query: 'lean', args: [{ virtuals: true, autopopulate: true }] }, ] ); @@ -3806,7 +3902,12 @@ describe('getCourse', () => { query: 'populate', args: [{ path: 'questionnaires' }], }, - { query: 'select', args: ['_id misc format type trainees gSheetId certificateGenerationMode tradeName'] }, + { + query: 'select', + args: [ + '_id misc format type trainees gSheetId certificateGenerationMode tradeName operationsRepresentative', + ], + }, { query: 'lean', args: [{ virtuals: true, autopopulate: true }] }, ] ); @@ -3950,7 +4051,12 @@ describe('getCourse', () => { query: 'populate', args: [{ path: 'questionnaires' }], }, - { query: 'select', args: ['_id misc format type trainees gSheetId certificateGenerationMode tradeName'] }, + { + query: 'select', + args: [ + '_id misc format type trainees gSheetId certificateGenerationMode tradeName operationsRepresentative', + ], + }, { query: 'lean', args: [{ virtuals: true, autopopulate: true }] }, ] ); @@ -3959,6 +4065,138 @@ describe('getCourse', () => { sinon.assert.notCalled(attendanceCountDocuments); }); + it('should return generic contact infos if contact is operations representative of single course', async () => { + process.env.MANAGEMENT_COMPANI_EMAIL = 'test@compani.fr'; + const loggedUser = { _id: new ObjectId(), company: { _id: new ObjectId() } }; + const operationsRepresentativeId = new ObjectId(); + const course = { + _id: new ObjectId(), + type: SINGLE, + subProgram: { isStrictlyELearning: false, steps: [] }, + slots: [], + slotsToPlan: [], + trainers: [{ _id: loggedUser._id }], + trainees: [new ObjectId()], + operationsRepresentative: operationsRepresentativeId, + contact: { + _id: operationsRepresentativeId, + identity: { firstname: 'Jean', lastname: 'Opération' }, + local: { email: 'jean.operation@compani.fr' }, + contact: { phone: '0123456789', countryCode: '+33' }, + }, + }; + + findOne.returns(SinonMongoose.stubChainedQueries(course, ['populate', 'select', 'lean'])); + + const result = await CourseHelper.getCourse({ action: PEDAGOGY }, { _id: course._id }, loggedUser); + + expect(result.contact).toEqual({ + _id: operationsRepresentativeId, + identity: { firstname: 'Jean', lastname: 'Opération' }, + local: { email: 'test@compani.fr' }, + }); + expect(result.operationsRepresentative).toBeUndefined(); + SinonMongoose.calledOnceWithExactly( + findOne, + [ + { query: 'findOne', args: [{ _id: course._id }] }, + { + query: 'populate', + args: [{ + path: 'subProgram', + select: 'program steps', + populate: [ + { path: 'program', select: 'name image description learningGoals' }, + { + path: 'steps', + select: 'name type activities theoreticalDuration', + populate: { + path: 'activities', + select: 'name type cards activityHistories', + populate: [ + { path: 'activityHistories', match: { user: loggedUser._id } }, + { path: 'cards', select: 'template' }, + ], + }, + }, + ], + }], + }, + { + query: 'populate', + args: [ + { + path: 'slots', + select: 'startDate endDate step address meetingLink trainees trainers', + populate: { path: 'step', select: 'type durationCountedPerTrainer' }, + options: { sort: { startDate: 1 } }, + }, + ], + }, + { query: 'populate', args: [{ path: 'slotsToPlan', select: '_id' }] }, + { + query: 'populate', + args: [{ path: 'trainers', select: 'identity.firstname identity.lastname biography picture' }], + }, + { query: 'populate', args: [{ path: 'tutors', select: 'identity.firstname identity.lastname picture' }] }, + { + query: 'populate', + args: [{ path: 'contact', select: 'identity.firstname identity.lastname contact local.email' }], + }, + { + query: 'populate', + args: [{ + path: 'attendanceSheets', + match: { $or: [{ trainee: loggedUser._id }, { 'slots.traineesSignature.traineeId': loggedUser._id }] }, + options: { requestingOwnInfos: true }, + populate: [ + { path: 'slots.slotId', select: 'startDate endDate step' }, { path: 'trainer', select: 'identity' }, + ], + }], + }, + { query: 'populate', args: [{ path: 'questionnaires' }] }, + { + query: 'select', + args: [ + '_id misc format type trainees gSheetId certificateGenerationMode tradeName operationsRepresentative', + ], + }, + { query: 'lean', args: [{ virtuals: true, autopopulate: true }] }, + ] + ); + process.env.MANAGEMENT_COMPANI_EMAIL = ''; + }); + + it('should return contact infos if contact is not operations representative of single course', async () => { + process.env.MANAGEMENT_COMPANI_EMAIL = 'test@compani.fr'; + const loggedUser = { _id: new ObjectId(), company: { _id: new ObjectId() } }; + const contact = { + _id: loggedUser._id, + identity: { firstname: 'Paul', lastname: 'Formateur' }, + local: { email: 'paul.formateur@compani.fr' }, + contact: { phone: '0123456789', countryCode: '+33' }, + }; + const course = { + _id: new ObjectId(), + type: SINGLE, + subProgram: { isStrictlyELearning: false, steps: [] }, + slots: [], + slotsToPlan: [], + trainers: [{ _id: loggedUser._id }], + trainees: [new ObjectId()], + operationsRepresentative: new ObjectId(), + contact, + }; + + findOne.returns(SinonMongoose.stubChainedQueries(course, ['populate', 'select', 'lean'])); + + const result = await CourseHelper.getCourse({ action: PEDAGOGY }, { _id: course._id }, loggedUser); + + expect(result.contact).toEqual(contact); + expect(result.operationsRepresentative).toBeUndefined(); + process.env.MANAGEMENT_COMPANI_EMAIL = ''; + }); + it('should return course as tutor', async () => { const authCompanyId = new ObjectId(); const loggedUser = { @@ -4093,7 +4331,12 @@ describe('getCourse', () => { query: 'populate', args: [{ path: 'questionnaires' }], }, - { query: 'select', args: ['_id misc format type trainees gSheetId certificateGenerationMode tradeName'] }, + { + query: 'select', + args: [ + '_id misc format type trainees gSheetId certificateGenerationMode tradeName operationsRepresentative', + ], + }, { query: 'lean', args: [{ virtuals: true, autopopulate: true }] }, ] ); From 009ea401a17cfc2bf18fd5fdbd4605f1ff29858d Mon Sep 17 00:00:00 2001 From: Manon Palin Date: Mon, 23 Nov 2026 10:06:41 +0100 Subject: [PATCH 2/2] COM-4332 - fix reviews --- src/helpers/courses.js | 2 +- tests/unit/helpers/courses.test.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/helpers/courses.js b/src/helpers/courses.js index d5b807c32f..f07b7c5462 100644 --- a/src/helpers/courses.js +++ b/src/helpers/courses.js @@ -576,7 +576,7 @@ const _formatSingleCourseInterlocutors = (course) => { return { operationsRepresentative: _formatUserWithGenericContactInfos(course.operationsRepresentative), - ...(isOperationsRepresentativeContact && { contact: _formatUserWithGenericContactInfos(course.contact) }), + ...(isOperationsRepresentativeContact && { contact: omit(course.contact, 'contact') }), }; }; diff --git a/tests/unit/helpers/courses.test.js b/tests/unit/helpers/courses.test.js index 25cf31bc8d..c34a63190a 100644 --- a/tests/unit/helpers/courses.test.js +++ b/tests/unit/helpers/courses.test.js @@ -2995,7 +2995,6 @@ describe('getCourse', () => { expect(result.contact).toEqual({ _id: operationsRepresentative._id, identity: { firstname: 'Jean', lastname: 'Opération' }, - local: { email: 'test@compani.fr' }, }); process.env.MANAGEMENT_COMPANI_EMAIL = ''; });