mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 14:31:57 -05:00
Added free_sms_fragment_limit model, schema, dao and Rest
This commit is contained in:
14
app/billing/billing_schemas.py
Normal file
14
app/billing/billing_schemas.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from app.schema_validation.definitions import uuid, https_url
|
||||
|
||||
|
||||
create_or_update_free_sms_fragment_limit_schema = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "POST annual billing schema",
|
||||
"type": "object",
|
||||
"title": "Create",
|
||||
"properties": {
|
||||
"free_sms_fragment_limit": {"type": "integer", "minimum": 1},
|
||||
"financial_year_start": {"type": "integer", "minimum": 1}
|
||||
},
|
||||
"required": ["free_sms_fragment_limit", "financial_year_start"]
|
||||
}
|
||||
@@ -11,6 +11,14 @@ from app.dao.date_util import get_financial_year, get_months_for_financial_year
|
||||
from app.errors import register_errors
|
||||
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_new_annual_billing_for_year,
|
||||
dao_update_new_free_sms_fragment_limit_for_year)
|
||||
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
|
||||
|
||||
billing_blueprint = Blueprint(
|
||||
'billing',
|
||||
@@ -86,3 +94,55 @@ def _transform_billing_for_month(billing_for_month):
|
||||
"notification_type": billing_for_month.notification_type,
|
||||
"rate": rate
|
||||
}
|
||||
|
||||
|
||||
# @billing_blueprint.route('/annual-billing', methods=["GET"])
|
||||
# def get_annual_billing(service_id):
|
||||
#
|
||||
# results = dao_get_annual_billing(service_id)
|
||||
#
|
||||
# if len(results)==0:
|
||||
# raise InvalidRequest('no annual billing information for this service', status_code=404)
|
||||
#
|
||||
# return jsonify(data=[row.serialize() for row in results]), 200
|
||||
|
||||
|
||||
@billing_blueprint.route('/free-sms-fragment-limit', methods=["GET"])
|
||||
def get_free_sms_fragment_limit(service_id):
|
||||
|
||||
financial_year_start = request.args.get('financial_year_start')
|
||||
|
||||
if financial_year_start is None:
|
||||
results = dao_get_all_free_sms_fragment_limit(service_id)
|
||||
|
||||
if len(results) == 0:
|
||||
raise InvalidRequest('no annual billing information for this service', status_code=404)
|
||||
return jsonify(data=[row.serialize() for row in results]), 200
|
||||
else:
|
||||
result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start)
|
||||
if result is None:
|
||||
raise InvalidRequest('no free-sms-fragment-limit-info for this service and year', status_code=404)
|
||||
|
||||
return jsonify(data=result.serialize()), 200
|
||||
|
||||
|
||||
@billing_blueprint.route('/free-sms-fragment-limit', methods=["POST"])
|
||||
def create_or_update_free_sms_fragment_limit(service_id):
|
||||
|
||||
form = validate(request.get_json(), 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)
|
||||
|
||||
annual_billing = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start,
|
||||
free_sms_fragment_limit=free_sms_fragment_limit)
|
||||
|
||||
if result is None:
|
||||
dao_create_new_annual_billing_for_year(annual_billing)
|
||||
|
||||
else:
|
||||
dao_update_new_free_sms_fragment_limit_for_year(annual_billing)
|
||||
|
||||
return jsonify(data=form), 201
|
||||
|
||||
39
app/dao/annual_billing_dao.py
Normal file
39
app/dao/annual_billing_dao.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from app import db, create_uuid
|
||||
from app.dao.dao_utils import (
|
||||
transactional,
|
||||
version_class
|
||||
)
|
||||
from app.models import AnnualBilling
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def dao_get_annual_billing(service_id):
|
||||
return AnnualBilling.query.filter_by(
|
||||
service_id=service_id,
|
||||
).all()
|
||||
|
||||
|
||||
def dao_create_new_annual_billing_for_year(annual_billing):
|
||||
annual_billing.id = create_uuid()
|
||||
db.session.add(annual_billing)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def dao_get_free_sms_fragment_limit_for_year(service_id, year):
|
||||
|
||||
return AnnualBilling.query.filter_by(
|
||||
service_id=service_id,
|
||||
financial_year_start=year
|
||||
).first()
|
||||
|
||||
|
||||
def dao_get_all_free_sms_fragment_limit(service_id):
|
||||
|
||||
return AnnualBilling.query.filter_by(
|
||||
service_id=service_id,
|
||||
).all()
|
||||
|
||||
|
||||
def dao_update_new_free_sms_fragment_limit_for_year(annual_billing):
|
||||
db.session.add(annual_billing)
|
||||
db.session.commit()
|
||||
@@ -191,12 +191,8 @@ class AnnualBilling(db.Model):
|
||||
|
||||
def serialize(self):
|
||||
return {
|
||||
'id': str(self.id),
|
||||
'service_id': str(self.service_id),
|
||||
'free_sms_fragment_limit': str(self.free_sms_fragment_limit),
|
||||
'financial_year_start': str(self.financial_year_start),
|
||||
'created_at': self.created_at.strftime(DATETIME_FORMAT),
|
||||
'updated_at': self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None
|
||||
'free_sms_fragment_limit': self.free_sms_fragment_limit,
|
||||
'financial_year_start': self.financial_year_start,
|
||||
}
|
||||
|
||||
|
||||
@@ -234,7 +230,6 @@ class Service(db.Model, Versioned):
|
||||
organisation_id = db.Column(UUID(as_uuid=True), db.ForeignKey('organisation.id'), index=True, nullable=True)
|
||||
free_sms_fragment_limit = db.Column(db.BigInteger, index=False, unique=False, nullable=True)
|
||||
organisation = db.relationship('Organisation')
|
||||
annual_billing = db.relationship('AnnualBilling')
|
||||
dvla_organisation_id = db.Column(
|
||||
db.String,
|
||||
db.ForeignKey('dvla_organisation.id'),
|
||||
|
||||
@@ -66,7 +66,8 @@ from app.errors import (
|
||||
InvalidRequest,
|
||||
register_errors
|
||||
)
|
||||
from app.models import Service, ServiceInboundApi
|
||||
|
||||
from app.models import Service, ServiceInboundApi, AnnualBilling
|
||||
from app.schema_validation import validate
|
||||
from app.service import statistics
|
||||
from app.service.service_inbound_api_schema import service_inbound_api, update_service_inbound_api_schema
|
||||
@@ -87,6 +88,7 @@ from app.schemas import (
|
||||
detailed_service_schema
|
||||
)
|
||||
from app.utils import pagination_links
|
||||
from app.dao.notifications_dao import get_financial_year
|
||||
|
||||
service_blueprint = Blueprint('service', __name__)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user