diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 1706be8e3..7ffbda915 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -594,7 +594,6 @@ def fetch_email_usage_for_organisation(organisation_id, start_date, end_date): @statsd(namespace="dao") def fetch_sms_billing_for_organisation(organisation_id, start_date, end_date): - # ASSUMPTION: AnnualBilling has been populated for year. free_allowance_remainder = fetch_sms_free_allowance_remainder(start_date).subquery() @@ -675,15 +674,15 @@ def fetch_usage_year_for_organisation(organisation_id, year): for usage in sms_usages: service_with_usage[str(usage.service_id)] = { - 'service_id': usage.service_id, - 'service_name': usage.service_name, - 'free_sms_limit': usage.free_sms_fragment_limit, - 'sms_remainder': usage.sms_remainder, - 'sms_billable_units': usage.sms_billable_units, - 'chargeable_billable_sms': usage.chargeable_billable_sms, - 'sms_cost': usage.sms_cost, - 'letter_cost': 0, - 'emails_sent': 0 + 'service_id': usage.service_id, + 'service_name': usage.service_name, + 'free_sms_limit': usage.free_sms_fragment_limit, + 'sms_remainder': usage.sms_remainder, + 'sms_billable_units': usage.sms_billable_units, + 'chargeable_billable_sms': usage.chargeable_billable_sms, + 'sms_cost': usage.sms_cost, + 'letter_cost': 0, + 'emails_sent': 0 } for letter_usage in letter_usages: service_with_usage[str(letter_usage.service_id)]['letter_cost'] = letter_usage.letter_cost diff --git a/app/organisation/rest.py b/app/organisation/rest.py index 913df584a..bf7aaebe5 100644 --- a/app/organisation/rest.py +++ b/app/organisation/rest.py @@ -1,7 +1,10 @@ +from datetime import datetime + from flask import abort, Blueprint, jsonify, request, current_app from sqlalchemy.exc import IntegrityError from app.config import QueueNames +from app.dao.fact_billing_dao import fetch_usage_year_for_organisation from app.dao.organisation_dao import ( dao_create_organisation, dao_get_organisations, @@ -127,9 +130,9 @@ def get_organisation_services(organisation_id): @organisation_blueprint.route('//services-with-usage', methods=['GET']) def get_organisation_services_usage(organisation_id): - services = dao_get_organisation_services(organisation_id) - sorted_services = sorted(services, key=lambda s: (-s.active, s.name)) - return jsonify([s.serialize_for_org_dashboard() for s in sorted_services]) + services = fetch_usage_year_for_organisation(organisation_id, datetime.utcnow().year) + + return jsonify(services=services) @organisation_blueprint.route('//users/', methods=['POST']) diff --git a/tests/app/organisation/test_rest.py b/tests/app/organisation/test_rest.py index 38df2b986..ce22c056b 100644 --- a/tests/app/organisation/test_rest.py +++ b/tests/app/organisation/test_rest.py @@ -1,3 +1,5 @@ +from datetime import datetime + import uuid import pytest @@ -11,6 +13,8 @@ from tests.app.db import ( create_organisation, create_service, create_user, + create_template, + create_ft_billing ) @@ -737,3 +741,16 @@ def test_is_organisation_name_unique_returns_400_when_name_does_not_exist(admin_ assert response["message"][0]["org_id"] == ["Can't be empty"] assert response["message"][1]["name"] == ["Can't be empty"] + + +def test_get_organisation_services_usage(admin_request, notify_db_session): + org = create_organisation(name='Organisation without live services') + service = create_service() + template = create_template(service=service) + dao_add_service_to_organisation(service=service, organisation_id=org.id) + create_ft_billing(bst_date=datetime.utcnow().date(), template=template, billable_unit=19, notifications_sent=19) + response = admin_request.get( + 'organisation.get_organisation_services_usage', + organisation_id=org.id + ) + assert len(response) == 1 diff --git a/tests/app/platform_stats/test_rest.py b/tests/app/platform_stats/test_rest.py index 45348e9bd..93b4eda11 100644 --- a/tests/app/platform_stats/test_rest.py +++ b/tests/app/platform_stats/test_rest.py @@ -122,7 +122,8 @@ def test_validate_date_is_within_a_financial_year_when_input_is_not_a_date(start def test_get_usage_for_all_services(notify_db_session, admin_request): - org, org_2, service, service_2, service_3, service_sms_only = set_up_usage_data(datetime(2019, 5, 1)) + org, org_2, service, service_2, service_3, service_sms_only, \ + org_with_emails, service_with_emails = set_up_usage_data(datetime(2019, 5, 1)) response = admin_request.get("platform_stats.get_usage_for_all_services", start_date='2019-05-01', end_date='2019-06-30')