mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-06 14:08:25 -04:00
Merge pull request #1190 from alphagov/imdad-feat-get-monthly-usage-from-billing-table
Get monthly usage breakdown from the correct table
This commit is contained in:
@@ -3,12 +3,14 @@ import json
|
|||||||
|
|
||||||
from flask import Blueprint, jsonify, request
|
from flask import Blueprint, jsonify, request
|
||||||
|
|
||||||
from app.dao.notification_usage_dao import get_billing_data_for_month
|
from app.dao.monthly_billing_dao import (
|
||||||
from app.dao.monthly_billing_dao import get_billing_data_for_financial_year
|
get_billing_data_for_financial_year,
|
||||||
from app.dao.date_util import get_financial_year
|
get_monthly_billing_by_notification_type
|
||||||
|
)
|
||||||
|
from app.dao.date_util import get_financial_year, get_months_for_financial_year
|
||||||
from app.errors import register_errors
|
from app.errors import register_errors
|
||||||
from app.models import SMS_TYPE, EMAIL_TYPE
|
from app.models import SMS_TYPE, EMAIL_TYPE
|
||||||
|
from app.utils import convert_utc_to_bst
|
||||||
|
|
||||||
billing_blueprint = Blueprint(
|
billing_blueprint = Blueprint(
|
||||||
'billing',
|
'billing',
|
||||||
@@ -25,16 +27,13 @@ def get_yearly_usage_by_month(service_id):
|
|||||||
try:
|
try:
|
||||||
year = int(request.args.get('year'))
|
year = int(request.args.get('year'))
|
||||||
start_date, end_date = get_financial_year(year)
|
start_date, end_date = get_financial_year(year)
|
||||||
results = get_billing_data_for_month(service_id, start_date, end_date, SMS_TYPE)
|
results = []
|
||||||
json_results = [{
|
for month in get_months_for_financial_year(year):
|
||||||
"month": datetime.strftime(x[0], "%B"),
|
billing_for_month = get_monthly_billing_by_notification_type(service_id, month, SMS_TYPE)
|
||||||
"billing_units": x[1],
|
if billing_for_month:
|
||||||
"rate_multiplier": x[2],
|
results.append(_transform_billing_for_month(billing_for_month))
|
||||||
"international": x[3],
|
return json.dumps(results)
|
||||||
"notification_type": x[4],
|
|
||||||
"rate": x[5]
|
|
||||||
} for x in results]
|
|
||||||
return json.dumps(json_results)
|
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return jsonify(result='error', message='No valid year provided'), 400
|
return jsonify(result='error', message='No valid year provided'), 400
|
||||||
|
|
||||||
@@ -71,3 +70,23 @@ def _get_total_billable_units_and_rate_for_notification_type(billing_data, noti_
|
|||||||
"billing_units": total_sent,
|
"billing_units": total_sent,
|
||||||
"rate": rate
|
"rate": rate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _transform_billing_for_month(billing_for_month):
|
||||||
|
month_name = datetime.strftime(convert_utc_to_bst(billing_for_month.start_date), "%B")
|
||||||
|
billing_units = rate = rate_multiplier = international = 0
|
||||||
|
|
||||||
|
if billing_for_month.monthly_totals:
|
||||||
|
billing_units = billing_for_month.monthly_totals[0]['billing_units']
|
||||||
|
rate = billing_for_month.monthly_totals[0]['rate']
|
||||||
|
rate_multiplier = billing_for_month.monthly_totals[0]['rate_multiplier']
|
||||||
|
international = billing_for_month.monthly_totals[0]['international']
|
||||||
|
|
||||||
|
return {
|
||||||
|
"month": month_name,
|
||||||
|
"billing_units": billing_units,
|
||||||
|
"rate_multiplier": rate_multiplier,
|
||||||
|
"international": bool(international),
|
||||||
|
"notification_type": billing_for_month.notification_type,
|
||||||
|
"rate": rate
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,20 @@ import pytz
|
|||||||
from app.utils import convert_bst_to_utc
|
from app.utils import convert_bst_to_utc
|
||||||
|
|
||||||
|
|
||||||
|
def get_months_for_financial_year(year):
|
||||||
|
return [
|
||||||
|
convert_bst_to_utc(month) for month in (
|
||||||
|
get_months_for_year(4, 13, year) +
|
||||||
|
get_months_for_year(1, 4, year + 1)
|
||||||
|
)
|
||||||
|
if month < datetime.now()
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def get_months_for_year(start, end, year):
|
||||||
|
return [datetime(year, month, 1) for month in range(start, end)]
|
||||||
|
|
||||||
|
|
||||||
def get_financial_year(year):
|
def get_financial_year(year):
|
||||||
return get_april_fools(year), get_april_fools(year + 1) - timedelta(microseconds=1)
|
return get_april_fools(year), get_april_fools(year + 1) - timedelta(microseconds=1)
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,23 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from app.dao.monthly_billing_dao import create_or_update_monthly_billing
|
from app.billing.rest import _transform_billing_for_month
|
||||||
|
from app.dao.monthly_billing_dao import (
|
||||||
|
create_or_update_monthly_billing,
|
||||||
|
get_monthly_billing_by_notification_type,
|
||||||
|
)
|
||||||
from app.models import SMS_TYPE, EMAIL_TYPE
|
from app.models import SMS_TYPE, EMAIL_TYPE
|
||||||
from tests.app.db import (
|
from tests.app.db import (
|
||||||
create_notification,
|
create_notification,
|
||||||
create_rate
|
create_rate,
|
||||||
|
create_monthly_billing_entry
|
||||||
)
|
)
|
||||||
|
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
|
|
||||||
|
APR_2016_MONTH_START = datetime(2016, 3, 31, 23, 00, 00)
|
||||||
|
APR_2016_MONTH_END = datetime(2016, 4, 30, 22, 59, 59, 99999)
|
||||||
|
|
||||||
IN_MAY_2016 = datetime(2016, 5, 10, 23, 00, 00)
|
IN_MAY_2016 = datetime(2016, 5, 10, 23, 00, 00)
|
||||||
IN_JUN_2016 = datetime(2016, 6, 3, 23, 00, 00)
|
IN_JUN_2016 = datetime(2016, 6, 3, 23, 00, 00)
|
||||||
|
|
||||||
@@ -76,7 +85,7 @@ def test_get_yearly_usage_by_month_returns_400_if_missing_year(client, sample_se
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_get_yearly_usage_by_month_returns_empty_list_if_no_notifications(client, sample_template):
|
def test_get_yearly_usage_by_month_returns_empty_list_if_no_usage(client, sample_template):
|
||||||
create_rate(start_date=IN_MAY_2016 - timedelta(days=1), value=0.12, notification_type=SMS_TYPE)
|
create_rate(start_date=IN_MAY_2016 - timedelta(days=1), value=0.12, notification_type=SMS_TYPE)
|
||||||
response = client.get(
|
response = client.get(
|
||||||
'/service/{}/billing/monthly-usage?year=2016'.format(sample_template.service.id),
|
'/service/{}/billing/monthly-usage?year=2016'.format(sample_template.service.id),
|
||||||
@@ -128,3 +137,55 @@ def test_get_yearly_usage_by_month_returns_correctly(client, sample_template):
|
|||||||
'rate': 0.12,
|
'rate': 0.12,
|
||||||
'rate_multiplier': 3
|
'rate_multiplier': 3
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def test_transform_billing_for_month_returns_empty_if_no_monthly_totals(sample_service):
|
||||||
|
create_monthly_billing_entry(
|
||||||
|
service=sample_service,
|
||||||
|
monthly_totals=[],
|
||||||
|
start_date=APR_2016_MONTH_START,
|
||||||
|
end_date=APR_2016_MONTH_END,
|
||||||
|
notification_type=SMS_TYPE
|
||||||
|
)
|
||||||
|
|
||||||
|
transformed_billing_data = _transform_billing_for_month(get_monthly_billing_by_notification_type(
|
||||||
|
sample_service.id, APR_2016_MONTH_START, SMS_TYPE
|
||||||
|
))
|
||||||
|
|
||||||
|
_assert_dict_equals(transformed_billing_data, {
|
||||||
|
'notification_type': SMS_TYPE,
|
||||||
|
'billing_units': 0,
|
||||||
|
'month': 'April',
|
||||||
|
'international': False,
|
||||||
|
'rate_multiplier': 0,
|
||||||
|
'rate': 0,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def test_transform_billing_for_month_formats_monthly_totals_correctly(sample_service):
|
||||||
|
create_monthly_billing_entry(
|
||||||
|
service=sample_service,
|
||||||
|
monthly_totals=[{
|
||||||
|
"billing_units": 12,
|
||||||
|
"rate": 0.0158,
|
||||||
|
"rate_multiplier": 5,
|
||||||
|
"total_cost": 2.1804,
|
||||||
|
"international": False
|
||||||
|
}],
|
||||||
|
start_date=APR_2016_MONTH_START,
|
||||||
|
end_date=APR_2016_MONTH_END,
|
||||||
|
notification_type=SMS_TYPE
|
||||||
|
)
|
||||||
|
|
||||||
|
transformed_billing_data = _transform_billing_for_month(get_monthly_billing_by_notification_type(
|
||||||
|
sample_service.id, APR_2016_MONTH_START, SMS_TYPE
|
||||||
|
))
|
||||||
|
|
||||||
|
_assert_dict_equals(transformed_billing_data, {
|
||||||
|
'notification_type': SMS_TYPE,
|
||||||
|
'billing_units': 12,
|
||||||
|
'month': 'April',
|
||||||
|
'international': False,
|
||||||
|
'rate_multiplier': 5,
|
||||||
|
'rate': 0.0158,
|
||||||
|
})
|
||||||
|
|||||||
@@ -50,26 +50,6 @@ NO_BILLING_DATA = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _create_sample_monthly_billing_entry(
|
|
||||||
service_id,
|
|
||||||
monthly_totals,
|
|
||||||
start_date,
|
|
||||||
end_date,
|
|
||||||
notification_type=SMS_TYPE
|
|
||||||
):
|
|
||||||
entry = MonthlyBilling(
|
|
||||||
service_id=service_id,
|
|
||||||
notification_type=notification_type,
|
|
||||||
monthly_totals=monthly_totals,
|
|
||||||
start_date=start_date,
|
|
||||||
end_date=end_date
|
|
||||||
)
|
|
||||||
db.session.add(entry)
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
return entry
|
|
||||||
|
|
||||||
|
|
||||||
def _assert_monthly_billing(monthly_billing, service_id, notification_type, month_start, month_end):
|
def _assert_monthly_billing(monthly_billing, service_id, notification_type, month_start, month_end):
|
||||||
assert monthly_billing.service_id == service_id
|
assert monthly_billing.service_id == service_id
|
||||||
assert monthly_billing.notification_type == notification_type
|
assert monthly_billing.notification_type == notification_type
|
||||||
@@ -85,8 +65,8 @@ def _assert_monthly_billing_totals(monthly_billing_totals, expected_dict):
|
|||||||
def test_get_monthly_billing_by_notification_type_returns_correct_totals(notify_db, notify_db_session):
|
def test_get_monthly_billing_by_notification_type_returns_correct_totals(notify_db, notify_db_session):
|
||||||
service = create_service(service_name="Service One")
|
service = create_service(service_name="Service One")
|
||||||
|
|
||||||
_create_sample_monthly_billing_entry(
|
create_monthly_billing_entry(
|
||||||
service_id=service.id,
|
service=service,
|
||||||
monthly_totals=[{
|
monthly_totals=[{
|
||||||
"billing_units": 12,
|
"billing_units": 12,
|
||||||
"rate": 0.0158,
|
"rate": 0.0158,
|
||||||
@@ -116,8 +96,8 @@ def test_get_monthly_billing_by_notification_type_returns_correct_totals(notify_
|
|||||||
def test_get_monthly_billing_by_notification_type_filters_by_type(notify_db, notify_db_session):
|
def test_get_monthly_billing_by_notification_type_filters_by_type(notify_db, notify_db_session):
|
||||||
service = create_service(service_name="Service One")
|
service = create_service(service_name="Service One")
|
||||||
|
|
||||||
_create_sample_monthly_billing_entry(
|
create_monthly_billing_entry(
|
||||||
service_id=service.id,
|
service=service,
|
||||||
monthly_totals=[{
|
monthly_totals=[{
|
||||||
"billing_units": 138,
|
"billing_units": 138,
|
||||||
"rate": 0.0158,
|
"rate": 0.0158,
|
||||||
@@ -130,8 +110,8 @@ def test_get_monthly_billing_by_notification_type_filters_by_type(notify_db, not
|
|||||||
notification_type=SMS_TYPE
|
notification_type=SMS_TYPE
|
||||||
)
|
)
|
||||||
|
|
||||||
_create_sample_monthly_billing_entry(
|
create_monthly_billing_entry(
|
||||||
service_id=service.id,
|
service=service,
|
||||||
monthly_totals=[],
|
monthly_totals=[],
|
||||||
start_date=APR_2016_MONTH_START,
|
start_date=APR_2016_MONTH_START,
|
||||||
end_date=APR_2016_MONTH_END,
|
end_date=APR_2016_MONTH_END,
|
||||||
@@ -149,8 +129,8 @@ def test_get_monthly_billing_by_notification_type_filters_by_type(notify_db, not
|
|||||||
def test_get_monthly_billing_by_notification_type_normalises_start_date(notify_db, notify_db_session):
|
def test_get_monthly_billing_by_notification_type_normalises_start_date(notify_db, notify_db_session):
|
||||||
service = create_service(service_name="Service One")
|
service = create_service(service_name="Service One")
|
||||||
|
|
||||||
_create_sample_monthly_billing_entry(
|
create_monthly_billing_entry(
|
||||||
service_id=service.id,
|
service=service,
|
||||||
monthly_totals=[{
|
monthly_totals=[{
|
||||||
"billing_units": 321,
|
"billing_units": 321,
|
||||||
"rate": 0.0158,
|
"rate": 0.0158,
|
||||||
@@ -354,18 +334,20 @@ def test_get_monthly_billing_entry_filters_by_service(notify_db, notify_db_sessi
|
|||||||
service_2 = create_service(service_name="Service Two")
|
service_2 = create_service(service_name="Service Two")
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
|
|
||||||
_create_sample_monthly_billing_entry(
|
create_monthly_billing_entry(
|
||||||
service_id=service_1.id,
|
service=service_1,
|
||||||
monthly_totals=[],
|
monthly_totals=[],
|
||||||
start_date=now,
|
start_date=now,
|
||||||
end_date=now + timedelta(days=30)
|
end_date=now + timedelta(days=30),
|
||||||
|
notification_type=SMS_TYPE
|
||||||
)
|
)
|
||||||
|
|
||||||
_create_sample_monthly_billing_entry(
|
create_monthly_billing_entry(
|
||||||
service_id=service_2.id,
|
service=service_2,
|
||||||
monthly_totals=[],
|
monthly_totals=[],
|
||||||
start_date=now,
|
start_date=now,
|
||||||
end_date=now + timedelta(days=30)
|
end_date=now + timedelta(days=30),
|
||||||
|
notification_type=SMS_TYPE
|
||||||
)
|
)
|
||||||
|
|
||||||
entry = get_monthly_billing_entry(service_2.id, now, SMS_TYPE)
|
entry = get_monthly_billing_entry(service_2.id, now, SMS_TYPE)
|
||||||
|
|||||||
Reference in New Issue
Block a user