From 88eaa7362a2ae18146c5a82fa353cfe317d6c15d Mon Sep 17 00:00:00 2001 From: aliya Date: Tue, 18 Jul 2023 11:40:06 +0000 Subject: [PATCH] [FIX] account_peppol: add option to deregister Currently, users cannot deregister from Peppol. There is an option to migrate away (i.e. request a migration key) but not every provider supports migration. It is not straightforward to deregister from Peppol once a participant is marked as "prepared for migration", so the users really should deregister instead. Also, it is important to run the cron one last time for the user to fetch new documents/message status updates they've received since the last time the cron ran. closes odoo/odoo#128867 Signed-off-by: Laurent Smet (las) --- .../models/account_edi_proxy_user.py | 2 +- addons/account_peppol/models/res_company.py | 1 - .../models/res_config_settings.py | 38 ++++++++++++++----- .../res_config_settings_buttons.js | 23 +++++++++-- .../res_config_settings_buttons.xml | 12 +++++- 5 files changed, 59 insertions(+), 17 deletions(-) diff --git a/addons/account_peppol/models/account_edi_proxy_user.py b/addons/account_peppol/models/account_edi_proxy_user.py index 1e54d8e0e1e..9e7a14cc74c 100644 --- a/addons/account_peppol/models/account_edi_proxy_user.py +++ b/addons/account_peppol/models/account_edi_proxy_user.py @@ -82,7 +82,7 @@ class AccountEdiProxyClientUser(models.Model): message_uuids = [ message['uuid'] - for message in messages['messages'] + for message in messages.get('messages', []) if message['direction'] == 'incoming' and message['receiver'] == edi_user.edi_identification ] diff --git a/addons/account_peppol/models/res_company.py b/addons/account_peppol/models/res_company.py index 40f9fa50993..c9b21e2b30d 100644 --- a/addons/account_peppol/models/res_company.py +++ b/addons/account_peppol/models/res_company.py @@ -57,7 +57,6 @@ class ResCompany(models.Model): ('not_verified', 'Not verified'), ('sent_verification', 'Verification code sent'), ('pending', 'Pending'), - ('manually_approved', 'Approved'), ('active', 'Active'), ('rejected', 'Rejected'), ('canceled', 'Canceled'), diff --git a/addons/account_peppol/models/res_config_settings.py b/addons/account_peppol/models/res_config_settings.py index 6e845edf2e0..6ce09a2e55d 100644 --- a/addons/account_peppol/models/res_config_settings.py +++ b/addons/account_peppol/models/res_config_settings.py @@ -52,10 +52,11 @@ class ResConfigSettings(models.TransientModel): # HELPER METHODS # ------------------------------------------------------------------------- - def _call_peppol_proxy(self, endpoint, params, edi_user=None): + def _call_peppol_proxy(self, endpoint, params=None, edi_user=None): if not edi_user: edi_user = self.company_id.account_edi_proxy_client_ids[0] + params = params or {} try: response = edi_user._make_request( f"{edi_user._get_server_url()}{endpoint}", @@ -68,6 +69,12 @@ class ResConfigSettings(models.TransientModel): raise UserError(response['error'].get('message') or response['error']['data']['message']) return response + def _trigger_peppol_crons(self): + # fetch all documents and message statuses before migrating/deleting + # so that they are acknowledged + self.env['account_edi_proxy_client.user']._cron_peppol_get_message_status() + self.env['account_edi_proxy_client.user']._cron_peppol_get_new_documents() + # ------------------------------------------------------------------------- # COMPUTE METHODS # ------------------------------------------------------------------------- @@ -235,10 +242,7 @@ class ResConfigSettings(models.TransientModel): "Can't cancel registration with this status: %s", self.account_peppol_proxy_state )) - self._call_peppol_proxy( - endpoint='/api/peppol/1/cancel_peppol_registration', - params={}, - ) + self._call_peppol_proxy(endpoint='/api/peppol/1/cancel_peppol_registration') self.account_peppol_proxy_state = 'canceled' def button_reopen_peppol_registration(self): @@ -277,9 +281,25 @@ class ResConfigSettings(models.TransientModel): "Can't migrate registration with this status: %s", self.account_peppol_proxy_state )) - response = self._call_peppol_proxy( - endpoint='/api/peppol/1/migrate_peppol_registration', - params={}, - ) + self._trigger_peppol_crons() + + response = self._call_peppol_proxy(endpoint='/api/peppol/1/migrate_peppol_registration') self.account_peppol_proxy_state = 'canceled' self.account_peppol_migration_key = response['migration_key'] + + def button_deregister_peppol_participant(self): + """ + Deregister the edi user from Peppol network + """ + self.ensure_one() + + if self.account_peppol_proxy_state != 'active': + raise UserError(_( + "Can't deregister with this status: %s", self.account_peppol_proxy_state + )) + + self._trigger_peppol_crons() + + self._call_peppol_proxy(endpoint='/api/peppol/1/cancel_peppol_registration') + self.account_peppol_proxy_state = 'not_registered' + self.account_peppol_edi_user.unlink() diff --git a/addons/account_peppol/static/src/components/res_config_settings_buttons/res_config_settings_buttons.js b/addons/account_peppol/static/src/components/res_config_settings_buttons/res_config_settings_buttons.js index ea1417b9f3c..a2fbdf3eb79 100644 --- a/addons/account_peppol/static/src/components/res_config_settings_buttons/res_config_settings_buttons.js +++ b/addons/account_peppol/static/src/components/res_config_settings_buttons/res_config_settings_buttons.js @@ -50,21 +50,36 @@ class PeppolSettingsButtons extends Component { }); } - showConfirmation() { - const message = this.env._t("This will migrate your Peppol registration away from Odoo. You will not be able to send or receive Peppol documents in Odoo anymore."); - const confirmMessage = this.env._t("Are you sure you want to proceed?"); + showConfirmation(warning, methodName) { + const message = this.env._t(warning); + const confirmMessage = this.env._t("You will not be able to send or receive Peppol documents in Odoo anymore. Are you sure you want to proceed?"); this.dialogService.add(ConfirmationDialog, { body: markup( `
${escape(message)}
${escape(confirmMessage)}
` ), confirm: async () => { - await this._callConfigMethod("button_migrate_peppol_registration"); + await this._callConfigMethod(methodName); }, cancel: () => { }, }); } + migrate() { + this.showConfirmation( + "This will migrate your Peppol registration away from Odoo. A migration key will be generated. \ + If the other service does not support migration, consider deregistering instead.", + "button_migrate_peppol_registration" + ) + } + + deregister() { + this.showConfirmation( + "This will delete your Peppol registration.", + "button_deregister_peppol_participant" + ) + } + async updateDetails() { // avoid making users click save on the settings // and then clicking the update button diff --git a/addons/account_peppol/static/src/components/res_config_settings_buttons/res_config_settings_buttons.xml b/addons/account_peppol/static/src/components/res_config_settings_buttons/res_config_settings_buttons.xml index 54451fd2f01..ff3eb9c64ce 100644 --- a/addons/account_peppol/static/src/components/res_config_settings_buttons/res_config_settings_buttons.xml +++ b/addons/account_peppol/static/src/components/res_config_settings_buttons/res_config_settings_buttons.xml @@ -3,7 +3,7 @@ -
+
+
+ +