Added the notification_to_sms_sender mapping table to the purge notifications query

This commit is contained in:
Rebecca Law
2017-10-30 15:17:01 +00:00
parent 0887910b1b
commit db6668eb61
3 changed files with 44 additions and 6 deletions

View File

@@ -33,6 +33,7 @@ from app.models import (
ServiceEmailReplyTo, ServiceEmailReplyTo,
Template, Template,
EMAIL_TYPE, EMAIL_TYPE,
SMS_TYPE,
KEY_TYPE_NORMAL, KEY_TYPE_NORMAL,
KEY_TYPE_TEST, KEY_TYPE_TEST,
LETTER_TYPE, LETTER_TYPE,
@@ -374,17 +375,22 @@ def delete_notifications_created_more_than_a_week_ago_by_type(notification_type)
seven_days_ago = date.today() - timedelta(days=7) seven_days_ago = date.today() - timedelta(days=7)
# Following could be refactored when NotificationSmsReplyTo and NotificationLetterContact in models.py # Following could be refactored when NotificationSmsReplyTo and NotificationLetterContact in models.py
if notification_type == EMAIL_TYPE: if notification_type in [EMAIL_TYPE, SMS_TYPE]:
subq = db.session.query(Notification.id).filter( subq = db.session.query(Notification.id).filter(
func.date(Notification.created_at) < seven_days_ago, func.date(Notification.created_at) < seven_days_ago,
Notification.notification_type == notification_type Notification.notification_type == notification_type
).subquery() ).subquery()
deleted = db.session.query( if notification_type == EMAIL_TYPE:
NotificationEmailReplyTo notification_sender_mapping_table = NotificationEmailReplyTo
if notification_type == SMS_TYPE:
notification_sender_mapping_table = NotificationSmsSender
db.session.query(
notification_sender_mapping_table
).filter( ).filter(
NotificationEmailReplyTo.notification_id.in_(subq) notification_sender_mapping_table.notification_id.in_(subq)
).delete(synchronize_session='fetch') ).delete(synchronize_session='fetch')
deleted = db.session.query(Notification).filter( deleted = db.session.query(Notification).filter(
func.date(Notification.created_at) < seven_days_ago, func.date(Notification.created_at) < seven_days_ago,
Notification.notification_type == notification_type, Notification.notification_type == notification_type,

View File

@@ -10,7 +10,7 @@ from app.dao.dao_utils import (
transactional, transactional,
version_class version_class
) )
from app.dao.notifications_dao import get_financial_year from app.dao.date_util import get_financial_year
from app.dao.service_sms_sender_dao import insert_service_sms_sender from app.dao.service_sms_sender_dao import insert_service_sms_sender
from app.models import ( from app.models import (
NotificationStatistics, NotificationStatistics,

View File

@@ -22,7 +22,7 @@ from app.models import (
KEY_TYPE_NORMAL, KEY_TYPE_NORMAL,
KEY_TYPE_TEAM, KEY_TYPE_TEAM,
KEY_TYPE_TEST, KEY_TYPE_TEST,
JOB_STATUS_IN_PROGRESS, NotificationSmsSender) JOB_STATUS_IN_PROGRESS, NotificationSmsSender, SMS_TYPE)
from app.dao.notifications_dao import ( from app.dao.notifications_dao import (
dao_create_notification, dao_create_notification,
@@ -1044,6 +1044,38 @@ def test_should_delete_notification_to_email_reply_to_after_seven_days(
assert notification.created_at.date() >= date(2016, 1, 3) assert notification.created_at.date() >= date(2016, 1, 3)
@freeze_time("2016-01-10 12:00:00.000000")
def test_should_delete_notification_to_sms_sender_after_seven_days(
sample_template
):
assert len(Notification.query.all()) == 0
sms_sender = create_service_sms_sender(service=sample_template.service, sms_sender='123456', is_default=False)
# create one notification a day between 1st and 10th from 11:00 to 19:00 of each type
for i in range(1, 11):
past_date = '2016-01-{0:02d} {0:02d}:00:00.000000'.format(i)
with freeze_time(past_date):
notification = create_notification(template=sample_template, sms_sender_id=sms_sender.id)
all_notifications = Notification.query.all()
assert len(all_notifications) == 10
all_notification_sms_senders = NotificationSmsSender.query.all()
assert len(all_notification_sms_senders) == 10
# Records before 3rd should be deleted
delete_notifications_created_more_than_a_week_ago_by_type(SMS_TYPE)
remaining_notifications = Notification.query.filter_by(notification_type=SMS_TYPE).all()
remaining_notification_to_sms_sender = NotificationSmsSender.query.filter_by().all()
assert len(remaining_notifications) == 8
assert len(remaining_notification_to_sms_sender) == 8
for notification in remaining_notifications:
assert notification.created_at.date() >= date(2016, 1, 3)
@pytest.mark.parametrize('notification_type', ['sms', 'email', 'letter']) @pytest.mark.parametrize('notification_type', ['sms', 'email', 'letter'])
@freeze_time("2016-01-10 12:00:00.000000") @freeze_time("2016-01-10 12:00:00.000000")
def test_should_not_delete_notification_history(notify_db, notify_db_session, sample_service, notification_type): def test_should_not_delete_notification_history(notify_db, notify_db_session, sample_service, notification_type):