2018-02-20 11:22:17 +00:00
|
|
|
from app.notify_client import NotifyAdminAPIClient, _attach_current_user
|
2016-03-02 13:52:05 +00:00
|
|
|
|
|
|
|
|
|
2016-11-30 17:01:44 +00:00
|
|
|
class NotificationApiClient(NotifyAdminAPIClient):
|
2016-03-02 13:52:05 +00:00
|
|
|
|
2016-09-21 09:28:06 +01:00
|
|
|
def get_notifications_for_service(
|
|
|
|
|
self,
|
|
|
|
|
service_id,
|
|
|
|
|
job_id=None,
|
|
|
|
|
template_type=None,
|
|
|
|
|
status=None,
|
|
|
|
|
page=None,
|
|
|
|
|
page_size=None,
|
2019-01-08 15:35:44 +00:00
|
|
|
count_pages=None,
|
2016-09-21 09:32:20 +01:00
|
|
|
limit_days=None,
|
|
|
|
|
include_jobs=None,
|
2017-04-20 14:57:31 +01:00
|
|
|
include_from_test_key=None,
|
2017-05-30 12:55:13 +01:00
|
|
|
format_for_csv=None,
|
|
|
|
|
to=None,
|
2018-07-17 17:02:59 +01:00
|
|
|
include_one_off=None,
|
2016-09-21 09:28:06 +01:00
|
|
|
):
|
2019-01-08 15:35:44 +00:00
|
|
|
|
|
|
|
|
params = {
|
|
|
|
|
'page': page,
|
|
|
|
|
'page_size': page_size,
|
|
|
|
|
'template_type': template_type,
|
|
|
|
|
'status': status,
|
|
|
|
|
'include_jobs': include_jobs,
|
|
|
|
|
'include_from_test_key': include_from_test_key,
|
|
|
|
|
'format_for_csv': format_for_csv,
|
|
|
|
|
'to': to,
|
|
|
|
|
'include_one_off': include_one_off,
|
|
|
|
|
'count_pages': count_pages,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
params = {k: v for k, v in params.items() if v is not None}
|
|
|
|
|
|
2016-03-02 13:52:05 +00:00
|
|
|
if job_id:
|
|
|
|
|
return self.get(
|
|
|
|
|
url='/service/{}/job/{}/notifications'.format(service_id, job_id),
|
|
|
|
|
params=params
|
|
|
|
|
)
|
|
|
|
|
else:
|
2016-04-28 15:50:05 +01:00
|
|
|
if limit_days is not None:
|
|
|
|
|
params['limit_days'] = limit_days
|
2016-03-02 13:52:05 +00:00
|
|
|
return self.get(
|
|
|
|
|
url='/service/{}/notifications'.format(service_id),
|
|
|
|
|
params=params
|
|
|
|
|
)
|
2017-05-24 13:19:31 +01:00
|
|
|
|
2017-10-16 16:35:35 +01:00
|
|
|
def send_notification(self, service_id, *, template_id, recipient, personalisation, sender_id):
|
2017-06-22 15:56:05 +01:00
|
|
|
data = {
|
|
|
|
|
'template_id': template_id,
|
|
|
|
|
'to': recipient,
|
|
|
|
|
'personalisation': personalisation,
|
|
|
|
|
}
|
2017-10-16 16:35:35 +01:00
|
|
|
if sender_id:
|
|
|
|
|
data['sender_id'] = sender_id
|
2017-06-22 15:56:05 +01:00
|
|
|
data = _attach_current_user(data)
|
|
|
|
|
return self.post(url='/service/{}/send-notification'.format(service_id), data=data)
|
|
|
|
|
|
2017-06-19 12:31:14 +01:00
|
|
|
def get_notification(self, service_id, notification_id):
|
2017-06-12 17:21:25 +01:00
|
|
|
return self.get(url='/service/{}/notifications/{}'.format(service_id, notification_id))
|
2017-09-20 16:02:15 +01:00
|
|
|
|
|
|
|
|
def get_api_notifications_for_service(self, service_id):
|
2018-07-17 17:02:59 +01:00
|
|
|
ret = self.get_notifications_for_service(
|
|
|
|
|
service_id,
|
|
|
|
|
include_jobs=False,
|
|
|
|
|
include_from_test_key=True,
|
2019-01-08 15:35:44 +00:00
|
|
|
include_one_off=False,
|
|
|
|
|
count_pages=False
|
2018-07-17 17:02:59 +01:00
|
|
|
)
|
2017-09-20 16:02:15 +01:00
|
|
|
return self.map_letters_to_accepted(ret)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def map_letters_to_accepted(notifications):
|
|
|
|
|
for notification in notifications['notifications']:
|
2019-01-07 15:17:09 +00:00
|
|
|
if notification['notification_type'] == 'letter':
|
|
|
|
|
if notification['status'] in ('created', 'sending'):
|
|
|
|
|
notification['status'] = 'accepted'
|
|
|
|
|
|
|
|
|
|
if notification['status'] in ('delivered', 'returned-letter'):
|
|
|
|
|
notification['status'] = 'received'
|
2017-09-20 16:02:15 +01:00
|
|
|
return notifications
|
2018-03-08 12:25:07 +00:00
|
|
|
|
|
|
|
|
def get_notification_letter_preview(self, service_id, notification_id, file_type, page=None):
|
|
|
|
|
|
|
|
|
|
get_url = '/service/{}/template/preview/{}/{}{}'.format(
|
|
|
|
|
service_id,
|
|
|
|
|
notification_id,
|
|
|
|
|
file_type,
|
|
|
|
|
'?page={}'.format(page) if page else ''
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return self.get(url=get_url)
|
2018-10-26 15:39:32 +01:00
|
|
|
|
2018-11-15 16:40:55 +00:00
|
|
|
def update_notification_to_cancelled(self, service_id, notification_id):
|
|
|
|
|
return self.post(
|
|
|
|
|
url='/service/{}/notifications/{}/cancel'.format(service_id, notification_id),
|
|
|
|
|
data={})
|
|
|
|
|
|
2018-10-26 15:39:32 +01:00
|
|
|
|
|
|
|
|
notification_api_client = NotificationApiClient()
|