generated from communitiesuk/funding-service-design-TEMPLATE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
notifier.py
182 lines (158 loc) · 6.83 KB
/
notifier.py
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
from __future__ import annotations
from typing import TYPE_CHECKING
from app.notification.application.map_contents import Application
from app.notification.application_reminder.map_contents import (
ApplicationReminder,
)
from app.notification.magic_link.map_contents import MagicLink
from app.notification.model.response import invalid_data_error
from config import Config
from flask import current_app
from notifications_python_client import errors
from notifications_python_client import NotificationsAPIClient
from notifications_python_client import prepare_upload
if TYPE_CHECKING:
from app.notification.model.notification import Notification
class Notifier:
"""Class holds notification operations"""
@staticmethod
def send_magic_link(notification: Notification, code: int = 200) -> tuple:
"""Function makes a call to govuk-notify-service with mapped contents
that are expected by the govuk-notify-service template.
Args: Takes an instance of Notification class.
Raises HTTPError if any of the required contents are incorrect
or missing.
"""
try:
notifications_client = NotificationsAPIClient(
Config.GOV_NOTIFY_API_KEY
)
contents = MagicLink.from_notification(notification)
response = notifications_client.send_email_notification(
email_address=contents.contact_info,
template_id=Config.MAGIC_LINK_TEMPLATE_ID,
email_reply_to_id=contents.reply_to_email_id,
personalisation={
"name of fund": contents.fund_name,
"link to application": contents.magic_link,
"request new link url": contents.request_new_link_url,
"contact details": contents.contact_help_email,
},
)
current_app.logger.info("Call made to govuk Notify API")
return response, code
except errors.HTTPError:
current_app.logger.exception(
"HTTPError while sending notification"
)
return invalid_data_error(
MagicLink.from_notification(notification)
)
@staticmethod
def send_submitted_application(
notification: Notification, code: int = 200
) -> tuple:
"""Function makes a call to govuk-notify-service with mapped contents
that are expected by the govuk-notify-service template.
Args: Takes an instance of Notification class.
Raises HTTPError if any of the required contents are incorrect
or missing.
"""
try:
notifications_client = NotificationsAPIClient(
Config.GOV_NOTIFY_API_KEY
)
contents = Application.from_notification(notification)
response = notifications_client.send_email_notification(
email_address=contents.contact_info,
template_id=Config.APPLICATION_RECORD_TEMPLATE_ID[
contents.fund_id
]["template_id"],
email_reply_to_id=contents.reply_to_email_id,
personalisation={
"name of fund": contents.fund_name,
"application reference": contents.reference,
"date submitted": contents.format_submission_date,
"round name": contents.round_name,
"question": prepare_upload(contents.questions),
},
)
current_app.logger.info("Call made to govuk Notify API")
return response, code
except errors.HTTPError:
current_app.logger.exception(
"HTTPError while sending notification"
)
return invalid_data_error(
Application.from_notification(notification)
)
@staticmethod
def send_incomplete_application(
notification: Notification, code: int = 200
) -> tuple:
"""Function makes a call to govuk-notify-service with mapped contents
that are expected by the govuk-notify-service template.
Args: Takes an instance of Notification class.
Raises HTTPError if any of the required contents are incorrect
or missing.
"""
try:
notifications_client = NotificationsAPIClient(
Config.GOV_NOTIFY_API_KEY
)
contents = Application.from_notification(notification)
response = notifications_client.send_email_notification(
email_address=contents.contact_info,
template_id=Config.INCOMPLETE_APPLICATION_TEMPLATE_ID[
contents.fund_id
]["template_id"],
personalisation={
"name of fund": contents.fund_name,
"application reference": contents.reference,
"round name": contents.round_name,
"question": prepare_upload(contents.questions),
},
)
current_app.logger.info("Call made to govuk Notify API")
return response, code
except errors.HTTPError:
current_app.logger.exception(
"HTTPError while sending notification"
)
return invalid_data_error(
Application.from_notification(notification)
)
@staticmethod
def send_application_reminder(
notification: Notification, code: int = 200
) -> tuple:
"""Function makes a call to govuk-notify-service with mapped contents
that are expected by the govuk-notify-service template.
Args: Takes an instance of Notification class.
Raises HTTPError if any of the required contents are incorrect
or missing.
"""
try:
notifications_client = NotificationsAPIClient(
Config.GOV_NOTIFY_API_KEY
)
contents = ApplicationReminder.from_notification(notification)
response = notifications_client.send_email_notification(
email_address=contents.contact_info,
template_id=Config.APPLICATION_DEADLINE_REMINDER_TEMPLATE_ID,
personalisation={
"name of fund": contents.fund_name,
"application reference": contents.reference,
"round name": contents.round_name,
"application deadline": contents.deadline_date,
},
)
current_app.logger.info("Call made to govuk Notify API")
return response, code
except errors.HTTPError:
current_app.logger.exception(
"HTTPError while sending notification"
)
return invalid_data_error(
Application.from_notification(notification)
)