diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 7ffbda915..5689d4aee 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -577,7 +577,6 @@ def fetch_email_usage_for_organisation(organisation_id, start_date, end_date): ).join( FactBilling, FactBilling.service_id == Service.id, ).filter( - FactBilling.service_id == Service.id, FactBilling.bst_date >= start_date, FactBilling.bst_date <= end_date, FactBilling.notification_type == EMAIL_TYPE, @@ -588,7 +587,6 @@ def fetch_email_usage_for_organisation(organisation_id, start_date, end_date): ).order_by( Service.name ) - return query.all() @@ -671,7 +669,6 @@ def fetch_usage_year_for_organisation(organisation_id, year): 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) email_usages = fetch_email_usage_for_organisation(organisation_id, year_start_date, year_end_date) - for usage in sms_usages: service_with_usage[str(usage.service_id)] = { 'service_id': usage.service_id, @@ -679,13 +676,13 @@ def fetch_usage_year_for_organisation(organisation_id, year): '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, + 'chargeable_billable_sms': float(usage.chargeable_billable_sms), + 'sms_cost': float(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 + service_with_usage[str(letter_usage.service_id)]['letter_cost'] = float(letter_usage.letter_cost) for email_usage in email_usages: service_with_usage[str(email_usage.service_id)]['emails_sent'] = email_usage.emails_sent diff --git a/app/organisation/rest.py b/app/organisation/rest.py index bf7aaebe5..0cdf51b3c 100644 --- a/app/organisation/rest.py +++ b/app/organisation/rest.py @@ -1,4 +1,3 @@ -from datetime import datetime from flask import abort, Blueprint, jsonify, request, current_app from sqlalchemy.exc import IntegrityError @@ -130,9 +129,14 @@ def get_organisation_services(organisation_id): @organisation_blueprint.route('//services-with-usage', methods=['GET']) def get_organisation_services_usage(organisation_id): - services = fetch_usage_year_for_organisation(organisation_id, datetime.utcnow().year) - - return jsonify(services=services) + try: + year = int(request.args.get('year')) + except ValueError: + 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()) + return jsonify(services=sorted_services) @organisation_blueprint.route('//users/', methods=['POST']) diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index 2bcb5053f..c01409656 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -649,8 +649,8 @@ def test_fetch_usage_year_for_organisation(notify_db_session): assert first_row['free_sms_limit'] == 10 assert first_row['sms_remainder'] == 10 assert first_row['chargeable_billable_sms'] == 0 - assert first_row['sms_cost'] == Decimal('0.0') - assert first_row['letter_cost'] == Decimal('3.40') + assert first_row['sms_cost'] == 0.0 + assert first_row['letter_cost'] == 3.4 assert first_row['emails_sent'] == 0 second_row = results[str(service_with_emails_for_org.id)] diff --git a/tests/app/organisation/test_rest.py b/tests/app/organisation/test_rest.py index ce22c056b..6a332ba0a 100644 --- a/tests/app/organisation/test_rest.py +++ b/tests/app/organisation/test_rest.py @@ -3,6 +3,7 @@ from datetime import datetime import uuid import pytest +from freezegun import freeze_time from app.models import Organisation from app.dao.organisation_dao import dao_add_service_to_organisation, dao_add_user_to_organisation @@ -743,6 +744,7 @@ def test_is_organisation_name_unique_returns_400_when_name_does_not_exist(admin_ assert response["message"][1]["name"] == ["Can't be empty"] +@freeze_time('2020-02-24 13:30') def test_get_organisation_services_usage(admin_request, notify_db_session): org = create_organisation(name='Organisation without live services') service = create_service() @@ -751,6 +753,20 @@ def test_get_organisation_services_usage(admin_request, notify_db_session): 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 + organisation_id=org.id, + **{"year": 2019} ) assert len(response) == 1 + assert len(response['services']) == 1 + print(response) + assert response['services'][0]['service_id'] == str(service.id) + + +def test_get_organisation_services_usage_returns_400_if_year_is_invalid(admin_request): + response = admin_request.get( + 'organisation.get_organisation_services_usage', + organisation_id=uuid.uuid4(), + **{"year": 'year'}, + _expected_status=400 + ) + assert response['message'] == 'No valid year provided'