Skip to content

Commit

Permalink
[TECH] Migrer la route PATCH /api/admin/certification-center-membersh…
Browse files Browse the repository at this point in the history
…ips/{id} dans src (PIX-15537)

 #10704
  • Loading branch information
pix-service-auto-merge authored Dec 6, 2024
2 parents 77443db + 1577375 commit 841e80c
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 223 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BadRequestError, ForbiddenError } from '../../../src/shared/application/http-errors.js';
import { ForbiddenError } from '../../../src/shared/application/http-errors.js';
import * as certificationCenterMembershipSerializer from '../../../src/shared/infrastructure/serializers/jsonapi/certification-center-membership.serializer.js';
import * as requestResponseUtils from '../../../src/shared/infrastructure/utils/request-response-utils.js';
import { usecases as teamUsecases } from '../../../src/team/domain/usecases/index.js';
import { certificationCenterMembershipRepository } from '../../../src/team/infrastructure/repositories/certification-center-membership.repository.js';
import { usecases } from '../../domain/usecases/index.js';

Expand All @@ -26,32 +27,6 @@ const disableFromPixCertif = async function (request, h, dependencies = { reques
return h.response().code(204);
};

const updateFromPixAdmin = async function (
request,
h,
dependencies = { requestResponseUtils, certificationCenterMembershipSerializer },
) {
const certificationCenterMembershipId = request.params.id;
const certificationCenterMembership = dependencies.certificationCenterMembershipSerializer.deserialize(
request.payload,
);
const pixAgentUserId = dependencies.requestResponseUtils.extractUserIdFromRequest(request);

if (certificationCenterMembershipId !== certificationCenterMembership.id) {
throw new BadRequestError();
}

const updatedCertificationCenterMembership = await usecases.updateCertificationCenterMembership({
certificationCenterMembershipId,
role: certificationCenterMembership.role,
updatedByUserId: pixAgentUserId,
});

return h.response(
dependencies.certificationCenterMembershipSerializer.serializeForAdmin(updatedCertificationCenterMembership),
);
};

const updateFromPixCertif = async function (
request,
h,
Expand All @@ -71,7 +46,7 @@ const updateFromPixCertif = async function (
throw new ForbiddenError('Wrong certification center');
}

const updatedCertificationCenterMembership = await usecases.updateCertificationCenterMembership({
const updatedCertificationCenterMembership = await teamUsecases.updateCertificationCenterMembership({
certificationCenterMembershipId,
role: certificationCenterMembership.role,
updatedByUserId: currentUserId,
Expand All @@ -85,7 +60,6 @@ const updateFromPixCertif = async function (
const certificationCenterMembershipController = {
disableFromPixAdmin,
disableFromPixCertif,
updateFromPixAdmin,
updateFromPixCertif,
};

Expand Down
25 changes: 0 additions & 25 deletions api/lib/application/certification-center-memberships/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,6 @@ const register = async function (server) {
tags: ['api', 'certification-center-membership'],
},
},

{
method: 'PATCH',
path: '/api/admin/certification-center-memberships/{id}',
config: {
handler: certificationCenterMembershipController.updateFromPixAdmin,
pre: [
{
method: (request, h) =>
securityPreHandlers.hasAtLeastOneAccessOf([
securityPreHandlers.checkAdminMemberHasRoleSuperAdmin,
securityPreHandlers.checkAdminMemberHasRoleCertif,
securityPreHandlers.checkAdminMemberHasRoleSupport,
securityPreHandlers.checkAdminMemberHasRoleMetier,
])(request, h),
assign: 'hasAuthorizationToAccessAdminScope',
},
],
notes: [
"- **Cette route est restreinte aux utilisateurs ayant les droits d'accès**\n" +
"- Modification des informations d'un membre d'un centre de certification\n",
],
tags: ['api', 'certification-center-membership'],
},
},
];

server.route([...globalRoutes, ...adminRoutes]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { BadRequestError } from '../../../shared/application/http-errors.js';
import * as certificationCenterMembershipSerializer from '../../../shared/infrastructure/serializers/jsonapi/certification-center-membership.serializer.js';
import { requestResponseUtils } from '../../../shared/infrastructure/utils/request-response-utils.js';
import { usecases } from '../../domain/usecases/index.js';

const updateRole = async function (
request,
h,
dependencies = { requestResponseUtils, certificationCenterMembershipSerializer },
) {
const certificationCenterMembershipId = request.params.id;
const certificationCenterMembership = dependencies.certificationCenterMembershipSerializer.deserialize(
request.payload,
);
const pixAgentUserId = dependencies.requestResponseUtils.extractUserIdFromRequest(request);

if (certificationCenterMembershipId !== certificationCenterMembership.id) {
throw new BadRequestError();
}

const updatedCertificationCenterMembership = await usecases.updateCertificationCenterMembership({
certificationCenterMembershipId,
role: certificationCenterMembership.role,
updatedByUserId: pixAgentUserId,
});

return h.response(
dependencies.certificationCenterMembershipSerializer.serializeForAdmin(updatedCertificationCenterMembership),
);
};

const certificationCenterMembershipAdminController = {
updateRole,
};

export { certificationCenterMembershipAdminController };
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { securityPreHandlers } from '../../../shared/application/security-pre-handlers.js';
import { certificationCenterMembershipAdminController } from './certification-center-membership.admin.controller.js';

export const certificationCenterMembershipAdminRoutes = [
{
method: 'PATCH',
path: '/api/admin/certification-center-memberships/{id}',
config: {
handler: (request, h) => certificationCenterMembershipAdminController.updateRole(request, h),
pre: [
{
method: (request, h) =>
securityPreHandlers.hasAtLeastOneAccessOf([
securityPreHandlers.checkAdminMemberHasRoleSuperAdmin,
securityPreHandlers.checkAdminMemberHasRoleCertif,
securityPreHandlers.checkAdminMemberHasRoleSupport,
securityPreHandlers.checkAdminMemberHasRoleMetier,
])(request, h),
assign: 'hasAuthorizationToAccessAdminScope',
},
],
notes: [
"- **Cette route est restreinte aux utilisateurs ayant les droits d'accès**\n" +
"- Modification du rôle d'un membre d'un centre de certification\n",
],
tags: ['api', 'team', 'admin', 'certification-center-membership'],
},
},
];
2 changes: 2 additions & 0 deletions api/src/team/application/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { adminMemberRoutes } from './admin-member/admin-member.route.js';
import { certificationCenterInvitationAdminRoutes } from './certification-center-invitation/certification-center-invitation.admin.route.js';
import { certificationCenterInvitationRoutes } from './certification-center-invitation/certification-center-invitation.route.js';
import { certificationCenterMembershipAdminRoutes } from './certification-center-membership/certification-center-membership.admin.route.js';
import { certificationCenterMembershipRoute } from './certification-center-membership/certification-center-membership.route.js';
import { membershipAdminRoutes } from './membership/membership.admin.route.js';
import { membershipRoutes } from './membership/membership.route.js';
Expand All @@ -14,6 +15,7 @@ const register = async function (server) {
...certificationCenterInvitationRoutes,
...certificationCenterInvitationAdminRoutes,
...certificationCenterMembershipRoute,
...certificationCenterMembershipAdminRoutes,
...membershipAdminRoutes,
...membershipRoutes,
...prescriberInformationsRoute,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/**
* @typedef {import('../../infrastructure/repositories/certification-center-membership.repository.js').certificationCenterMembershipRepository} CertificationCenterMembershipRepository
*/

/**
* @param{object} params
* @param{string} params.certificationCenterMembershipId
* @param{string} role
* @param{string} updatedByUserId
* @param{CertificationCenterMembershipRepository} certificationCenterMembershipRepository
* @returns {Promise<CertificationCenterMembership>}
*/
const updateCertificationCenterMembership = async function ({
certificationCenterMembershipId,
role,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,168 +17,6 @@ describe('Acceptance | API | Certification Center Membership', function () {
});

context('Admin routes', function () {
describe('PATCH /api/admin/certification-center-memberships/{id}', function () {
let certificationCenter;
let certificationCenterMembership;
let user;

beforeEach(async function () {
certificationCenter = databaseBuilder.factory.buildCertificationCenter();
user = databaseBuilder.factory.buildUser();
certificationCenterMembership = databaseBuilder.factory.buildCertificationCenterMembership({
certificationCenterId: certificationCenter.id,
userId: user.id,
});
await databaseBuilder.commit();
});

context('Success cases', function () {
it('returns a 200 HTTP status code with the updated certification center membership', async function () {
// given
const pixAgentWithAdminRole = databaseBuilder.factory.buildUser.withRole({ role: 'SUPER_ADMIN' });
const request = {
method: 'PATCH',
url: `/api/admin/certification-center-memberships/${certificationCenterMembership.id}`,
payload: {
data: {
id: certificationCenterMembership.id.toString(),
type: 'certification-center-memberships',
attributes: {
role: 'ADMIN',
},
},
},
headers: {
authorization: generateValidRequestAuthorizationHeader(pixAgentWithAdminRole.id),
},
};
await databaseBuilder.commit();

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

// then
const expectedUpdatedCertificationCenterMembership = {
data: {
type: 'certification-center-memberships',
id: certificationCenterMembership.id.toString(),
attributes: {
role: 'ADMIN',
'created-at': response.result.data.attributes['created-at'],
'updated-at': response.result.data.attributes['updated-at'],
},
relationships: {
'certification-center': {
data: {
type: 'certificationCenters',
id: certificationCenter.id.toString(),
},
},
user: {
data: {
type: 'users',
id: user.id.toString(),
},
},
},
},
};
expect(response.statusCode).to.equal(200);
expect(_.omit(response.result, 'included')).to.deep.equal(expectedUpdatedCertificationCenterMembership);
});

context('when pix agent have "CERTIF" as role', function () {
it('returns a 200 HTTP status code with the updated certification center membership', async function () {
// given
const pixAgentWithCertifRole = databaseBuilder.factory.buildUser.withRole({ role: 'CERTIF' });

const request = {
method: 'PATCH',
url: `/api/admin/certification-center-memberships/${certificationCenterMembership.id}`,
payload: {
data: {
id: certificationCenterMembership.id.toString(),
type: 'certification-center-memberships',
attributes: {
role: 'ADMIN',
},
},
},
headers: {
authorization: generateValidRequestAuthorizationHeader(pixAgentWithCertifRole.id),
},
};

await databaseBuilder.commit();

// when
const { result, statusCode } = await server.inject(request);

const expectedUpdatedCertificationCenterMembership = {
data: {
type: 'certification-center-memberships',
id: certificationCenterMembership.id.toString(),
attributes: {
role: 'ADMIN',
'created-at': result.data.attributes['created-at'],
'updated-at': result.data.attributes['updated-at'],
},
relationships: {
'certification-center': {
data: {
type: 'certificationCenters',
id: certificationCenter.id.toString(),
},
},
user: {
data: {
type: 'users',
id: user.id.toString(),
},
},
},
},
};

expect(statusCode).to.equal(200);
expect(_.omit(result, 'included')).to.deep.equal(expectedUpdatedCertificationCenterMembership);
});
});
});

context('Error cases', function () {
context('when given certification center membership ID is different from the one in the payload', function () {
it('returns a 400 HTTP status code', async function () {
// given
const pixAgentWithSupportRole = databaseBuilder.factory.buildUser.withRole({ role: 'SUPPORT' });
const request = {
method: 'PATCH',
url: `/api/admin/certification-center-memberships/1`,
payload: {
data: {
id: '2',
type: 'certification-center-memberships',
attributes: {
role: 'ADMIN',
},
},
},
headers: {
authorization: generateValidRequestAuthorizationHeader(pixAgentWithSupportRole.id),
},
};
await databaseBuilder.commit();

// when
const { statusCode } = await server.inject(request);

// then
expect(statusCode).to.equal(400);
});
});
});
});

describe('DELETE /api/admin/certification-center-memberships/{id}', function () {
let certificationCenter;
let certificationCenterMembership;
Expand Down
Loading

0 comments on commit 841e80c

Please sign in to comment.