mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
- Add update dao_update_annual_billing_for_current_and_future_years
- moved get_current_financial_year_start_year from service.utils to dao.date_utils - Moved logic for data persistence from rest to dao when updating records in db
This commit is contained in:
@@ -13,12 +13,12 @@ from app.models import SMS_TYPE, EMAIL_TYPE
|
||||
from app.utils import convert_utc_to_bst
|
||||
from app.dao.annual_billing_dao import (dao_get_free_sms_fragment_limit_for_year,
|
||||
dao_get_all_free_sms_fragment_limit,
|
||||
dao_create_or_update_annual_billing_for_year)
|
||||
dao_create_or_update_annual_billing_for_year,
|
||||
dao_update_annual_billing_for_current_and_future_years)
|
||||
from app.billing.billing_schemas import create_or_update_free_sms_fragment_limit_schema
|
||||
from app.errors import InvalidRequest
|
||||
from app.schema_validation import validate
|
||||
from app.models import AnnualBilling
|
||||
from app.service.utils import get_current_financial_year_start_year
|
||||
from app.dao.date_util import get_current_financial_year_start_year
|
||||
|
||||
billing_blueprint = Blueprint(
|
||||
'billing',
|
||||
@@ -97,66 +97,51 @@ def _transform_billing_for_month(billing_for_month):
|
||||
|
||||
|
||||
@billing_blueprint.route('/free-sms-fragment-limit', methods=["GET"])
|
||||
@billing_blueprint.route('/free-sms-fragment-limit/current-year', methods=["GET"])
|
||||
def get_free_sms_fragment_limit(service_id):
|
||||
|
||||
if request.path.split('/')[-1] == 'current-year':
|
||||
financial_year_start = get_current_financial_year_start_year()
|
||||
else:
|
||||
financial_year_start = request.args.get('financial_year_start')
|
||||
financial_year_start = request.args.get('financial_year_start')
|
||||
|
||||
if financial_year_start is None:
|
||||
# return a list of all entries related to the service
|
||||
result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start)
|
||||
|
||||
if result 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)
|
||||
if len(sms_list) == 0:
|
||||
raise InvalidRequest('no free-sms-fragment-limit list for this service in DB', status_code=404)
|
||||
return jsonify([row.serialize_free_sms_items() for row in sms_list]), 200
|
||||
else:
|
||||
result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start)
|
||||
|
||||
if result 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)
|
||||
if len(sms_list) == 0:
|
||||
raise InvalidRequest('no free-sms-fragment-limit entry for this service in DB', status_code=404)
|
||||
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():
|
||||
result = sms_list[0]
|
||||
result = sms_list[0] # The oldest entry
|
||||
else:
|
||||
result = sms_list[-1] # The newest entry
|
||||
result = sms_list[-1] # The newest entry
|
||||
|
||||
new_sms = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start,
|
||||
free_sms_fragment_limit=result.free_sms_fragment_limit)
|
||||
result = dao_create_or_update_annual_billing_for_year(service_id, result.free_sms_fragment_limit,
|
||||
financial_year_start)
|
||||
|
||||
dao_create_or_update_annual_billing_for_year(new_sms)
|
||||
|
||||
return jsonify(result.serialize_free_sms_items()), 200
|
||||
return jsonify(result.serialize_free_sms_items()), 200
|
||||
|
||||
|
||||
@billing_blueprint.route('/free-sms-fragment-limit', methods=["POST"])
|
||||
def create_or_update_free_sms_fragment_limit(service_id):
|
||||
|
||||
dict_arg = request.get_json()
|
||||
req_args = request.get_json()
|
||||
|
||||
if 'financial_year_start' not in dict_arg:
|
||||
dict_arg['financial_year_start'] = get_current_financial_year_start_year()
|
||||
|
||||
form = validate(dict_arg, create_or_update_free_sms_fragment_limit_schema)
|
||||
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')
|
||||
|
||||
result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start)
|
||||
|
||||
if result:
|
||||
result.free_sms_fragment_limit = free_sms_fragment_limit
|
||||
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:
|
||||
result = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start,
|
||||
free_sms_fragment_limit=free_sms_fragment_limit)
|
||||
dao_create_or_update_annual_billing_for_year(service_id,
|
||||
free_sms_fragment_limit, financial_year_start)
|
||||
|
||||
dao_create_or_update_annual_billing_for_year(result)
|
||||
|
||||
return jsonify(data=form), 201
|
||||
return jsonify(form), 201
|
||||
|
||||
Reference in New Issue
Block a user