mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-16 11:20:41 -04:00
Add dao to delete inbound sms after seven days
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
from datetime import (
|
||||
timedelta,
|
||||
datetime
|
||||
)
|
||||
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.models import InboundSms
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
|
||||
@transactional
|
||||
@@ -28,3 +35,15 @@ def dao_count_inbound_sms_for_service(service_id):
|
||||
return InboundSms.query.filter(
|
||||
InboundSms.service_id == service_id
|
||||
).count()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
@transactional
|
||||
def delete_inbound_sms_created_more_than_a_week_ago():
|
||||
seven_days_ago = datetime.utcnow() - timedelta(days=7)
|
||||
|
||||
deleted = db.session.query(InboundSms).filter(
|
||||
InboundSms.created_at < seven_days_ago
|
||||
).delete(synchronize_session='fetch')
|
||||
|
||||
return deleted
|
||||
|
||||
@@ -2,7 +2,8 @@ import functools
|
||||
from datetime import (
|
||||
datetime,
|
||||
timedelta,
|
||||
date)
|
||||
date
|
||||
)
|
||||
|
||||
from flask import current_app
|
||||
|
||||
@@ -26,6 +27,7 @@ from app.models import (
|
||||
NotificationHistory,
|
||||
NotificationStatistics,
|
||||
Template,
|
||||
ScheduledNotification,
|
||||
NOTIFICATION_CREATED,
|
||||
NOTIFICATION_DELIVERED,
|
||||
NOTIFICATION_SENDING,
|
||||
@@ -35,7 +37,8 @@ from app.models import (
|
||||
NOTIFICATION_PERMANENT_FAILURE,
|
||||
KEY_TYPE_NORMAL, KEY_TYPE_TEST,
|
||||
LETTER_TYPE,
|
||||
NOTIFICATION_SENT, ScheduledNotification)
|
||||
NOTIFICATION_SENT,
|
||||
)
|
||||
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from freezegun import freeze_time
|
||||
|
||||
from app.dao.inbound_sms_dao import dao_get_inbound_sms_for_service, dao_count_inbound_sms_for_service
|
||||
|
||||
from app.dao.inbound_sms_dao import (
|
||||
dao_get_inbound_sms_for_service,
|
||||
dao_count_inbound_sms_for_service,
|
||||
delete_inbound_sms_created_more_than_a_week_ago
|
||||
)
|
||||
from tests.app.db import create_inbound_sms, create_service
|
||||
|
||||
from app.models import InboundSms
|
||||
|
||||
|
||||
def test_get_all_inbound_sms(sample_service):
|
||||
inbound = create_inbound_sms(sample_service)
|
||||
@@ -57,3 +62,27 @@ def test_count_inbound_sms_for_service(notify_db_session):
|
||||
create_inbound_sms(service_two)
|
||||
|
||||
assert dao_count_inbound_sms_for_service(service_one.id) == 2
|
||||
|
||||
|
||||
@freeze_time("2017-01-01 12:00:00")
|
||||
def test_should_delete_inbound_sms_older_than_seven_days(sample_service):
|
||||
older_than_seven_days = datetime.utcnow() - timedelta(days=7, seconds=1)
|
||||
create_inbound_sms(sample_service, created_at=older_than_seven_days)
|
||||
delete_inbound_sms_created_more_than_a_week_ago()
|
||||
|
||||
assert len(InboundSms.query.all()) == 0
|
||||
|
||||
|
||||
@freeze_time("2017-01-01 12:00:00")
|
||||
def test_should_not_delete_inbound_sms_before_seven_days(sample_service):
|
||||
yesterday = datetime.utcnow() - timedelta(days=1)
|
||||
just_before_seven_days = datetime.utcnow() - timedelta(days=6, hours=23, minutes=59, seconds=59)
|
||||
older_than_seven_days = datetime.utcnow() - timedelta(days=7, seconds=1)
|
||||
|
||||
create_inbound_sms(sample_service, created_at=yesterday)
|
||||
create_inbound_sms(sample_service, created_at=just_before_seven_days)
|
||||
create_inbound_sms(sample_service, created_at=older_than_seven_days)
|
||||
|
||||
delete_inbound_sms_created_more_than_a_week_ago()
|
||||
|
||||
assert len(InboundSms.query.all()) == 2
|
||||
|
||||
@@ -2,6 +2,7 @@ from datetime import datetime
|
||||
import uuid
|
||||
|
||||
|
||||
from app.dao.inbound_sms_dao import dao_create_inbound_sms
|
||||
from app.dao.jobs_dao import dao_create_job
|
||||
from app.models import (
|
||||
InboundSms,
|
||||
@@ -12,6 +13,7 @@ from app.models import (
|
||||
ScheduledNotification,
|
||||
ServicePermission,
|
||||
Job,
|
||||
InboundSms,
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE,
|
||||
KEY_TYPE_NORMAL,
|
||||
@@ -151,14 +153,15 @@ def create_notification(
|
||||
return notification
|
||||
|
||||
|
||||
def create_job(template,
|
||||
notification_count=1,
|
||||
created_at=None,
|
||||
job_status='pending',
|
||||
scheduled_for=None,
|
||||
processing_started=None,
|
||||
original_file_name='some.csv'):
|
||||
|
||||
def create_job(
|
||||
template,
|
||||
notification_count=1,
|
||||
created_at=None,
|
||||
job_status='pending',
|
||||
scheduled_for=None,
|
||||
processing_started=None,
|
||||
original_file_name='some.csv'
|
||||
):
|
||||
data = {
|
||||
'id': uuid.uuid4(),
|
||||
'service_id': template.service_id,
|
||||
@@ -193,11 +196,12 @@ def create_inbound_sms(
|
||||
user_number='447700900111',
|
||||
provider_date=None,
|
||||
provider_reference=None,
|
||||
content='Hello'
|
||||
content='Hello',
|
||||
created_at=None
|
||||
):
|
||||
inbound = InboundSms(
|
||||
service=service,
|
||||
created_at=datetime.utcnow(),
|
||||
created_at=created_at or datetime.utcnow(),
|
||||
notify_number=notify_number or service.sms_sender,
|
||||
user_number=user_number,
|
||||
provider_date=provider_date or datetime.utcnow(),
|
||||
|
||||
Reference in New Issue
Block a user