Skip to content

Commit

Permalink
[FIX] mass_mailing: make recipients unique by email
Browse files Browse the repository at this point in the history
This commit makes the recipients of any mass.mailing res_model unique by email. The email can be read from either the email field or the partner_id.email field, if they exist, otherwise the behavior is as usual.

The partner_id case requires a further loop of deduplication because partner_id.email is not stored in sale.order and read_group does not allow fetching indirect fields.

Related: https://gitlab.com/ircanada/ircodoo/-/issues/2849
  • Loading branch information
maneandrea committed Sep 18, 2024
1 parent 4be86b2 commit a40852f
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion addons/mass_mailing/models/mailing.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,27 @@ def _get_mass_mailing_context(self):

def _get_recipients(self):
mailing_domain = self._parse_mailing_domain()
res_ids = self.env[self.mailing_model_real].search(mailing_domain).ids
res_model = self.env[self.mailing_model_real]
if "email" in res_model._fields:
res_ids_grouped = res_model.read_group(
mailing_domain, ["__my_id:max(id)"], "email"
)
res_ids = [r["__my_id"] for r in res_ids_grouped]

elif "partner_id" in res_model._fields:
res_ids_grouped = res_model.read_group(
mailing_domain, ["__my_id:max(id)"], "partner_id", orderby="partner_id"
)
seen = set()
res_ids = []
for r in res_ids_grouped:
curr_email = self.env["res.partner"].browse(r["partner_id"][0]).email
if curr_email in seen:
continue
seen.add(curr_email)
res_ids.append(r["__my_id"])
else:
res_ids = res_model.search(mailing_domain).ids

# randomly choose a fragment
if self.ab_testing_enabled and self.ab_testing_pc < 100:
Expand Down

0 comments on commit a40852f

Please sign in to comment.