diff --git a/shared/utils.py b/shared/utils.py index 657c610537858e44c2db4937f30280032e026fec..1adb7eb05bdffd9066b36f7462a9686c94e790a2 100644 --- a/shared/utils.py +++ b/shared/utils.py @@ -72,11 +72,11 @@ def subscribe_to_newsletter(email, list_id): def subscribe_to_ecomail_newsletter( email: str, - categories: str, + tags: str, source: str, list_id: int | None = None, custom_data: dict | None = None, - update_existing: bool = True + update_existing: bool = True, ): if custom_data is None: custom_data = {} @@ -84,13 +84,25 @@ def subscribe_to_ecomail_newsletter( if not list_id: list_id = settings.ECOMAIL_DEFAULT_LIST_ID - split_categories = categories.split(",") if categories is not None else [] + existing_subscriber_data = requests.get( + urljoin(settings.ECOMAIL_API_URL, f"lists/{list_id}/subscriber/{email}"), + headers={"key": settings.ECOMAIL_API_KEY}, + ) + + existing_tags = [] + + if existing_subscriber_data.status_code == 200: + existing_subscriber_data = existing_subscriber_data.json() + existing_tags = existing_subscriber_data["subscriber"]["tags"] + + split_tags = tags.split(",") if tags is not None else [] + split_tags += existing_tags data = { "subscriber_data": { "email": email, "source": source, - "tags": split_categories, + "tags": split_tags, **custom_data, }, "update_existing": update_existing,