From a3cd5a883b809237150e0da3247f68f02f4b4c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Fri, 24 May 2024 09:45:57 +0200 Subject: [PATCH] [IMP] resource_booking: Add support for mail_note subtype by default If the default mail_note subtype is defined, when generating the calendar.event is generated and emails are sent to attendees, the mail_notify_author because each email would also be sent to the author (the event organizer). Now it is changed to use a specific context and only send the email to the author if it is the the recipient of the email (the email of their attendance to the event). Related to https://github.com/OCA/calendar/commit/575d2aa6d24d26ead89995d96e9709cdd5d9975a TT49045 --- resource_booking/models/calendar_event.py | 25 +++++++++-- resource_booking/tests/test_backend.py | 52 ++++++++++++++++++++++- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/resource_booking/models/calendar_event.py b/resource_booking/models/calendar_event.py index 12a4c7b2..c347bed8 100644 --- a/resource_booking/models/calendar_event.py +++ b/resource_booking/models/calendar_event.py @@ -52,13 +52,32 @@ def write(self, vals): rescheduled._validate_booking_modifications() return result + def _notify_thread(self, message, msg_vals=False, **kwargs): + """If we are creating the calendar event from the resource booking + (detected from the resource_booking_event context key), we need to + inject the standard mail context `mail_notify_author` to super to get + the own author notified when someone books a reservation, but only + in the case that the mail is being sent to them, as if not the author + may receive one copy per each of the attendees. This happens only when + the the subtype not is enabled by default in the instance. + """ + if self.env.context.get("resource_booking_event") and msg_vals.get( + "author_id" + ) in msg_vals.get("partner_ids", []): + self = self.with_context(mail_notify_author=True) + return super()._notify_thread(message=message, msg_vals=msg_vals, **kwargs) + @api.model_create_multi def create(self, vals_list): """Transfer resource booking to _attendees_values by context. We need to serialize the creation in that case. - mail_notify_author key from context is necessary to force the notification - to be sent to author. + resource_booking_event custom key from context is necessary. + We cannot use mail_notify_author key in the context because if the mail_note + subtype is set by default, the email of each attendee would be sent also to + the author (example: a meeting with 2 attendees would send 2 emails but + each of them would be sent to the partner of the attendee + author of + the email). """ vals_list2 = [] records = self.env["calendar.event"] @@ -68,7 +87,7 @@ def create(self, vals_list): CalendarEvent, self.with_context( resource_booking_ids=vals["resource_booking_ids"], - mail_notify_author=True, + resource_booking_event=True, ), ).create(vals) else: diff --git a/resource_booking/tests/test_backend.py b/resource_booking/tests/test_backend.py index 595db4aa..55d2c991 100644 --- a/resource_booking/tests/test_backend.py +++ b/resource_booking/tests/test_backend.py @@ -23,14 +23,16 @@ _2dt = fields.Datetime.to_datetime -@freeze_time("2021-02-26 09:00:00", tick=True) # Last Friday of February -class BackendCase(TransactionCase): +class BackendCaseBase(TransactionCase): @classmethod def setUpClass(cls): super().setUpClass() create_test_data(cls) cls.plain_user = new_test_user(cls.env, login="plain", groups="base.group_user") + +@freeze_time("2021-02-26 09:00:00", tick=True) # Last Friday of February +class BackendCaseMisc(BackendCaseBase): @users("plain") def test_plain_user_calendar_event(self): """Check that a simple user is able to handle manual calendar events.""" @@ -1042,3 +1044,49 @@ def test_resource_booking_schedule_unschedule(self): self.assertNotEqual( self.mail_activity.date_deadline, fields.Date.from_string("2024-01-01") ) + + +@freeze_time("2021-02-26 09:00:00", tick=True) # Last Friday of February +class BackendCaseCustom(BackendCaseBase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=False)) + cls.mt_note = cls.env.ref("mail.mt_note") + cls.mt_note.default = True + cls.partner.email = "ŧest@test.com" + + def test_resource_booking_message(self): + rb_model = self.env["resource.booking"] + rb = rb_model.create( + { + "partner_ids": [(4, self.partner.id)], + "type_id": self.rbt.id, + "combination_auto_assign": False, + "combination_id": self.rbcs[0].id, + "user_id": self.users[0].id, + } + ) + # Simulate the same as portal_booking_confirm + booking_sudo = rb_model.sudo().browse(rb.id) + booking_sudo = booking_sudo.with_context( + using_portal=True, tz=booking_sudo.type_id.resource_calendar_id.tz + ) + with Form(booking_sudo) as booking_form: + booking_form.start = datetime(2021, 3, 1, 10) + booking_sudo.action_confirm() + meeting = rb.meeting_id + follower = meeting.message_follower_ids.filtered( + lambda x: x.partner_id == meeting.user_id.partner_id + ) + self.assertIn(self.mt_note, follower.subtype_ids) + meesages = meeting.message_ids.filtered( + lambda x: x.message_type != "notification" + ) + self.assertEqual(len(meesages), 2) + partner_message = meesages.filtered(lambda x: self.partner in x.partner_ids) + self.assertNotIn(rb.user_id.partner_id, partner_message.notified_partner_ids) + user_message = meesages.filtered( + lambda x: meeting.user_id.partner_id in x.partner_ids + ) + self.assertIn(meeting.user_id.partner_id, user_message.notified_partner_ids)