diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 594ad004e..eced55218 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -244,11 +244,9 @@ def fetch_monthly_billing_for_year(service_id, year): today = convert_utc_to_bst(datetime.utcnow()).date() # if year end date is less than today, we are calculating for data in the past and have no need for deltas. if year_end_date >= today: - yesterday = today - timedelta(days=1) - for day in [yesterday, today]: - data = fetch_billing_data_for_day(process_day=day, service_id=service_id, check_permissions=True) - for d in data: - update_fact_billing(data=d, process_day=day) + data = fetch_billing_data_for_day(process_day=today, service_id=service_id, check_permissions=True) + for d in data: + update_fact_billing(data=d, process_day=today) email_and_letters = db.session.query( func.date_trunc('month', FactBilling.bst_date).cast(Date).label("month"), @@ -648,12 +646,10 @@ def fetch_usage_year_for_organisation(organisation_id, year): services = dao_get_organisation_live_services(organisation_id) # if year end date is less than today, we are calculating for data in the past and have no need for deltas. if year_end_date >= today: - yesterday = today - timedelta(days=1) for service in services: - for day in [yesterday, today]: - data = fetch_billing_data_for_day(process_day=day, service_id=service.id) - for d in data: - update_fact_billing(data=d, process_day=day) + data = fetch_billing_data_for_day(process_day=today, service_id=service.id) + for d in data: + update_fact_billing(data=d, process_day=today) service_with_usage = {} # initialise results for service in services: diff --git a/app/organisation/rest.py b/app/organisation/rest.py index 0cdf51b3c..b1eb613df 100644 --- a/app/organisation/rest.py +++ b/app/organisation/rest.py @@ -130,7 +130,7 @@ def get_organisation_services(organisation_id): @organisation_blueprint.route('//services-with-usage', methods=['GET']) def get_organisation_services_usage(organisation_id): try: - year = int(request.args.get('year')) + year = int(request.args.get('year', 'none')) except ValueError: return jsonify(result='error', message='No valid year provided'), 400 services = fetch_usage_year_for_organisation(organisation_id, year) diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index 1a173d428..b12c68fad 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -631,6 +631,7 @@ def test_fetch_letter_line_items_for_all_service(notify_db_session): assert results[4] == (None, None, service_3.name, service_3.id, Decimal("0.55"), 'second', 15) +@freeze_time('2019-06-01 13:30') def test_fetch_usage_year_for_organisation(notify_db_session): 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)) diff --git a/tests/app/db.py b/tests/app/db.py index 29e36c04b..0c847b920 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -908,7 +908,7 @@ def set_up_usage_data(start_date): service_2 = create_service(service_name='b - emails') email_template = create_template(service=service_2, template_type='email') - org_2 = create_organisation(name='Org for {}'.format(service_2)) + org_2 = create_organisation(name='Org for {}'.format(service_2.name)) dao_add_service_to_organisation(service=service_2, organisation_id=org_2.id) service_3 = create_service(service_name='c - letters only') diff --git a/tests/app/organisation/test_rest.py b/tests/app/organisation/test_rest.py index fc741c89d..2f697871b 100644 --- a/tests/app/organisation/test_rest.py +++ b/tests/app/organisation/test_rest.py @@ -777,7 +777,16 @@ def test_get_organisation_services_usage_returns_400_if_year_is_invalid(admin_re response = admin_request.get( 'organisation.get_organisation_services_usage', organisation_id=uuid.uuid4(), - **{"year": 'year'}, + **{"year": 'not-a-valid-year'}, + _expected_status=400 + ) + assert response['message'] == 'No valid year provided' + + +def test_get_organisation_services_usage_returns_400_if_year_is_empty(admin_request): + response = admin_request.get( + 'organisation.get_organisation_services_usage', + organisation_id=uuid.uuid4(), _expected_status=400 ) assert response['message'] == 'No valid year provided'