From 9c4a02e71ab1e455fcc94827469f4e918187e8e2 Mon Sep 17 00:00:00 2001 From: LEGO Technix <109212476+lego-technix@users.noreply.github.com> Date: Fri, 29 Nov 2024 14:29:26 +0100 Subject: [PATCH] feat(api): send notification email for route DELETE /api/users/me --- .../application/user/user.controller.js | 5 +-- .../create-self-delete-user-account.email.js | 34 +++++++++++++++++++ .../self-delete-user-account.usecase.js | 19 ++++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 api/src/identity-access-management/domain/emails/create-self-delete-user-account.email.js 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/emails/create-self-delete-user-account.email.js b/api/src/identity-access-management/domain/emails/create-self-delete-user-account.email.js new file mode 100644 index 00000000000..dcbe0668680 --- /dev/null +++ b/api/src/identity-access-management/domain/emails/create-self-delete-user-account.email.js @@ -0,0 +1,34 @@ +import { urlBuilder } from '../../../shared/infrastructure/utils/url-builder.js'; +import { EmailFactory } from '../../../shared/mail/domain/models/EmailFactory.js'; +import { mailer } from '../../../shared/mail/infrastructure/services/mailer.js'; + +export function createSelfDeleteUserAccountEmail({ locale, email, firstName, token, redirectionUrl }) { + const factory = new EmailFactory({ app: 'pix-app', locale }); + + const { i18n, defaultVariables } = factory; + + const redirectUrl = redirectionUrl || defaultVariables.pixAppConnectionUrl; + + return factory.buildEmail({ + template: mailer.accountCreationTemplateId, + subject: i18n.__('pix-account-creation-email.subject'), + to: email, + variables: { + homeName: defaultVariables.homeName, + homeUrl: defaultVariables.homeUrl, + helpdeskUrl: defaultVariables.helpdeskUrl, + displayNationalLogo: defaultVariables.displayNationalLogo, + askForHelp: i18n.__('pix-account-creation-email.params.askForHelp'), + disclaimer: i18n.__('pix-account-creation-email.params.disclaimer'), + doNotAnswer: i18n.__('pix-account-creation-email.params.doNotAnswer'), + goToPix: i18n.__('pix-account-creation-email.params.goToPix'), + helpdeskLinkLabel: i18n.__('pix-account-creation-email.params.helpdeskLinkLabel'), + moreOn: i18n.__('pix-account-creation-email.params.moreOn'), + pixPresentation: i18n.__('pix-account-creation-email.params.pixPresentation'), + subtitle: i18n.__('pix-account-creation-email.params.subtitle'), + subtitleDescription: i18n.__('pix-account-creation-email.params.subtitleDescription'), + title: i18n.__('pix-account-creation-email.params.title', { firstName }), + redirectionUrl: urlBuilder.getEmailValidationUrl({ locale, redirectUrl, token }), + }, + }); +} 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, + }), + ); };