diff --git a/scripts/build_healthcare_campaigns_cron.py b/scripts/build_healthcare_campaigns_cron.py index b25ed78..faf3049 100644 --- a/scripts/build_healthcare_campaigns_cron.py +++ b/scripts/build_healthcare_campaigns_cron.py @@ -191,7 +191,9 @@ def add_subscriber(list_id: int, email: str, name: str, attribs: dict) -> bool: }) return True except SystemExit as e: - # Already exists -> attach to the list instead. + # Already exists -> attach to the list AND refresh attribs so per-segment + # merge fields (e.g. days_until for the due-soon template) are correct + # even when the same provider already exists from another segment import. if "409" in str(e) or "already exists" in str(e).lower(): try: q = "subscribers.email = '" + email.replace("'", "''") + "'" @@ -199,6 +201,14 @@ def add_subscriber(list_id: int, email: str, name: str, attribs: dict) -> bool: results = found.get("data", {}).get("results", []) if results: sid = results[0]["id"] + existing_attribs = results[0].get("attribs") or {} + # Merge: keep prior fields, overwrite with this segment's + # values (npi/practice/due-date/days_until/days_overdue). + merged = {**existing_attribs, **attribs} + lm(f"/subscribers/{sid}", { + "email": email, "name": name or email.split("@")[0], + "attribs": merged, + }, "PUT") lm("/subscribers/lists", {"ids": [sid], "action": "add", "target_list_ids": [list_id], "status": "confirmed"}, "PUT")