Files
notifications-api/app/billing/rest.py

106 lines
3.7 KiB
Python
Raw Normal View History

from flask import Blueprint, jsonify, request
2024-11-18 09:26:04 -08:00
from app import db
from app.billing.billing_schemas import (
create_or_update_free_sms_fragment_limit_schema,
serialize_ft_billing_remove_emails,
serialize_ft_billing_yearly_totals,
)
from app.dao.annual_billing_dao import (
dao_create_or_update_annual_billing_for_year,
2021-03-10 13:55:06 +00:00
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_calendar_year_start_year
from app.dao.fact_billing_dao import (
2021-03-10 13:55:06 +00:00
fetch_billing_totals_for_year,
fetch_monthly_billing_for_year,
)
from app.errors import register_errors
from app.models import Service
from app.schema_validation import validate
billing_blueprint = Blueprint(
2023-08-29 14:54:30 -07:00
"billing", __name__, url_prefix="/service/<uuid:service_id>/billing"
)
register_errors(billing_blueprint)
2023-08-29 14:54:30 -07:00
@billing_blueprint.route("/monthly-usage")
def get_yearly_usage_by_monthly_from_ft_billing(service_id):
2018-04-27 15:15:55 +01:00
try:
2023-08-29 14:54:30 -07:00
year = int(request.args.get("year"))
2018-04-27 15:15:55 +01:00
except TypeError:
2023-08-29 14:54:30 -07:00
return jsonify(result="error", message="No valid year provided"), 400
results = fetch_monthly_billing_for_year(service_id=service_id, year=year)
data = serialize_ft_billing_remove_emails(results)
return jsonify(data)
2018-04-27 15:15:55 +01:00
2023-08-29 14:54:30 -07:00
@billing_blueprint.route("/yearly-usage-summary")
def get_yearly_billing_usage_summary_from_ft_billing(service_id):
try:
2023-08-29 14:54:30 -07:00
year = int(request.args.get("year"))
except TypeError:
2023-08-29 14:54:30 -07:00
return jsonify(result="error", message="No valid year provided"), 400
billing_data = fetch_billing_totals_for_year(service_id, year)
data = serialize_ft_billing_yearly_totals(billing_data)
return jsonify(data)
2023-08-29 14:54:30 -07:00
@billing_blueprint.route("/free-sms-fragment-limit", methods=["GET"])
def get_free_sms_fragment_limit(service_id):
2023-08-29 14:54:30 -07:00
financial_year_start = request.args.get("financial_year_start")
2023-08-29 14:54:30 -07:00
annual_billing = dao_get_free_sms_fragment_limit_for_year(
service_id, financial_year_start
)
2017-11-02 17:02:00 +00:00
if annual_billing is None:
2024-11-18 09:26:04 -08:00
service = db.session.get(Service, service_id)
# An entry does not exist in annual_billing table for that service and year.
2023-07-10 11:06:29 -07:00
# Set the annual billing to the default free allowance based on the organization type of the service.
annual_billing = set_default_free_allowance_for_service(
service=service,
2023-08-29 14:54:30 -07:00
year_start=int(financial_year_start) if financial_year_start else None,
)
2017-11-02 17:02:00 +00:00
return jsonify(annual_billing.serialize_free_sms_items()), 200
2023-08-29 14:54:30 -07:00
@billing_blueprint.route("/free-sms-fragment-limit", methods=["POST"])
def create_or_update_free_sms_fragment_limit(service_id):
req_args = request.get_json()
form = validate(req_args, create_or_update_free_sms_fragment_limit_schema)
2023-08-29 14:54:30 -07:00
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
2023-08-29 14:54:30 -07:00
def update_free_sms_fragment_limit_data(
service_id, free_sms_fragment_limit, financial_year_start
):
current_year = get_current_calendar_year_start_year()
if not financial_year_start:
financial_year_start = current_year
dao_create_or_update_annual_billing_for_year(
2023-08-29 14:54:30 -07:00
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:
2017-12-06 14:41:32 +00:00
dao_update_annual_billing_for_future_years(
2023-08-29 14:54:30 -07:00
service_id, free_sms_fragment_limit, financial_year_start
)