From 851dc9a6bba7a0241857ce15312a5edbee1e6b5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Tue, 18 Jun 2024 10:46:43 +0200 Subject: [PATCH] [IMP] resource_booking: Add support for mail_note subtype by default If we are creating the calendar event from resource booking, we want to notify only the partner_ids and not all followers (to avoid that each email is sent to all followers). Example: Resource booking with combination of several users. This happens only when the the subtype not is enabled by default in the instance. Related to https://github.com/OCA/calendar/pull/127/commits/a3cd5a883b809237150e0da3247f68f02f4b4c4c TT49045 --- resource_booking/models/calendar_event.py | 19 ++++++++++ resource_booking/tests/test_backend.py | 45 ++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/resource_booking/models/calendar_event.py b/resource_booking/models/calendar_event.py index c347bed8..03a50258 100644 --- a/resource_booking/models/calendar_event.py +++ b/resource_booking/models/calendar_event.py @@ -67,6 +67,25 @@ def _notify_thread(self, message, msg_vals=False, **kwargs): self = self.with_context(mail_notify_author=True) return super()._notify_thread(message=message, msg_vals=msg_vals, **kwargs) + def _notify_get_recipients(self, message, msg_vals, **kwargs): + """If we are creating the calendar event from resource booking, we want to + notify only the partner_ids and not all the followers (to avoid that each email + is sent to all followers). Example: Resource booking with combination of several + users. This only happens when the subtype note is enabled by default in the + instance. + """ + res = super()._notify_get_recipients( + message=message, msg_vals=msg_vals, **kwargs + ) + if self.env.context.get("resource_booking_event"): + res2 = [] + partner_ids = msg_vals.get("partner_ids", []) + for item in res: + if item["id"] in partner_ids: + res2.append(item) + return res2 + return res + @api.model_create_multi def create(self, vals_list): """Transfer resource booking to _attendees_values by context. diff --git a/resource_booking/tests/test_backend.py b/resource_booking/tests/test_backend.py index 55d2c991..b6a6b49e 100644 --- a/resource_booking/tests/test_backend.py +++ b/resource_booking/tests/test_backend.py @@ -1056,7 +1056,7 @@ def setUpClass(cls): cls.mt_note.default = True cls.partner.email = "ŧest@test.com" - def test_resource_booking_message(self): + def test_resource_booking_message_01(self): rb_model = self.env["resource.booking"] rb = rb_model.create( { @@ -1090,3 +1090,46 @@ def test_resource_booking_message(self): lambda x: meeting.user_id.partner_id in x.partner_ids ) self.assertIn(meeting.user_id.partner_id, user_message.notified_partner_ids) + + def test_resource_booking_message_02(self): + rb_model = self.env["resource.booking"] + combination = self.rbcs[0] + user_0 = self.users[0] + user_1 = self.users[1] + r_user_1 = self.r_users.filtered(lambda x: x.user_id == user_1) + combination.write({"resource_ids": [(4, r_user_1.id)]}) + cal_mon = self.r_calendars[0] + combination.forced_calendar_id = cal_mon + rb = rb_model.create( + { + "partner_ids": [(4, self.partner.id)], + "type_id": self.rbt.id, + "combination_auto_assign": False, + "combination_id": combination.id, + "user_id": user_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), 3) + partner_message = meesages.filtered(lambda x: self.partner in x.partner_ids) + self.assertNotIn(user_0.partner_id, partner_message.notified_partner_ids) + self.assertNotIn(user_1.partner_id, partner_message.notified_partner_ids) + user_0_message = meesages.filtered(lambda x: user_0.partner_id in x.partner_ids) + self.assertIn(user_0.partner_id, user_0_message.notified_partner_ids) + user_1_message = meesages.filtered(lambda x: user_1.partner_id in x.partner_ids) + self.assertIn(user_1.partner_id, user_1_message.notified_partner_ids)