mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
store api_key_id and key_type on notification
pass through from POST /notification/<type> to the celery task also removed a couple of asserts that can fail (based on unfrozen time comparisons)
This commit is contained in:
@@ -38,7 +38,8 @@ from app.dao.templates_dao import dao_get_template_by_id
|
||||
from app.models import (
|
||||
Notification,
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE
|
||||
SMS_TYPE,
|
||||
KEY_TYPE_NORMAL
|
||||
)
|
||||
|
||||
|
||||
@@ -133,7 +134,13 @@ def remove_job(job_id):
|
||||
|
||||
|
||||
@notify_celery.task(bind=True, name="send-sms", max_retries=5, default_retry_delay=5)
|
||||
def send_sms(self, service_id, notification_id, encrypted_notification, created_at):
|
||||
def send_sms(self,
|
||||
service_id,
|
||||
notification_id,
|
||||
encrypted_notification,
|
||||
created_at,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL):
|
||||
task_start = monotonic()
|
||||
notification = encryption.decrypt(encrypted_notification)
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
@@ -158,7 +165,9 @@ def send_sms(self, service_id, notification_id, encrypted_notification, created_
|
||||
status='created',
|
||||
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
|
||||
personalisation=notification.get('personalisation'),
|
||||
notification_type=SMS_TYPE
|
||||
notification_type=SMS_TYPE,
|
||||
api_key_id=api_key_id,
|
||||
key_type=key_type
|
||||
)
|
||||
dao_create_notification(notification_db_object, SMS_TYPE)
|
||||
|
||||
@@ -176,7 +185,13 @@ def send_sms(self, service_id, notification_id, encrypted_notification, created_
|
||||
|
||||
|
||||
@notify_celery.task(name="send-email")
|
||||
def send_email(service_id, notification_id, encrypted_notification, created_at, reply_to_addresses=None):
|
||||
def send_email(service_id,
|
||||
notification_id,
|
||||
encrypted_notification,
|
||||
created_at,
|
||||
reply_to_addresses=None,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL):
|
||||
task_start = monotonic()
|
||||
notification = encryption.decrypt(encrypted_notification)
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
@@ -204,7 +219,9 @@ def send_email(service_id, notification_id, encrypted_notification, created_at,
|
||||
sent_at=sent_at,
|
||||
sent_by=provider.get_name(),
|
||||
personalisation=notification.get('personalisation'),
|
||||
notification_type=EMAIL_TYPE
|
||||
notification_type=EMAIL_TYPE,
|
||||
api_key_id=api_key_id,
|
||||
key_type=key_type
|
||||
)
|
||||
|
||||
dao_create_notification(notification_db_object, EMAIL_TYPE)
|
||||
|
||||
@@ -267,19 +267,33 @@ def send_notification(notification_type):
|
||||
notification_id = create_uuid()
|
||||
notification.update({"template_version": template.version})
|
||||
if notification_type == SMS_TYPE:
|
||||
send_sms.apply_async((
|
||||
service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
), queue='sms')
|
||||
send_sms.apply_async(
|
||||
(
|
||||
service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
),
|
||||
kwargs={
|
||||
'api_key_id': str(api_user.id),
|
||||
'key_type': api_user.key_type
|
||||
},
|
||||
queue='sms'
|
||||
)
|
||||
else:
|
||||
send_email.apply_async((
|
||||
service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
), queue='email')
|
||||
send_email.apply_async(
|
||||
(
|
||||
service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
),
|
||||
kwargs={
|
||||
'api_key_id': str(api_user.id),
|
||||
'key_type': api_user.key_type
|
||||
},
|
||||
queue='email'
|
||||
)
|
||||
|
||||
statsd_client.incr('notifications.api.{}'.format(notification_type))
|
||||
return jsonify(
|
||||
|
||||
Reference in New Issue
Block a user