[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) <fda@odoo.com>
This commit is contained in:
Louis Baudoux
2023-09-04 15:02:50 +00:00
parent cea1cab535
commit 76f6257b6e
4 changed files with 56 additions and 44 deletions
+9 -13
View File
@@ -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))
+26 -7
View File
@@ -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)
@@ -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`<a class='btn btn-link' href='${notif.url}' target='_blank' ><i class='oi oi-arrow-right'></i> ${translatedText}</a>`;
const message = Markup`
<a class='btn btn-link' href='${params.get_credits_url}' target='_blank'>
<i class='oi oi-arrow-right'></i>
${translatedText}
</a>`;
notification.add(message, {
type: notif.error_type,
title: notif.title
title: params.title,
type: 'danger',
});
}
}
+4 -6
View File
@@ -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.<br>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.<br>Error: %s', self._get_error_message(error))
letter_data = {
'info_msg': note,