From 76f6257b6e7204c7a83818b0ef8ed245ea781e68 Mon Sep 17 00:00:00 2001 From: Louis Baudoux Date: Thu, 17 Aug 2023 16:18:04 +0200 Subject: [PATCH] [IMP] crm_iap_enrich,iap_mail,snailmail: improve IAP notification system The old implementation had some inconsistencies and didn't allow to have general error notifications, it was only meant for credit error notifications. The notification/log messages of lead enrichment were also improved as some of them were unclear. closes odoo/odoo#132233 Related: odoo/enterprise#44488 Signed-off-by: Florian Daloze (fda) --- addons/crm_iap_enrich/models/crm_lead.py | 22 +++++------- addons/iap_mail/models/iap_account.py | 33 +++++++++++++---- .../js/services/iap_notification_service.js | 35 +++++++++---------- addons/snailmail/models/snailmail_letter.py | 10 +++--- 4 files changed, 56 insertions(+), 44 deletions(-) diff --git a/addons/crm_iap_enrich/models/crm_lead.py b/addons/crm_iap_enrich/models/crm_lead.py index 8c65cd6beba..4917458e308 100644 --- a/addons/crm_iap_enrich/models/crm_lead.py +++ b/addons/crm_iap_enrich/models/crm_lead.py @@ -89,27 +89,23 @@ class Lead(models.Model): try: iap_response = self.env['iap.enrich.api']._request_enrich(lead_emails) except iap_tools.InsufficientCreditError: - _logger.info('Sent batch %s enrich requests: failed because of credit', len(lead_emails)) + _logger.info('Lead enrichment failed because of insufficient credit') if not from_cron: - self.env['iap.account']._send_iap_bus_notification( + self.env['iap.account']._send_no_credit_notification( service_name='reveal', - title=_("Not enough credits for Lead Enrichment"), - error_type='credit') + title=_("Not enough credits for Lead Enrichment")) # Since there are no credits left, there is no point to process the other batches break except Exception as e: if not from_cron: - self.env['iap.account']._send_iap_bus_notification( - service_name='reveal', - error_type="exception", - title=_('Sent batch %s enrich requests: failed with exception %s', len(lead_emails), e)) - _logger.info('Sent batch %s enrich requests: failed with exception %s', len(lead_emails), e) + self.env['iap.account']._send_error_notification( + message=_('An error occurred during lead enrichment')) + _logger.info('An error occurred during lead enrichment: %s', e) else: if not from_cron: - self.env['iap.account']._send_iap_bus_notification( - service_name='reveal', - title=_("The leads/opportunities have successfully been enriched")) - _logger.info('Sent batch %s enrich requests: success', len(lead_emails)) + self.env['iap.account']._send_success_notification( + message=_("The leads/opportunities have successfully been enriched")) + _logger.info('Batch of %s leads successfully enriched', len(lead_emails)) self._iap_enrich_from_response(iap_response) except OperationalError: _logger.error('A batch of leads could not be enriched :%s', repr(leads)) diff --git a/addons/iap_mail/models/iap_account.py b/addons/iap_mail/models/iap_account.py index 18f5de32c11..a3d899c2b56 100644 --- a/addons/iap_mail/models/iap_account.py +++ b/addons/iap_mail/models/iap_account.py @@ -7,11 +7,30 @@ class IapAccount(models.Model): _inherit = 'iap.account' @api.model - def _send_iap_bus_notification(self, service_name, title, error_type=False): - param = { - 'title': title, - 'error_type': 'danger' if error_type else 'success' + def _send_success_notification(self, message, title=None): + self._send_status_notification(message, 'success', title=title) + + @api.model + def _send_error_notification(self, message, title=None): + self._send_status_notification(message, 'danger', title=title) + + @api.model + def _send_status_notification(self, message, status, title=None): + params = { + 'message': message, + 'type': status, } - if error_type == 'credit': - param['url'] = self.env['iap.account'].get_credits_url(service_name) - self.env['bus.bus']._sendone(self.env.user.partner_id, 'iap_notification', param) + + if title is not None: + params['title'] = title + + self.env['bus.bus']._sendone(self.env.user.partner_id, 'iap_notification', params) + + @api.model + def _send_no_credit_notification(self, service_name, title): + params = { + 'title': title, + 'type': 'no_credit', + 'get_credits_url': self.env['iap.account'].get_credits_url(service_name), + } + self.env['bus.bus']._sendone(self.env.user.partner_id, 'iap_notification', params) diff --git a/addons/iap_mail/static/src/js/services/iap_notification_service.js b/addons/iap_mail/static/src/js/services/iap_notification_service.js index e7fc9c65408..aa954d092d4 100644 --- a/addons/iap_mail/static/src/js/services/iap_notification_service.js +++ b/addons/iap_mail/static/src/js/services/iap_notification_service.js @@ -8,35 +8,34 @@ export const iapNotificationService = { dependencies: ["bus_service", "notification"], start(env, { bus_service, notification }) { - bus_service.subscribe("iap_notification", (payload) => { - if (payload.error_type == "success") { - displaySuccessIapNotification(payload); - } else if (payload.error_type == "danger") { - displayFailureIapNotification(payload); + bus_service.subscribe("iap_notification", (params) => { + if (params.type == "no_credit") { + displayCreditErrorNotification(params); + } else { + displayNotification(params); } }); bus_service.start(); - /** - * Displays the IAP success notification on user's screen - */ - function displaySuccessIapNotification(notif) { - notification.add(notif.title, { - type: notif.error_type, + function displayNotification(params) { + notification.add(params.message, { + title: params.title, + type: params.type, }); } - /** - * Displays the IAP failure notification on user's screen - */ - function displayFailureIapNotification(notif) { + function displayCreditErrorNotification(params) { // ℹ️ `_t` can only be inlined directly inside JS template literals // after Babel has been updated to version 2.12. const translatedText = _t("Buy more credits"); - const message = Markup` ${translatedText}`; + const message = Markup` + + + ${translatedText} + `; notification.add(message, { - type: notif.error_type, - title: notif.title + title: params.title, + type: 'danger', }); } } diff --git a/addons/snailmail/models/snailmail_letter.py b/addons/snailmail/models/snailmail_letter.py index d84ad412315..4b3373479f1 100644 --- a/addons/snailmail/models/snailmail_letter.py +++ b/addons/snailmail/models/snailmail_letter.py @@ -381,9 +381,8 @@ class SnailmailLetter(models.Model): raise ae for doc in response['request']['documents']: if doc.get('sent') and response['request_code'] == 200: - self.env['iap.account']._send_iap_bus_notification( - service_name='snailmail', - title=_("Snail Mails are successfully sent")) + self.env['iap.account']._send_success_notification( + message=_("Snail Mails are successfully sent")) note = _('The document was correctly sent by post.
The tracking id is %s', doc['send_id']) letter_data = {'info_msg': note, 'state': 'sent', 'error_code': False} notification_data = { @@ -395,10 +394,9 @@ class SnailmailLetter(models.Model): error = doc['error'] if response['request_code'] == 200 else response['reason'] if error == 'CREDIT_ERROR': - self.env['iap.account']._send_iap_bus_notification( + self.env['iap.account']._send_no_credit_notification( service_name='snailmail', - title=_("Not enough credits for Snail Mail"), - error_type="credit") + title=_("Not enough credits for Snail Mail")) note = _('An error occurred when sending the document by post.
Error: %s', self._get_error_message(error)) letter_data = { 'info_msg': note,