mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 01:41:05 -05:00
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,17 +1,40 @@
|
|||||||
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(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
service_id,
|
||||||
|
notification_type,
|
||||||
|
monthly_totals,
|
||||||
|
start_date,
|
||||||
|
end_date
|
||||||
|
):
|
||||||
|
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)
|
||||||
@@ -136,3 +159,34 @@ 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(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
service_1.id,
|
||||||
|
'sms',
|
||||||
|
[],
|
||||||
|
now,
|
||||||
|
now + timedelta(days=30)
|
||||||
|
)
|
||||||
|
|
||||||
|
create_sample_monthly_billing_entry(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
service_2.id,
|
||||||
|
'sms',
|
||||||
|
[],
|
||||||
|
now,
|
||||||
|
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