From 16760458473fcd58da129399b49373d3dc26633a Mon Sep 17 00:00:00 2001 From: LEGO Technix <109212476+lego-technix@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:06:34 +0100 Subject: [PATCH] feat(api): send notification email for route DELETE /api/users/me --- .../application/user/user.controller.js | 5 +++-- .../self-delete-user-account.usecase.js | 19 ++++++++++++++++++- .../self-delete-user-account.usecase.test.js | 2 ++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/api/src/identity-access-management/application/user/user.controller.js b/api/src/identity-access-management/application/user/user.controller.js index 5d0d3aebccf..ceaaf2a384f 100644 --- a/api/src/identity-access-management/application/user/user.controller.js +++ b/api/src/identity-access-management/application/user/user.controller.js @@ -203,10 +203,11 @@ const rememberUserHasSeenLastDataProtectionPolicyInformation = async function ( return dependencies.userSerializer.serialize(updatedUser); }; -const selfDeleteUserAccount = async function (request, h) { +const selfDeleteUserAccount = async function (request, h, dependencies = { requestResponseUtils }) { const authenticatedUserId = request.auth.credentials.userId; + const localeFromHeader = dependencies.requestResponseUtils.extractLocaleFromRequest(request); - await usecases.selfDeleteUserAccount({ userId: authenticatedUserId }); + await usecases.selfDeleteUserAccount({ userId: authenticatedUserId, localeFromHeader }); return h.response().code(204); }; diff --git a/api/src/identity-access-management/domain/usecases/self-delete-user-account.usecase.js b/api/src/identity-access-management/domain/usecases/self-delete-user-account.usecase.js index 6b9a4e9b5ff..d79d55e02de 100644 --- a/api/src/identity-access-management/domain/usecases/self-delete-user-account.usecase.js +++ b/api/src/identity-access-management/domain/usecases/self-delete-user-account.usecase.js @@ -1,4 +1,5 @@ import { ForbiddenAccess } from '../../../shared/domain/errors.js'; +import { createSelfDeleteUserAccountEmail } from '../emails/create-self-delete-user-account.email.js'; /** * @typedef {import('../../infrastructure/repositories/privacy-users-api.repository.js')} PrivacyUsersApiRepository @@ -10,15 +11,31 @@ import { ForbiddenAccess } from '../../../shared/domain/errors.js'; * @param{PrivacyUsersApiRepository} privacyUsersApiRepository * @returns {Promise} */ -export const selfDeleteUserAccount = async function ({ userId, privacyUsersApiRepository }) { +export const selfDeleteUserAccount = async function ({ + userId, + localeFromHeader, + userRepository, + privacyUsersApiRepository, + emailRepository, +}) { const canSelfDeleteAccount = await privacyUsersApiRepository.canSelfDeleteAccount({ userId }); if (!canSelfDeleteAccount) { throw new ForbiddenAccess(); } + const user = await userRepository.get(userId); + const anonymizedByUserId = userId; const anonymizedByUserRole = 'USER'; const client = 'PIX_APP'; await privacyUsersApiRepository.anonymizeUser({ userId, anonymizedByUserId, anonymizedByUserRole, client }); + + await emailRepository.sendEmailAsync( + createSelfDeleteUserAccountEmail({ + locale: localeFromHeader, + email: user.email, + firstName: user.firstName, + }), + ); }; diff --git a/api/tests/identity-access-management/integration/domain/usecases/self-delete-user-account.usecase.test.js b/api/tests/identity-access-management/integration/domain/usecases/self-delete-user-account.usecase.test.js index c7247aa3df9..1cef68995b9 100644 --- a/api/tests/identity-access-management/integration/domain/usecases/self-delete-user-account.usecase.test.js +++ b/api/tests/identity-access-management/integration/domain/usecases/self-delete-user-account.usecase.test.js @@ -14,6 +14,8 @@ describe('Integration | Identity Access Management | Domain | UseCase | self-del // when & then await expect(usecases.selfDeleteUserAccount({ userId })).to.not.be.rejectedWith(ForbiddenAccess); + + await expect('SendEmailJob').to.have.been.performed.withJobsCount(1); }); });