diff --git a/app/billing/rest.py b/app/billing/rest.py index 614842d3f..1bdeaf6a9 100644 --- a/app/billing/rest.py +++ b/app/billing/rest.py @@ -135,14 +135,16 @@ def create_or_update_free_sms_fragment_limit(service_id): form = validate(req_args, create_or_update_free_sms_fragment_limit_schema) - financial_year_start = form.get('financial_year_start') - free_sms_fragment_limit = form.get('free_sms_fragment_limit') + update_free_sms_fragment_limit_data(service_id, + free_sms_fragment_limit=form.get('free_sms_fragment_limit'), + financial_year_start=form.get('financial_year_start')) + return jsonify(form), 201 + +def update_free_sms_fragment_limit_data(service_id, free_sms_fragment_limit, financial_year_start=None): current_year = get_current_financial_year_start_year() if financial_year_start is None or financial_year_start >= current_year: dao_update_annual_billing_for_current_and_future_years(service_id, free_sms_fragment_limit) else: dao_create_or_update_annual_billing_for_year(service_id, free_sms_fragment_limit, financial_year_start) - - return jsonify(form), 201 diff --git a/app/service/rest.py b/app/service/rest.py index c6e8c4201..a17920d36 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -98,6 +98,7 @@ from app.schemas import ( detailed_service_schema ) from app.utils import pagination_links +from app.billing.rest import update_free_sms_fragment_limit_data service_blueprint = Blueprint('service', __name__) @@ -202,6 +203,10 @@ def update_service(service_id): if 'letter_contact_block' in req_json: create_or_update_letter_contact(fetched_service.id, req_json['letter_contact_block']) + # bridging code between frontend is deployed and data has not been migrated yet. Can only update current year + if 'free_sms_fragment_limit' in req_json: + update_free_sms_fragment_limit_data(fetched_service.id, req_json['free_sms_fragment_limit']) + if service_going_live: send_notification_to_service_users( service_id=service_id, diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index 7895bd8a9..6a440aaa7 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -18,6 +18,7 @@ from tests import create_authorization_header from app.dao.date_util import get_current_financial_year_start_year from app.dao.annual_billing_dao import dao_get_free_sms_fragment_limit_for_year from tests.app.db import create_annual_billing +from app.billing.rest import update_free_sms_fragment_limit_data APR_2016_MONTH_START = datetime(2016, 3, 31, 23, 00, 00) @@ -380,3 +381,14 @@ def test_get_free_sms_fragment_limit_future_year_not_exist(client, sample_servic 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): + current_year = get_current_financial_year_start_year() + annual_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year) + assert annual_billing.free_sms_fragment_limit == 250000 + + update_free_sms_fragment_limit_data(sample_service.id, 9999) + + annual_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year) + assert annual_billing.free_sms_fragment_limit == 9999