Skip to content

Commit

Permalink
feat(api): migrate route
Browse files Browse the repository at this point in the history
  • Loading branch information
xav-car authored Nov 8, 2024
1 parent dde0137 commit 17ba50f
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 346 deletions.
48 changes: 0 additions & 48 deletions api/lib/application/target-profiles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,54 +34,6 @@ const register = async function (server) {
],
},
},
{
method: 'PATCH',
path: '/api/admin/target-profiles/{id}',
config: {
pre: [
{
method: (request, h) =>
securityPreHandlers.hasAtLeastOneAccessOf([
securityPreHandlers.checkAdminMemberHasRoleSuperAdmin,
securityPreHandlers.checkAdminMemberHasRoleSupport,
securityPreHandlers.checkAdminMemberHasRoleMetier,
])(request, h),
assign: 'hasAuthorizationToAccessAdminScope',
},
],
validate: {
params: Joi.object({
id: identifiersType.targetProfileId,
}),
payload: Joi.object({
data: {
attributes: {
'are-knowledge-elements-resettable': Joi.boolean(),
category: Joi.string(),
comment: Joi.string().allow(null).max(500),
description: Joi.string().allow(null).max(500),
'image-url': Joi.string().uri().allow(null),
name: Joi.string(),
tubes: Joi.array()
.optional()
.items(
Joi.object({
id: Joi.string(),
level: Joi.number(),
}),
),
},
},
}),
},
handler: targetProfileController.updateTargetProfile,
tags: ['api', 'admin', 'target-profiles'],
notes: [
"- **Cette route est restreinte aux utilisateurs authentifiés ayant les droits d'accès**\n" +
"- Elle permet de mettre à jour les attributs d'un profil cible",
],
},
},
]);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ const getTargetProfileForAdmin = async function (request, h, dependencies = { ta
return dependencies.targetProfileForAdminSerializer.serialize({ targetProfile, filter });
};

const updateTargetProfile = async function (request, h, dependencies = { usecases, targetProfileSerializer }) {
const targetProfileId = request.params.id;
const updateTargetProfile = async function (
request,
h,
dependencies = { prescriptionTargetProfileUsecases, targetProfileSerializer },
) {
const targetProfileId = request.params.targetProfileId;
const attributesToUpdate = dependencies.targetProfileSerializer.deserialize(request.payload);

await DomainTransaction.execute(async () => {
await dependencies.usecases.updateTargetProfile({
await dependencies.prescriptionTargetProfileUsecases.updateTargetProfile({
id: targetProfileId,
attributesToUpdate,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,54 @@ const register = async function (server) {
],
},
},
{
method: 'PATCH',
path: '/api/admin/target-profiles/{targetProfileId}',
config: {
pre: [
{
method: (request, h) =>
securityPreHandlers.hasAtLeastOneAccessOf([
securityPreHandlers.checkAdminMemberHasRoleSuperAdmin,
securityPreHandlers.checkAdminMemberHasRoleSupport,
securityPreHandlers.checkAdminMemberHasRoleMetier,
])(request, h),
assign: 'hasAuthorizationToAccessAdminScope',
},
],
validate: {
params: Joi.object({
targetProfileId: identifiersType.targetProfileId,
}),
payload: Joi.object({
data: {
attributes: {
'are-knowledge-elements-resettable': Joi.boolean(),
category: Joi.string(),
comment: Joi.string().allow(null).max(500),
description: Joi.string().allow(null).max(500),
'image-url': Joi.string().uri().allow(null),
name: Joi.string(),
tubes: Joi.array()
.optional()
.items(
Joi.object({
id: Joi.string(),
level: Joi.number(),
}),
),
},
},
}),
},
handler: targetProfileController.updateTargetProfile,
tags: ['api', 'admin', 'target-profiles'],
notes: [
"- **Cette route est restreinte aux utilisateurs authentifiés ayant les droits d'accès**\n" +
"- Elle permet de mettre à jour les attributs d'un profil cible",
],
},
},
]);
};

Expand Down
116 changes: 0 additions & 116 deletions api/tests/acceptance/application/target-profiles/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,131 +3,15 @@ import {
databaseBuilder,
expect,
generateValidRequestAuthorizationHeader,
mockLearningContent,
} from '../../../test-helper.js';

describe('Acceptance | Route | target-profiles', function () {
let server;

const skillId = 'recArea1_Competence1_Tube1_Skill1';
const tubeId = 'recArea1_Competence1_Tube1';

const learningContent = {
areas: [{ id: 'recArea1', competenceIds: ['recArea1_Competence1'] }],
competences: [
{
id: 'recArea1_Competence1',
areaId: 'recArea1',
skillIds: [skillId],
origin: 'Pix',
},
],
thematics: [],
tubes: [
{
id: 'recArea1_Competence1_Tube1',
competenceId: 'recArea1_Competence1',
},
],
skills: [
{
id: skillId,
name: '@recArea1_Competence1_Tube1_Skill1',
status: 'actif',
tubeId: 'recArea1_Competence1_Tube1',
competenceId: 'recArea1_Competence1',
},
],
challenges: [
{
id: 'recArea1_Competence1_Tube1_Skill1_Challenge1',
skillId: skillId,
competenceId: 'recArea1_Competence1',
status: 'validé',
locales: ['fr-fr'],
},
],
};

beforeEach(async function () {
server = await createServer();
});

describe('PATCH /api/admin/target-profiles/{id}', function () {
beforeEach(async function () {
mockLearningContent(learningContent);
});

describe('when there is no tube to update', function () {
it('should return 204', async function () {
const targetProfile = databaseBuilder.factory.buildTargetProfile();
const user = databaseBuilder.factory.buildUser.withRole();
await databaseBuilder.commit();

const options = {
method: 'PATCH',
url: `/api/admin/target-profiles/${targetProfile.id}`,
headers: { authorization: generateValidRequestAuthorizationHeader(user.id) },
payload: {
data: {
attributes: {
name: 'CoolPixer',
description: 'Amazing description',
comment: 'Amazing comment',
category: 'OTHER',
'image-url': 'http://valid-uri.com/image.png',
'are-knowledge-elements-resettable': false,
},
},
},
};

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

// then
expect(response.statusCode).to.equal(204);
});
});

describe('when there is some tube update and the target profile is not linked with campaign', function () {
it('should return 204', async function () {
const targetProfile = databaseBuilder.factory.buildTargetProfile();
const targetProfileTube = databaseBuilder.factory.buildTargetProfileTube({
targetProfileId: targetProfile.id,
tubeId,
level: 1,
});
const user = databaseBuilder.factory.buildUser.withRole();
await databaseBuilder.commit();

const options = {
method: 'PATCH',
url: `/api/admin/target-profiles/${targetProfile.id}`,
headers: { authorization: generateValidRequestAuthorizationHeader(user.id) },
payload: {
data: {
attributes: {
name: 'nom changé',
category: 'COMPETENCES',
description: 'description changée.',
comment: 'commentaire changé.',
'image-url': null,
tubes: [{ id: targetProfileTube.tubeId, level: 99 }],
},
},
},
};

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

// then
expect(response.statusCode).to.equal(204);
});
});
});

describe('GET /api/admin/target-profiles/{id}/training-summaries', function () {
let user;
let targetProfileId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,121 @@ describe('Acceptance | TargetProfile | Application | Route | admin-target-profil
server = await createServer();
});

describe('PATCH /api/admin/target-profiles/{targetProfileId}', function () {
const skillId = 'recArea1_Competence1_Tube1_Skill1';
const tubeId = 'recArea1_Competence1_Tube1';

const learningContent = {
areas: [{ id: 'recArea1', competenceIds: ['recArea1_Competence1'] }],
competences: [
{
id: 'recArea1_Competence1',
areaId: 'recArea1',
skillIds: [skillId],
origin: 'Pix',
},
],
thematics: [],
tubes: [
{
id: 'recArea1_Competence1_Tube1',
competenceId: 'recArea1_Competence1',
},
],
skills: [
{
id: skillId,
name: '@recArea1_Competence1_Tube1_Skill1',
status: 'actif',
tubeId: 'recArea1_Competence1_Tube1',
competenceId: 'recArea1_Competence1',
},
],
challenges: [
{
id: 'recArea1_Competence1_Tube1_Skill1_Challenge1',
skillId: skillId,
competenceId: 'recArea1_Competence1',
status: 'validé',
locales: ['fr-fr'],
},
],
};

beforeEach(async function () {
mockLearningContent(learningContent);
});

describe('when there is no tube to update', function () {
it('should return 204', async function () {
const targetProfile = databaseBuilder.factory.buildTargetProfile();
const user = databaseBuilder.factory.buildUser.withRole();
await databaseBuilder.commit();

const options = {
method: 'PATCH',
url: `/api/admin/target-profiles/${targetProfile.id}`,
headers: { authorization: generateValidRequestAuthorizationHeader(user.id) },
payload: {
data: {
attributes: {
name: 'CoolPixer',
description: 'Amazing description',
comment: 'Amazing comment',
category: 'OTHER',
'image-url': 'http://valid-uri.com/image.png',
'are-knowledge-elements-resettable': false,
},
},
},
};

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

// then
expect(response.statusCode).to.equal(204);
});
});

describe('when there is some tube update and the target profile is not linked with campaign', function () {
it('should return 204', async function () {
const targetProfile = databaseBuilder.factory.buildTargetProfile();
const targetProfileTube = databaseBuilder.factory.buildTargetProfileTube({
targetProfileId: targetProfile.id,
tubeId,
level: 1,
});
const user = databaseBuilder.factory.buildUser.withRole();
await databaseBuilder.commit();

const options = {
method: 'PATCH',
url: `/api/admin/target-profiles/${targetProfile.id}`,
headers: { authorization: generateValidRequestAuthorizationHeader(user.id) },
payload: {
data: {
attributes: {
name: 'nom changé',
category: 'COMPETENCES',
description: 'description changée.',
comment: 'commentaire changé.',
'image-url': null,
tubes: [{ id: targetProfileTube.tubeId, level: 99 }],
},
},
},
};

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

// then
expect(response.statusCode).to.equal(204);
});
});
});

describe('GET /api/admin/target-profiles/{id}', function () {
let user;
const skillId = 'recArea1_Competence1_Tube1_Skill1';
Expand Down
Loading

0 comments on commit 17ba50f

Please sign in to comment.