def get_free_sms_fragment_limit(service_id): financial_year_start = request.args.get('financial_year_start') 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) 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) return jsonify(annual_billing.serialize_free_sms_items()), 200
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) 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
def test_dao_update_free_sms_fragment_limit(notify_db_session, sample_service): new_limit = 9999 year = get_current_financial_year_start_year() dao_create_or_update_annual_billing_for_year(sample_service.id, new_limit, year) new_free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, year) assert new_free_limit.free_sms_fragment_limit == new_limit
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 upgrade(): current_year = get_current_financial_year_start_year() default_limit = 250000 # Step 1: update the column free_sms_fragment_limit in service table if it is empty update_service_table = """ UPDATE services SET free_sms_fragment_limit = {} where free_sms_fragment_limit is null """.format(default_limit) op.execute(update_service_table) # Step 2: insert at least one row for every service in current year if none exist for that service insert_row_if_not_exist = """ INSERT INTO annual_billing (id, service_id, financial_year_start, free_sms_fragment_limit, created_at, updated_at) SELECT uuid_in(md5(random()::text)::cstring), id, {}, {}, '{}', '{}' FROM services WHERE id NOT IN (select service_id from annual_billing) """.format(current_year, default_limit, datetime.utcnow(), datetime.utcnow()) op.execute(insert_row_if_not_exist) # Step 3: copy the free_sms_fragment_limit data from the services table across to annual_billing table. update_sms_allowance = """ UPDATE annual_billing SET free_sms_fragment_limit = services.free_sms_fragment_limit FROM services WHERE annual_billing.service_id = services.id """ op.execute(update_sms_allowance)
def test_update_free_sms_fragment_limit_data(client, sample_service): current_year = get_current_financial_year_start_year() create_annual_billing(sample_service.id, free_sms_fragment_limit=250000, financial_year_start=current_year - 1) update_free_sms_fragment_limit_data(sample_service.id, 9999, current_year) annual_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year) assert annual_billing.free_sms_fragment_limit == 9999
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=financial_year_start).first()
def update_free_sms_fragment_limit_data(service_id, free_sms_fragment_limit, financial_year_start): current_year = get_current_financial_year_start_year() if not financial_year_start: financial_year_start = current_year dao_create_or_update_annual_billing_for_year(service_id, free_sms_fragment_limit, financial_year_start) # if we're trying to update historical data, don't touch other rows. # Otherwise, make sure that future years will get the new updated value. if financial_year_start >= current_year: dao_update_annual_billing_for_future_years(service_id, free_sms_fragment_limit, financial_year_start)
def dao_insert_annual_billing_for_this_year(service, free_sms_fragment_limit): """ This method is called from create_service which is wrapped in a transaction. """ annual_billing = AnnualBilling( free_sms_fragment_limit=free_sms_fragment_limit, financial_year_start=get_current_financial_year_start_year(), service=service, ) db.session.add(annual_billing)
def test_create_free_sms_fragment_limit_updates_existing_year(admin_request, sample_service): current_year = get_current_financial_year_start_year() annual_billing = create_annual_billing(sample_service.id, 1, current_year) admin_request.post( 'billing.create_or_update_free_sms_fragment_limit', service_id=sample_service.id, _data={'financial_year_start': current_year, 'free_sms_fragment_limit': 2}, _expected_status=201) assert annual_billing.free_sms_fragment_limit == 2
def set_default_free_allowance_for_service(service, year_start=None): default_free_sms_fragment_limits = { 'central': { 2020: 250_000, 2021: 150_000, }, 'local': { 2020: 25_000, 2021: 25_000, }, 'nhs_central': { 2020: 250_000, 2021: 150_000, }, 'nhs_local': { 2020: 25_000, 2021: 25_000, }, 'nhs_gp': { 2020: 25_000, 2021: 10_000, }, 'emergency_service': { 2020: 25_000, 2021: 25_000, }, 'school_or_college': { 2020: 25_000, 2021: 10_000, }, 'other': { 2020: 25_000, 2021: 10_000, }, } if not year_start: year_start = get_current_financial_year_start_year() # handle cases where the year is less than 2020 or greater than 2021 if year_start < 2020: year_start = 2020 if year_start > 2021: year_start = 2021 if service.organisation_type: free_allowance = default_free_sms_fragment_limits[ service.organisation_type][year_start] else: current_app.logger.info( f"no organisation type for service {service.id}. Using other default of " f"{default_free_sms_fragment_limits['other'][year_start]}") free_allowance = default_free_sms_fragment_limits['other'][year_start] return dao_create_or_update_annual_billing_for_year( service.id, free_allowance, year_start)
def get_free_sms_fragment_limit(service_id): financial_year_start = request.args.get('financial_year_start') annual_billing = dao_get_free_sms_fragment_limit( service_id, int(financial_year_start) if financial_year_start else None, get_current_financial_year_start_year() ) if not annual_billing: raise InvalidRequest('no free-sms-fragment-limit entry for service {} in DB'.format(service_id), 404) return jsonify(annual_billing.serialize_free_sms_items()), 200
def test_dao_update_annual_billing_for_future_years(notify_db_session, sample_service): current_year = get_current_financial_year_start_year() limits = [1, 2, 3, 4] create_annual_billing(sample_service.id, limits[0], current_year - 1) create_annual_billing(sample_service.id, limits[2], current_year + 1) create_annual_billing(sample_service.id, limits[3], current_year + 2) dao_update_annual_billing_for_future_years(sample_service.id, 9999, current_year) assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year - 1).free_sms_fragment_limit == 1 # current year is not created assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year) is None assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year + 1).free_sms_fragment_limit == 9999 assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year + 2).free_sms_fragment_limit == 9999
def test_create_free_sms_fragment_limit_current_year_updates_future_years(admin_request, sample_service): current_year = get_current_financial_year_start_year() future_billing = create_annual_billing(sample_service.id, 1, current_year + 1) admin_request.post( 'billing.create_or_update_free_sms_fragment_limit', service_id=sample_service.id, _data={'free_sms_fragment_limit': 9999}, _expected_status=201 ) current_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year) assert future_billing.free_sms_fragment_limit == 9999 assert current_billing.financial_year_start == current_year assert current_billing.free_sms_fragment_limit == 9999
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), 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_create_or_update_free_sms_fragment_limit_past_year_doenst_update_other_years( admin_request, sample_service, update_existing ): current_year = get_current_financial_year_start_year() create_annual_billing(sample_service.id, 1, current_year) if update_existing: create_annual_billing(sample_service.id, 1, current_year - 1) data = {"financial_year_start": current_year - 1, "free_sms_fragment_limit": 9999} admin_request.post( "billing.create_or_update_free_sms_fragment_limit", service_id=sample_service.id, _data=data, _expected_status=201, ) assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year - 1).free_sms_fragment_limit == 9999 assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year).free_sms_fragment_limit == 1
def test_get_current_financial_year_start_year_before_march(): current_fy = get_current_financial_year_start_year() assert current_fy == 2016
def test_get_current_financial_year_start_year_after_april(): current_fy = get_current_financial_year_start_year() assert current_fy == 2017
def fetch_sms_free_allowance_for_financial_year(service_id, year): # this might come back as none but it's not recoverable. fail here or somewhere else results = dao_get_free_sms_fragment_limit( service_id, year, get_current_financial_year_start_year()) return results.free_sms_fragment_limit