forked from csg-org/CompactConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_service_client.py
More file actions
597 lines (534 loc) · 22.1 KB
/
email_service_client.py
File metadata and controls
597 lines (534 loc) · 22.1 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
import json
from dataclasses import dataclass
from datetime import date
from typing import Any, Protocol
from uuid import UUID
import boto3
from aws_lambda_powertools.logging import Logger
from cc_common.exceptions import CCInternalException
@dataclass
class EncumbranceNotificationTemplateVariables:
"""
Template variables for encumbrance notification emails.
"""
provider_first_name: str
provider_last_name: str
encumbered_jurisdiction: str
license_type: str
effective_date: date
provider_id: UUID | None = None
class ProviderNotificationMethod(Protocol):
"""Protocol for provider encumbrance notification methods."""
def __call__(
self, *, compact: str, provider_email: str, template_variables: EncumbranceNotificationTemplateVariables
) -> dict[str, Any]: ...
class EmailServiceClient:
"""
Client for sending email notifications through the email notification service lambda.
This class abstracts the lambda client and provides a clean interface for sending emails.
"""
def __init__(self, lambda_client: boto3.client, email_notification_service_lambda_name: str, logger: Logger):
"""
Initialize the EmailServiceClient.
:param lambda_client: boto3 lambda client.
:param email_notification_service_lambda_name: Name of the email notification service lambda.
"""
self._lambda_client = lambda_client
self._email_notification_service_lambda_name = email_notification_service_lambda_name
self._logger = logger
def _invoke_lambda(self, payload: dict[str, Any]) -> dict[str, Any]:
"""
Invoke the email notification service lambda with the given payload.
:param payload: Payload to send to the lambda
:return: Response from the lambda
:raises CCInternalException: If the lambda invocation fails
"""
if not self._email_notification_service_lambda_name:
raise CCInternalException('Email notification service lambda name not set')
try:
response = self._lambda_client.invoke(
FunctionName=self._email_notification_service_lambda_name,
InvocationType='RequestResponse',
Payload=json.dumps(payload),
)
if response.get('FunctionError'):
error_message = f'Failed to send email notification: {response.get("FunctionError")}'
self._logger.error(error_message, payload=payload)
raise CCInternalException(error_message)
return response
except Exception as e:
error_message = f'Error invoking email notification service lambda: {str(e)}'
self._logger.error(error_message, payload=payload, exception=str(e))
raise CCInternalException(error_message) from e
def send_provider_privilege_deactivation_email(
self,
compact: str,
provider_email: str,
privilege_id: str,
) -> dict[str, str]:
"""
Send a privilege deactivation notification email to providers.
:param compact: Compact name
:param provider_email: Email address of the provider
:param privilege_id: ID of the privilege being deactivated
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'template': 'privilegeDeactivationProviderNotification',
'recipientType': 'SPECIFIC',
'specificEmails': [
provider_email,
],
'templateVariables': {
'privilegeId': privilege_id,
},
}
return self._invoke_lambda(payload)
def send_jurisdiction_privilege_deactivation_email(
self,
compact: str,
jurisdiction: str,
privilege_id: str,
provider_first_name: str,
provider_last_name: str,
) -> dict[str, str]:
"""
Send a privilege deactivation notification email to jurisdiction.
:param compact: Compact name
:param jurisdiction: Jurisdiction name
:param privilege_id: ID of the privilege being deactivated
:param provider_first_name: First name of the provider whose privilege was deactivated
:param provider_last_name: Last name of the provider whose privilege was deactivated
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'jurisdiction': jurisdiction,
'template': 'privilegeDeactivationJurisdictionNotification',
# for now, we send this notification to the same contacts as the summary report recipients
'recipientType': 'JURISDICTION_SUMMARY_REPORT',
'templateVariables': {
'privilegeId': privilege_id,
'providerFirstName': provider_first_name,
'providerLastName': provider_last_name,
},
}
return self._invoke_lambda(payload)
def send_compact_transaction_report_email(
self,
compact: str,
report_s3_path: str,
reporting_cycle: str,
start_date: date,
end_date: date,
) -> dict[str, Any]:
"""
Send a compact transaction report email.
:param compact: Compact name
:param report_s3_path: S3 path to the report zip file
:param reporting_cycle: Reporting cycle (e.g., 'weekly', 'monthly')
:param start_date: Start date of the reporting period
:param end_date: End date of the reporting period
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'template': 'CompactTransactionReporting',
'recipientType': 'COMPACT_SUMMARY_REPORT',
'templateVariables': {
'reportS3Path': report_s3_path,
'reportingCycle': reporting_cycle,
'startDate': start_date.strftime('%Y-%m-%d'),
'endDate': end_date.strftime('%Y-%m-%d'),
},
}
return self._invoke_lambda(payload)
def send_jurisdiction_transaction_report_email(
self,
compact: str,
jurisdiction: str,
report_s3_path: str,
reporting_cycle: str,
start_date: date,
end_date: date,
) -> dict[str, str]:
"""
Send a jurisdiction transaction report email.
:param compact: Compact name
:param jurisdiction: Jurisdiction name
:param report_s3_path: S3 path to the report zip file
:param reporting_cycle: Reporting cycle (e.g., 'weekly', 'monthly')
:param start_date: Start date of the reporting period
:param end_date: End date of the reporting period
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'jurisdiction': jurisdiction,
'template': 'JurisdictionTransactionReporting',
'recipientType': 'JURISDICTION_SUMMARY_REPORT',
'templateVariables': {
'reportS3Path': report_s3_path,
'reportingCycle': reporting_cycle,
'startDate': start_date.strftime('%Y-%m-%d'),
'endDate': end_date.strftime('%Y-%m-%d'),
},
}
return self._invoke_lambda(payload)
def send_privilege_purchase_email(
self,
provider_email: str,
transaction_date: str,
privileges: list[dict],
total_cost: str,
cost_line_items: list[dict],
) -> dict[str, str]:
"""
Send a privilege(s) purchase notification email.
:param provider_email: email of the provider who purchased privileges
:param transaction_date: date of the transaction
:param privileges: privileges purchased
:param total_cost: Total cost of the transaction
:param cost_line_items: Line items (name, unitPrice, quantity) of transaction
:return: Response from the email notification service
"""
payload = {
'template': 'privilegePurchaseProviderNotification',
'specificEmails': [
provider_email,
],
'templateVariables': {
'transactionDate': transaction_date,
'privileges': privileges,
'totalCost': total_cost,
'costLineItems': cost_line_items,
},
}
return self._invoke_lambda(payload)
def send_provider_multiple_registration_attempt_email(
self,
compact: str,
provider_email: str,
) -> dict[str, str]:
"""
Send a notification email to a provider when someone attempts to register with their email address.
:param compact: Compact name
:param provider_email: Email address of the provider
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'template': 'multipleRegistrationAttemptNotification',
'recipientType': 'SPECIFIC',
'specificEmails': [
provider_email,
],
'templateVariables': {},
}
return self._invoke_lambda(payload)
def send_license_encumbrance_provider_notification_email(
self,
*,
compact: str,
provider_email: str,
template_variables: EncumbranceNotificationTemplateVariables,
) -> dict[str, str]:
"""
Send a license encumbrance notification email to a provider.
:param compact: Compact name
:param provider_email: Email address of the provider
:param template_variables: Template variables for the email
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'template': 'licenseEncumbranceProviderNotification',
'recipientType': 'SPECIFIC',
'specificEmails': [provider_email],
'templateVariables': {
'providerFirstName': template_variables.provider_first_name,
'providerLastName': template_variables.provider_last_name,
'encumberedJurisdiction': template_variables.encumbered_jurisdiction,
'licenseType': template_variables.license_type,
'effectiveStartDate': template_variables.effective_date.strftime('%B %d, %Y'),
},
}
return self._invoke_lambda(payload)
def send_license_encumbrance_state_notification_email(
self,
*,
compact: str,
jurisdiction: str,
template_variables: EncumbranceNotificationTemplateVariables,
) -> dict[str, str]:
"""
Send a license encumbrance notification email to a state.
:param compact: Compact name
:param jurisdiction: Jurisdiction to notify
:param template_variables: Template variables for the email
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'jurisdiction': jurisdiction,
'template': 'licenseEncumbranceStateNotification',
'recipientType': 'JURISDICTION_ADVERSE_ACTIONS',
'templateVariables': {
'providerFirstName': template_variables.provider_first_name,
'providerLastName': template_variables.provider_last_name,
'providerId': str(template_variables.provider_id),
'encumberedJurisdiction': template_variables.encumbered_jurisdiction,
'licenseType': template_variables.license_type,
'effectiveStartDate': template_variables.effective_date.strftime('%B %d, %Y'),
},
}
return self._invoke_lambda(payload)
def send_license_encumbrance_lifting_provider_notification_email(
self,
*,
compact: str,
provider_email: str,
template_variables: EncumbranceNotificationTemplateVariables,
) -> dict[str, str]:
"""
Send a license encumbrance lifting notification email to a provider.
:param compact: Compact name
:param provider_email: Email address of the provider
:param template_variables: Template variables for the email
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'template': 'licenseEncumbranceLiftingProviderNotification',
'recipientType': 'SPECIFIC',
'specificEmails': [provider_email],
'templateVariables': {
'providerFirstName': template_variables.provider_first_name,
'providerLastName': template_variables.provider_last_name,
'liftedJurisdiction': template_variables.encumbered_jurisdiction,
'licenseType': template_variables.license_type,
'effectiveLiftDate': template_variables.effective_date.strftime('%B %d, %Y'),
},
}
return self._invoke_lambda(payload)
def send_license_encumbrance_lifting_state_notification_email(
self,
*,
compact: str,
jurisdiction: str,
template_variables: EncumbranceNotificationTemplateVariables,
) -> dict[str, str]:
"""
Send a license encumbrance lifting notification email to a state.
:param compact: Compact name
:param jurisdiction: Jurisdiction to notify
:param template_variables: Template variables for the email
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'jurisdiction': jurisdiction,
'template': 'licenseEncumbranceLiftingStateNotification',
'recipientType': 'JURISDICTION_ADVERSE_ACTIONS',
'templateVariables': {
'providerFirstName': template_variables.provider_first_name,
'providerLastName': template_variables.provider_last_name,
'providerId': str(template_variables.provider_id),
'liftedJurisdiction': template_variables.encumbered_jurisdiction,
'licenseType': template_variables.license_type,
'effectiveLiftDate': template_variables.effective_date.strftime('%B %d, %Y'),
},
}
return self._invoke_lambda(payload)
def send_privilege_encumbrance_provider_notification_email(
self,
*,
compact: str,
provider_email: str,
template_variables: EncumbranceNotificationTemplateVariables,
) -> dict[str, str]:
"""
Send a privilege encumbrance notification email to a provider.
:param compact: Compact name
:param provider_email: Email address of the provider
:param template_variables: Template variables for the email
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'template': 'privilegeEncumbranceProviderNotification',
'recipientType': 'SPECIFIC',
'specificEmails': [provider_email],
'templateVariables': {
'providerFirstName': template_variables.provider_first_name,
'providerLastName': template_variables.provider_last_name,
'encumberedJurisdiction': template_variables.encumbered_jurisdiction,
'licenseType': template_variables.license_type,
'effectiveStartDate': template_variables.effective_date.strftime('%B %d, %Y'),
},
}
return self._invoke_lambda(payload)
def send_privilege_encumbrance_state_notification_email(
self,
*,
compact: str,
jurisdiction: str,
template_variables: EncumbranceNotificationTemplateVariables,
) -> dict[str, str]:
"""
Send a privilege encumbrance notification email to a state.
:param compact: Compact name
:param jurisdiction: Jurisdiction to notify
:param template_variables: Template variables for the email
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'jurisdiction': jurisdiction,
'template': 'privilegeEncumbranceStateNotification',
'recipientType': 'JURISDICTION_ADVERSE_ACTIONS',
'templateVariables': {
'providerFirstName': template_variables.provider_first_name,
'providerLastName': template_variables.provider_last_name,
'providerId': str(template_variables.provider_id),
'encumberedJurisdiction': template_variables.encumbered_jurisdiction,
'licenseType': template_variables.license_type,
'effectiveStartDate': template_variables.effective_date.strftime('%B %d, %Y'),
},
}
return self._invoke_lambda(payload)
def send_privilege_encumbrance_lifting_provider_notification_email(
self,
*,
compact: str,
provider_email: str,
template_variables: EncumbranceNotificationTemplateVariables,
) -> dict[str, str]:
"""
Send a privilege encumbrance lifting notification email to a provider.
:param compact: Compact name
:param provider_email: Email address of the provider
:param template_variables: Template variables for the email
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'template': 'privilegeEncumbranceLiftingProviderNotification',
'recipientType': 'SPECIFIC',
'specificEmails': [provider_email],
'templateVariables': {
'providerFirstName': template_variables.provider_first_name,
'providerLastName': template_variables.provider_last_name,
'liftedJurisdiction': template_variables.encumbered_jurisdiction,
'licenseType': template_variables.license_type,
'effectiveLiftDate': template_variables.effective_date.strftime('%B %d, %Y'),
},
}
return self._invoke_lambda(payload)
def send_privilege_encumbrance_lifting_state_notification_email(
self,
*,
compact: str,
jurisdiction: str,
template_variables: EncumbranceNotificationTemplateVariables,
) -> dict[str, str]:
"""
Send a privilege encumbrance lifting notification email to a state.
:param compact: Compact name
:param jurisdiction: Jurisdiction to notify
:param template_variables: Template variables for the email
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'jurisdiction': jurisdiction,
'template': 'privilegeEncumbranceLiftingStateNotification',
'recipientType': 'JURISDICTION_ADVERSE_ACTIONS',
'templateVariables': {
'providerFirstName': template_variables.provider_first_name,
'providerLastName': template_variables.provider_last_name,
'providerId': str(template_variables.provider_id),
'liftedJurisdiction': template_variables.encumbered_jurisdiction,
'licenseType': template_variables.license_type,
'effectiveLiftDate': template_variables.effective_date.strftime('%B %d, %Y'),
},
}
return self._invoke_lambda(payload)
def send_provider_email_verification_code(
self,
compact: str,
provider_email: str,
verification_code: str,
) -> dict[str, str]:
"""
Send an email verification code to a provider's new email address.
:param compact: Compact name
:param provider_email: Email address to send the verification code to
:param verification_code: 4-digit verification code
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'template': 'providerEmailVerificationCode',
'recipientType': 'SPECIFIC',
'specificEmails': [
provider_email,
],
'templateVariables': {
'verificationCode': verification_code,
},
}
return self._invoke_lambda(payload)
def send_provider_email_change_notification(
self,
compact: str,
old_email_address: str,
new_email_address: str,
) -> dict[str, str]:
"""
Send a notification to the old email address when a provider's email is changed.
:param compact: Compact name
:param old_email_address: The previous email address
:param new_email_address: The new email address
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'template': 'providerEmailChangeNotification',
'recipientType': 'SPECIFIC',
'specificEmails': [
old_email_address,
],
'templateVariables': {
'newEmailAddress': new_email_address,
},
}
return self._invoke_lambda(payload)
def send_provider_account_recovery_confirmation_email(
self,
*,
compact: str,
provider_email: str,
provider_id: str,
recovery_token: str,
) -> dict[str, str]:
"""
Send an account recovery confirmation email to a provider with a secure link.
:param compact: The compact name
:param provider_email: Email address of the provider
:param provider_id: The id of the provider
:param recovery_token: Recovery token
:return: Response from the email notification service
"""
payload = {
'compact': compact,
'template': 'providerAccountRecoveryConfirmation',
'recipientType': 'SPECIFIC',
'specificEmails': [provider_email],
'templateVariables': {
'providerId': str(provider_id),
'recoveryToken': str(recovery_token),
},
}
return self._invoke_lambda(payload)