Skip to content

Commit

Permalink
[BUGFIX] Suppression des doublons de réponses lors de l'appel à /next (
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Dec 9, 2024
2 parents c76e030 + ba2a8a2 commit 173b177
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
14 changes: 10 additions & 4 deletions api/src/shared/infrastructure/repositories/answer-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,16 @@ const findByAssessment = async function (assessmentId) {

const findByAssessmentExcludingChallengeIds = async function ({ assessmentId, excludedChallengeIds = [] }) {
const answerDTOs = await knex
.select(COLUMNS)
.from('answers')
.where({ assessmentId })
.whereNotIn('challengeId', excludedChallengeIds);
.with('all-first-answers', (qb) => {
qb.select('*')
.distinctOn('challengeId', 'assessmentId')
.from('answers')
.where({ assessmentId })
.whereNotIn('challengeId', excludedChallengeIds)
.orderBy(['challengeId', 'assessmentId', 'createdAt']);
})
.from('all-first-answers')
.orderBy('all-first-answers.createdAt');

return _toDomainArray(answerDTOs);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ describe('Integration | Repository | answerRepository', function () {
expect(foundAnswers).to.be.empty;
});
});

context('when assessment exists', function () {
context('when excludingChallengeIds is not provided', function () {
it('should return all answers', async function () {
Expand Down Expand Up @@ -310,6 +311,40 @@ describe('Integration | Repository | answerRepository', function () {
});
});
});

context('when there are doubled answers', function () {
it('should return unique answers', async function () {
// given
databaseBuilder.factory.buildAssessment({ id: 123 });
const firstAnswer = domainBuilder.buildAnswer({ id: 1, assessmentId: 123, challengeId: 'recChallenge123' });
const secondAnswer = domainBuilder.buildAnswer({ id: 3, assessmentId: 123, challengeId: 'recChallenge456' });
databaseBuilder.factory.buildAnswer({
...firstAnswer,
id: 1,
result: firstAnswer.result.status,
});
databaseBuilder.factory.buildAnswer({
...firstAnswer,
id: 2,
result: firstAnswer.result.status,
});
databaseBuilder.factory.buildAnswer({
...secondAnswer,
id: 3,
result: secondAnswer.result.status,
});
await databaseBuilder.commit();

// when
const foundAnswers = await answerRepository.findByAssessmentExcludingChallengeIds({
assessmentId: 123,
excludedChallengeIds: [],
});

// then
expect(foundAnswers).to.deep.equal([firstAnswer, secondAnswer]);
});
});
});

describe('#findChallengeIdsFromAnswerIds', function () {
Expand Down

0 comments on commit 173b177

Please sign in to comment.