mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
Merge pull request #1141 from alphagov/imdad-fix-monthly-billing-update
Fix issue where monthly billing data was not being updated
This commit is contained in:
@@ -25,20 +25,33 @@ def create_or_update_monthly_billing_sms(service_id, billing_month):
|
|||||||
monthly = get_billing_data_for_month(service_id=service_id, start_date=start_date, end_date=end_date)
|
monthly = get_billing_data_for_month(service_id=service_id, start_date=start_date, end_date=end_date)
|
||||||
# update monthly
|
# update monthly
|
||||||
monthly_totals = _monthly_billing_data_to_json(monthly)
|
monthly_totals = _monthly_billing_data_to_json(monthly)
|
||||||
row = MonthlyBilling.query.filter_by(start_date=start_date,
|
row = get_monthly_billing_entry(service_id, start_date, SMS_TYPE)
|
||||||
notification_type='sms').first()
|
|
||||||
if row:
|
if row:
|
||||||
row.monthly_totals = monthly_totals
|
row.monthly_totals = monthly_totals
|
||||||
row.updated_at = datetime.utcnow()
|
row.updated_at = datetime.utcnow()
|
||||||
else:
|
else:
|
||||||
row = MonthlyBilling(service_id=service_id,
|
row = MonthlyBilling(
|
||||||
notification_type=SMS_TYPE,
|
service_id=service_id,
|
||||||
monthly_totals=monthly_totals,
|
notification_type=SMS_TYPE,
|
||||||
start_date=start_date,
|
monthly_totals=monthly_totals,
|
||||||
end_date=end_date)
|
start_date=start_date,
|
||||||
|
end_date=end_date
|
||||||
|
)
|
||||||
|
|
||||||
db.session.add(row)
|
db.session.add(row)
|
||||||
|
|
||||||
|
|
||||||
|
def get_monthly_billing_entry(service_id, start_date, notification_type):
|
||||||
|
entry = MonthlyBilling.query.filter_by(
|
||||||
|
service_id=service_id,
|
||||||
|
start_date=start_date,
|
||||||
|
notification_type=notification_type
|
||||||
|
).first()
|
||||||
|
|
||||||
|
return entry
|
||||||
|
|
||||||
|
|
||||||
@statsd(namespace="dao")
|
@statsd(namespace="dao")
|
||||||
def get_monthly_billing_sms(service_id, billing_month):
|
def get_monthly_billing_sms(service_id, billing_month):
|
||||||
start_date, end_date = get_month_start_end_date(billing_month)
|
start_date, end_date = get_month_start_end_date(billing_month)
|
||||||
|
|||||||
@@ -1,22 +1,43 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
from freezegun.api import FakeDatetime
|
from freezegun.api import FakeDatetime
|
||||||
|
|
||||||
|
from app import db
|
||||||
from app.dao.monthly_billing_dao import (
|
from app.dao.monthly_billing_dao import (
|
||||||
create_or_update_monthly_billing_sms,
|
create_or_update_monthly_billing_sms,
|
||||||
|
get_monthly_billing_entry,
|
||||||
get_monthly_billing_sms,
|
get_monthly_billing_sms,
|
||||||
get_service_ids_that_need_sms_billing_populated
|
get_service_ids_that_need_sms_billing_populated
|
||||||
)
|
)
|
||||||
from app.models import MonthlyBilling
|
from app.models import MonthlyBilling, SMS_TYPE
|
||||||
from tests.app.db import create_notification, create_rate, create_service, create_template
|
from tests.app.db import create_notification, create_rate, create_service, create_template
|
||||||
|
|
||||||
|
|
||||||
|
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 test_add_monthly_billing(sample_template):
|
def test_add_monthly_billing(sample_template):
|
||||||
jan = datetime(2017, 1, 1)
|
jan = datetime(2017, 1, 1)
|
||||||
feb = datetime(2017, 2, 15)
|
feb = datetime(2017, 2, 15)
|
||||||
create_rate(start_date=jan, value=0.0158, notification_type='sms')
|
create_rate(start_date=jan, value=0.0158, notification_type=SMS_TYPE)
|
||||||
create_rate(start_date=datetime(2017, 3, 31, 23, 00, 00), value=0.123, notification_type='sms')
|
create_rate(start_date=datetime(2017, 3, 31, 23, 00, 00), value=0.123, notification_type=SMS_TYPE)
|
||||||
create_notification(template=sample_template, created_at=jan, billable_units=1, status='delivered')
|
create_notification(template=sample_template, created_at=jan, billable_units=1, status='delivered')
|
||||||
create_notification(template=sample_template, created_at=feb, billable_units=2, status='delivered')
|
create_notification(template=sample_template, created_at=feb, billable_units=2, status='delivered')
|
||||||
|
|
||||||
@@ -51,8 +72,8 @@ def test_add_monthly_billing(sample_template):
|
|||||||
def test_add_monthly_billing_multiple_rates_in_a_month(sample_template):
|
def test_add_monthly_billing_multiple_rates_in_a_month(sample_template):
|
||||||
rate_1 = datetime(2016, 12, 1)
|
rate_1 = datetime(2016, 12, 1)
|
||||||
rate_2 = datetime(2017, 1, 15)
|
rate_2 = datetime(2017, 1, 15)
|
||||||
create_rate(start_date=rate_1, value=0.0158, notification_type='sms')
|
create_rate(start_date=rate_1, value=0.0158, notification_type=SMS_TYPE)
|
||||||
create_rate(start_date=rate_2, value=0.0124, notification_type='sms')
|
create_rate(start_date=rate_2, value=0.0124, notification_type=SMS_TYPE)
|
||||||
|
|
||||||
create_notification(template=sample_template, created_at=datetime(2017, 1, 1), billable_units=1, status='delivered')
|
create_notification(template=sample_template, created_at=datetime(2017, 1, 1), billable_units=1, status='delivered')
|
||||||
create_notification(template=sample_template, created_at=datetime(2017, 1, 14, 23, 59), billable_units=1,
|
create_notification(template=sample_template, created_at=datetime(2017, 1, 14, 23, 59), billable_units=1,
|
||||||
@@ -87,7 +108,7 @@ def test_add_monthly_billing_multiple_rates_in_a_month(sample_template):
|
|||||||
|
|
||||||
def test_update_monthly_billing_overwrites_old_totals(sample_template):
|
def test_update_monthly_billing_overwrites_old_totals(sample_template):
|
||||||
july = datetime(2017, 7, 1)
|
july = datetime(2017, 7, 1)
|
||||||
create_rate(july, 0.123, 'sms')
|
create_rate(july, 0.123, SMS_TYPE)
|
||||||
create_notification(template=sample_template, created_at=datetime(2017, 7, 2), billable_units=1, status='delivered')
|
create_notification(template=sample_template, created_at=datetime(2017, 7, 2), billable_units=1, status='delivered')
|
||||||
with freeze_time('2017-07-20 02:30:00'):
|
with freeze_time('2017-07-20 02:30:00'):
|
||||||
create_or_update_monthly_billing_sms(sample_template.service_id, july)
|
create_or_update_monthly_billing_sms(sample_template.service_id, july)
|
||||||
@@ -136,3 +157,28 @@ def test_get_service_id(notify_db_session):
|
|||||||
end_date=datetime(2017, 7, 16))
|
end_date=datetime(2017, 7, 16))
|
||||||
expected_services = [service_1.id, service_2.id]
|
expected_services = [service_1.id, service_2.id]
|
||||||
assert sorted([x.service_id for x in services]) == sorted(expected_services)
|
assert sorted([x.service_id for x in services]) == sorted(expected_services)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_monthly_billing_entry_filters_by_service(notify_db, notify_db_session):
|
||||||
|
service_1 = create_service(service_name="Service One")
|
||||||
|
service_2 = create_service(service_name="Service Two")
|
||||||
|
now = datetime.utcnow()
|
||||||
|
|
||||||
|
create_sample_monthly_billing_entry(
|
||||||
|
service_id=service_1.id,
|
||||||
|
monthly_totals=[],
|
||||||
|
start_date=now,
|
||||||
|
end_date=now + timedelta(days=30)
|
||||||
|
)
|
||||||
|
|
||||||
|
create_sample_monthly_billing_entry(
|
||||||
|
service_id=service_2.id,
|
||||||
|
monthly_totals=[],
|
||||||
|
start_date=now,
|
||||||
|
end_date=now + timedelta(days=30)
|
||||||
|
)
|
||||||
|
|
||||||
|
entry = get_monthly_billing_entry(service_2.id, now, SMS_TYPE)
|
||||||
|
|
||||||
|
assert entry.start_date == now
|
||||||
|
assert entry.service_id == service_2.id
|
||||||
|
|||||||
Reference in New Issue
Block a user