Skip to content

Commit

Permalink
feat(api): send notification email for route DELETE /api/users/me
Browse files Browse the repository at this point in the history
  • Loading branch information
lego-technix committed Nov 29, 2024
1 parent 5245b78 commit 9c4a02e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }),
},
});
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,15 +11,31 @@ import { ForbiddenAccess } from '../../../shared/domain/errors.js';
* @param{PrivacyUsersApiRepository} privacyUsersApiRepository
* @returns {Promise<boolean>}
*/
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,
}),
);
};

0 comments on commit 9c4a02e

Please sign in to comment.