mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-21 16:01:15 -05:00
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:
@@ -7,16 +7,17 @@ from app.billing.billing_schemas import (
|
||||
)
|
||||
from app.dao.annual_billing_dao import (
|
||||
dao_create_or_update_annual_billing_for_year,
|
||||
dao_get_all_free_sms_fragment_limit,
|
||||
dao_get_free_sms_fragment_limit_for_year,
|
||||
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.fact_billing_dao import (
|
||||
fetch_billing_totals_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
|
||||
|
||||
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)
|
||||
|
||||
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,
|
||||
# we return the oldest entry.
|
||||
# If it is the current or future years, we create an entry in the db table using the newest record,
|
||||
# and return that number. If all fails, we return InvalidRequest.
|
||||
sms_list = dao_get_all_free_sms_fragment_limit(service_id)
|
||||
service = Service.query.get(service_id)
|
||||
# An entry does not exist in annual_billing table for that service and year.
|
||||
# Set the annual billing to the default free allowance based on the organisation type of the service.
|
||||
|
||||
if not sms_list:
|
||||
raise InvalidRequest('no free-sms-fragment-limit entry for service {} in DB'.format(service_id), 404)
|
||||
else:
|
||||
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)
|
||||
annual_billing = set_default_free_allowance_for_service(
|
||||
service=service,
|
||||
year_start=int(financial_year_start) if financial_year_start else None
|
||||
)
|
||||
|
||||
return jsonify(annual_billing.serialize_free_sms_items()), 200
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ def set_default_free_allowance_for_service(service, year_start=None):
|
||||
f"{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,
|
||||
free_allowance,
|
||||
year_start
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
def test_get_free_sms_fragment_limit_current_year_creates_new_row(client, sample_service):
|
||||
|
||||
current_year = get_current_financial_year_start_year()
|
||||
create_annual_billing(sample_service.id, 9999, current_year - 1)
|
||||
|
||||
@freeze_time('2021-04-02 13:00')
|
||||
def test_get_free_sms_fragment_limit(
|
||||
client, sample_service
|
||||
):
|
||||
create_annual_billing(service_id=sample_service.id, free_sms_fragment_limit=11000, financial_year_start=2021)
|
||||
response_get = client.get(
|
||||
'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
||||
|
||||
json_resp = json.loads(response_get.get_data(as_text=True))
|
||||
assert response_get.status_code == 200
|
||||
assert json_resp['financial_year_start'] == get_current_financial_year_start_year()
|
||||
assert json_resp['free_sms_fragment_limit'] == 9999
|
||||
assert json_resp['financial_year_start'] == 2021
|
||||
assert json_resp['free_sms_fragment_limit'] == 11000
|
||||
|
||||
|
||||
def test_get_free_sms_fragment_limit_past_year_not_exist(client, sample_service):
|
||||
current_year = get_current_financial_year_start_year()
|
||||
create_annual_billing(sample_service.id, 9999, current_year - 1)
|
||||
create_annual_billing(sample_service.id, 10000, 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),
|
||||
@freeze_time('2021-04-02 13:00')
|
||||
def test_get_free_sms_fragment_limit_current_year_creates_new_row_if_annual_billing_is_missing(
|
||||
client, sample_service
|
||||
):
|
||||
response_get = client.get(
|
||||
'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
|
||||
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 - 1
|
||||
assert json_resp['free_sms_fragment_limit'] == 9999
|
||||
|
||||
|
||||
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
|
||||
json_resp = json.loads(response_get.get_data(as_text=True))
|
||||
assert response_get.status_code == 200
|
||||
assert json_resp['financial_year_start'] == 2021
|
||||
assert json_resp['free_sms_fragment_limit'] == 10000 # based on other organisation type
|
||||
|
||||
|
||||
def test_update_free_sms_fragment_limit_data(client, sample_service):
|
||||
|
||||
Reference in New Issue
Block a user