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 Dec 5, 2024
1 parent 2ac796c commit 62344b6
Show file tree
Hide file tree
Showing 2 changed files with 21 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
@@ -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 62344b6

Please sign in to comment.