diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 5b5da3f31..c9811967f 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -4,7 +4,7 @@ from flask import current_app from notifications_utils.statsd_decorators import statsd from notifications_utils.timezones import convert_bst_to_utc, convert_utc_to_bst from sqlalchemy.dialects.postgresql import insert -from sqlalchemy import func, case, desc, Date, Integer, and_ +from sqlalchemy import func, desc, Date, Integer, and_ from sqlalchemy.sql.expression import literal from app import db @@ -310,7 +310,7 @@ def delete_billing_data_for_service_for_day(process_day, service_id): @statsd(namespace="dao") -def fetch_billing_data_for_day(process_day, service_id=None): +def fetch_billing_data_for_day(process_day, service_id=None, check_permissions=False): start_date = convert_bst_to_utc(datetime.combine(process_day, time.min)) end_date = convert_bst_to_utc(datetime.combine(process_day + timedelta(days=1), time.min)) current_app.logger.info("Populate ft_billing for {} to {}".format(start_date, end_date)) @@ -322,17 +322,17 @@ def fetch_billing_data_for_day(process_day, service_id=None): for service in services: for notification_type in (SMS_TYPE, EMAIL_TYPE, LETTER_TYPE): - table = get_notification_table_to_use(service, notification_type, process_day, has_delete_task_run=False) - - results = _query_for_billing_data( - table=table, - notification_type=notification_type, - start_date=start_date, - end_date=end_date, - service=service - ) - - transit_data += results + if (not check_permissions) or service.has_permission(notification_type): + table = get_notification_table_to_use(service, notification_type, process_day, + has_delete_task_run=False) + results = _query_for_billing_data( + table=table, + notification_type=notification_type, + start_date=start_date, + end_date=end_date, + service=service + ) + transit_data += results return transit_data diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index 77507474a..6d07de2a1 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -70,6 +70,34 @@ def test_fetch_billing_data_for_today_includes_data_with_the_right_key_type(noti assert results[0].notifications_sent == 2 +@pytest.mark.parametrize("notification_type", ["email", "sms", "letter"]) +def test_fetch_billing_data_for_day_only_calls_query_for_permission_type(notify_db_session, notification_type): + service = create_service(service_permissions=[notification_type]) + email_template = create_template(service=service, template_type="email") + sms_template = create_template(service=service, template_type="sms") + letter_template = create_template(service=service, template_type="letter") + create_notification(template=email_template, status='delivered') + create_notification(template=sms_template, status='delivered') + create_notification(template=letter_template, status='delivered') + today = convert_utc_to_bst(datetime.utcnow()) + results = fetch_billing_data_for_day(process_day=today.date(), check_permissions=True) + assert len(results) == 1 + + +@pytest.mark.parametrize("notification_type", ["email", "sms", "letter"]) +def test_fetch_billing_data_for_day_only_calls_query_for_all_channels(notify_db_session, notification_type): + service = create_service(service_permissions=[notification_type]) + email_template = create_template(service=service, template_type="email") + sms_template = create_template(service=service, template_type="sms") + letter_template = create_template(service=service, template_type="letter") + create_notification(template=email_template, status='delivered') + create_notification(template=sms_template, status='delivered') + create_notification(template=letter_template, status='delivered') + today = convert_utc_to_bst(datetime.utcnow()) + results = fetch_billing_data_for_day(process_day=today.date(), check_permissions=False) + assert len(results) == 3 + + @freeze_time('2018-04-02 01:20:00') def test_fetch_billing_data_for_today_includes_data_with_the_right_date(notify_db_session): process_day = datetime(2018, 4, 1, 13, 30, 0) @@ -288,6 +316,7 @@ def test_fetch_billing_data_for_day_bills_correctly_for_status(notify_db_session assert 4 == email_results[0].notifications_sent assert 3 == letter_results[0].notifications_sent + def test_get_rates_for_billing(notify_db_session): create_rate(start_date=datetime.utcnow(), value=12, notification_type='email') create_rate(start_date=datetime.utcnow(), value=22, notification_type='sms')