From 06ec579cf71d8f45172036f888cb5027cfefe65f Mon Sep 17 00:00:00 2001 From: "Andrzej(pian)" Date: Fri, 9 Feb 2024 11:24:00 +0100 Subject: [PATCH] [FIX] sms: prevent sms duplication when using additional numbers When sending an SMS via the action from the sale order view (specifically with sale_subscription), it is possible to specify a number to send the SMS to. However, if the specified number is identical to the partner's number (the number of the sale order's customer), Odoo attempts to send the message twice, resulting in duplication. [This commit change] This commit addresses this issue by ensuring that additional numbers are skipped if they are the same as the partner's number. [Reproduce] - Install mass_mailing_sms, sale_management, and sale_subscription modules. - Add an SMS token to the IAP account. - Create a contact (C) with a valid phone number. - Create a new quotation with contact (C) as the partner. - Go to Actions > "Send an SMS Text Message" (requires the sale_subscription module). - Do not change the contact number on the pop-up (ensure it matches C's phone number exactly). - Bug: Odoo attempts to send two SMS messages, with the first being successful and the second resulting in an error. opw-3596207 closes odoo/odoo#159677 X-original-commit: 131b0eccb00d24f0525a6a541221bfd2c66cb04b Signed-off-by: Thibault Delavallee (tde) --- addons/sms/models/mail_thread.py | 3 ++- addons/test_mail_sms/tests/test_sms_post.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/addons/sms/models/mail_thread.py b/addons/sms/models/mail_thread.py index 6ac839e5201..d1cbe67ba38 100644 --- a/addons/sms/models/mail_thread.py +++ b/addons/sms/models/mail_thread.py @@ -275,13 +275,14 @@ class MailThread(models.AbstractModel): self._phone_format(number=sms_number) or sms_number for sms_number in sms_numbers ] + existing_partners_numbers = {vals_dict['number'] for vals_dict in sms_create_vals} sms_create_vals += [dict( sms_base_vals, partner_id=False, number=n, state='outgoing' if n else 'error', failure_type='' if n else 'sms_number_missing', - ) for n in tocreate_numbers] + ) for n in tocreate_numbers if n not in existing_partners_numbers] # create sms and notification existing_pids, existing_numbers = [], [] diff --git a/addons/test_mail_sms/tests/test_sms_post.py b/addons/test_mail_sms/tests/test_sms_post.py index c8f9daeeb0e..888c8a72690 100644 --- a/addons/test_mail_sms/tests/test_sms_post.py +++ b/addons/test_mail_sms/tests/test_sms_post.py @@ -57,6 +57,24 @@ class TestSMSPost(SMSCommon, TestSMSRecipients, CronMixinCase): self.assertSMSNotification([{'partner': self.partner_1}, {'number': self.random_numbers_san[0]}, {'number': self.random_numbers_san[1]}], self._test_body, messages) + def test_message_sms_internals_sms_numbers_duplicate(self): + """ _message_sms ( which uses _notify_thread_by_sms) allows for specifying additional number to send sms to + This test checks for situation where this additional number is the same as partner telephone number. + In that case sms shall NOT be sent twice.""" + with self.with_user('employee'), self.mockSMSGateway(): + test_record = self.env['mail.test.sms'].browse(self.test_record.id) + additional_number_same_as_partner_number = self.partner_1.mobile + subtype_id = self.env['ir.model.data']._xmlid_to_res_id('mail.mt_note') + test_record._message_sms( + body=self._test_body, + partner_ids=self.partner_1.ids, + subtype_id=subtype_id, + sms_numbers=[additional_number_same_as_partner_number], + number_field='mobile' + ) + self.assertEqual(len(self._new_sms.filtered(lambda s: s.number == self.partner_numbers[0])), 1, + "There should be one message sent if additional number is the same as partner number") + def test_message_sms_internals_subtype(self): with self.with_user('employee'), self.mockSMSGateway(): test_record = self.env['mail.test.sms'].browse(self.test_record.id)