Change the sort order for the organisation usage page.

Ensure the archived services are at the bottom of the list. The organisation trial mode page already sorts the archived services to the bottom.
This commit is contained in:
Rebecca Law
2021-01-12 09:44:35 +00:00
parent 4980c3e0fa
commit e05e9bb5e0
3 changed files with 50 additions and 3 deletions

View File

@@ -607,6 +607,7 @@ def fetch_sms_billing_for_organisation(organisation_id, start_date, end_date):
sms_billable_units.label('sms_billable_units'), sms_billable_units.label('sms_billable_units'),
chargeable_sms.label("chargeable_billable_sms"), chargeable_sms.label("chargeable_billable_sms"),
sms_cost.label('sms_cost'), sms_cost.label('sms_cost'),
Service.active.label("active")
).select_from( ).select_from(
Service Service
).outerjoin( ).outerjoin(
@@ -658,7 +659,8 @@ def fetch_usage_year_for_organisation(organisation_id, year):
'chargeable_billable_sms': 0, 'chargeable_billable_sms': 0,
'sms_cost': 0.0, 'sms_cost': 0.0,
'letter_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) 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) 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, 'chargeable_billable_sms': usage.chargeable_billable_sms,
'sms_cost': float(usage.sms_cost), 'sms_cost': float(usage.sms_cost),
'letter_cost': 0.0, 'letter_cost': 0.0,
'emails_sent': 0 'emails_sent': 0,
'active': usage.active
} }
for letter_usage in letter_usages: for letter_usage in letter_usages:
service_with_usage[str(letter_usage.service_id)]['letter_cost'] = float(letter_usage.letter_cost) service_with_usage[str(letter_usage.service_id)]['letter_cost'] = float(letter_usage.letter_cost)

View File

@@ -135,7 +135,7 @@ def get_organisation_services_usage(organisation_id):
return jsonify(result='error', message='No valid year provided'), 400 return jsonify(result='error', message='No valid year provided'), 400
services = fetch_usage_year_for_organisation(organisation_id, year) services = fetch_usage_year_for_organisation(organisation_id, year)
list_services = services.values() 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) return jsonify(services=sorted_services)

View File

@@ -5,6 +5,7 @@ import uuid
import pytest import pytest
from freezegun import freeze_time from freezegun import freeze_time
from app.dao.services_dao import dao_archive_service
from app.models import Organisation from app.models import Organisation
from app.dao.organisation_dao import dao_add_service_to_organisation, dao_add_user_to_organisation from app.dao.organisation_dao import dao_add_service_to_organisation, dao_add_user_to_organisation
from tests.app.db import ( 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 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): def test_get_organisation_services_usage_returns_400_if_year_is_invalid(admin_request):
response = admin_request.get( response = admin_request.get(
'organisation.get_organisation_services_usage', 'organisation.get_organisation_services_usage',