mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
Query ft_billing aggregate for a month
This commit is contained in:
34
app/dao/fact_billing_dao.py
Normal file
34
app/dao/fact_billing_dao.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from app import db
|
||||
from app.dao.date_util import get_month_start_and_end_date_in_utc
|
||||
from app.models import FactBilling
|
||||
from app.utils import convert_utc_to_bst
|
||||
|
||||
|
||||
def fetch_annual_billing_by_month(service_id, billing_month, notification_type):
|
||||
billing_month_in_bst = convert_utc_to_bst(billing_month)
|
||||
start_date, end_date = get_month_start_and_end_date_in_utc(billing_month_in_bst)
|
||||
|
||||
monthly_data = db.session.query(
|
||||
func.sum(FactBilling.notifications_sent).label('notifications_sent'),
|
||||
func.sum(FactBilling.billable_units).label('billing_units'),
|
||||
FactBilling.service_id,
|
||||
FactBilling.notification_type,
|
||||
FactBilling.rate,
|
||||
FactBilling.rate_multiplier,
|
||||
FactBilling.international
|
||||
).filter(
|
||||
FactBilling.notification_type == notification_type,
|
||||
FactBilling.service_id == service_id,
|
||||
FactBilling.bst_date >= start_date,
|
||||
FactBilling.bst_date <= end_date
|
||||
).group_by(
|
||||
FactBilling.service_id,
|
||||
FactBilling.notification_type,
|
||||
FactBilling.rate,
|
||||
FactBilling.rate_multiplier,
|
||||
FactBilling.international
|
||||
).all()
|
||||
|
||||
return monthly_data, start_date
|
||||
24
tests/app/dao/test_ft_billing_dao.py
Normal file
24
tests/app/dao/test_ft_billing_dao.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
|
||||
from app.dao.fact_billing_dao import fetch_annual_billing_by_month
|
||||
from tests.app.db import create_ft_billing, create_service, create_template
|
||||
|
||||
|
||||
def test_fetch_annual_billing_by_month(notify_db_session):
|
||||
service = create_service()
|
||||
template = create_template(service=service, template_type="email")
|
||||
for i in range(1, 32):
|
||||
record = create_ft_billing(bst_date='2018-01-{}'.format(i),
|
||||
service=service,
|
||||
template=template,
|
||||
notification_type='email')
|
||||
|
||||
results, month = fetch_annual_billing_by_month(service_id=record.service_id,
|
||||
billing_month=datetime(2018, 1, 1),
|
||||
notification_type='email')
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0] == (31, Decimal('31'), service.id, 'email', Decimal('0'), Decimal('1'), False)
|
||||
assert month == datetime(2018, 1, 1)
|
||||
|
||||
@@ -34,6 +34,7 @@ from app.models import (
|
||||
AnnualBilling,
|
||||
LetterRate,
|
||||
InvitedOrganisationUser,
|
||||
FactBilling
|
||||
)
|
||||
from app.dao.users_dao import save_model_user
|
||||
from app.dao.notifications_dao import (
|
||||
@@ -524,3 +525,34 @@ def create_daily_sorted_letter(billing_day=date(2018, 1, 18),
|
||||
db.session.commit()
|
||||
|
||||
return daily_sorted_letter
|
||||
|
||||
|
||||
def create_ft_billing(bst_date,
|
||||
notification_type,
|
||||
template = None,
|
||||
service = None,
|
||||
provider='test',
|
||||
rate_multiplier=1,
|
||||
international=False,
|
||||
rate=0,
|
||||
billable_unit=1,
|
||||
notifications_sent=1
|
||||
):
|
||||
if not service:
|
||||
service = create_service()
|
||||
if not template:
|
||||
template = create_template(service=service, template_type=notification_type)
|
||||
|
||||
data = FactBilling(bst_date=bst_date,
|
||||
service_id=service.id,
|
||||
template_id=template.id,
|
||||
notification_type=notification_type,
|
||||
provider=provider,
|
||||
rate_multiplier=rate_multiplier,
|
||||
international=international,
|
||||
rate=rate,
|
||||
billable_units=billable_unit,
|
||||
notifications_sent=notifications_sent)
|
||||
db.session.add(data)
|
||||
db.session.commit()
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user