Skip to content

Commit

Permalink
feat(api): make badge acquisition work for exam
Browse files Browse the repository at this point in the history
  • Loading branch information
laura-bergoens committed Feb 28, 2025
1 parent 95c54d4 commit ece23eb
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const handleBadgeAcquisition = async function ({
badgeAcquisitionRepository,
knowledgeElementRepository,
}) {
if (assessment.isForCampaign()) {
if (assessment.isForCampaign() || assessment.isExam()) {
const campaignParticipationId = assessment.campaignParticipationId;
const associatedBadges = await _fetchPossibleCampaignAssociatedBadges(
campaignParticipationId,
Expand All @@ -15,6 +15,7 @@ const handleBadgeAcquisition = async function ({
if (_.isEmpty(associatedBadges)) {
return;
}
// TODO LAURA : ici pas les bons KEs
const knowledgeElements = await knowledgeElementRepository.findUniqByUserId({
userId: assessment.userId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Unit | UseCase | handle-badge-acquisition', function () {
};
});

context('when assessment is not of type campaign', function () {
context('when assessment is not of type campaign or exam', function () {
it('should not attempt to create any badge acquisition', async function () {
// given
badgeForCalculationRepository.findByCampaignParticipationId.rejects('I should not be called');
Expand Down Expand Up @@ -89,4 +89,61 @@ describe('Unit | UseCase | handle-badge-acquisition', function () {
});
});
});

context('when assessment is of type exam', function () {
const userId = 123,
campaignParticipationId = 456;

beforeEach(function () {
const assessment = domainBuilder.buildAssessment.ofTypeExam({ userId, campaignParticipationId });
args = { ...args, assessment };
});

context('when campaign has no badges', function () {
it('should not attempt to create any badge acquisition', async function () {
// given
badgeForCalculationRepository.findByCampaignParticipationId.withArgs({ campaignParticipationId }).resolves([]);
knowledgeElementRepository.findUniqByUserId.rejects('I should not be called');

// when
await handleBadgeAcquisition(args);

// then
expect(badgeAcquisitionRepository.createOrUpdate).to.not.have.been.called;
});
});

context('when campaign has badges', function () {
let badgeObtained1, badgeNotObtained2, badgeObtained3;

beforeEach(function () {
badgeObtained1 = domainBuilder.buildBadgeForCalculation.mockObtainable({ id: 1 });
badgeNotObtained2 = domainBuilder.buildBadgeForCalculation.mockNotObtainable({ id: 2 });
badgeObtained3 = domainBuilder.buildBadgeForCalculation.mockObtainable({ id: 3 });
});

it('should create or update badge acquisitions of obtained badges', async function () {
// given
badgeForCalculationRepository.findByCampaignParticipationId
.withArgs({ campaignParticipationId })
.resolves([badgeObtained1, badgeNotObtained2, badgeObtained3]);
knowledgeElementRepository.findUniqByUserId
.withArgs({ userId })
.resolves([domainBuilder.buildKnowledgeElement()]);

// when
await handleBadgeAcquisition(args);

// then
const badgeAcquisitionsToCreate = [
{ badgeId: badgeObtained1.id, userId, campaignParticipationId },
{ badgeId: badgeObtained3.id, userId, campaignParticipationId },
];
expect(badgeAcquisitionRepository.createOrUpdate).to.have.been.calledOnce;
expect(badgeAcquisitionRepository.createOrUpdate.firstCall).to.have.been.calledWithExactly({
badgeAcquisitionsToCreate,
});
});
});
});
});
55 changes: 55 additions & 0 deletions api/tests/tooling/domain-builder/factory/build-assessment.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,61 @@ buildAssessment.ofTypeCampaign = function ({
});
};

buildAssessment.ofTypeExam = function ({
id = 123,
courseId = 'courseId',
createdAt = new Date('1992-06-12T01:02:03Z'),
updatedAt = new Date('1992-06-12T01:02:03Z'),
userId = 456,
competenceId = null,
state = Assessment.states.COMPLETED,
isImproving = false,
lastQuestionDate = new Date(),
lastChallengeId = null,
lastQuestionState = Assessment.statesOfLastQuestion.ASKED,
answers = [buildAnswer()],
course = buildCourse({ id: 'courseId' }),
targetProfile = buildTargetProfile(),
campaignParticipation = null,
campaignParticipationId = null,
title = 'campaignTitle',
campaignCode,
} = {}) {
if (!_.isNil(campaignParticipation) && _.isNil(campaignParticipationId)) {
campaignParticipationId = campaignParticipation.id;
}
if (_.isNil(campaignParticipation) && !_.isNil(campaignParticipationId)) {
campaignParticipation = buildCampaignParticipation({ id: campaignParticipationId });
}
if (_.isNil(campaignParticipation) && _.isNil(campaignParticipationId)) {
campaignParticipation = buildCampaignParticipation();
campaignParticipationId = campaignParticipation.id;
}

return new Assessment({
id,
courseId,
createdAt,
updatedAt,
userId,
competenceId,
title,
type: Assessment.types.EXAM,
state,
isImproving,
campaignParticipationId,
lastQuestionDate,
lastChallengeId,
lastQuestionState,
answers,
course,
targetProfile,
campaignParticipation,
method: Assessment.methods.SMART_RANDOM,
campaignCode,
});
};

buildAssessment.ofTypeCompetenceEvaluation = function ({
id = 123,
courseId = 'courseId',
Expand Down

0 comments on commit ece23eb

Please sign in to comment.