Simplify the get_free_sms_fragment limit for the case when the row is

missing, by setting the free allowance to the default.
This commit is contained in:
Rebecca Law
2021-04-19 13:29:04 +01:00
parent bcd1939179
commit ae57521b39
3 changed files with 28 additions and 64 deletions

View File

@@ -7,16 +7,17 @@ from app.billing.billing_schemas import (
) )
from app.dao.annual_billing_dao import ( from app.dao.annual_billing_dao import (
dao_create_or_update_annual_billing_for_year, dao_create_or_update_annual_billing_for_year,
dao_get_all_free_sms_fragment_limit,
dao_get_free_sms_fragment_limit_for_year, dao_get_free_sms_fragment_limit_for_year,
dao_update_annual_billing_for_future_years, dao_update_annual_billing_for_future_years,
set_default_free_allowance_for_service,
) )
from app.dao.date_util import get_current_financial_year_start_year from app.dao.date_util import get_current_financial_year_start_year
from app.dao.fact_billing_dao import ( from app.dao.fact_billing_dao import (
fetch_billing_totals_for_year, fetch_billing_totals_for_year,
fetch_monthly_billing_for_year, fetch_monthly_billing_for_year,
) )
from app.errors import InvalidRequest, register_errors from app.errors import register_errors
from app.models import Service
from app.schema_validation import validate from app.schema_validation import validate
billing_blueprint = Blueprint( billing_blueprint = Blueprint(
@@ -62,27 +63,14 @@ def get_free_sms_fragment_limit(service_id):
annual_billing = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start) annual_billing = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start)
if annual_billing is None: if annual_billing is None:
# An entry does not exist in annual_billing table for that service and year. If it is a past year, service = Service.query.get(service_id)
# we return the oldest entry. # An entry does not exist in annual_billing table for that service and year.
# If it is the current or future years, we create an entry in the db table using the newest record, # Set the annual billing to the default free allowance based on the organisation type of the service.
# and return that number. If all fails, we return InvalidRequest.
sms_list = dao_get_all_free_sms_fragment_limit(service_id)
if not sms_list: annual_billing = set_default_free_allowance_for_service(
raise InvalidRequest('no free-sms-fragment-limit entry for service {} in DB'.format(service_id), 404) service=service,
else: year_start=int(financial_year_start) if financial_year_start else None
if financial_year_start is None: )
financial_year_start = get_current_financial_year_start_year()
if int(financial_year_start) < get_current_financial_year_start_year():
# return the earliest historical entry
annual_billing = sms_list[0] # The oldest entry
else:
annual_billing = sms_list[-1] # The newest entry
annual_billing = dao_create_or_update_annual_billing_for_year(service_id,
annual_billing.free_sms_fragment_limit,
financial_year_start)
return jsonify(annual_billing.serialize_free_sms_items()), 200 return jsonify(annual_billing.serialize_free_sms_items()), 200

View File

@@ -102,7 +102,7 @@ def set_default_free_allowance_for_service(service, year_start=None):
f"{default_free_sms_fragment_limits['other'][year_start]}") f"{default_free_sms_fragment_limits['other'][year_start]}")
free_allowance = default_free_sms_fragment_limits['other'][year_start] free_allowance = default_free_sms_fragment_limits['other'][year_start]
dao_create_or_update_annual_billing_for_year( return dao_create_or_update_annual_billing_for_year(
service.id, service.id,
free_allowance, free_allowance,
year_start year_start

View File

@@ -96,57 +96,33 @@ def test_create_free_sms_fragment_limit_updates_existing_year(admin_request, sam
assert annual_billing.free_sms_fragment_limit == 2 assert annual_billing.free_sms_fragment_limit == 2
def test_get_free_sms_fragment_limit_current_year_creates_new_row(client, sample_service): @freeze_time('2021-04-02 13:00')
def test_get_free_sms_fragment_limit(
current_year = get_current_financial_year_start_year() client, sample_service
create_annual_billing(sample_service.id, 9999, current_year - 1) ):
create_annual_billing(service_id=sample_service.id, free_sms_fragment_limit=11000, financial_year_start=2021)
response_get = client.get( response_get = client.get(
'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), 'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), create_authorization_header()]) headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response_get.get_data(as_text=True)) json_resp = json.loads(response_get.get_data(as_text=True))
assert response_get.status_code == 200 assert response_get.status_code == 200
assert json_resp['financial_year_start'] == get_current_financial_year_start_year() assert json_resp['financial_year_start'] == 2021
assert json_resp['free_sms_fragment_limit'] == 9999 assert json_resp['free_sms_fragment_limit'] == 11000
def test_get_free_sms_fragment_limit_past_year_not_exist(client, sample_service): @freeze_time('2021-04-02 13:00')
current_year = get_current_financial_year_start_year() def test_get_free_sms_fragment_limit_current_year_creates_new_row_if_annual_billing_is_missing(
create_annual_billing(sample_service.id, 9999, current_year - 1) client, sample_service
create_annual_billing(sample_service.id, 10000, current_year + 1) ):
response_get = client.get(
annual_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year - 2) 'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
assert annual_billing is None
res_get = client.get(
'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'
.format(sample_service.id, current_year - 2),
headers=[('Content-Type', 'application/json'), create_authorization_header()]) headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(res_get.get_data(as_text=True))
assert res_get.status_code == 200 json_resp = json.loads(response_get.get_data(as_text=True))
assert json_resp['financial_year_start'] == current_year - 1 assert response_get.status_code == 200
assert json_resp['free_sms_fragment_limit'] == 9999 assert json_resp['financial_year_start'] == 2021
assert json_resp['free_sms_fragment_limit'] == 10000 # based on other organisation type
def test_get_free_sms_fragment_limit_future_year_not_exist(client, sample_service):
current_year = get_current_financial_year_start_year()
create_annual_billing(sample_service.id, free_sms_fragment_limit=9999, financial_year_start=current_year - 1)
create_annual_billing(sample_service.id, free_sms_fragment_limit=10000, financial_year_start=current_year + 1)
annual_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year + 2)
assert annual_billing is None
res_get = client.get(
'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'
.format(sample_service.id, current_year + 2),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(res_get.get_data(as_text=True))
assert res_get.status_code == 200
assert json_resp['financial_year_start'] == current_year + 2
assert json_resp['free_sms_fragment_limit'] == 10000
def test_update_free_sms_fragment_limit_data(client, sample_service): def test_update_free_sms_fragment_limit_data(client, sample_service):