From b4a6aecbb946f062a051a17aa174cc36ce3a7ab1 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Fri, 19 Jul 2019 10:57:14 +0100 Subject: [PATCH] Add endpoint to return monthly notification stats per service This will be used by notifications-admin to create a CSV report. --- app/service/rest.py | 12 +++++++++++- tests/app/service/test_rest.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/service/rest.py b/app/service/rest.py index f36d4b729..5917234ac 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -13,7 +13,7 @@ from sqlalchemy.exc import IntegrityError from sqlalchemy.orm.exc import NoResultFound from app.config import QueueNames -from app.dao import notifications_dao +from app.dao import fact_notification_status_dao, notifications_dao from app.dao.dao_utils import dao_rollback from app.dao.date_util import get_financial_year from app.dao.api_key_dao import ( @@ -887,6 +887,16 @@ def modify_service_data_retention(service_id, data_retention_id): return '', 204 +@service_blueprint.route('/monthly-data-by-service') +def get_monthly_notification_data_by_service(): + start_date = request.args.get('start_date') + end_date = request.args.get('end_date') + + result = fact_notification_status_dao.fetch_monthly_notification_statuses_per_service(start_date, end_date) + + return jsonify(result) + + def check_request_args(request): service_id = request.args.get('service_id') name = request.args.get('name', None) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 028584452..a741c1fed 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -3275,3 +3275,21 @@ def test_cancel_notification_for_service_updates_letter_if_still_time_to_cancel( notification_id=sample_letter_notification.id, ) assert response['status'] == 'cancelled' + + +def test_get_monthly_notification_data_by_service(mocker, admin_request): + dao_mock = mocker.patch( + 'app.service.rest.fact_notification_status_dao.fetch_monthly_notification_statuses_per_service', + return_value=[]) + + start_date = '2019-01-01' + end_date = '2019-06-17' + + response = admin_request.get( + 'service.get_monthly_notification_data_by_service', + start_date=start_date, + end_date=end_date + ) + + dao_mock.assert_called_once_with(start_date, end_date) + assert response == []