From 4b11d776ee7c87970a5243a23925287395ba7122 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 25 Feb 2020 17:49:55 +0000 Subject: [PATCH] Add usage stats to organisation page. --- app/main/views/organisations.py | 2 ++ app/models/organisation.py | 4 +++ app/notify_client/organisations_api_client.py | 6 ++++ .../organisations/organisation/index.html | 10 ++++-- .../views/organisations/test_organisation.py | 31 ++++++++++++------- tests/app/test_navigation.py | 5 ++- 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/app/main/views/organisations.py b/app/main/views/organisations.py index 68ae4995d..05b4fec6d 100644 --- a/app/main/views/organisations.py +++ b/app/main/views/organisations.py @@ -125,8 +125,10 @@ def add_organisation_from_nhs_local_service(service_id): @main.route("/organisations/", methods=['GET']) @user_has_permissions() def organisation_dashboard(org_id): + services = current_organisation.services_and_usage() return render_template( 'views/organisations/organisation/index.html', + services=services ) diff --git a/app/models/organisation.py b/app/models/organisation.py index 327be5670..07ceccb45 100644 --- a/app/models/organisation.py +++ b/app/models/organisation.py @@ -5,6 +5,7 @@ from app.models import JSONModel, ModelList from app.notify_client.email_branding_client import email_branding_client from app.notify_client.letter_branding_client import letter_branding_client from app.notify_client.organisations_api_client import organisations_client +from app.utils import get_current_financial_year class Organisation(JSONModel): @@ -198,6 +199,9 @@ class Organisation(JSONModel): self.id ) + def services_and_usage(self): + return organisations_client.get_services_and_usage(self.id, get_current_financial_year()) + class Organisations(ModelList): client_method = organisations_client.get_organisations diff --git a/app/notify_client/organisations_api_client.py b/app/notify_client/organisations_api_client.py index a62949350..d1c309081 100644 --- a/app/notify_client/organisations_api_client.py +++ b/app/notify_client/organisations_api_client.py @@ -87,5 +87,11 @@ class OrganisationsClient(NotifyAdminAPIClient): params={"org_id": org_id, "name": name} )["result"] + def get_services_and_usage(self, org_id, year): + return self.get( + url=f"/organisations/{org_id}/services-with-usage", + params={"year": str(year)} + ) + organisations_client = OrganisationsClient() diff --git a/app/templates/views/organisations/organisation/index.html b/app/templates/views/organisations/organisation/index.html index 9a5a973de..c26046906 100644 --- a/app/templates/views/organisations/organisation/index.html +++ b/app/templates/views/organisations/organisation/index.html @@ -9,11 +9,17 @@

Usage

+ diff --git a/tests/app/main/views/organisations/test_organisation.py b/tests/app/main/views/organisations/test_organisation.py index 087234de5..e2a48d01a 100644 --- a/tests/app/main/views/organisations/test_organisation.py +++ b/tests/app/main/views/organisations/test_organisation.py @@ -64,7 +64,7 @@ def test_view_organisation_shows_the_correct_organisation( 'app.organisations_client.get_organisation', return_value=org ) mocker.patch( - 'app.organisations_client.get_organisation_services', return_value=[] + 'app.organisations_client.get_services_and_usage', return_value=[] ) page = client_request.get( @@ -389,26 +389,28 @@ def test_nhs_local_assigns_to_selected_organisation( mock_update_service_organisation.assert_called_once_with(SERVICE_ONE_ID, ORGANISATION_ID) -def test_organisation_services_shows_live_services_only( +def test_organisation_services_shows_live_services_and_usage( client_request, mock_get_organisation, mocker, active_user_with_permissions, fake_uuid, ): - mocker.patch( - 'app.organisations_client.get_organisation_services', - return_value=[ - service_json(id_=SERVICE_ONE_ID, name='1', restricted=False, active=True), # live - service_json(id_='2', name='2', restricted=True, active=True), # trial - service_json(id_='3', name='3', restricted=True, active=False), # trial, now archived - service_json(id_='4', name='4', restricted=False, active=False), # was live, now archived - service_json(id_=SERVICE_TWO_ID, name='5', restricted=False, active=True), # live, member of - ] + mock = mocker.patch( + 'app.organisations_client.get_services_and_usage', + return_value={"services": [ + {'service_id': SERVICE_ONE_ID, 'service_name': '1', 'chargeable_billable_sms': 250122, 'emails_sent': 13000, + 'free_sms_limit': 250000, 'letter_cost': 30.50, 'sms_billable_units': 122, 'sms_cost': 1.93, + 'sms_remainder': None}, + {'service_id': SERVICE_TWO_ID, 'service_name': '5', 'chargeable_billable_sms': 0, 'emails_sent': 20000, + 'free_sms_limit': 250000, 'letter_cost': 0, 'sms_billable_units': 2500, 'sms_cost': 0.0, + 'sms_remainder': None} + ]} ) client_request.login(active_user_with_permissions) page = client_request.get('.organisation_dashboard', org_id=ORGANISATION_ID) + mock.assert_called_once_with(ORGANISATION_ID, 2019) services = page.select('.browse-list-item') assert len(services) == 2 @@ -416,7 +418,14 @@ def test_organisation_services_shows_live_services_only( assert normalize_spaces(services[0].text) == '1' assert normalize_spaces(services[1].text) == '5' assert services[0].find('a')['href'] == url_for('main.usage', service_id=SERVICE_ONE_ID) + usage_rows = page.find_all("div", class_="column-one-third") + assert normalize_spaces(usage_rows[0].text) == "13,000 emails sent" + assert normalize_spaces(usage_rows[1].text) == "£1.93 spent on text messages" + assert normalize_spaces(usage_rows[2].text) == "£30.50 spent on letters" assert services[1].find('a')['href'] == url_for('main.usage', service_id=SERVICE_TWO_ID) + assert normalize_spaces(usage_rows[3].text) == "20,000 emails sent" + assert normalize_spaces(usage_rows[4].text) == "£0.00 spent on text messages" + assert normalize_spaces(usage_rows[5].text) == "£0.00 spent on letters" def test_organisation_trial_mode_services_shows_all_non_live_services( diff --git a/tests/app/test_navigation.py b/tests/app/test_navigation.py index 6a544db6c..1a5582bcd 100644 --- a/tests/app/test_navigation.py +++ b/tests/app/test_navigation.py @@ -143,12 +143,15 @@ def test_a_page_should_nave_selected_header_navigation_item( def test_a_page_should_nave_selected_org_navigation_item( client_request, mock_get_organisation, - mock_get_organisation_services, mock_get_users_for_organisation, mock_get_invited_users_for_organisation, endpoint, selected_nav_item, + mocker ): + mocker.patch( + 'app.organisations_client.get_services_and_usage', return_value=[] + ) page = client_request.get(endpoint, org_id=ORGANISATION_ID) selected_nav_items = page.select('.navigation a.selected') assert len(selected_nav_items) == 1