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 3371d46 commit 1676045
Show file tree
Hide file tree
Showing 3 changed files with 23 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,
}),
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 1676045

Please sign in to comment.