mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Added query for billing usage of letter notifications.
This commit is contained in:
@@ -12,7 +12,10 @@ from app.models import (
|
|||||||
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
||||||
KEY_TYPE_TEST,
|
KEY_TYPE_TEST,
|
||||||
SMS_TYPE,
|
SMS_TYPE,
|
||||||
EMAIL_TYPE
|
EMAIL_TYPE,
|
||||||
|
LETTER_TYPE,
|
||||||
|
LetterRate,
|
||||||
|
Service
|
||||||
)
|
)
|
||||||
from app.statsd_decorators import statsd
|
from app.statsd_decorators import statsd
|
||||||
from app.utils import get_london_month_from_utc_column
|
from app.utils import get_london_month_from_utc_column
|
||||||
@@ -139,3 +142,33 @@ def rate_multiplier():
|
|||||||
(NotificationHistory.rate_multiplier == None, literal_column("'1'")), # noqa
|
(NotificationHistory.rate_multiplier == None, literal_column("'1'")), # noqa
|
||||||
(NotificationHistory.rate_multiplier != None, NotificationHistory.rate_multiplier), # noqa
|
(NotificationHistory.rate_multiplier != None, NotificationHistory.rate_multiplier), # noqa
|
||||||
]), Integer())
|
]), Integer())
|
||||||
|
|
||||||
|
|
||||||
|
@statsd(namespace="dao")
|
||||||
|
def billing_letter_data_per_month_query(service_id, start_date, end_date):
|
||||||
|
month = get_london_month_from_utc_column(NotificationHistory.created_at)
|
||||||
|
crown = Service.query.get(service_id).crown
|
||||||
|
results = db.session.query(
|
||||||
|
month.label('month'),
|
||||||
|
func.sum(NotificationHistory.billable_units).label('billing_units'),
|
||||||
|
rate_multiplier().label('rate_multiplier'),
|
||||||
|
NotificationHistory.international,
|
||||||
|
NotificationHistory.notification_type,
|
||||||
|
cast(LetterRate.rate, Float()).label('rate')
|
||||||
|
).filter(
|
||||||
|
*billing_data_filter(LETTER_TYPE, start_date, end_date, service_id),
|
||||||
|
LetterRate.sheet_count == NotificationHistory.billable_units,
|
||||||
|
LetterRate.crown == crown,
|
||||||
|
NotificationHistory.created_at.between(LetterRate.start_date, end_date),
|
||||||
|
LetterRate.post_class == 'second'
|
||||||
|
).group_by(
|
||||||
|
NotificationHistory.notification_type,
|
||||||
|
month,
|
||||||
|
NotificationHistory.rate_multiplier,
|
||||||
|
NotificationHistory.international
|
||||||
|
).order_by(
|
||||||
|
month,
|
||||||
|
rate_multiplier()
|
||||||
|
).all()
|
||||||
|
|
||||||
|
return results
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
from _decimal import Decimal
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
@@ -7,13 +8,14 @@ from app.dao.date_util import get_financial_year
|
|||||||
from app.dao.notification_usage_dao import (
|
from app.dao.notification_usage_dao import (
|
||||||
get_rates_for_daterange,
|
get_rates_for_daterange,
|
||||||
get_billing_data_for_month,
|
get_billing_data_for_month,
|
||||||
get_monthly_billing_data
|
get_monthly_billing_data,
|
||||||
|
billing_letter_data_per_month_query
|
||||||
)
|
)
|
||||||
from app.models import (
|
from app.models import (
|
||||||
Rate,
|
Rate,
|
||||||
SMS_TYPE,
|
SMS_TYPE,
|
||||||
)
|
)
|
||||||
from tests.app.db import create_notification, create_rate
|
from tests.app.db import create_notification, create_rate, create_letter_rate, create_template, create_service
|
||||||
|
|
||||||
|
|
||||||
def test_get_rates_for_daterange(notify_db, notify_db_session):
|
def test_get_rates_for_daterange(notify_db, notify_db_session):
|
||||||
@@ -211,3 +213,24 @@ def test_get_monthly_billing_data_where_start_date_before_rate_returns_empty(
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert not results
|
assert not results
|
||||||
|
|
||||||
|
|
||||||
|
def test_billing_letter_data_per_month_query(
|
||||||
|
notify_db_session
|
||||||
|
):
|
||||||
|
rate = create_letter_rate()
|
||||||
|
service = create_service()
|
||||||
|
template = create_template(service=service, template_type='letter')
|
||||||
|
create_notification(template=template, billable_units=1, created_at=datetime(2017, 2, 1, 13, 21),
|
||||||
|
status='delivered')
|
||||||
|
create_notification(template=template, billable_units=1, created_at=datetime(2017, 2, 1, 13, 21),
|
||||||
|
status='delivered')
|
||||||
|
create_notification(template=template, billable_units=1, created_at=datetime(2017, 2, 1, 13, 21),
|
||||||
|
status='delivered')
|
||||||
|
|
||||||
|
results = billing_letter_data_per_month_query(service_id=service.id,
|
||||||
|
start_date=datetime(2017, 2, 1),
|
||||||
|
end_date=datetime(2017, 2, 28))
|
||||||
|
|
||||||
|
assert len(results) == 1
|
||||||
|
assert results[0].rate == Decimal('0.31')
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ from app.models import (
|
|||||||
SMS_TYPE,
|
SMS_TYPE,
|
||||||
KEY_TYPE_NORMAL,
|
KEY_TYPE_NORMAL,
|
||||||
AnnualBilling,
|
AnnualBilling,
|
||||||
|
LetterRate
|
||||||
)
|
)
|
||||||
from app.dao.users_dao import save_model_user
|
from app.dao.users_dao import save_model_user
|
||||||
from app.dao.notifications_dao import (
|
from app.dao.notifications_dao import (
|
||||||
@@ -68,7 +69,8 @@ def create_service(
|
|||||||
active=True,
|
active=True,
|
||||||
email_from=None,
|
email_from=None,
|
||||||
prefix_sms=True,
|
prefix_sms=True,
|
||||||
message_limit=1000
|
message_limit=1000,
|
||||||
|
crown=True
|
||||||
):
|
):
|
||||||
service = Service(
|
service = Service(
|
||||||
name=service_name,
|
name=service_name,
|
||||||
@@ -77,6 +79,7 @@ def create_service(
|
|||||||
email_from=email_from if email_from else service_name.lower().replace(' ', '.'),
|
email_from=email_from if email_from else service_name.lower().replace(' ', '.'),
|
||||||
created_by=user or create_user(email='{}@digital.cabinet-office.gov.uk'.format(uuid.uuid4())),
|
created_by=user or create_user(email='{}@digital.cabinet-office.gov.uk'.format(uuid.uuid4())),
|
||||||
prefix_sms=prefix_sms,
|
prefix_sms=prefix_sms,
|
||||||
|
crown=crown
|
||||||
)
|
)
|
||||||
|
|
||||||
dao_create_service(service, service.created_by, service_id, service_permissions=service_permissions)
|
dao_create_service(service, service.created_by, service_id, service_permissions=service_permissions)
|
||||||
@@ -453,3 +456,25 @@ def create_annual_billing(
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return annual_billing
|
return annual_billing
|
||||||
|
|
||||||
|
|
||||||
|
def create_letter_rate(
|
||||||
|
start_date=datetime(2017, 1,1, 00,00,00),
|
||||||
|
end_date=None,
|
||||||
|
sheet_count=1,
|
||||||
|
rate=0.31,
|
||||||
|
crown=True,
|
||||||
|
post_class='second'
|
||||||
|
):
|
||||||
|
rate = LetterRate(
|
||||||
|
start_date=start_date,
|
||||||
|
end_date=end_date,
|
||||||
|
sheet_count=sheet_count,
|
||||||
|
rate=rate,
|
||||||
|
crown=crown,
|
||||||
|
post_class=post_class
|
||||||
|
)
|
||||||
|
db.session.add(rate)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return rate
|
||||||
|
|||||||
Reference in New Issue
Block a user