Skip to content
Open
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
30 changes: 28 additions & 2 deletions src/helpers/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: omit(course.contact, 'contact') }),
};
};

const getCourseForOperations = async (courseId, credentials, origin) => {
const isRofOrAdmin = [VENDOR_ADMIN, TRAINING_ORGANISATION_MANAGER].includes(get(credentials, 'role.vendor.name'));

Expand Down Expand Up @@ -700,6 +715,7 @@ const getCourseForOperations = async (courseId, credentials, origin) => {
totalTheoreticalDuration: exports.getTotalTheoreticalDuration(fetchedCourse),
trainees,
...isBlended && { slots },
...(origin === WEBAPP && fetchedCourse.type === SINGLE && _formatSingleCourseInterlocutors(fetchedCourse)),
Comment thread
ulysseferreira marked this conversation as resolved.
Resolved
};
};

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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) : [];
Expand Down
252 changes: 247 additions & 5 deletions tests/unit/helpers/courses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2954,6 +2954,91 @@ 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' },
});
process.env.MANAGEMENT_COMPANI_EMAIL = '';

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 on vide la variable d'env ?

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.

Quand on définit une var d'env dans les tests on la défini et la vide au debut et a la fin de chaqeu test, souvent on le fait dans le beforeEach et afterEach mais la il vaut mieux le faire dans les tests directement

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.

justement pourquoi l'avoir mis dans chaque test plutôt que définir la variable dans beforeeach et la reset dans aftereach ?

});

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(),
Expand Down Expand Up @@ -3466,7 +3551,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 }] },
]
);
Expand Down Expand Up @@ -3632,7 +3722,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 }] },
]
);
Expand Down Expand Up @@ -3806,7 +3901,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 }] },
]
);
Expand Down Expand Up @@ -3950,7 +4050,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 }] },
]
);
Expand All @@ -3959,6 +4064,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 = {
Expand Down Expand Up @@ -4093,7 +4330,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 }] },
]
);
Expand Down
Loading