diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 9741ec99b..db8a0da81 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -607,6 +607,7 @@ def fetch_sms_billing_for_organisation(organisation_id, start_date, end_date): sms_billable_units.label('sms_billable_units'), chargeable_sms.label("chargeable_billable_sms"), sms_cost.label('sms_cost'), + Service.active.label("active") ).select_from( Service ).outerjoin( @@ -658,7 +659,8 @@ def fetch_usage_year_for_organisation(organisation_id, year): 'chargeable_billable_sms': 0, 'sms_cost': 0.0, 'letter_cost': 0.0, - 'emails_sent': 0 + 'emails_sent': 0, + 'active': service.active } sms_usages = fetch_sms_billing_for_organisation(organisation_id, year_start_date, year_end_date) letter_usages = fetch_letter_costs_for_organisation(organisation_id, year_start_date, year_end_date) @@ -673,7 +675,8 @@ def fetch_usage_year_for_organisation(organisation_id, year): 'chargeable_billable_sms': usage.chargeable_billable_sms, 'sms_cost': float(usage.sms_cost), 'letter_cost': 0.0, - 'emails_sent': 0 + 'emails_sent': 0, + 'active': usage.active } for letter_usage in letter_usages: service_with_usage[str(letter_usage.service_id)]['letter_cost'] = float(letter_usage.letter_cost) diff --git a/app/organisation/rest.py b/app/organisation/rest.py index b1eb613df..f544be041 100644 --- a/app/organisation/rest.py +++ b/app/organisation/rest.py @@ -135,7 +135,7 @@ def get_organisation_services_usage(organisation_id): return jsonify(result='error', message='No valid year provided'), 400 services = fetch_usage_year_for_organisation(organisation_id, year) list_services = services.values() - sorted_services = sorted(list_services, key=lambda s: s['service_name'].lower()) + sorted_services = sorted(list_services, key=lambda s: (-s['active'], s['service_name'].lower())) return jsonify(services=sorted_services) diff --git a/tests/app/organisation/test_rest.py b/tests/app/organisation/test_rest.py index 2f697871b..0fce3ef8b 100644 --- a/tests/app/organisation/test_rest.py +++ b/tests/app/organisation/test_rest.py @@ -5,6 +5,7 @@ import uuid import pytest from freezegun import freeze_time +from app.dao.services_dao import dao_archive_service from app.models import Organisation from app.dao.organisation_dao import dao_add_service_to_organisation, dao_add_user_to_organisation from tests.app.db import ( @@ -773,6 +774,49 @@ def test_get_organisation_services_usage(admin_request, notify_db_session): assert service_usage['sms_cost'] == 0.54 +@freeze_time('2020-02-24 13:30') +def test_get_organisation_services_usage_sort_active_first(admin_request, notify_db_session): + org = create_organisation(name='Organisation without live services') + service = create_service(service_name='live service') + archived_service = create_service(service_name='archived_service') + template = create_template(service=service) + dao_add_service_to_organisation(service=service, organisation_id=org.id) + dao_add_service_to_organisation(service=archived_service, organisation_id=org.id) + create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2019) + create_ft_billing(bst_date=datetime.utcnow().date(), template=template, billable_unit=19, rate=0.060, + notifications_sent=19) + response = admin_request.get( + 'organisation.get_organisation_services_usage', + organisation_id=org.id, + **{"year": 2019} + ) + assert len(response) == 1 + assert len(response['services']) == 2 + first_service = response['services'][0] + assert first_service['service_id'] == str(archived_service.id) + assert first_service['service_name'] == archived_service.name + assert first_service['active'] is True + last_service = response['services'][1] + assert last_service['service_id'] == str(service.id) + assert last_service['service_name'] == service.name + assert last_service['active'] is True + + dao_archive_service(service_id=archived_service.id) + response_after_archive = admin_request.get( + 'organisation.get_organisation_services_usage', + organisation_id=org.id, + **{"year": 2019} + ) + first_service = response_after_archive['services'][0] + assert first_service['service_id'] == str(service.id) + assert first_service['service_name'] == service.name + assert first_service['active'] is True + last_service = response_after_archive['services'][1] + assert last_service['service_id'] == str(archived_service.id) + assert last_service['service_name'] == archived_service.name + assert last_service['active'] is False + + def test_get_organisation_services_usage_returns_400_if_year_is_invalid(admin_request): response = admin_request.get( 'organisation.get_organisation_services_usage',