Skip to content

Commit

Permalink
[TECH] Migrer la route GET /api/admin/users/{id} dans src (PIX-15170)
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Nov 6, 2024
2 parents dae1a77 + 1e47392 commit 69aeecf
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 308 deletions.
32 changes: 0 additions & 32 deletions api/lib/application/users/index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,11 @@
import Joi from 'joi';

import { BadRequestError, sendJsonApiError } from '../../../src/shared/application/http-errors.js';
import { securityPreHandlers } from '../../../src/shared/application/security-pre-handlers.js';
import { identifiersType } from '../../../src/shared/domain/types/identifiers-type.js';
import { userController } from './user-controller.js';

const register = async function (server) {
const adminRoutes = [
{
method: 'GET',
path: '/api/admin/users/{id}',
config: {
validate: {
params: Joi.object({
id: identifiersType.userId,
}),
failAction: (request, h) => {
return sendJsonApiError(new BadRequestError("L'identifiant de l'utilisateur n'est pas au bon format."), h);
},
},
pre: [
{
method: (request, h) =>
securityPreHandlers.hasAtLeastOneAccessOf([
securityPreHandlers.checkAdminMemberHasRoleSuperAdmin,
securityPreHandlers.checkAdminMemberHasRoleCertif,
securityPreHandlers.checkAdminMemberHasRoleSupport,
securityPreHandlers.checkAdminMemberHasRoleMetier,
])(request, h),
},
],
handler: userController.getUserDetailsForAdmin,
notes: [
'- **Cette route est restreinte aux utilisateurs administrateurs**\n' +
"- Elle permet de récupérer le détail d'un utilisateur dans un contexte d'administration",
],
tags: ['api', 'admin', 'user'],
},
},
{
method: 'GET',
path: '/api/admin/users/{id}/organizations',
Expand Down
10 changes: 2 additions & 8 deletions api/lib/application/users/user-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { usecases as devcompUsecases } from '../../../src/devcomp/domain/usecase
import * as trainingSerializer from '../../../src/devcomp/infrastructure/serializers/jsonapi/training-serializer.js';
import { evaluationUsecases } from '../../../src/evaluation/domain/usecases/index.js';
import * as scorecardSerializer from '../../../src/evaluation/infrastructure/serializers/jsonapi/scorecard-serializer.js';
import { usecases as identityAccessManagementUsecases } from '../../../src/identity-access-management/domain/usecases/index.js';
import * as userDetailsForAdminSerializer from '../../../src/identity-access-management/infrastructure/serializers/jsonapi/user-details-for-admin.serializer.js';
import * as campaignParticipationSerializer from '../../../src/prescription/campaign-participation/infrastructure/serializers/jsonapi/campaign-participation-serializer.js';
import * as userSerializer from '../../../src/shared/infrastructure/serializers/jsonapi/user-serializer.js';
Expand All @@ -13,12 +14,6 @@ import * as certificationCenterMembershipSerializer from '../../infrastructure/s
import * as userAnonymizedDetailsForAdminSerializer from '../../infrastructure/serializers/jsonapi/user-anonymized-details-for-admin-serializer.js';
import * as userOrganizationForAdminSerializer from '../../infrastructure/serializers/jsonapi/user-organization-for-admin-serializer.js';

const getUserDetailsForAdmin = async function (request, h, dependencies = { userDetailsForAdminSerializer }) {
const userId = request.params.id;
const userDetailsForAdmin = await usecases.getUserDetailsForAdmin({ userId });
return dependencies.userDetailsForAdminSerializer.serialize(userDetailsForAdmin);
};

const rememberUserHasSeenAssessmentInstructions = async function (request, h, dependencies = { userSerializer }) {
const authenticatedUserId = request.auth.credentials.userId;

Expand Down Expand Up @@ -121,7 +116,7 @@ const anonymizeUser = async function (request, h, dependencies = { userAnonymize
});
});

const anonymizedUser = await usecases.getUserDetailsForAdmin({ userId: userToAnonymizeId });
const anonymizedUser = await identityAccessManagementUsecases.getUserDetailsForAdmin({ userId: userToAnonymizeId });

return h.response(dependencies.userAnonymizedDetailsForAdminSerializer.serialize(anonymizedUser)).code(200);
};
Expand Down Expand Up @@ -196,7 +191,6 @@ const userController = {
getCampaignParticipationOverviews,
getCampaignParticipations,
getUserCampaignParticipationToCampaign,
getUserDetailsForAdmin,
reassignAuthenticationMethods,
rememberUserHasSeenAssessmentInstructions,
rememberUserHasSeenChallengeTooltip,
Expand Down
5 changes: 0 additions & 5 deletions api/lib/domain/usecases/get-user-details-for-admin.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,32 @@ const updateUserDetailsByAdmin = async function (request, h, dependencies = { us
return dependencies.userDetailsForAdminSerializer.serializeForUpdate(updatedUser);
};

/**
*
* @param request
* @param h
* @param dependencies
* @param {UserDetailsForAdminSerializer} dependencies.userDetailsForAdminSerializer
* @returns {Promise<*>}
*/
const getUserDetails = async function (request, h, dependencies = { userDetailsForAdminSerializer }) {
const userId = request.params.id;
const userDetailsForAdmin = await usecases.getUserDetailsForAdmin({ userId });
return dependencies.userDetailsForAdminSerializer.serialize(userDetailsForAdmin);
};

/**
* @typedef {object} UserAdminController
* @property {function} findPaginatedFilteredUsers
* @property {function} getUserDetails
* @property {function} unblockUserAccount
* @property {function} updateUserDetailsByAdmin
*/
const userAdminController = { findPaginatedFilteredUsers, unblockUserAccount, updateUserDetailsByAdmin };
const userAdminController = {
findPaginatedFilteredUsers,
getUserDetails,
unblockUserAccount,
updateUserDetailsByAdmin,
};

export { userAdminController };
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Joi from 'joi';

import { BadRequestError, sendJsonApiError } from '../../../shared/application/http-errors.js';
import { securityPreHandlers } from '../../../shared/application/security-pre-handlers.js';
import { SUPPORTED_LOCALES } from '../../../shared/domain/constants.js';
import { AVAILABLE_LANGUAGES } from '../../../shared/domain/services/language-service.js';
Expand Down Expand Up @@ -120,4 +121,35 @@ export const userAdminRoutes = [
tags: ['api', 'admin', 'identity-access-management'],
},
},
{
method: 'GET',
path: '/api/admin/users/{id}',
config: {
validate: {
params: Joi.object({
id: identifiersType.userId,
}),
failAction: (request, h) => {
return sendJsonApiError(new BadRequestError("L'identifiant de l'utilisateur n'est pas au bon format."), h);
},
},
pre: [
{
method: (request, h) =>
securityPreHandlers.hasAtLeastOneAccessOf([
securityPreHandlers.checkAdminMemberHasRoleSuperAdmin,
securityPreHandlers.checkAdminMemberHasRoleCertif,
securityPreHandlers.checkAdminMemberHasRoleSupport,
securityPreHandlers.checkAdminMemberHasRoleMetier,
])(request, h),
},
],
handler: (request, h) => userAdminController.getUserDetails(request, h),
notes: [
'- **Cette route est restreinte aux utilisateurs administrateurs**\n' +
"- Elle permet de récupérer le détail d'un utilisateur dans un contexte d'administration",
],
tags: ['api', 'admin', 'identity-access-management', 'user'],
},
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @param {Object} params
* @param {string} params.userId
* @param {UserRepository} userRepository
* @throws UserNotFoundError
* @returns {Promise<UserDetailsForAdmin>}
*/
const getUserDetailsForAdmin = async function ({ userId, userRepository }) {
return await userRepository.getUserDetailsForAdmin(userId);
};

export { getUserDetailsForAdmin };

This file was deleted.

Loading

0 comments on commit 69aeecf

Please sign in to comment.