Skip to content

Commit

Permalink
feat(mon-pix): call anonymize API from Pix App modal dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
lego-technix committed Nov 29, 2024
1 parent 4ca36b2 commit 5245b78
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 9 deletions.
37 changes: 36 additions & 1 deletion mon-pix/app/components/user-account/delete-account-section.gjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import PixButton from '@1024pix/pix-ui/components/pix-button';
import PixMessage from '@1024pix/pix-ui/components/pix-message';
import PixModal from '@1024pix/pix-ui/components/pix-modal';
import { action } from '@ember/object';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { t } from 'ember-intl';
import ENV from 'mon-pix/config/environment';

export default class DeleteAccountSection extends Component {
@service url;
@service requestManager;
@service router;

@tracked modalOpen = false;
@tracked isLoading = false;
@tracked globalError;

get supportHomeUrl() {
return this.url.supportHomeUrl;
Expand All @@ -28,6 +35,26 @@ export default class DeleteAccountSection extends Component {
this.modalOpen = false;
}

@action
async selfDeleteUserAccount() {
try {
await this.requestManager.request({
url: `${ENV.APP.API_HOST}/api/users/me`,
method: 'DELETE',
});

this.router.replaceWith('logout');
} catch (error) {
if (error.status === 403) {
this.globalError = 'pages.user-account.delete-account.modal.error-403';
} else {
this.globalError = 'common.api-error-messages.internal-server-error';
}
} finally {
this.isLoading = false;
}
}

<template>
<section class="delete-account-section">
<h2>{{t "pages.user-account.delete-account.title"}}</h2>
Expand Down Expand Up @@ -84,13 +111,21 @@ export default class DeleteAccountSection extends Component {
{{/if}}
<p>{{t "pages.user-account.delete-account.modal.warning-1"}}</p>
<p>{{t "pages.user-account.delete-account.modal.warning-2"}}</p>

{{#if this.globalError}}
<PixMessage @type="error" @withIcon={{true}} role="alert">
{{t this.globalError}}
</PixMessage>
{{/if}}
</:content>

<:footer>
<PixButton @variant="secondary" @isBorderVisible={{true}} @triggerAction={{this.closeModal}}>
{{t "common.actions.cancel"}}
</PixButton>
<PixButton @variant="error">{{t "pages.user-account.delete-account.actions.delete"}}</PixButton>
<PixButton @variant="error" @triggerAction={{this.selfDeleteUserAccount}} @isLoading={{this.isLoading}}>{{t
"pages.user-account.delete-account.actions.delete"
}}</PixButton>
</:footer>
</PixModal>
</section>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { clickByName, render, within } from '@1024pix/ember-testing-library';
import { click } from '@ember/test-helpers';
import { t } from 'ember-intl/test-support';
import DeleteAccountSection from 'mon-pix/components/user-account/delete-account-section';
import { module, test } from 'qunit';
import sinon from 'sinon';

import setupIntlRenderingTest from '../../../helpers/setup-intl-rendering';
import { waitForDialog, waitForDialogClose } from '../../../helpers/wait-for.js';
Expand Down Expand Up @@ -126,4 +128,114 @@ module('Integration | Component | UserAccount | DeleteAccountSection', function
assert.ok(true);
});
});

module('selfDeleteUserAccount button', function (hooks) {
let requestManagerService;
let router;

hooks.beforeEach(function () {
requestManagerService = this.owner.lookup('service:requestManager');
sinon.stub(requestManagerService, 'request');

router = this.owner.lookup('service:router');
router.replaceWith = sinon.stub();
});

module('when the action is a success', function () {
test('it logouts the user', async function (assert) {
// given
requestManagerService.request.resolves({ response: { ok: true, status: 204 } });

const user = {
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
profile: { pixScore: 42 },
};
const screen = await render(<template><DeleteAccountSection @user={{user}} /></template>);

await clickByName(t('pages.user-account.delete-account.actions.delete'));
await waitForDialog();

// when
const dialog = screen.getByRole('dialog');
const button = within(dialog).getByRole('button', {
name: t('pages.user-account.delete-account.actions.delete'),
});
await click(button);

// then
sinon.assert.calledWithExactly(router.replaceWith, 'logout');
assert.ok(true);
});
});

module('when the user is not allowed to self delete their account', function () {
test('it displays a forbidden message', async function (assert) {
// given
requestManagerService.request.rejects({ status: 403 });

const user = {
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
profile: { pixScore: 42 },
};
const screen = await render(<template><DeleteAccountSection @user={{user}} /></template>);

await clickByName(t('pages.user-account.delete-account.actions.delete'));
await waitForDialog();

// when
const dialog = screen.getByRole('dialog');
const button = within(dialog).getByRole('button', {
name: t('pages.user-account.delete-account.actions.delete'),
});
await click(button);

// then
assert
.dom(
screen.getByRole('alert', {
value: t('pages.user-account.delete-account.modal.error-403'),
}),
)
.exists();
});
});

module('when there is an internal server error', function () {
test('it displays an internal server error message', async function (assert) {
// given
requestManagerService.request.rejects({ status: 500 });

const user = {
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
profile: { pixScore: 42 },
};
const screen = await render(<template><DeleteAccountSection @user={{user}} /></template>);

await clickByName(t('pages.user-account.delete-account.actions.delete'));
await waitForDialog();

// when
const dialog = screen.getByRole('dialog');
const button = within(dialog).getByRole('button', {
name: t('pages.user-account.delete-account.actions.delete'),
});
await click(button);

// then
assert
.dom(
screen.getByRole('alert', {
value: t('common.api-error-messages.internal-server-error'),
}),
)
.exists();
});
});
});
});
7 changes: 4 additions & 3 deletions mon-pix/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
},
"errors": {
"invalid-locale-format": "Provided locale \"{invalidLocale}\" has an invalid format.",
"invalid-or-already-used-email" : "Invalid or already used e-mail address",
"invalid-or-already-used-email": "Invalid or already used e-mail address",
"locale-not-supported": "Provided locale \"{localeNotSupported}\" isn't currently supported."
},
"fields": {
Expand Down Expand Up @@ -366,7 +366,7 @@
"new-email": ",if not, enter a new one."
},
"empty-email": "The e-mail address field is mandatory.",
"invalid-or-already-used-email" : "Invalid or already used e-mail address",
"invalid-or-already-used-email": "Invalid or already used e-mail address",
"new-backup-email": "Please enter a valid e-mail address to recover your account",
"new-email-already-exist": "This e-mail address is already in use",
"wrong-email-format": "Your e-mail address is invalid."
Expand Down Expand Up @@ -2100,7 +2100,7 @@
"errors": {
"empty-password": "Your password can't be empty.",
"invalid-email": "Your email address is invalid.",
"invalid-or-already-used-email" : "Invalid or already used e-mail address",
"invalid-or-already-used-email": "Invalid or already used e-mail address",
"invalid-password": "There was an error in the password entered.",
"new-email-already-exist": "This email address is already in use."
},
Expand Down Expand Up @@ -2132,6 +2132,7 @@
},
"menu-link-title": "Delete my account",
"modal": {
"error-403": "You are not allowed to delete your account. Please contact the support.",
"question": "Are you sure you want to delete your account?",
"title": "You’re about to delete your account",
"warning-1": "Please note that the account cannot be recovered after deletion.",
Expand Down
3 changes: 2 additions & 1 deletion mon-pix/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -2146,7 +2146,8 @@
"title": "You’re about to delete your account",
"question": "Are you sure you want to delete your account?",
"warning-1": "Please note that the account cannot be recovered after deletion.",
"warning-2": "As soon as you confirm, you will be automatically disconnected and your request will be taken into account immediately."
"warning-2": "As soon as you confirm, you will be automatically disconnected and your request will be taken into account immediately.",
"error-403": "You are not allowed to delete your account. Please contact the support."
},
"more-information": "For more information, ",
"more-information-contact-support": "please contact support.",
Expand Down
7 changes: 4 additions & 3 deletions mon-pix/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
},
"errors": {
"invalid-locale-format": "Votre locale \"{invalidLocale}\" n'est pas dans le bon format.",
"invalid-or-already-used-email" : "Adresse e-mail invalide ou déjà utilisée",
"invalid-or-already-used-email": "Adresse e-mail invalide ou déjà utilisée",
"locale-not-supported": "Votre locale \"{localeNotSupported}\" n'est actuellement pas supportée."
},
"fields": {
Expand Down Expand Up @@ -366,7 +366,7 @@
"new-email": ", sinon saisissez une nouvelle."
},
"empty-email": "Le champ adresse e-mail est obligatoire.",
"invalid-or-already-used-email" : "Adresse e-mail invalide ou déjà utilisée",
"invalid-or-already-used-email": "Adresse e-mail invalide ou déjà utilisée",
"new-backup-email": "Veuillez saisir un e-mail valide pour récupérer votre compte",
"new-email-already-exist": "Cette adresse e-mail est déjà utilisée",
"wrong-email-format": "Votre adresse e-mail n’est pas valide."
Expand Down Expand Up @@ -2101,7 +2101,7 @@
"errors": {
"empty-password": "Votre mot de passe ne peut pas être vide.",
"invalid-email": "Votre adresse e-mail n’est pas valide.",
"invalid-or-already-used-email" : "Adresse e-mail invalide ou déjà utilisée",
"invalid-or-already-used-email": "Adresse e-mail invalide ou déjà utilisée",
"invalid-password": "Le mot de passe que vous avez saisi est invalide.",
"new-email-already-exist": "Cette adresse e-mail est déjà utilisée."
},
Expand Down Expand Up @@ -2133,6 +2133,7 @@
},
"menu-link-title": "Supprimer mon compte",
"modal": {
"error-403": "Vous ne pouvez pas supprimer votre compte. Veuillez contacter le support de Pix.",
"question": "Êtes-vous sûr(e) de vouloir supprimer votre compte ?",
"title": "Vous êtes sur le point de supprimer votre compte",
"warning-1": "Veuillez noter que le compte ne pourra pas être récupéré après la suppression.",
Expand Down
3 changes: 2 additions & 1 deletion mon-pix/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -2142,7 +2142,8 @@
"title": "You’re about to delete your account",
"question": "Are you sure you want to delete your account?",
"warning-1": "Please note that the account cannot be recovered after deletion.",
"warning-2": "As soon as you confirm, you will be automatically disconnected and your request will be taken into account immediately."
"warning-2": "As soon as you confirm, you will be automatically disconnected and your request will be taken into account immediately.",
"error-403": "You are not allowed to delete your account. Please contact the support."
},
"more-information": "For more information, ",
"more-information-contact-support": "please contact support.",
Expand Down

0 comments on commit 5245b78

Please sign in to comment.