summaryrefslogtreecommitdiff
path: root/addons
diff options
context:
space:
mode:
authorThibault Delavallée <tde@odoo.com>2020-04-30 13:27:26 +0000
committerThibault Delavallée <tde@odoo.com>2020-04-30 14:47:49 +0000
commitc5c7a02302f6095c2ecf1e215421a8c9ff630638 (patch)
treed106f752ab55b393b625f35f7b1b8325b16244fd /addons
parent9a3581d925af1a00849418608fd5db6d3cae203b (diff)
[FIX][IMP] sms: create failed notification when SMS-ing record without any phone number
When no valid phone number was found on a record, and when using _message_sms directly, no SMS and no notification was created. You could therefore think everything was ok while it was actually not. Instead we now create failed notifications. It allows to be notified of it and fix numbers through cancel / resend wizards. Task ID 2244192 closes odoo/odoo#50486 X-original-commit: baecdfaa2d31714a20d1fb8af7799af1805cd939 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
Diffstat (limited to 'addons')
-rw-r--r--addons/sms/models/mail_thread.py22
-rw-r--r--addons/sms/tests/common.py3
-rw-r--r--addons/test_mail_full/tests/test_sms_post.py8
3 files changed, 23 insertions, 10 deletions
diff --git a/addons/sms/models/mail_thread.py b/addons/sms/models/mail_thread.py
index e39eb5c029d..1db4ecd9d53 100644
--- a/addons/sms/models/mail_thread.py
+++ b/addons/sms/models/mail_thread.py
@@ -215,8 +215,12 @@ class MailThread(models.AbstractModel):
sms_pid_to_number[info_partner_ids[0]] = info_number
if info_partner_ids:
partner_ids = info_partner_ids + (partner_ids or [])
- if info_number and not info_partner_ids:
- sms_numbers = [info_number] + (sms_numbers or [])
+ if not info_partner_ids:
+ if info_number:
+ sms_numbers = [info_number] + (sms_numbers or [])
+ # will send a falsy notification allowing to fix it through SMS wizards
+ elif not sms_numbers:
+ sms_numbers = [False]
if subtype_id is False:
subtype_id = self.env['ir.model.data'].xmlid_to_res_id('mail.mt_note')
@@ -284,9 +288,14 @@ class MailThread(models.AbstractModel):
tocreate_numbers = [
value['sanitized'] or original
for original, value in sanitized.items()
- if value['code'] != 'empty'
]
- sms_create_vals += [dict(sms_base_vals, partner_id=False, number=n) for n in tocreate_numbers]
+ sms_create_vals += [dict(
+ sms_base_vals,
+ partner_id=False,
+ number=n,
+ state='outgoing' if n else 'error',
+ error_code='' if n else 'sms_number_missing',
+ ) for n in tocreate_numbers]
# create sms and notification
existing_pids, existing_numbers = [], []
@@ -313,7 +322,8 @@ class MailThread(models.AbstractModel):
'notification_type': 'sms',
'sms_id': sms.id,
'is_read': True, # discard Inbox notification
- 'notification_status': 'ready',
+ 'notification_status': 'ready' if sms.state == 'outgoing' else 'exception',
+ 'failure_type': '' if sms.state == 'outgoing' else sms.error_code,
} for sms in sms_all if (sms.partner_id and sms.partner_id.id not in existing_pids) or (not sms.partner_id and sms.number not in existing_numbers)]
if notif_create_values:
self.env['mail.notification'].sudo().create(notif_create_values)
@@ -332,6 +342,6 @@ class MailThread(models.AbstractModel):
})
if sms_all and not put_in_queue:
- sms_all.send(auto_commit=False, raise_exception=False)
+ sms_all.filtered(lambda sms: sms.state == 'outgoing').send(auto_commit=False, raise_exception=False)
return True
diff --git a/addons/sms/tests/common.py b/addons/sms/tests/common.py
index a5f4e511cfd..10335fc1890 100644
--- a/addons/sms/tests/common.py
+++ b/addons/sms/tests/common.py
@@ -130,6 +130,9 @@ class MockSMS(common.BaseCase):
"""
partners = self.env['res.partner'].concat(*list(p['partner'] for p in recipients_info if p.get('partner')))
numbers = [p['number'] for p in recipients_info if p.get('number')]
+ # special case of void notifications: check for False / False notifications
+ if not partners and not numbers:
+ numbers = [False]
base_domain = [
'|', ('res_partner_id', 'in', partners.ids),
'&', ('res_partner_id', '=', False), ('sms_number', 'in', numbers),
diff --git a/addons/test_mail_full/tests/test_sms_post.py b/addons/test_mail_full/tests/test_sms_post.py
index 26b5c5268b5..e0c67d1407f 100644
--- a/addons/test_mail_full/tests/test_sms_post.py
+++ b/addons/test_mail_full/tests/test_sms_post.py
@@ -105,9 +105,8 @@ class TestSMSPost(test_mail_full_common.TestSMSCommon, test_mail_full_common.Tes
with self.mockSMSGateway():
messages = record._message_sms(self._test_body)
- # should not crash but no sms / no recipients
- notifs = self.env['mail.notification'].search([('mail_message_id', 'in', messages.ids)])
- self.assertFalse(notifs)
+ # should not crash but have a failed notification
+ self.assertSMSNotification([{'partner': self.env['res.partner'], 'number': False, 'state': 'exception', 'failure_type': 'sms_number_missing'}], self._test_body, messages)
def test_message_sms_on_field_w_partner(self):
with self.with_user('employee'), self.mockSMSGateway():
@@ -137,7 +136,8 @@ class TestSMSPost(test_mail_full_common.TestSMSCommon, test_mail_full_common.Tes
test_record = self.env['mail.test.sms'].browse(self.test_record.id)
messages = test_record._message_sms(self._test_body)
- self.assertNoSMSNotification(messages)
+ # should not crash but have a failed notification
+ self.assertSMSNotification([{'partner': self.env['res.partner'], 'number': False, 'state': 'exception', 'failure_type': 'sms_number_missing'}], self._test_body, messages)
def test_message_sms_on_field_wo_partner_default_field(self):
self.test_record.write({'customer_id': False})