mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 15:15:38 -05:00
Add scheduled_for in the post notification request form.
Return scheduled for in get_notification requests.
This commit is contained in:
@@ -463,3 +463,9 @@ def dao_get_notifications_by_to_field(service_id, search_term):
|
||||
return Notification.query.filter(
|
||||
Notification.service_id == service_id,
|
||||
func.replace(func.lower(Notification.to), " ", "") == search_term.lower().replace(" ", "")).all()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_created_scheduled_notification(scheduled_notification):
|
||||
db.session.add(scheduled_notification)
|
||||
db.session.commit()
|
||||
|
||||
@@ -686,6 +686,8 @@ class Notification(db.Model):
|
||||
foreign(template_version) == remote(TemplateHistory.version)
|
||||
))
|
||||
|
||||
scheduled_for = db.relationship('ScheduledNotification')
|
||||
|
||||
client_reference = db.Column(db.String, index=True, nullable=True)
|
||||
|
||||
international = db.Column(db.Boolean, nullable=False, default=False)
|
||||
@@ -846,7 +848,9 @@ class Notification(db.Model):
|
||||
"subject": self.subject,
|
||||
"created_at": self.created_at.strftime(DATETIME_FORMAT),
|
||||
"sent_at": self.sent_at.strftime(DATETIME_FORMAT) if self.sent_at else None,
|
||||
"completed_at": self.completed_at()
|
||||
"completed_at": self.completed_at(),
|
||||
"scheduled_for": self.scheduled_for[0].scheduled_for.strftime(
|
||||
DATETIME_FORMAT) if self.scheduled_for else None
|
||||
}
|
||||
|
||||
return serialized
|
||||
|
||||
@@ -10,8 +10,10 @@ from notifications_utils.recipients import (
|
||||
from app import redis_store
|
||||
from app.celery import provider_tasks
|
||||
from notifications_utils.clients import redis
|
||||
from app.dao.notifications_dao import dao_create_notification, dao_delete_notifications_and_history_by_id
|
||||
from app.models import SMS_TYPE, Notification, KEY_TYPE_TEST, EMAIL_TYPE
|
||||
from app.dao.notifications_dao import (dao_create_notification,
|
||||
dao_delete_notifications_and_history_by_id,
|
||||
dao_created_scheduled_notification)
|
||||
from app.models import SMS_TYPE, Notification, KEY_TYPE_TEST, EMAIL_TYPE, ScheduledNotification
|
||||
from app.v2.errors import BadRequestError, SendNotificationToQueueError
|
||||
from app.utils import get_template_instance, cache_key_for_service_template_counter
|
||||
|
||||
@@ -120,3 +122,9 @@ def simulated_recipient(to_address, notification_type):
|
||||
return to_address in formatted_simulated_numbers
|
||||
else:
|
||||
return to_address in current_app.config['SIMULATED_EMAIL_ADDRESSES']
|
||||
|
||||
|
||||
def persist_scheduled_notification(notification_id, scheduled_for):
|
||||
scheduled_notification = ScheduledNotification(notification_id=notification_id,
|
||||
scheduled_for=scheduled_for)
|
||||
dao_created_scheduled_notification(scheduled_notification)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from datetime import datetime
|
||||
|
||||
from app.models import NOTIFICATION_STATUS_TYPES, TEMPLATE_TYPES
|
||||
from app.schema_validation.definitions import (uuid, personalisation)
|
||||
|
||||
@@ -192,7 +194,7 @@ post_email_response = {
|
||||
}
|
||||
|
||||
|
||||
def create_post_sms_response_from_notification(notification, body, from_number, url_root, service_id):
|
||||
def create_post_sms_response_from_notification(notification, body, from_number, url_root, service_id, scheduled_for):
|
||||
return {"id": notification.id,
|
||||
"reference": notification.client_reference,
|
||||
"content": {'body': body,
|
||||
@@ -200,11 +202,13 @@ def create_post_sms_response_from_notification(notification, body, from_number,
|
||||
"uri": "{}v2/notifications/{}".format(url_root, str(notification.id)),
|
||||
"template": __create_template_from_notification(notification=notification,
|
||||
url_root=url_root,
|
||||
service_id=service_id)
|
||||
service_id=service_id),
|
||||
"scheduled_for": scheduled_for
|
||||
}
|
||||
|
||||
|
||||
def create_post_email_response_from_notification(notification, content, subject, email_from, url_root, service_id):
|
||||
def create_post_email_response_from_notification(notification, content, subject, email_from, url_root, service_id,
|
||||
scheduled_for):
|
||||
return {
|
||||
"id": notification.id,
|
||||
"reference": notification.client_reference,
|
||||
@@ -216,7 +220,8 @@ def create_post_email_response_from_notification(notification, content, subject,
|
||||
"uri": "{}v2/notifications/{}".format(url_root, str(notification.id)),
|
||||
"template": __create_template_from_notification(notification=notification,
|
||||
url_root=url_root,
|
||||
service_id=service_id)
|
||||
service_id=service_id),
|
||||
"scheduled_for": scheduled_for
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@ from app.notifications.process_notifications import (
|
||||
create_content_for_notification,
|
||||
persist_notification,
|
||||
send_notification_to_queue,
|
||||
simulated_recipient)
|
||||
simulated_recipient,
|
||||
persist_scheduled_notification)
|
||||
from app.notifications.validators import (
|
||||
check_template_is_for_notification_type,
|
||||
check_template_is_active,
|
||||
@@ -57,26 +58,32 @@ def post_notification(notification_type):
|
||||
key_type=api_user.key_type,
|
||||
client_reference=form.get('reference', None),
|
||||
simulated=simulated)
|
||||
|
||||
if not simulated:
|
||||
queue_name = 'priority' if template.process_type == PRIORITY else None
|
||||
send_notification_to_queue(notification=notification, research_mode=service.research_mode, queue=queue_name)
|
||||
scheduled_for = form.get("scheduled_for", None)
|
||||
if scheduled_for:
|
||||
persist_scheduled_notification(notification.id, form["scheduled_for"])
|
||||
else:
|
||||
current_app.logger.info("POST simulated notification for id: {}".format(notification.id))
|
||||
if not simulated:
|
||||
queue_name = 'priority' if template.process_type == PRIORITY else None
|
||||
send_notification_to_queue(notification=notification, research_mode=service.research_mode, queue=queue_name)
|
||||
else:
|
||||
current_app.logger.info("POST simulated notification for id: {}".format(notification.id))
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
sms_sender = service.sms_sender if service.sms_sender else current_app.config.get('FROM_NUMBER')
|
||||
resp = create_post_sms_response_from_notification(notification=notification,
|
||||
body=str(template_with_content),
|
||||
from_number=sms_sender,
|
||||
url_root=request.url_root,
|
||||
service_id=service.id)
|
||||
service_id=service.id,
|
||||
scheduled_for=scheduled_for)
|
||||
else:
|
||||
resp = create_post_email_response_from_notification(notification=notification,
|
||||
content=str(template_with_content),
|
||||
subject=template_with_content.subject,
|
||||
email_from=service.email_from,
|
||||
url_root=request.url_root,
|
||||
service_id=service.id)
|
||||
service_id=service.id,
|
||||
scheduled_for=scheduled_for)
|
||||
return jsonify(resp), 201
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user