Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TECH] Eviter de re-déclencher les calculs pour déterminer la prochaine épreuve dans assessments/:id/next si la dernière épreuve proposée n'a pas encore été répondue #11843

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
12 changes: 11 additions & 1 deletion api/src/shared/domain/models/Challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const Accessibility = Object.freeze({
OK: 'OK',
});

const Statuses = Object.freeze({
VALIDATED: 'validé',
ARCHIVED: 'archivé',
OBSOLETE: 'périmé',
});

/**
* Traduction: Épreuve
*/
Expand Down Expand Up @@ -163,6 +169,10 @@ class Challenge {
return this._isCompliant('Tablet');
}

get isOperative() {
return [Statuses.VALIDATED, Statuses.ARCHIVED].includes(this.status);
}

get isAccessible() {
return (
(this.blindnessCompatibility === Accessibility.OK || this.blindnessCompatibility === Accessibility.RAS) &&
Expand Down Expand Up @@ -208,4 +218,4 @@ class Challenge {

Challenge.Type = ChallengeType;

export { Accessibility, Challenge, ChallengeType as Type };
export { Accessibility, Challenge, Statuses, ChallengeType as Type };
30 changes: 27 additions & 3 deletions api/src/shared/domain/usecases/get-next-challenge.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import { AssessmentEndedError } from '../errors.js';

export async function getNextChallenge({
assessmentId,
userId,
locale,
assessmentRepository,
answerRepository,
challengeRepository,
evaluationUsecases,
certificationEvaluationRepository,
}) {
const assessment = await assessmentRepository.get(assessmentId);

if (assessment.isStarted()) {
await assessmentRepository.updateLastQuestionDate({ id: assessment.id, lastQuestionDate: new Date() });
if (!assessment.isStarted()) {
throw new AssessmentEndedError();
}
await assessmentRepository.updateLastQuestionDate({ id: assessment.id, lastQuestionDate: new Date() });

let nextChallenge = null;
const answers = await answerRepository.findByAssessment(assessment.id);
const waitingForLatestChallengeAnswer = checkIfLatestChallengeOfAssessmentIsAwaitingToBeAnswered({
answers,
lastChallengeId: assessment.lastChallengeId,
});
if (waitingForLatestChallengeAnswer) {
nextChallenge = await challengeRepository.get(assessment.lastChallengeId);
if (nextChallenge.isOperative) {
return nextChallenge;
} else {
nextChallenge = null;
}
}
if (assessment.isCertification()) {
nextChallenge = await certificationEvaluationRepository.selectNextCertificationChallenge({
assessmentId: assessment.id,
Expand Down Expand Up @@ -45,3 +62,10 @@ export async function getNextChallenge({

return nextChallenge;
}

function checkIfLatestChallengeOfAssessmentIsAwaitingToBeAnswered({ answers, lastChallengeId }) {
if (!lastChallengeId) {
return false;
}
return !answers.some((answer) => answer.challengeId === lastChallengeId);
}
2 changes: 2 additions & 0 deletions api/src/shared/domain/usecases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url';
import * as complementaryCertificationBadgeRepository from '../../../certification/complementary-certification/infrastructure/repositories/complementary-certification-badge-repository.js';
import { evaluationUsecases } from '../../../evaluation/domain/usecases/index.js';
import * as badgeRepository from '../../../evaluation/infrastructure/repositories/badge-repository.js';
import * as answerRepository from '../../infrastructure/repositories/answer-repository.js';
import * as assessmentRepository from '../../infrastructure/repositories/assessment-repository.js';
import * as challengeRepository from '../../infrastructure/repositories/challenge-repository.js';
import { repositories as sharedInjectedRepositories } from '../../infrastructure/repositories/index.js';
Expand All @@ -17,6 +18,7 @@ const usecasesWithoutInjectedDependencies = {

const dependencies = {
assessmentRepository,
answerRepository,
complementaryCertificationBadgeRepository,
badgeRepository,
challengeRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Assessment } from '../../../../../src/shared/domain/models/index.js';
import {
createServer,
databaseBuilder,
Expand Down Expand Up @@ -58,6 +59,7 @@ describe('Acceptance | API | assessment-controller-get-next-challenge-for-demo',
id: assessmentId,
type: 'DEMO',
courseId: 'course_id',
state: Assessment.states.STARTED,
});
return databaseBuilder.commit();
});
Expand All @@ -83,6 +85,7 @@ describe('Acceptance | API | assessment-controller-get-next-challenge-for-demo',
id: assessmentId,
type: 'DEMO',
courseId: 'course_id',
state: Assessment.states.STARTED,
});
databaseBuilder.factory.buildAnswer({ challengeId: 'first_challenge', assessmentId });
return databaseBuilder.commit();
Expand All @@ -103,6 +106,34 @@ describe('Acceptance | API | assessment-controller-get-next-challenge-for-demo',
});
});

context('when the first challenge has not been answered yet', function () {
beforeEach(function () {
databaseBuilder.factory.buildAssessment({
id: assessmentId,
type: 'DEMO',
courseId: 'course_id',
state: Assessment.states.STARTED,
lastChallengeId: 'first_challenge',
});
databaseBuilder.factory.buildAnswer({ challengeId: 'some_other_challenge', assessmentId });
return databaseBuilder.commit();
});

it('should return the first challenge again', async function () {
// given
const options = {
method: 'GET',
url: '/api/assessments/' + assessmentId + '/next',
};

// when
const response = await server.inject(options);

// then
expect(response.result.data.id).to.equal('first_challenge');
});
});

context('when all challenges are answered', function () {
beforeEach(function () {
databaseBuilder.factory.buildAssessment({
Expand Down
Loading