Skip to content

Commit

Permalink
Add boolean to disable automatic subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
louck committed Oct 30, 2024
1 parent 74e34f6 commit 473514a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions commown_b2b_mail_channel/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ class ResPartner(models.Model):
domain=[("public", "=", "private"), ("channel_type", "=", "channel")],
)

disable_channel_subscription = fields.Boolean(
"Disable automatic subscription to mail channel",
default=False,
)

def _update_subscription_on_mail_channel_change(self, new_chan_id):
if self.mail_channel_id and self.mail_channel_id.id != new_chan_id:
self.remove_partners_from_channel(self.mail_channel_id, self.child_ids)
if new_chan_id:
if new_chan_id and not self.disable_channel_subscription:
partners_to_add = self.child_ids.filtered(
lambda p: p.company_type == "person" and p.user_ids
)
Expand All @@ -28,7 +33,10 @@ def _update_subscription_on_mail_channel_change(self, new_chan_id):
def _update_mail_channel_subscription_on_parent_change(self, new_parent_id):
if new_parent_id:
new_parent = self.env["res.partner"].browse(new_parent_id)
if new_parent.mail_channel_id:
if (
new_parent.mail_channel_id
and not new_parent.disable_channel_subscription
):
new_parent.mail_channel_id.channel_invite(self.id)

elif not new_parent.mail_channel_id and self.contract_ids.filtered(
Expand Down
22 changes: 22 additions & 0 deletions commown_b2b_mail_channel/tests/test_res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ def test_support_channel_creation_on_contract_start(self):
)
self.assertEqual(company_chan.group_ids, expected_groups)

def test_disable_automatic_subscription(self):
self.company.disable_channel_subscription = True
self.part1.parent_id = self.company
self.company.create_mail_channel()
mail_channel = self.company.mail_channel_id
self.assertTrue(mail_channel)

channel_partners = mail_channel.channel_last_seen_partner_ids.mapped(
"partner_id"
)
self.assertNotIn(self.part1, channel_partners)
self.assertIn(self.user_support.partner_id, channel_partners)

self.company.disable_channel_subscription = False
self.part2.parent_id = self.company

channel_partners = mail_channel.channel_last_seen_partner_ids.mapped(
"partner_id"
)
self.assertNotIn(self.part1, channel_partners)
self.assertIn(self.part2, channel_partners)

def test_partner_is_added_when_parent_has_channel(self):
self.company.create_mail_channel()
mail_channel = self.company.mail_channel_id
Expand Down
1 change: 1 addition & 0 deletions commown_b2b_mail_channel/views/res_partner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<field name="arch" type="xml">
<field name="vat" position="after">
<field name="mail_channel_id" attrs="{'invisible': [('is_company', '=', 'False')]}"/>
<field name="disable_channel_subscription" attrs="{'invisible': [('is_company', '=', 'False')]}"/>
</field>
</field>
</record>
Expand Down

0 comments on commit 473514a

Please sign in to comment.