Skip to content

Commit

Permalink
feat(api): throw an error when user provide same id in badge and unac…
Browse files Browse the repository at this point in the history
…quiredBadge filters
  • Loading branch information
lionelB committed Feb 17, 2025
1 parent d70346b commit afb19b3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
10 changes: 10 additions & 0 deletions api/src/prescription/campaign/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,18 @@ class CampaignParticipationDoesNotBelongToUser extends DomainError {
}
}

class AssessmentParticipationResultFilterError extends DomainError {
constructor() {
super(
'Filtering on both acquired and unacquired badge is impossible',
'ASSESSMENT_PARTICIPATION_RESULT_FILTER_ERROR',
);
}
}

export {
ArchivedCampaignError,
AssessmentParticipationResultFilterError,
CampaignCodeFormatError,
CampaignParticipationDoesNotBelongToUser,
CampaignUniqueCodeError,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { AssessmentParticipationResultFilterError } from '../errors.js';

async function findAssessmentParticipationResultList({
campaignId,
filters,
page,
campaignAssessmentParticipationResultListRepository,
}) {
if (filters?.badges?.some((id) => filters?.unacquiredBadges?.includes(id))) {
throw new AssessmentParticipationResultFilterError();
}
return campaignAssessmentParticipationResultListRepository.findPaginatedByCampaignId({ campaignId, filters, page });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,52 @@ describe('Unit | Application | Controller | Campaign Results', function () {
expect(errorCatched).to.be.instanceof(UserNotAuthorizedToAccessEntityError);
});
});

describe('#findAssessmentParticipationResults', function () {
const campaignId = 1;
const userId = 1;
const locale = FRENCH_SPOKEN;
let campaignAssessmentResultMinimalSerializer;
let pageSymbol, filterSymbol, resultSymbol, serializerResponseSymbol;

beforeEach(function () {
sinon.stub(usecases, 'findAssessmentParticipationResultList');
campaignAssessmentResultMinimalSerializer = {
serialize: sinon.stub(),
};
pageSymbol = Symbol('pageSymbol');
filterSymbol = Symbol('filter');
resultSymbol = Symbol('result');
serializerResponseSymbol = Symbol('serialize');
});

it('should return serialized results', async function () {
// given
usecases.findAssessmentParticipationResultList
.withArgs({
campaignId,
page: pageSymbol,
filters: filterSymbol,
})
.resolves(resultSymbol);
campaignAssessmentResultMinimalSerializer.serialize.withArgs(resultSymbol).resolves(serializerResponseSymbol);
const request = {
auth: { credentials: { userId } },
params: { campaignId },
query: {
page: pageSymbol,
filter: filterSymbol,
},
headers: { 'accept-language': locale },
};

// when
const response = await campaignResultsController.findAssessmentParticipationResults(request, hFake, {
campaignAssessmentResultMinimalSerializer,
});

// then
expect(response).to.equal(serializerResponseSymbol);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AssessmentParticipationResultFilterError } from '../../../../../../src/prescription/campaign/domain/errors.js';
import { findAssessmentParticipationResultList } from '../../../../../../src/prescription/campaign/domain/usecases/find-assessment-participation-result-list.js';
import { expect, sinon } from '../../../../../test-helper.js';
import { catchErr, expect, sinon } from '../../../../../test-helper.js';

describe('Unit | UseCase | find-assessment-participation-result-list', function () {
it('return the assessmentParticipationResultMinimal list', async function () {
Expand All @@ -20,4 +21,18 @@ describe('Unit | UseCase | find-assessment-participation-result-list', function
expect(findPaginatedByCampaignId).to.have.been.calledWithExactly({ page, campaignId, filters });
expect(results).to.equal(participations);
});

it('throw when filter contain the same id on both badge and unacquiredBadge filters', async function () {
const campaignId = 1;
const filters = { unacquiredBadges: [1, 3], badges: [1, 2] };
const page = Symbol('page');

const error = await catchErr(findAssessmentParticipationResultList)({
campaignId,
filters,
page,
});

expect(error).instanceOf(AssessmentParticipationResultFilterError);
});
});

0 comments on commit afb19b3

Please sign in to comment.