mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
Merge branch 'master' of https://github.com/alphagov/notifications-api into vb-receipt-callback-dao
This commit is contained in:
@@ -24,14 +24,10 @@ from app import db, create_uuid
|
||||
from app.dao import days_ago
|
||||
from app.models import (
|
||||
Notification,
|
||||
NotificationEmailReplyTo,
|
||||
NotificationHistory,
|
||||
ScheduledNotification,
|
||||
ServiceEmailReplyTo,
|
||||
Template,
|
||||
TemplateHistory,
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE,
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEST,
|
||||
LETTER_TYPE,
|
||||
@@ -42,9 +38,7 @@ from app.models import (
|
||||
NOTIFICATION_TECHNICAL_FAILURE,
|
||||
NOTIFICATION_TEMPORARY_FAILURE,
|
||||
NOTIFICATION_PERMANENT_FAILURE,
|
||||
NOTIFICATION_SENT,
|
||||
NotificationSmsSender,
|
||||
ServiceSmsSender
|
||||
NOTIFICATION_SENT
|
||||
)
|
||||
|
||||
from app.dao.dao_utils import transactional
|
||||
@@ -315,23 +309,6 @@ def _filter_query(query, filter_dict=None):
|
||||
@transactional
|
||||
def delete_notifications_created_more_than_a_week_ago_by_type(notification_type):
|
||||
seven_days_ago = date.today() - timedelta(days=7)
|
||||
|
||||
# Following could be refactored when NotificationSmsReplyTo and NotificationLetterContact in models.py
|
||||
if notification_type in [EMAIL_TYPE, SMS_TYPE]:
|
||||
subq = db.session.query(Notification.id).filter(
|
||||
func.date(Notification.created_at) < seven_days_ago,
|
||||
Notification.notification_type == notification_type
|
||||
).subquery()
|
||||
if notification_type == EMAIL_TYPE:
|
||||
notification_sender_mapping_table = NotificationEmailReplyTo
|
||||
if notification_type == SMS_TYPE:
|
||||
notification_sender_mapping_table = NotificationSmsSender
|
||||
db.session.query(
|
||||
notification_sender_mapping_table
|
||||
).filter(
|
||||
notification_sender_mapping_table.notification_id.in_(subq)
|
||||
).delete(synchronize_session='fetch')
|
||||
|
||||
deleted = db.session.query(Notification).filter(
|
||||
func.date(Notification.created_at) < seven_days_ago,
|
||||
Notification.notification_type == notification_type,
|
||||
@@ -570,26 +547,6 @@ def dao_set_created_live_letter_api_notifications_to_pending():
|
||||
return notifications
|
||||
|
||||
|
||||
@transactional
|
||||
def dao_create_notification_email_reply_to_mapping(notification_id, email_reply_to_id):
|
||||
notification_email_reply_to = NotificationEmailReplyTo(
|
||||
notification_id=notification_id,
|
||||
service_email_reply_to_id=email_reply_to_id
|
||||
)
|
||||
db.session.add(notification_email_reply_to)
|
||||
|
||||
|
||||
def dao_get_notification_email_reply_for_notification(notification_id):
|
||||
email_reply_to = ServiceEmailReplyTo.query.join(
|
||||
NotificationEmailReplyTo
|
||||
).filter(
|
||||
NotificationEmailReplyTo.notification_id == notification_id
|
||||
).first()
|
||||
|
||||
if email_reply_to:
|
||||
return email_reply_to.email_address
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_last_notification_added_for_job_id(job_id):
|
||||
last_notification_added = Notification.query.filter(
|
||||
@@ -599,23 +556,3 @@ def dao_get_last_notification_added_for_job_id(job_id):
|
||||
).first()
|
||||
|
||||
return last_notification_added
|
||||
|
||||
|
||||
@transactional
|
||||
def dao_create_notification_sms_sender_mapping(notification_id, sms_sender_id):
|
||||
notification_to_sms_sender = NotificationSmsSender(
|
||||
notification_id=notification_id,
|
||||
service_sms_sender_id=sms_sender_id
|
||||
)
|
||||
db.session.add(notification_to_sms_sender)
|
||||
|
||||
|
||||
def dao_get_notification_sms_sender_mapping(notification_id):
|
||||
sms_sender = ServiceSmsSender.query.join(
|
||||
NotificationSmsSender
|
||||
).filter(
|
||||
NotificationSmsSender.notification_id == notification_id
|
||||
).first()
|
||||
|
||||
if sms_sender:
|
||||
return sms_sender.sms_sender
|
||||
|
||||
@@ -1572,48 +1572,6 @@ class ServiceLetterContact(db.Model):
|
||||
}
|
||||
|
||||
|
||||
class NotificationEmailReplyTo(db.Model):
|
||||
__tablename__ = "notification_to_email_reply_to"
|
||||
|
||||
notification_id = db.Column(
|
||||
UUID(as_uuid=True),
|
||||
db.ForeignKey('notifications.id'),
|
||||
unique=True,
|
||||
index=True,
|
||||
nullable=False,
|
||||
primary_key=True
|
||||
)
|
||||
service_email_reply_to_id = db.Column(
|
||||
UUID(as_uuid=True),
|
||||
db.ForeignKey('service_email_reply_to.id'),
|
||||
unique=False,
|
||||
index=True,
|
||||
nullable=False,
|
||||
primary_key=True
|
||||
)
|
||||
|
||||
|
||||
class NotificationSmsSender(db.Model):
|
||||
__tablename__ = "notification_to_sms_sender"
|
||||
|
||||
notification_id = db.Column(
|
||||
UUID(as_uuid=True),
|
||||
db.ForeignKey('notifications.id'),
|
||||
unique=True,
|
||||
index=True,
|
||||
nullable=False,
|
||||
primary_key=True
|
||||
)
|
||||
service_sms_sender_id = db.Column(
|
||||
UUID(as_uuid=True),
|
||||
db.ForeignKey('service_sms_senders.id'),
|
||||
unique=False,
|
||||
index=True,
|
||||
nullable=False,
|
||||
primary_key=True
|
||||
)
|
||||
|
||||
|
||||
class AuthType(db.Model):
|
||||
__tablename__ = 'auth_type'
|
||||
|
||||
|
||||
@@ -13,12 +13,19 @@ from notifications_utils.recipients import (
|
||||
from app import redis_store
|
||||
from app.celery import provider_tasks
|
||||
from app.config import QueueNames
|
||||
from app.models import SMS_TYPE, Notification, KEY_TYPE_TEST, EMAIL_TYPE, NOTIFICATION_CREATED, ScheduledNotification
|
||||
from app.dao.notifications_dao import (dao_create_notification,
|
||||
dao_delete_notifications_and_history_by_id,
|
||||
dao_created_scheduled_notification,
|
||||
dao_create_notification_email_reply_to_mapping,
|
||||
dao_create_notification_sms_sender_mapping)
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
KEY_TYPE_TEST,
|
||||
SMS_TYPE,
|
||||
NOTIFICATION_CREATED,
|
||||
Notification,
|
||||
ScheduledNotification
|
||||
)
|
||||
from app.dao.notifications_dao import (
|
||||
dao_create_notification,
|
||||
dao_delete_notifications_and_history_by_id,
|
||||
dao_created_scheduled_notification
|
||||
)
|
||||
from app.v2.errors import BadRequestError
|
||||
from app.utils import get_template_instance, cache_key_for_service_template_counter, convert_bst_to_utc
|
||||
|
||||
@@ -145,11 +152,3 @@ def persist_scheduled_notification(notification_id, scheduled_for):
|
||||
scheduled_notification = ScheduledNotification(notification_id=notification_id,
|
||||
scheduled_for=scheduled_datetime)
|
||||
dao_created_scheduled_notification(scheduled_notification)
|
||||
|
||||
|
||||
def persist_email_reply_to_id_for_notification(notification_id, email_reply_to_id):
|
||||
dao_create_notification_email_reply_to_mapping(notification_id, email_reply_to_id)
|
||||
|
||||
|
||||
def persist_sms_sender_id_for_notification(notification_id, sms_sender_id):
|
||||
dao_create_notification_sms_sender_mapping(notification_id, sms_sender_id)
|
||||
|
||||
@@ -8,9 +8,7 @@ from app.notifications.validators import (
|
||||
)
|
||||
from app.notifications.process_notifications import (
|
||||
persist_notification,
|
||||
send_notification_to_queue,
|
||||
persist_email_reply_to_id_for_notification,
|
||||
persist_sms_sender_id_for_notification
|
||||
send_notification_to_queue
|
||||
)
|
||||
from app.models import (
|
||||
KEY_TYPE_NORMAL,
|
||||
@@ -71,11 +69,6 @@ def send_one_off_notification(service_id, post_data):
|
||||
created_by_id=post_data['created_by'],
|
||||
reply_to_text=reply_to
|
||||
)
|
||||
if sender_id:
|
||||
if template.template_type == EMAIL_TYPE:
|
||||
persist_email_reply_to_id_for_notification(notification.id, sender_id)
|
||||
if template.template_type == SMS_TYPE:
|
||||
persist_sms_sender_id_for_notification(notification.id, sender_id)
|
||||
|
||||
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
|
||||
send_notification_to_queue(
|
||||
|
||||
@@ -16,12 +16,11 @@ from app.models import (
|
||||
)
|
||||
from app.celery.tasks import update_letter_notifications_to_sent_to_dvla
|
||||
from app.notifications.process_notifications import (
|
||||
persist_email_reply_to_id_for_notification,
|
||||
persist_notification,
|
||||
persist_scheduled_notification,
|
||||
send_notification_to_queue,
|
||||
simulated_recipient,
|
||||
persist_sms_sender_id_for_notification)
|
||||
simulated_recipient
|
||||
)
|
||||
from app.notifications.process_letter_notifications import (
|
||||
create_letter_notification
|
||||
)
|
||||
@@ -144,8 +143,6 @@ def process_sms_or_email_notification(*, form, notification_type, api_key, templ
|
||||
reply_to_text=reply_to_text
|
||||
)
|
||||
|
||||
persist_sender_to_notification_mapping(form, notification)
|
||||
|
||||
scheduled_for = form.get("scheduled_for", None)
|
||||
if scheduled_for:
|
||||
persist_scheduled_notification(notification.id, form["scheduled_for"])
|
||||
@@ -163,15 +160,6 @@ def process_sms_or_email_notification(*, form, notification_type, api_key, templ
|
||||
return notification
|
||||
|
||||
|
||||
def persist_sender_to_notification_mapping(form, notification):
|
||||
email_reply_to_id = form.get("email_reply_to_id", None)
|
||||
if email_reply_to_id:
|
||||
persist_email_reply_to_id_for_notification(notification.id, email_reply_to_id)
|
||||
sms_sender_id = form.get("sms_sender_id", None)
|
||||
if sms_sender_id:
|
||||
persist_sms_sender_id_for_notification(notification.id, sms_sender_id)
|
||||
|
||||
|
||||
def process_letter_notification(*, letter_data, api_key, template):
|
||||
if api_key.key_type == KEY_TYPE_TEAM:
|
||||
raise BadRequestError(message='Cannot send letters with a team api key', status_code=403)
|
||||
|
||||
Reference in New Issue
Block a user