From 2b0e2e8e13a0c1627b1394bb2771a83f8084ed85 Mon Sep 17 00:00:00 2001 From: Eric Lim Date: Wed, 6 Nov 2024 16:50:04 +0100 Subject: [PATCH] feat(mon-pix): move anonymize user usecase in src --- .../application/user/user.admin.controller.js | 3 +-- .../usecases/anonymize-user.usecase.js} | 25 ++++++++++++++++--- .../domain/usecases/index.js | 6 +++++ .../scripts/backfill-anonymized-users.js | 2 +- .../usecases/anonymize-user.usecase.test.js | 2 +- .../user/user.admin.controller.test.js | 5 ++-- 6 files changed, 32 insertions(+), 11 deletions(-) rename api/{lib/domain/usecases/anonymize-user.js => src/identity-access-management/domain/usecases/anonymize-user.usecase.js} (70%) diff --git a/api/src/identity-access-management/application/user/user.admin.controller.js b/api/src/identity-access-management/application/user/user.admin.controller.js index 83b0505ecdd..01e5c6d6402 100644 --- a/api/src/identity-access-management/application/user/user.admin.controller.js +++ b/api/src/identity-access-management/application/user/user.admin.controller.js @@ -1,4 +1,3 @@ -import { usecases as libUsecases } from '../../../../lib/domain/usecases/index.js'; import * as userAnonymizedDetailsForAdminSerializer from '../../../../lib/infrastructure/serializers/jsonapi/user-anonymized-details-for-admin-serializer.js'; import { DomainTransaction } from '../../../shared/domain/DomainTransaction.js'; import { usecases } from '../../domain/usecases/index.js'; @@ -78,7 +77,7 @@ const anonymizeUser = async function (request, h, dependencies = { userAnonymize const adminMemberId = request.auth.credentials.userId; await DomainTransaction.execute(async (domainTransaction) => { - await libUsecases.anonymizeUser({ + await usecases.anonymizeUser({ userId: userToAnonymizeId, updatedByUserId: adminMemberId, domainTransaction, diff --git a/api/lib/domain/usecases/anonymize-user.js b/api/src/identity-access-management/domain/usecases/anonymize-user.usecase.js similarity index 70% rename from api/lib/domain/usecases/anonymize-user.js rename to api/src/identity-access-management/domain/usecases/anonymize-user.usecase.js index bd22e6283bd..60740209e7d 100644 --- a/api/lib/domain/usecases/anonymize-user.js +++ b/api/src/identity-access-management/domain/usecases/anonymize-user.usecase.js @@ -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} + */ const anonymizeUser = async function ({ userId, updatedByUserId, diff --git a/api/src/identity-access-management/domain/usecases/index.js b/api/src/identity-access-management/domain/usecases/index.js index 38cd89a6812..c198433926f 100644 --- a/api/src/identity-access-management/domain/usecases/index.js +++ b/api/src/identity-access-management/domain/usecases/index.js @@ -7,6 +7,8 @@ import * as userReconciliationService from '../../../../lib/domain/services/user import { oidcAuthenticationServiceRegistry } from '../../../../lib/domain/usecases/index.js'; import * as campaignParticipationRepository from '../../../../lib/infrastructure/repositories/campaign-participation-repository.js'; import * as campaignRepository from '../../../../lib/infrastructure/repositories/campaign-repository.js'; +import { userAnonymizedEventLoggingJobRepository } from '../../../../lib/infrastructure/repositories/jobs/user-anonymized-event-logging-job-repository.js'; +import * as membershipRepository from '../../../../lib/infrastructure/repositories/membership-repository.js'; import * as organizationLearnerRepository from '../../../../lib/infrastructure/repositories/organization-learner-repository.js'; import * as userRecommendedTrainingRepository from '../../../devcomp/infrastructure/repositories/user-recommended-training-repository.js'; import { repositories as campaignRepositories } from '../../../prescription/campaign/infrastructure/repositories/index.js'; @@ -21,6 +23,7 @@ import * as userLoginRepository from '../../../shared/infrastructure/repositorie 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 { certificationCenterMembershipRepository } from '../../../team/infrastructure/repositories/certification-center-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'; @@ -47,12 +50,15 @@ const repositories = { campaignParticipationRepository, campaignRepository, campaignToJoinRepository: campaignRepositories.campaignToJoinRepository, + certificationCenterMembershipRepository, emailValidationDemandRepository, eventLoggingJobRepository, + membershipRepository, oidcProviderRepository, organizationLearnerRepository, refreshTokenRepository, resetPasswordDemandRepository, + userAnonymizedEventLoggingJobRepository, userEmailRepository, userLoginRepository, userRecommendedTrainingRepository, diff --git a/api/src/identity-access-management/scripts/backfill-anonymized-users.js b/api/src/identity-access-management/scripts/backfill-anonymized-users.js index 5551724a2f2..f61ed4e36a5 100644 --- a/api/src/identity-access-management/scripts/backfill-anonymized-users.js +++ b/api/src/identity-access-management/scripts/backfill-anonymized-users.js @@ -1,11 +1,11 @@ import * as url from 'node:url'; import { disconnect } from '../../../db/knex-database-connection.js'; -import { usecases } from '../../../lib/domain/usecases/index.js'; import { withTransaction } from '../../shared/domain/DomainTransaction.js'; import { learningContentCache } from '../../shared/infrastructure/caches/learning-content-cache.js'; import { temporaryStorage } from '../../shared/infrastructure/temporary-storage/index.js'; import { logger } from '../../shared/infrastructure/utils/logger.js'; +import { usecases } from '../domain/usecases/index.js'; import { anonymizedUserRepository } from '../infrastructure/repositories/anonymized-user.repository.js'; const modulePath = url.fileURLToPath(import.meta.url); diff --git a/api/tests/identity-access-management/integration/domain/usecases/anonymize-user.usecase.test.js b/api/tests/identity-access-management/integration/domain/usecases/anonymize-user.usecase.test.js index 81926e21fd5..68d326712c0 100644 --- a/api/tests/identity-access-management/integration/domain/usecases/anonymize-user.usecase.test.js +++ b/api/tests/identity-access-management/integration/domain/usecases/anonymize-user.usecase.test.js @@ -1,10 +1,10 @@ -import { anonymizeUser } from '../../../../../lib/domain/usecases/anonymize-user.js'; import { userAnonymizedEventLoggingJobRepository } from '../../../../../lib/infrastructure/repositories/jobs/user-anonymized-event-logging-job-repository.js'; import * as membershipRepository from '../../../../../lib/infrastructure/repositories/membership-repository.js'; import * as organizationLearnerRepository from '../../../../../lib/infrastructure/repositories/organization-learner-repository.js'; import { PIX_ADMIN } from '../../../../../src/authorization/domain/constants.js'; import { RefreshToken } from '../../../../../src/identity-access-management/domain/models/RefreshToken.js'; import { UserAnonymizedEventLoggingJob } from '../../../../../src/identity-access-management/domain/models/UserAnonymizedEventLoggingJob.js'; +import { anonymizeUser } from '../../../../../src/identity-access-management/domain/usecases/anonymize-user.usecase.js'; import * as authenticationMethodRepository from '../../../../../src/identity-access-management/infrastructure/repositories/authentication-method.repository.js'; import { refreshTokenRepository } from '../../../../../src/identity-access-management/infrastructure/repositories/refresh-token.repository.js'; import { resetPasswordDemandRepository } from '../../../../../src/identity-access-management/infrastructure/repositories/reset-password-demand.repository.js'; diff --git a/api/tests/identity-access-management/unit/application/user/user.admin.controller.test.js b/api/tests/identity-access-management/unit/application/user/user.admin.controller.test.js index 84b68e748d3..282f156c350 100644 --- a/api/tests/identity-access-management/unit/application/user/user.admin.controller.test.js +++ b/api/tests/identity-access-management/unit/application/user/user.admin.controller.test.js @@ -1,4 +1,3 @@ -import { usecases as libUsecases } from '../../../../../lib/domain/usecases/index.js'; import { userAdminController } from '../../../../../src/identity-access-management/application/user/user.admin.controller.js'; import { User } from '../../../../../src/identity-access-management/domain/models/User.js'; import { usecases } from '../../../../../src/identity-access-management/domain/usecases/index.js'; @@ -207,7 +206,7 @@ describe('Unit | Identity Access Management | Application | Controller | Admin | const domainTransaction = { knexTransaction: Symbol('transaction'), }; - sinon.stub(libUsecases, 'anonymizeUser'); + sinon.stub(usecases, 'anonymizeUser'); sinon.stub(usecases, 'getUserDetailsForAdmin').resolves(userDetailsForAdmin); sinon.stub(DomainTransaction, 'execute').callsFake((callback) => { return callback(domainTransaction); @@ -227,7 +226,7 @@ describe('Unit | Identity Access Management | Application | Controller | Admin | // then expect(DomainTransaction.execute).to.have.been.called; - expect(libUsecases.anonymizeUser).to.have.been.calledWithExactly({ userId, updatedByUserId, domainTransaction }); + expect(usecases.anonymizeUser).to.have.been.calledWithExactly({ userId, updatedByUserId, domainTransaction }); expect(usecases.getUserDetailsForAdmin).to.have.been.calledWithExactly({ userId }); expect(userAnonymizedDetailsForAdminSerializer.serialize).to.have.been.calledWithExactly(userDetailsForAdmin); expect(response.statusCode).to.equal(200);