Added new endpoints to return the yearly-usage and monthly-usage for a given financial year and service id.

Since the response has changed I have created new endpoints so that the deployments for Admin are more managable.

Removed print statements from some tests.
This commit is contained in:
Rebecca Law
2017-04-27 10:00:09 +01:00
committed by Ken Tsang
parent e1e55edd9c
commit a186fc95be
7 changed files with 144 additions and 51 deletions

View File

@@ -1,4 +1,5 @@
import itertools
import json
from datetime import datetime
from flask import (
@@ -8,6 +9,7 @@ from flask import (
)
from sqlalchemy.orm.exc import NoResultFound
from app.dao import notification_usage_dao
from app.dao.dao_utils import dao_rollback
from app.dao.api_key_dao import (
save_model_api_key,
@@ -411,3 +413,39 @@ def get_monthly_template_stats(service_id):
))
except ValueError:
raise InvalidRequest('Year must be a number', status_code=400)
@service_blueprint.route('/<uuid:service_id>/yearly-usage')
def get_yearly_billing_usage(service_id):
try:
year = int(request.args.get('year'))
results = notification_usage_dao.get_yearly_billing_data(service_id, year)
json_result = [{"billing_units": x[0],
"notification_type": x[1],
"rate_multiplier": x[2],
"international": x[3],
"phone_prefix": x[4],
"rate": x[5]
} for x in results]
return json.dumps(json_result)
except TypeError:
return jsonify(result='error', message='No valid year provided'), 400
@service_blueprint.route('/<uuid:service_id>/monthly-usage')
def get_yearly_monthly_usage(service_id):
try:
year = int(request.args.get('year'))
results = notification_usage_dao.get_monthly_billing_data(service_id, year)
json_results = [{"month": x[0],
"billing_units": x[1],
"notification_type": x[2],
"rate_multiplier": x[3],
"international": x[4],
"phone_prefix": x[5],
"rate": x[6]
} for x in results]
return json.dumps(json_results)
except TypeError:
return jsonify(result='error', message='No valid year provided'), 400