Skip to content

Commit

Permalink
Subrscribe partner to company channel when added to a company
Browse files Browse the repository at this point in the history
If the company doesn't have a channel and the partner has an active
contract we create the channel.
When a partner is removed from a company, he is also removed from the
channel.
  • Loading branch information
louck committed Sep 26, 2024
1 parent be14e49 commit 6e13515
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions commown_b2b_mail_channel/models/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
class Contract(models.Model):
_inherit = "contract.contract"

def is_active_contract(self):
if self.date_start and self.date_start <= date.today():
return not self.date_end or self.date_end > date.today()
else:
return False

@api.constrains("date_start")
def on_contract_start_create_channel(self):
"""Trigger support channel creation if contract is about to start"""
Expand Down
17 changes: 17 additions & 0 deletions commown_b2b_mail_channel/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ def write(self, vals):
self.env["mail.channel"].browse(new_chan_id).channel_invite(
partners_to_add.ids
)
if not self.is_company and "parent_id" in vals.keys():
new_parent_id = vals["parent_id"]
if new_parent_id:
new_parent = self.env["res.partner"].browse(new_parent_id)
if new_parent.mail_channel_id:
new_parent.mail_channel_id.channel_invite(self.id)

elif not new_parent.mail_channel_id and self.contract_ids.filtered(
lambda c: c.is_active_contract()
):
new_parent.sudo().create_mail_channel()

if not new_parent_id:
old_parent = self.parent_id
if old_parent and old_parent.mail_channel_id:
self.remove_partners_from_channel(old_parent.mail_channel_id, self)

return super().write(vals)

def remove_partners_from_channel(self, channel, partners):
Expand Down
29 changes: 29 additions & 0 deletions commown_b2b_mail_channel/tests/test_res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,32 @@ def test_support_channel_creation_on_contract_start(self):
self.part1 + self.part2 + self.user_support.partner_id,
)
self.assertEqual(company_chan.group_ids, expected_groups)

def test_partner_is_added_when_parent_has_channel(self):
self.company.create_mail_channel()
mail_channel = self.company.mail_channel_id
self.assertTrue(mail_channel)

initial_parts = mail_channel.channel_last_seen_partner_ids.mapped("partner_id")

self.part1.parent_id = self.company
self.assertEqual(
mail_channel.channel_last_seen_partner_ids.mapped("partner_id"),
initial_parts + self.part1,
)

self.part1.parent_id = False
self.assertEqual(
mail_channel.channel_last_seen_partner_ids.mapped("partner_id"),
initial_parts,
)

def test_channel_creation_on_active_contract_join_company(self):
"""Test if channel is created when a partner with an active contract join
company"""
self.part2.parent_id = self.company
self.contract.date_start = date.today()
self.assertFalse(self.company.mail_channel_id)

self.part1.parent_id = self.company
self.assertTrue(self.company.mail_channel_id)

0 comments on commit 6e13515

Please sign in to comment.