hc: refresh attribs when cross-adding an existing subscriber to a segment

add_subscriber only attached an already-existing subscriber to the new list
without updating attribs, so the due-soon template's days_until merge field was
blank for providers already imported by another segment. Now PUT the merged
attribs (existing + this segment's npi/practice/due-date/days_until) before
adding to the list.
This commit is contained in:
justin 2026-06-12 19:37:01 -05:00
parent c8c9a04c1d
commit 6c8c823e5e

View file

@ -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")