[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) <las@odoo.com>
This commit is contained in:
aliya
2023-07-19 11:40:06 +02:00
parent 1586274912
commit 88eaa7362a
5 changed files with 59 additions and 17 deletions
@@ -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
]
@@ -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'),
@@ -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()
@@ -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(
`<div class="text-danger">${escape(message)}</div>
<div class="text-danger">${escape(confirmMessage)}</div>`
),
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
@@ -3,7 +3,7 @@
<templates>
<t t-name="account_peppol.ActionButtons" owl="1">
<xpath expr="//div[hasclass('action_buttons')]" position="inside">
<div class="d-flex" colspan="2">
<div class="d-flex" colspan="3">
<div class="mt-3">
<button type="button"
class="btn btn-primary"
@@ -15,11 +15,19 @@
<div class="mt-3">
<button type="button"
class="btn btn-secondary me-1"
t-on-click="showConfirmation"
t-on-click="migrate"
t-if="proxyState === 'active'">
Migrate registration to another service
</button>
</div>
<div class="mt-3">
<button type="button"
class="btn btn-secondary me-1"
t-on-click="deregister"
t-if="proxyState === 'active'">
Deregister from Peppol
</button>
</div>
<div class="mt-3">
<button type="button"
class="btn btn-primary"