mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-14 17:22:17 -05:00
Refactor send_sms and send_email to use common code to persist the notificaiton.
This commit is contained in:
@@ -17,7 +17,7 @@ from app.dao.jobs_dao import (
|
||||
dao_update_job,
|
||||
dao_get_job_by_id
|
||||
)
|
||||
from app.dao.notifications_dao import (dao_create_notification)
|
||||
from app.dao.notifications_dao import dao_create_notification
|
||||
from app.dao.services_dao import dao_fetch_service_by_id, fetch_todays_total_message_count
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
from app.models import (
|
||||
@@ -26,6 +26,7 @@ from app.models import (
|
||||
SMS_TYPE,
|
||||
KEY_TYPE_NORMAL
|
||||
)
|
||||
from app.notifications.process_notifications import persist_notification
|
||||
from app.service.utils import service_allowed_to_send_to
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
@@ -41,16 +42,7 @@ def process_job(job_id):
|
||||
|
||||
service = job.service
|
||||
|
||||
total_sent = fetch_todays_total_message_count(service.id)
|
||||
|
||||
if total_sent + job.notification_count > service.message_limit:
|
||||
job.job_status = 'sending limits exceeded'
|
||||
job.processing_finished = datetime.utcnow()
|
||||
dao_update_job(job)
|
||||
current_app.logger.info(
|
||||
"Job {} size {} error. Sending limits {} exceeded".format(
|
||||
job_id, job.notification_count, service.message_limit)
|
||||
)
|
||||
if __sending_limits_for_job_exceeded(service, job, job_id):
|
||||
return
|
||||
|
||||
job.job_status = 'in progress'
|
||||
@@ -106,6 +98,21 @@ def process_job(job_id):
|
||||
)
|
||||
|
||||
|
||||
def __sending_limits_for_job_exceeded(service, job, job_id):
|
||||
total_sent = fetch_todays_total_message_count(service.id)
|
||||
|
||||
if total_sent + job.notification_count > service.message_limit:
|
||||
job.job_status = 'sending limits exceeded'
|
||||
job.processing_finished = datetime.utcnow()
|
||||
dao_update_job(job)
|
||||
current_app.logger.info(
|
||||
"Job {} size {} error. Sending limits {} exceeded".format(
|
||||
job_id, job.notification_count, service.message_limit)
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@notify_celery.task(bind=True, name="send-sms", max_retries=5, default_retry_delay=300)
|
||||
@statsd(namespace="tasks")
|
||||
def send_sms(self,
|
||||
@@ -125,11 +132,18 @@ def send_sms(self,
|
||||
return
|
||||
|
||||
try:
|
||||
dao_create_notification(
|
||||
Notification.from_api_request(
|
||||
created_at, notification, notification_id, service.id, SMS_TYPE, api_key_id, key_type
|
||||
)
|
||||
)
|
||||
persist_notification(template_id=notification['template'],
|
||||
template_version=notification['template_version'],
|
||||
recipient=notification['to'],
|
||||
service_id=service.id,
|
||||
personalisation=notification.get('personalisation'),
|
||||
notification_type=SMS_TYPE,
|
||||
api_key_id=api_key_id,
|
||||
key_type=key_type,
|
||||
job_id=notification.get('job', None),
|
||||
job_row_number=notification.get('row_number', None),
|
||||
)
|
||||
|
||||
provider_tasks.deliver_sms.apply_async(
|
||||
[notification_id],
|
||||
queue='send-sms' if not service.research_mode else 'research-mode'
|
||||
@@ -166,10 +180,17 @@ def send_email(self, service_id,
|
||||
return
|
||||
|
||||
try:
|
||||
dao_create_notification(
|
||||
Notification.from_api_request(
|
||||
created_at, notification, notification_id, service.id, EMAIL_TYPE, api_key_id, key_type
|
||||
)
|
||||
persist_notification(
|
||||
template_id=notification['template'],
|
||||
template_version=notification['template_version'],
|
||||
recipient=notification['to'],
|
||||
service_id=service.id,
|
||||
personalisation=notification.get('personalisation'),
|
||||
notification_type=EMAIL_TYPE,
|
||||
api_key_id=api_key_id,
|
||||
key_type=key_type,
|
||||
job_id=notification.get('job', None),
|
||||
job_row_number=notification.get('row_number', None),
|
||||
)
|
||||
|
||||
provider_tasks.deliver_email.apply_async(
|
||||
|
||||
Reference in New Issue
Block a user