Skip to content

Commit

Permalink
[TECH] Migrer la route POST /api/admin/users/{id}/anonymize dans src (P…
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Nov 15, 2024
2 parents 5068b71 + f9b5b64 commit bb218c6
Show file tree
Hide file tree
Showing 27 changed files with 343 additions and 331 deletions.
23 changes: 0 additions & 23 deletions api/lib/application/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,6 @@ const register = async function (server) {
tags: ['api', 'admin', 'user', 'certification-centers'],
},
},
{
method: 'POST',
path: '/api/admin/users/{id}/anonymize',
config: {
validate: {
params: Joi.object({
id: identifiersType.userId,
}),
},
pre: [
{
method: (request, h) =>
securityPreHandlers.hasAtLeastOneAccessOf([
securityPreHandlers.checkAdminMemberHasRoleSuperAdmin,
securityPreHandlers.checkAdminMemberHasRoleSupport,
])(request, h),
},
],
handler: userController.anonymizeUser,
notes: ["- Permet à un administrateur d'anonymiser un utilisateur"],
tags: ['api', 'admin', 'user'],
},
},
{
method: 'POST',
path: '/api/admin/users/{id}/add-pix-authentication-method',
Expand Down
21 changes: 0 additions & 21 deletions api/lib/application/users/user-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ 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 certificationCenterMembershipSerializer from '../../../src/shared/infrastructure/serializers/jsonapi/certification-center-membership.serializer.js';
import * as userSerializer from '../../../src/shared/infrastructure/serializers/jsonapi/user-serializer.js';
import * as requestResponseUtils from '../../../src/shared/infrastructure/utils/request-response-utils.js';
import { usecases } from '../../domain/usecases/index.js';
import { DomainTransaction } from '../../infrastructure/DomainTransaction.js';
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 rememberUserHasSeenAssessmentInstructions = async function (request, h, dependencies = { userSerializer }) {
Expand Down Expand Up @@ -82,23 +79,6 @@ const getUserCampaignParticipationToCampaign = function (
.then((campaignParticipation) => dependencies.campaignParticipationSerializer.serialize(campaignParticipation));
};

const anonymizeUser = async function (request, h, dependencies = { userAnonymizedDetailsForAdminSerializer }) {
const userToAnonymizeId = request.params.id;
const adminMemberId = request.auth.credentials.userId;

await DomainTransaction.execute(async (domainTransaction) => {
await usecases.anonymizeUser({
userId: userToAnonymizeId,
updatedByUserId: adminMemberId,
domainTransaction,
});
});

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

return h.response(dependencies.userAnonymizedDetailsForAdminSerializer.serialize(anonymizedUser)).code(200);
};

const removeAuthenticationMethod = async function (request, h) {
const userId = request.params.id;
const authenticationMethodType = request.payload.data.attributes.type;
Expand Down Expand Up @@ -162,7 +142,6 @@ const findCertificationCenterMembershipsByUser = async function (

const userController = {
addPixAuthenticationMethodByEmail,
anonymizeUser,
findCertificationCenterMembershipsByUser,
findPaginatedUserRecommendedTrainings,
findUserOrganizationsForAdmin,
Expand Down
4 changes: 2 additions & 2 deletions api/lib/domain/usecases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { scoAccountRecoveryService } from '../../../src/identity-access-manageme
import { accountRecoveryDemandRepository } from '../../../src/identity-access-management/infrastructure/repositories/account-recovery-demand.repository.js';
import * as authenticationMethodRepository from '../../../src/identity-access-management/infrastructure/repositories/authentication-method.repository.js';
import { emailValidationDemandRepository } from '../../../src/identity-access-management/infrastructure/repositories/email-validation-demand.repository.js';
import { userAnonymizedEventLoggingJobRepository } from '../../../src/identity-access-management/infrastructure/repositories/jobs/user-anonymized-event-logging-job-repository.js';
import * as oidcProviderRepository from '../../../src/identity-access-management/infrastructure/repositories/oidc-provider-repository.js';
import { organizationLearnerIdentityRepository } from '../../../src/identity-access-management/infrastructure/repositories/organization-learner-identity.repository.js';
import { refreshTokenRepository } from '../../../src/identity-access-management/infrastructure/repositories/refresh-token.repository.js';
Expand Down Expand Up @@ -110,6 +111,7 @@ import { organizationInvitationService } from '../../../src/team/domain/services
import * as certificationCenterInvitationRepository from '../../../src/team/infrastructure/repositories/certification-center-invitation-repository.js';
import { certificationCenterInvitedUserRepository } from '../../../src/team/infrastructure/repositories/certification-center-invited-user.repository.js';
import { certificationCenterMembershipRepository } from '../../../src/team/infrastructure/repositories/certification-center-membership.repository.js';
import * as membershipRepository from '../../../src/team/infrastructure/repositories/membership.repository.js';
import { organizationInvitationRepository } from '../../../src/team/infrastructure/repositories/organization-invitation.repository.js';
import { userOrgaSettingsRepository } from '../../../src/team/infrastructure/repositories/user-orga-settings-repository.js';
import * as certificationChallengesService from '../../domain/services/certification-challenges-service.js';
Expand All @@ -136,10 +138,8 @@ import * as flashAssessmentResultRepository from '../../infrastructure/repositor
import * as frameworkRepository from '../../infrastructure/repositories/framework-repository.js';
import { repositories } from '../../infrastructure/repositories/index.js';
import { certificationCompletedJobRepository } from '../../infrastructure/repositories/jobs/certification-completed-job-repository.js';
import { userAnonymizedEventLoggingJobRepository } from '../../infrastructure/repositories/jobs/user-anonymized-event-logging-job-repository.js';
import * as knowledgeElementRepository from '../../infrastructure/repositories/knowledge-element-repository.js';
import * as learningContentRepository from '../../infrastructure/repositories/learning-content-repository.js';
import * as membershipRepository from '../../infrastructure/repositories/membership-repository.js';
import * as organizationLearnerRepository from '../../infrastructure/repositories/organization-learner-repository.js';
import * as organizationMemberIdentityRepository from '../../infrastructure/repositories/organization-member-identity-repository.js';
import * as organizationTagRepository from '../../infrastructure/repositories/organization-tag-repository.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { DomainTransaction } from '../../../shared/domain/DomainTransaction.js';
import { usecases } from '../../domain/usecases/index.js';
import * as userAnonymizedDetailsForAdminSerializer from '../../infrastructure/serializers/jsonapi/user-anonymized-details-for-admin.serializer.js';
import * as userDetailsForAdminSerializer from '../../infrastructure/serializers/jsonapi/user-details-for-admin.serializer.js';
import * as userForAdminSerializer from '../../infrastructure/serializers/jsonapi/user-for-admin.serializer.js';
import * as userLoginSerializer from '../../infrastructure/serializers/jsonapi/user-login-serializer.js';
Expand Down Expand Up @@ -62,14 +64,41 @@ const getUserDetails = async function (request, h, dependencies = { userDetailsF
return dependencies.userDetailsForAdminSerializer.serialize(userDetailsForAdmin);
};

/**
*
* @param request
* @param h
* @param dependencies
* @param {UserDetailsForAdminSerializer} dependencies.userDetailsForAdminSerializer
* @returns {Promise<*>}
*/
const anonymizeUser = async function (request, h, dependencies = { userAnonymizedDetailsForAdminSerializer }) {
const userToAnonymizeId = request.params.id;
const adminMemberId = request.auth.credentials.userId;

await DomainTransaction.execute(async (domainTransaction) => {
await usecases.anonymizeUser({
userId: userToAnonymizeId,
updatedByUserId: adminMemberId,
domainTransaction,
});
});

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

return h.response(dependencies.userAnonymizedDetailsForAdminSerializer.serialize(anonymizedUser)).code(200);
};

/**
* @typedef {object} UserAdminController
* @property {function} anonymizeUser
* @property {function} findPaginatedFilteredUsers
* @property {function} getUserDetails
* @property {function} unblockUserAccount
* @property {function} updateUserDetailsByAdmin
*/
const userAdminController = {
anonymizeUser,
findPaginatedFilteredUsers,
getUserDetails,
unblockUserAccount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,27 @@ export const userAdminRoutes = [
tags: ['api', 'admin', 'identity-access-management', 'user'],
},
},
{
method: 'POST',
path: '/api/admin/users/{id}/anonymize',
config: {
validate: {
params: Joi.object({
id: identifiersType.userId,
}),
},
pre: [
{
method: (request, h) =>
securityPreHandlers.hasAtLeastOneAccessOf([
securityPreHandlers.checkAdminMemberHasRoleSuperAdmin,
securityPreHandlers.checkAdminMemberHasRoleSupport,
])(request, h),
},
],
handler: (request, h) => userAdminController.anonymizeUser(request, h),
notes: ["- Permet à un administrateur d'anonymiser un utilisateur"],
tags: ['api', 'admin', 'identity-access-management', 'user'],
},
},
];
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { UserAnonymizedEventLoggingJob } from '../../../src/identity-access-management/domain/models/UserAnonymizedEventLoggingJob.js';
import { config } from '../../../src/shared/config.js';
import { UserNotFoundError } from '../../../src/shared/domain/errors.js';

import { config } from '../../../shared/config.js';
import { UserNotFoundError } from '../../../shared/domain/errors.js';
import { UserAnonymizedEventLoggingJob } from '../models/UserAnonymizedEventLoggingJob.js';

/**
* @param params
* @param{string} params.userId
* @param{string} params.updatedByUserId
* @param{boolean} params.preventAuditLogging
* @param{UserRepository} params.userRepository
* @param{AuthenticationMethodRepository} params.authenticationMethodRepository
* @param{MembershipRepository} params.membershipRepository
* @param{CertificationCenterMembershipRepository} params.certificationCenterMembershipRepository
* @param{OrganizationLearnerRepository} params.organizationLearnerRepository
* @param{RefreshTokenRepository} params.refreshTokenRepository
* @param{ResetPasswordDemandRepository} params.resetPasswordDemandRepository
* @param{UserLoginRepository} params.userLoginRepository
* @param{AdminMemberRepository} params.adminMemberRepository
* @param{UserAnonymizedEventLoggingJobRepository} params.userAnonymizedEventLoggingJobRepository
* @returns {Promise<null>}
*/
const anonymizeUser = async function ({
userId,
updatedByUserId,
Expand Down
6 changes: 6 additions & 0 deletions api/src/identity-access-management/domain/usecases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import * as codeUtils from '../../../shared/infrastructure/utils/code-utils.js';
import { injectDependencies } from '../../../shared/infrastructure/utils/dependency-injection.js';
import { importNamedExportsFromDirectory } from '../../../shared/infrastructure/utils/import-named-exports-from-directory.js';
import * as emailRepository from '../../../shared/mail/infrastructure/repositories/email.repository.js';
import { certificationCenterMembershipRepository } from '../../../team/infrastructure/repositories/certification-center-membership.repository.js';
import * as membershipRepository from '../../../team/infrastructure/repositories/membership.repository.js';
import { accountRecoveryDemandRepository } from '../../infrastructure/repositories/account-recovery-demand.repository.js';
import * as authenticationMethodRepository from '../../infrastructure/repositories/authentication-method.repository.js';
import { emailValidationDemandRepository } from '../../infrastructure/repositories/email-validation-demand.repository.js';
import { eventLoggingJobRepository } from '../../infrastructure/repositories/jobs/event-logging-job.repository.js';
import { garAnonymizedBatchEventsLoggingJobRepository } from '../../infrastructure/repositories/jobs/gar-anonymized-batch-events-logging-job-repository.js';
import { userAnonymizedEventLoggingJobRepository } from '../../infrastructure/repositories/jobs/user-anonymized-event-logging-job-repository.js';
import { oidcProviderRepository } from '../../infrastructure/repositories/oidc-provider-repository.js';
import { refreshTokenRepository } from '../../infrastructure/repositories/refresh-token.repository.js';
import { resetPasswordDemandRepository } from '../../infrastructure/repositories/reset-password-demand.repository.js';
Expand All @@ -48,13 +51,16 @@ const repositories = {
campaignParticipationRepository,
campaignRepository,
campaignToJoinRepository: campaignRepositories.campaignToJoinRepository,
certificationCenterMembershipRepository,
emailValidationDemandRepository,
emailRepository,
eventLoggingJobRepository,
membershipRepository,
oidcProviderRepository,
organizationLearnerRepository,
refreshTokenRepository,
resetPasswordDemandRepository,
userAnonymizedEventLoggingJobRepository,
userEmailRepository,
userLoginRepository,
userRecommendedTrainingRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UserAnonymizedEventLoggingJob } from '../../../../src/identity-access-management/domain/models/UserAnonymizedEventLoggingJob.js';
import { JobRepository, JobRetry } from '../../../../src/shared/infrastructure/repositories/jobs/job-repository.js';
import { JobRepository, JobRetry } from '../../../../shared/infrastructure/repositories/jobs/job-repository.js';
import { UserAnonymizedEventLoggingJob } from '../../../domain/models/UserAnonymizedEventLoggingJob.js';

class UserAnonymizedEventLoggingJobRepository extends JobRepository {
constructor() {
Expand Down
4 changes: 2 additions & 2 deletions api/src/prescription/campaign/domain/usecases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as campaignRepository from '../../../../../lib/infrastructure/repositor
import * as knowledgeElementRepository from '../../../../../lib/infrastructure/repositories/knowledge-element-repository.js';
import * as knowledgeElementSnapshotRepository from '../../../../../lib/infrastructure/repositories/knowledge-element-snapshot-repository.js';
import * as learningContentRepository from '../../../../../lib/infrastructure/repositories/learning-content-repository.js';
import * as membershipRepository from '../../../../../lib/infrastructure/repositories/membership-repository.js';
import * as stageCollectionRepository from '../../../../../lib/infrastructure/repositories/user-campaign-results/stage-collection-repository.js';
import * as tutorialRepository from '../../../../devcomp/infrastructure/repositories/tutorial-repository.js';
import * as badgeRepository from '../../../../evaluation/infrastructure/repositories/badge-repository.js';
Expand All @@ -18,6 +17,7 @@ import * as competenceRepository from '../../../../shared/infrastructure/reposit
import * as organizationRepository from '../../../../shared/infrastructure/repositories/organization-repository.js';
import { injectDependencies } from '../../../../shared/infrastructure/utils/dependency-injection.js';
import { importNamedExportsFromDirectory } from '../../../../shared/infrastructure/utils/import-named-exports-from-directory.js';
import * as membershipRepository from '../../../../team/infrastructure/repositories/membership.repository.js';
import * as campaignAnalysisRepository from '../../../campaign-participation/infrastructure/repositories/campaign-analysis-repository.js';
import * as campaignParticipationRepository from '../../../campaign-participation/infrastructure/repositories/campaign-participation-repository.js';
import * as organizationLearnerImportFormatRepository from '../../../learner-management/infrastructure/repositories/organization-learner-import-format-repository.js';
Expand Down Expand Up @@ -46,7 +46,7 @@ import * as campaignUpdateValidator from '../validators/campaign-update-validato
* @typedef { import ('../../../../../lib/infrastructure/repositories/knowledge-element-repository.js')} KnowledgeElementRepository
* @typedef { import ('../../../../../lib/infrastructure/repositories/knowledge-element-snapshot-repository.js')} KnowledgeElementSnapshotRepository
* @typedef { import ('../../../../../lib/infrastructure/repositories/learning-content-repository.js')} LearningContentRepository
* @typedef { import ('../../../../../lib/infrastructure/repositories/membership-repository.js')} MembershipRepository
* @typedef { import ('../../../../team/infrastructure/repositories/membership-repository.js')} MembershipRepository
* @typedef { import ('../../../../../lib/infrastructure/repositories/user-campaign-results/stage-collection-repository.js')} StageCollectionRepository
* @typedef { import ('../../../../evaluation/infrastructure/repositories/badge-repository.js')} BadgeRepository
* @typedef { import ('../../../../identity-access-management/infrastructure/repositories/user.repository.js')} UserRepository
Expand Down
Loading

0 comments on commit bb218c6

Please sign in to comment.