mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 02:11:11 -05:00
Update the query for the notifications activity page to return the data for the days of retention if set.
This commit is contained in:
@@ -20,6 +20,14 @@ def fetch_service_data_retention(service_id):
|
|||||||
return data_retention_list
|
return data_retention_list
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_service_data_retention_by_notification_type(service_id, notification_type):
|
||||||
|
data_retention_list = ServiceDataRetention.query.filter_by(
|
||||||
|
service_id=service_id,
|
||||||
|
notification_type=notification_type
|
||||||
|
).first()
|
||||||
|
return data_retention_list
|
||||||
|
|
||||||
|
|
||||||
@transactional
|
@transactional
|
||||||
def insert_service_data_retention(service_id, notification_type, days_of_retention):
|
def insert_service_data_retention(service_id, notification_type, days_of_retention):
|
||||||
new_data_retention = ServiceDataRetention(service_id=service_id,
|
new_data_retention = ServiceDataRetention(service_id=service_id,
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ from app.dao.service_data_retention_dao import (
|
|||||||
fetch_service_data_retention_by_id,
|
fetch_service_data_retention_by_id,
|
||||||
insert_service_data_retention,
|
insert_service_data_retention,
|
||||||
update_service_data_retention,
|
update_service_data_retention,
|
||||||
|
fetch_service_data_retention_by_notification_type
|
||||||
)
|
)
|
||||||
from app.dao.service_sms_sender_dao import (
|
from app.dao.service_sms_sender_dao import (
|
||||||
archive_sms_sender,
|
archive_sms_sender,
|
||||||
@@ -324,15 +325,20 @@ def get_service_history(service_id):
|
|||||||
@service_blueprint.route('/<uuid:service_id>/notifications', methods=['GET'])
|
@service_blueprint.route('/<uuid:service_id>/notifications', methods=['GET'])
|
||||||
def get_all_notifications_for_service(service_id):
|
def get_all_notifications_for_service(service_id):
|
||||||
data = notifications_filter_schema.load(request.args).data
|
data = notifications_filter_schema.load(request.args).data
|
||||||
|
notification_type = data.get('template_type')[0] if data.get('template_type') else None
|
||||||
if data.get('to'):
|
if data.get('to'):
|
||||||
notification_type = data.get('template_type')[0] if data.get('template_type') else None
|
|
||||||
return search_for_notification_by_to_field(service_id=service_id,
|
return search_for_notification_by_to_field(service_id=service_id,
|
||||||
search_term=data['to'],
|
search_term=data['to'],
|
||||||
statuses=data.get('status'),
|
statuses=data.get('status'),
|
||||||
notification_type=notification_type)
|
notification_type=notification_type)
|
||||||
page = data['page'] if 'page' in data else 1
|
page = data['page'] if 'page' in data else 1
|
||||||
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
|
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
|
||||||
limit_days = data.get('limit_days')
|
days_of_retention = None
|
||||||
|
if notification_type:
|
||||||
|
days_of_retention = fetch_service_data_retention_by_notification_type(
|
||||||
|
service_id=service_id,
|
||||||
|
notification_type=notification_type).days_of_retention
|
||||||
|
limit_days = days_of_retention if days_of_retention else data.get('limit_days')
|
||||||
include_jobs = data.get('include_jobs', True)
|
include_jobs = data.get('include_jobs', True)
|
||||||
include_from_test_key = data.get('include_from_test_key', False)
|
include_from_test_key = data.get('include_from_test_key', False)
|
||||||
include_one_off = data.get('include_one_off', True)
|
include_one_off = data.get('include_one_off', True)
|
||||||
|
|||||||
@@ -8,10 +8,11 @@ from app.dao.service_data_retention_dao import (
|
|||||||
fetch_service_data_retention,
|
fetch_service_data_retention,
|
||||||
insert_service_data_retention,
|
insert_service_data_retention,
|
||||||
update_service_data_retention,
|
update_service_data_retention,
|
||||||
fetch_service_data_retention_by_id
|
fetch_service_data_retention_by_id,
|
||||||
|
fetch_service_data_retention_by_notification_type
|
||||||
)
|
)
|
||||||
from app.models import ServiceDataRetention
|
from app.models import ServiceDataRetention
|
||||||
from tests.app.db import create_service
|
from tests.app.db import create_service, create_service_data_retention
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_service_data_retention(sample_service):
|
def test_fetch_service_data_retention(sample_service):
|
||||||
@@ -131,3 +132,17 @@ def test_update_service_data_retention_does_not_update_row_if_data_retention_is_
|
|||||||
service_id=uuid.uuid4(),
|
service_id=uuid.uuid4(),
|
||||||
days_of_retention=5)
|
days_of_retention=5)
|
||||||
assert updated_count == 0
|
assert updated_count == 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('notification_type, alternate',
|
||||||
|
[('sms', 'email'),
|
||||||
|
('email', 'sms'), ('letter', 'email')])
|
||||||
|
def test_fetch_service_data_retention_by_notification_type(sample_service, notification_type, alternate):
|
||||||
|
data_retention = create_service_data_retention(service_id=sample_service.id, notification_type=notification_type)
|
||||||
|
create_service_data_retention(service_id=sample_service.id, notification_type=alternate)
|
||||||
|
result = fetch_service_data_retention_by_notification_type(sample_service.id, notification_type)
|
||||||
|
assert result == data_retention
|
||||||
|
|
||||||
|
|
||||||
|
def test_fetch_service_data_retention_by_notification_type_returns_none_when_no_rows(sample_service):
|
||||||
|
assert not fetch_service_data_retention_by_notification_type(sample_service.id, 'email')
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ from tests.app.db import (
|
|||||||
create_letter_contact,
|
create_letter_contact,
|
||||||
create_inbound_number,
|
create_inbound_number,
|
||||||
create_service_sms_sender,
|
create_service_sms_sender,
|
||||||
create_service_with_defined_sms_sender
|
create_service_with_defined_sms_sender,
|
||||||
|
create_service_data_retention
|
||||||
)
|
)
|
||||||
from tests.app.db import create_user
|
from tests.app.db import create_user
|
||||||
|
|
||||||
@@ -1225,29 +1226,29 @@ def test_get_service_and_api_key_history(notify_api, notify_db, notify_db_sessio
|
|||||||
assert json_resp['data']['api_key_history'][0]['id'] == str(api_key.id)
|
assert json_resp['data']['api_key_history'][0]['id'] == str(api_key.id)
|
||||||
|
|
||||||
|
|
||||||
def test_get_all_notifications_for_service_in_order(notify_api, notify_db, notify_db_session):
|
def test_get_all_notifications_for_service_in_order(client, notify_db_session):
|
||||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
service_1 = create_service(service_name="1", email_from='1')
|
||||||
service_1 = create_service(service_name="1", email_from='1')
|
service_2 = create_service(service_name="2", email_from='2')
|
||||||
service_2 = create_service(service_name="2", email_from='2')
|
template_1 = create_template(service=service_1)
|
||||||
|
template_2 = create_template(service=service_2)
|
||||||
|
create_notification(template=template_2)
|
||||||
|
|
||||||
create_sample_notification(notify_db, notify_db_session, service=service_2)
|
notification_1 = create_notification(template=template_1)
|
||||||
|
notification_2 = create_notification(template=template_1)
|
||||||
|
notification_3 = create_notification(template=template_1)
|
||||||
|
|
||||||
notification_1 = create_sample_notification(notify_db, notify_db_session, service=service_1)
|
auth_header = create_authorization_header()
|
||||||
notification_2 = create_sample_notification(notify_db, notify_db_session, service=service_1)
|
|
||||||
notification_3 = create_sample_notification(notify_db, notify_db_session, service=service_1)
|
|
||||||
|
|
||||||
auth_header = create_authorization_header()
|
response = client.get(
|
||||||
|
path='/service/{}/notifications'.format(service_1.id),
|
||||||
|
headers=[auth_header])
|
||||||
|
|
||||||
response = client.get(
|
resp = json.loads(response.get_data(as_text=True))
|
||||||
path='/service/{}/notifications'.format(service_1.id),
|
assert len(resp['notifications']) == 3
|
||||||
headers=[auth_header])
|
assert resp['notifications'][0]['id'] == str(notification_3.id)
|
||||||
|
assert resp['notifications'][1]['id'] == str(notification_2.id)
|
||||||
resp = json.loads(response.get_data(as_text=True))
|
assert resp['notifications'][2]['id'] == str(notification_1.id)
|
||||||
assert len(resp['notifications']) == 3
|
assert response.status_code == 200
|
||||||
assert resp['notifications'][0]['to'] == notification_3.to
|
|
||||||
assert resp['notifications'][1]['to'] == notification_2.to
|
|
||||||
assert resp['notifications'][2]['to'] == notification_1.to
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_all_notifications_for_service_formatted_for_csv(client, sample_template):
|
def test_get_all_notifications_for_service_formatted_for_csv(client, sample_template):
|
||||||
@@ -1268,6 +1269,19 @@ def test_get_all_notifications_for_service_formatted_for_csv(client, sample_temp
|
|||||||
assert resp['notifications'][0]['status'] == 'Sending'
|
assert resp['notifications'][0]['status'] == 'Sending'
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_all_notifications_for_service_limits_days_for_number_of_days_of_retention(client, sample_template):
|
||||||
|
create_service_data_retention(service_id=sample_template.service_id, notification_type='sms', days_of_retention=9)
|
||||||
|
create_notification(template=sample_template)
|
||||||
|
create_notification(template=sample_template, created_at=datetime.utcnow() + timedelta(days=8))
|
||||||
|
|
||||||
|
response = client.get(
|
||||||
|
path='/service/{}/notifications?template_type=sms&limit_days=7'.format(sample_template.service_id),
|
||||||
|
headers=[create_authorization_header()])
|
||||||
|
assert response.status_code == 200
|
||||||
|
json_resp = json.loads(response.get_data(as_text=True))
|
||||||
|
assert len(json_resp['notifications']) == 2
|
||||||
|
|
||||||
|
|
||||||
def test_get_notification_for_service_without_uuid(client, notify_db, notify_db_session):
|
def test_get_notification_for_service_without_uuid(client, notify_db, notify_db_session):
|
||||||
service_1 = create_service(service_name="1", email_from='1')
|
service_1 = create_service(service_name="1", email_from='1')
|
||||||
response = client.get(
|
response = client.get(
|
||||||
|
|||||||
Reference in New Issue
Block a user