-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmailjetUtil.py
More file actions
678 lines (528 loc) · 21 KB
/
Copy pathmailjetUtil.py
File metadata and controls
678 lines (528 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
############### Asmbly Mailjet API Integrations ##################
# Mailjet API docs - https://dev.mailjet.com/email/guides/ #
##################################################################
import datetime
import logging
from urllib.parse import quote
from dataclasses import dataclass
from typing import Protocol, Literal, Self, Any
from enum import StrEnum
from zoneinfo import ZoneInfo
import boto3
from pydantic import BaseModel, Field, model_validator, field_serializer
from mailjet_rest import Client # type: ignore
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_result
from neonUtil import getNeonAccounts
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
class MJContactProperties(StrEnum):
FIRSTNAME = "first_name"
LASTNAME = "last_name"
ATTENDED_ORIENTATION = "attended_orientation"
SIGNED_WAIVER = "signed_waiver"
ACTIVE_MEMBER = "active_member"
ORIENTATION_DATE = "orientation_date"
LATEST_MEMBERSHIP_END = "latest_membership_end"
class MJContactListNames(StrEnum):
NEW_MEMBERS = "NewMembers"
ALL_CONTACTS = "AllContacts"
class MailjetAction(StrEnum):
ADD_FORCE = "addforce"
ADD_NOFORCE = "addnoforce"
REMOVE = "remove"
UNSUB = "unsub"
class StringProperty(BaseModel):
name: Literal[
MJContactProperties.FIRSTNAME,
MJContactProperties.LASTNAME,
] = Field(..., alias="Name")
value: str = Field(..., alias="Value")
class BoolProperty(BaseModel):
name: Literal[
MJContactProperties.ACTIVE_MEMBER,
MJContactProperties.SIGNED_WAIVER,
MJContactProperties.ATTENDED_ORIENTATION,
] = Field(..., alias="Name")
value: bool = Field(..., alias="Value")
class DateProperty(BaseModel):
name: Literal[
MJContactProperties.ORIENTATION_DATE, MJContactProperties.LATEST_MEMBERSHIP_END
] = Field(..., alias="Name")
value: datetime.datetime = Field(..., alias="Value")
class UnknownProperty(BaseModel):
name: str = Field(..., alias="Name")
value: datetime.datetime | bool | float | int | str = Field(..., alias="Value")
class MailjetContact(BaseModel):
created_at: datetime.datetime = Field(..., alias="CreatedAt", exclude=True)
email: str = Field(..., alias="Email")
id_: int = Field(..., alias="ID", exclude=True)
name: str = Field(..., alias="Name")
is_excluded_from_campaigns: bool = Field(..., alias="IsExcludedFromCampaigns")
properties: (
list[StringProperty | BoolProperty | DateProperty | UnknownProperty] | None
) = Field(None, alias="Properties")
@field_serializer("properties")
def serialize_properties(
self,
properties: (
list[StringProperty | BoolProperty | DateProperty | UnknownProperty] | None
),
) -> dict[str, Any] | None:
if properties is None:
return None
return {prop.name: prop.value for prop in properties}
class MJContactWithProperties(BaseModel):
contact_id: int = Field(..., alias="ContactID")
data: list[DateProperty | BoolProperty | StringProperty | UnknownProperty] = Field(
..., alias="Data"
)
id_: int = Field(..., alias="ID")
class MJContactList(BaseModel):
id_: int = Field(..., alias="ID")
name: str = Field(..., alias="Name")
is_deleted: bool = Field(..., alias="IsDeleted")
subscriber_count: int = Field(..., alias="SubscriberCount")
created_at: datetime.datetime = Field(..., alias="CreatedAt")
class MJContactListResponse(BaseModel):
count: int = Field(..., alias="Count")
data: list[MJContactList] = Field(..., alias="Data")
total: int = Field(..., alias="Total")
class MJContactDataResponse(BaseModel):
count: int = Field(..., alias="Count")
data: list[MJContactWithProperties] = Field(..., alias="Data")
total: int = Field(..., alias="Total")
class MJContactResponse(BaseModel):
count: int = Field(..., alias="Count")
data: list[MailjetContact] = Field(..., alias="Data")
total: int = Field(..., alias="Total")
class CustomContactMetadataField(BaseModel):
datatype: Literal["str", "int", "bool", "float", "datetime"] = Field(
"str", alias="Datatype"
)
name: str = Field(..., alias="Name")
namespace: Literal["static", "historic"] = Field("static", alias="NameSpace")
class MJBulkListUpdateRequest(BaseModel):
action: Literal[
MailjetAction.ADD_FORCE,
MailjetAction.ADD_NOFORCE,
MailjetAction.REMOVE,
MailjetAction.UNSUB,
] = Field(..., alias="Action")
contacts: list[MailjetContact] = Field(..., alias="Contacts")
class Subscriber(BaseModel):
email_: str | None
id_: int | None
first_name: str
last_name: str
attended_orientation: bool
orientation_date: datetime.datetime | None
signed_waiver: bool
active_member: bool
latest_membership_end: datetime.datetime | None
@property
def email(self) -> str | None:
if self.email_ is not None:
return self.email_.lower()
return self.email_
@property
def full_name(self) -> str:
return f"{self.first_name} {self.last_name}"
@model_validator(mode="after")
def validate_either_email_or_id(self) -> Self:
if self.email_ is None and self.id_ is None:
raise ValueError("Either email_ or id_ must be provided for a subscriber.")
return self
@dataclass
class MJCredentials:
public_key: str
secret_key: str
class MailserviceInterface(Protocol):
def send_email(self) -> None: ...
def bulk_update_subscribers_in_list(
self, list_id: str, subscribers: list[Subscriber], action: MailjetAction
) -> None | int: ...
def create_contact_metadata_fields(
self, metadata: list[CustomContactMetadataField]
) -> None: ...
def get_ind_contact(self, email: str) -> None | Subscriber: ...
def update_ind_contact_metadata(
self, email: str, metadata: list[DateProperty | BoolProperty | StringProperty]
) -> None: ...
def get_contacts(
self,
campaign_id: str | None = None,
list_id: str | None = None,
sort_key: str | None = None,
sort_order: str = "asc",
count_only: bool = False,
limit: int = 50,
offset: int = 0,
) -> None | tuple[int, list[Subscriber]]: ...
def get_all_contacts_in_list(self, list_id: int) -> list[Subscriber] | None: ...
class MJService:
new_members_list_id: int | None = None
all_contacts_list_id: int | None = None
def __init__(self, credentials: MJCredentials) -> None:
self.client = Client(
auth=(credentials.public_key, credentials.secret_key), version="v3"
)
self.set_list_ids()
def set_list_ids(self) -> None:
response = self.client.contactslist.get()
if not response.ok:
logging.error(
"Mailjet contact list retrieval request failed with status code %s. Response: %s.",
response.status_code,
response.json(),
)
return
contact_lists = MJContactListResponse.model_validate_json(response.content).data
lists = {list.name: list.id_ for list in contact_lists if not list.is_deleted}
self.new_members_list_id = lists.get(MJContactListNames.NEW_MEMBERS, None)
self.all_contacts_list_id = lists.get(MJContactListNames.ALL_CONTACTS, None)
def create_contact_metadata_fields(
self, metadata: list[CustomContactMetadataField]
) -> None:
if not metadata:
return
for field in metadata:
response = self.client.contactmetadata.create(
data=field.model_dump(by_alias=True)
)
if response.status_code != 201:
logging.error(
"Mailjet contact metadata field creation request failed with status code %s. Response: %s.",
response.status_code,
response.json(),
)
return
logging.info("Mailjet contact metadata field created: %s", response.json())
def update_ind_contact_metadata(
self, email: str, metadata: list[DateProperty | BoolProperty | StringProperty]
) -> None:
if not metadata:
return
# URL encode email address
encoded_email = quote(email)
data = {"Data": [field.model_dump(by_alias=True) for field in metadata]}
response = self.client.contactdata.update(id=encoded_email, data=data)
if response.status_code != 200:
logging.error(
"Mailjet contact metadata update request failed with status code %s. Response: %s.",
response.status_code,
response.json(),
)
return
logging.info(
"Mailjet contact metadata updated for %s: %s", email, response.json()
)
def bulk_update_subscribers_in_lists(
self, list_ids: list[int | None], subscribers: list[Subscriber], action: MailjetAction
) -> None | int:
if not subscribers:
return None
list_ids = [list_id for list_id in list_ids if list_id is not None]
if not list_ids:
return None
data = {
"Contacts": [
{
"Email": sub.email,
"IsExcludedFromCampaigns": False,
"Name": sub.full_name,
"Properties": {
"first_name": sub.first_name,
"last_name": sub.last_name,
"attended_orientation": sub.attended_orientation,
"signed_waiver": sub.signed_waiver,
"active_member": sub.active_member,
"latest_membership_end": (
sub.latest_membership_end.astimezone(
ZoneInfo("America/Chicago")
).isoformat()
if sub.latest_membership_end
else None
),
"orientation_date": (
sub.orientation_date.isoformat()
if sub.orientation_date
else None
),
},
}
for sub in subscribers
],
"ContactsLists": [
{
"ListID": list_id,
"Action": action.value,
}
for list_id in list_ids
],
}
response = self.client.contact_managemanycontacts.create(data=data)
if response.status_code != 201:
logging.error(
"Mailjet bulk list update request failed with status code %s. Response: %s.",
response.status_code,
response.json(),
)
return None
job_id: int = response.json().get("Data")[0].get("JobID")
return job_id
@retry(
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=1, min=2, max=10),
retry=retry_if_result(lambda x: x == "Processing"),
)
def get_job_status(self, job_id: int) -> str | None:
response = self.client.contact_managemanycontacts.get(action_id=job_id)
if response.status_code == 404:
logging.info("Job %s not yet available, retrying...", job_id)
return "Processing"
if response.status_code != 200:
logging.error(
"Mailjet get job status request failed with status code %s. Response: %s.",
response.status_code,
response.json(),
)
return None
return response.json().get("Data")[0].get("Status")
def send_email(self) -> None:
pass
def get_contacts(
self,
campaign_id: str | None = None,
list_id: int | None = None,
contact_email: str | None = None,
sort_key: str | None = None,
sort_order: str = "asc",
count_only: bool = False,
limit: int = 50,
offset: int = 0,
) -> None | tuple[int, list[Subscriber]]:
filters: dict[str, str | int] = {
"countOnly": 1 if count_only else 0,
"limit": limit,
"offset": offset,
}
if contact_email:
filters["ContactEmail"] = contact_email
if campaign_id:
filters["Campaign"] = campaign_id
if list_id:
filters["ContactsList"] = list_id
if sort_key:
filters["Sort"] = f"{sort_key} {sort_order}"
response = self.client.contactdata.get(filters=filters)
if response.status_code != 200:
logging.error(
"Mailjet get contacts request failed with status code %s. Response: %s.",
response.status_code,
response.json(),
)
return None
contacts_list = MJContactDataResponse.model_validate_json(response.content)
if len(contacts_list.data) == 0:
logging.info("No contacts found.")
return None
return (
contacts_list.count,
[self.validate_contact_props(contact) for contact in contacts_list.data],
)
def get_all_contacts_in_list(self, list_id: int) -> list[Subscriber] | None:
offset = 0
response = self.get_contacts(list_id=list_id, offset=offset)
if response is None:
return None
count = response[0]
subscribers = response[1]
offset += 50
while offset < count:
response = self.get_contacts(list_id=list_id, offset=offset)
if response is None:
return subscribers
subscribers.extend(response[1])
offset += 50
return subscribers
def validate_contact_props(
self,
contact_data: MJContactWithProperties,
email: str | None = None,
) -> Subscriber:
contact_props = {prop.name: prop.value for prop in contact_data.data}
latest_membership_end = contact_props.get(
MJContactProperties.LATEST_MEMBERSHIP_END, None
)
assert isinstance(latest_membership_end, (datetime.datetime, type(None)))
first_name = contact_props.get(MJContactProperties.FIRSTNAME, "")
assert isinstance(first_name, str)
last_name = contact_props.get(MJContactProperties.LASTNAME, "")
assert isinstance(last_name, str)
attended_orientation = bool(
contact_props.get(MJContactProperties.ATTENDED_ORIENTATION, False)
)
assert isinstance(attended_orientation, bool)
orientation_date = contact_props.get(MJContactProperties.ORIENTATION_DATE, None)
assert isinstance(orientation_date, (datetime.datetime, type(None)))
signed_waiver = bool(
contact_props.get(MJContactProperties.SIGNED_WAIVER, False)
)
assert isinstance(signed_waiver, bool)
active_member = bool(
contact_props.get(MJContactProperties.ACTIVE_MEMBER, False)
)
assert isinstance(active_member, bool)
return Subscriber(
email_=email,
id_=contact_data.id_,
first_name=first_name,
last_name=last_name,
attended_orientation=attended_orientation,
orientation_date=orientation_date,
signed_waiver=signed_waiver,
active_member=active_member,
latest_membership_end=latest_membership_end,
)
def get_ind_contact(self, email: str) -> Subscriber | None:
encoded_email = quote(email)
response = self.client.contactdata.get(id=encoded_email)
if response.status_code == 404:
logging.info("Contact not found.")
return None
if response.status_code != 200:
logging.error(
"Mailjet get contact request failed with status code %s. Response: %s.",
response.status_code,
response.json(),
)
return None
contact = MJContactDataResponse.model_validate_json(response.content).data[0]
return self.validate_contact_props(contact, email)
def update_mj_all_contacts_list(
mailjet: MJService, neon_account_dict: dict
) -> int | None:
all_contacts_mj_list_id = mailjet.all_contacts_list_id
if all_contacts_mj_list_id is None:
logging.error(
"Failed to get %s list ID from Mailjet.", MJContactListNames.ALL_CONTACTS
)
return None
accounts: list[Subscriber] = []
for account_id in neon_account_dict:
account = Subscriber(
email_=neon_account_dict[account_id].get("Email 1").lower(),
id_=neon_account_dict[account_id].get("MailjetContactID"),
first_name=neon_account_dict[account_id].get("First Name"),
last_name=neon_account_dict[account_id].get("Last Name"),
attended_orientation=neon_account_dict[account_id].get("FacilityTourDate")
is not None,
orientation_date=(
datetime.datetime.strptime(
neon_account_dict[account_id].get("FacilityTourDate"), "%m/%d/%Y"
).astimezone(ZoneInfo("America/Chicago"))
if neon_account_dict[account_id].get("FacilityTourDate")
else None
),
active_member=neon_account_dict[account_id].get(
"Account Current Membership Status"
)
== "Active",
latest_membership_end=neon_account_dict[account_id].get(
"Membership Expiration Date"
),
signed_waiver=neon_account_dict[account_id].get("WaiverDate") is not None,
)
accounts.append(account)
job_id = mailjet.bulk_update_subscribers_in_lists(
list_ids=[all_contacts_mj_list_id],
subscribers=accounts,
action=MailjetAction.ADD_NOFORCE,
)
return job_id
def run_mailjet_maintenance() -> None:
"""
Main entry point for running maintenance tasks on Mailjet.
"""
ssm_mj_creds = boto3.client("ssm").get_parameters(
Names=[
"/mailjet/api_key",
"/mailjet/api_secret",
],
WithDecryption=True,
)
mj_creds = MJCredentials(
public_key=ssm_mj_creds["Parameters"][0]["Value"],
secret_key=ssm_mj_creds["Parameters"][1]["Value"],
)
mailjet = MJService(mj_creds)
orientation_search_fields = [
{"field": "Account Type", "operator": "EQUAL", "value": "Individual"},
{"field": "Email 1", "operator": "NOT_BLANK"},
{
"field": "Email Opt-Out",
"operator": "EQUAL",
"value": "At least one email opted in",
},
{"field": "FacilityTourDate", "operator": "NOT_BLANK"},
]
waiver_search_fields = [
{"field": "Account Type", "operator": "EQUAL", "value": "Individual"},
{"field": "Email 1", "operator": "NOT_BLANK"},
{
"field": "Email Opt-Out",
"operator": "EQUAL",
"value": "At least one email opted in",
},
{"field": "WaiverDate", "operator": "NOT_BLANK"},
]
# This will only retrieve accounts who have had at least one membership at some point
member_search_fields = [
{"field": "Account Type", "operator": "EQUAL", "value": "Individual"},
{"field": "Email 1", "operator": "NOT_BLANK"},
{
"field": "Email Opt-Out",
"operator": "EQUAL",
"value": "At least one email opted in",
},
{
"field": "Most Recent Membership Only",
"operator": "EQUAL",
"value": "Yes",
},
]
# all_acct_search_fields = [
# {"field": "Account Type", "operator": "EQUAL", "value": "Individual"},
# {"field": "Email 1", "operator": "NOT_BLANK"},
# {
# "field": "Email Opt-Out",
# "operator": "EQUAL",
# "value": "At least one email opted in",
# },
# ]
orientation_accts: dict[str, dict] = {}
waiver_accts: dict[str, dict] = {}
member_accts: dict[str, dict] = {}
orientation_accts = getNeonAccounts(
searchFields=orientation_search_fields, neonAccountDict=orientation_accts
)
waiver_accts = getNeonAccounts(
searchFields=waiver_search_fields, neonAccountDict=waiver_accts
)
member_accts = getNeonAccounts(
searchFields=member_search_fields, neonAccountDict=member_accts
)
all_accts = orientation_accts | waiver_accts | member_accts
# all_accts = getNeonAccounts(searchFields=all_acct_search_fields)
job_id = update_mj_all_contacts_list(mailjet, all_accts)
if job_id is None:
logging.error("Failed to update all contacts list")
else:
logging.info("Updated all contacts list with job id %s", job_id)
logging.info("Job status: %s", mailjet.get_job_status(job_id))
logging.info("Finished running Mailjet maintenance tasks.")
if __name__ == "__main__":
run_mailjet_maintenance()