- 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:
venusbb
2017-11-02 12:19:17 +00:00
parent cc3d5ba8d1
commit 6f7793d761
10 changed files with 186 additions and 230 deletions

View File

@@ -10,5 +10,5 @@ create_or_update_free_sms_fragment_limit_schema = {
"free_sms_fragment_limit": {"type": "integer", "minimum": 1},
"financial_year_start": {"type": "integer", "minimum": 2016}
},
"required": ["free_sms_fragment_limit", "financial_year_start"]
"required": ["free_sms_fragment_limit"]
}

View File

@@ -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

View File

@@ -4,8 +4,24 @@ from app.dao.dao_utils import (
version_class
)
from app.models import AnnualBilling
from datetime import datetime
from app.service.utils import get_current_financial_year_start_year
from app.dao.date_util import get_current_financial_year_start_year
@transactional
def dao_create_or_update_annual_billing_for_year(service_id, free_sms_fragment_limit, financial_year_start=None):
if not financial_year_start:
financial_year_start = get_current_financial_year_start_year()
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
else:
result = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start,
free_sms_fragment_limit=free_sms_fragment_limit)
db.session.add(result)
return result
def dao_get_annual_billing(service_id):
@@ -14,16 +30,28 @@ def dao_get_annual_billing(service_id):
).order_by(AnnualBilling.financial_year_start).all()
def dao_create_or_update_annual_billing_for_year(annual_billing):
db.session.add(annual_billing)
db.session.commit()
@transactional
def dao_update_annual_billing_for_current_and_future_years(service_id, free_sms_fragment_limit,
financial_year_start=None):
if not financial_year_start:
financial_year_start = get_current_financial_year_start_year()
updated = AnnualBilling.query.filter(
AnnualBilling.service_id == service_id,
AnnualBilling.financial_year_start >= financial_year_start
).update(
{'free_sms_fragment_limit': free_sms_fragment_limit}
)
def dao_get_free_sms_fragment_limit_for_year(service_id, year):
def dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start=None):
if not financial_year_start:
financial_year_start = get_current_financial_year_start_year()
return AnnualBilling.query.filter_by(
service_id=service_id,
financial_year_start=year
financial_year_start=financial_year_start
).first()

View File

@@ -45,3 +45,12 @@ def get_month_start_and_end_date_in_utc(month_year):
first_day = datetime(month_year.year, month_year.month, 1, 0, 0, 0)
last_day = datetime(month_year.year, month_year.month, num_days, 23, 59, 59, 99999)
return convert_bst_to_utc(first_day), convert_bst_to_utc(last_day)
def get_current_financial_year_start_year():
now = datetime.now()
financial_year_start = now.year
start_date, end_date = get_financial_year(now.year)
if now < start_date:
financial_year_start = financial_year_start - 1
return financial_year_start

View File

@@ -53,12 +53,3 @@ def service_allowed_to_send_to(recipient, service, key_type):
whitelist_members
)
)
def get_current_financial_year_start_year():
now = datetime.now()
financial_year_start = now.year
start_date, end_date = get_financial_year(now.year)
if now < start_date:
financial_year_start = financial_year_start - 1
return financial_year_start