Skip to content

Commit

Permalink
feat(api): add route
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelB committed Feb 24, 2025
1 parent e045843 commit 8d2e100
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { buildAssessment } from './build-assessment.js';
import { buildCampaign } from './build-campaign.js';
import { buildCampaignParticipation } from './build-campaign-participation.js';
import { buildCampaignSkill } from './build-campaign-skill.js';
import { buildOrganizationLearner } from './build-organization-learner.js';
import { buildUser } from './build-user.js';

const { STARTED, SHARED, TO_SHARE } = CampaignParticipationStatuses;
Expand Down Expand Up @@ -166,4 +167,38 @@ const buildDeleted = function ({
return campaignParticipation;
};

export { build, buildArchived, buildDeleted, buildEnded, buildOnGoing, buildToShare };
const buildDeletedAndAnonymised = function ({
userId,
createdAt,
sharedAt,
assessmentCreatedAt,
deletedAt = new Date('1998-07-01'),
deletedBy = buildUser().id,
campaignSkills,
} = {}) {
const campaign = buildCampaign();
campaignSkills.forEach((skill) => buildCampaignSkill({ campaignId: campaign.id, skillId: skill }));
const learner = buildOrganizationLearner({ userId, campaignId: campaign.id });

const campaignParticipation = buildCampaignParticipation({
organizationLearnerId: learner.id,
campaignId: campaign.id,
createdAt: createdAt,
sharedAt: sharedAt || createdAt,
deletedAt,
deletedBy,
status: SHARED,
});

const assessment = buildAssessment({
userId,
campaignParticipationId: null,
createdAt: assessmentCreatedAt,
type: Assessment.types.CAMPAIGN,
state: Assessment.states.COMPLETED,
});

return { assessment, campaignParticipation };
};

export { build, buildArchived, buildDeleted, buildDeletedAndAnonymised, buildEnded, buildOnGoing, buildToShare };
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,32 @@ const register = async function (server) {
tags: ['api'],
},
},
{
method: 'GET',
path: '/api/users/{userId}/anonymised-campaign-assessments',
config: {
pre: [
{
method: securityPreHandlers.checkRequestedUserIsAuthenticatedUser,
assign: 'requestedUserIsAuthenticatedUser',
},
],
validate: {
params: Joi.object({
userId: identifiersType.userId,
}),
},
handler: campaignParticipationController.getAnonymisedCampaignAssessments,
notes: [
'- **Cette route est restreinte aux utilisateurs authentifiés**\n' +
'- Récupération des assessment du type campagnes qui ne sont plus liés à des participations\n' +
'- L’id demandé doit correspondre à celui de l’utilisateur authentifié' +
'- Les assessments sont triés par ordre inverse de création' +
' (les plus récentes en premier)',
],
tags: ['api'],
},
},
{
method: 'GET',
path: '/api/users/{userId}/campaigns/{campaignId}/campaign-participations',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,49 @@ describe('Acceptance | API | Campaign Participations', function () {
});
});

describe('GET /users/{userId}/anonymised-campaign-assessments', function () {
let userId;
let options;
const skillId = 'recSkillId';

beforeEach(function () {
const user = databaseBuilder.factory.buildUser();
userId = user.id;

databaseBuilder.factory.learningContent.buildSkill({ id: skillId });

return databaseBuilder.commit();
});

it('should return anonymised campaign assessment', async function () {
// given
const deletedCampaignParticipation =
databaseBuilder.factory.campaignParticipationOverviewFactory.buildDeletedAndAnonymised({
userId,
createdAt: new Date('2021-01-12'),
sharedAt: new Date('2021-01-14'),
deleted: new Date('2023-12-24'),
assessmentCreatedAt: new Date('2021-01-12'),
campaignSkills: [skillId],
});

await databaseBuilder.commit();
options = {
method: 'GET',
url: `/api/users/${userId}/anonymised-campaign-assessments`,
headers: generateAuthenticatedUserRequestHeaders({ userId }),
};

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

// then
expect(response.statusCode).to.equal(200);
const assessmentIds = response.result.data.map(({ id }) => Number(id));
expect(assessmentIds).to.deep.equals([deletedCampaignParticipation.assessment.id]);
});
});

describe('GET /users/{userId}/campaigns/{campaignId}/campaign-participations', function () {
let options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ describe('Integration | Application | Route | campaignParticipationRouter', func
.stub(campaignParticipationController, 'getUserCampaignAssessmentResult')
.callsFake((request, h) => h.response('ok').code(200));

sinon
.stub(campaignParticipationController, 'getAnonymisedCampaignAssessments')
.callsFake((request, h) => h.response('ok').code(200));

httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);
});
Expand Down Expand Up @@ -204,4 +208,33 @@ describe('Integration | Application | Route | campaignParticipationRouter', func
});
});
});

describe('GET /users/{userId}/anonymised-campaign-assessments', function () {
context('When authenticated user mismatch requested user or user is not authenticated ', function () {
beforeEach(function () {
securityPreHandlers.checkRequestedUserIsAuthenticatedUser.callsFake((request, h) => {
return Promise.resolve(h.response().code(403).takeover());
});
});

it('should return a 403 HTTP response', async function () {
// when
const response = await httpTestServer.request('GET', '/api/users/1234/anonymised-campaign-assessments');

// then
expect(response.statusCode).to.equal(403);
expect(campaignParticipationController.getAnonymisedCampaignAssessments).not.called;
});
});
context('When userId is not an integer', function () {
it('should return 400 - Bad request when userId is not an integer', async function () {
// when
const response = await httpTestServer.request('GET', '/api/users/NOTANID/anonymised-campaign-assessments');

// then
expect(response.statusCode).to.equal(400);
expect(campaignParticipationController.getAnonymisedCampaignAssessments).not.called;
});
});
});
});

0 comments on commit 8d2e100

Please sign in to comment.