mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-04 17:38:26 -04:00
Merge pull request #2661 from alphagov/fix-billing-query
Fix query to populate ft_billing table.
This commit is contained in:
@@ -336,25 +336,20 @@ def _query_for_billing_data(table, notification_type, start_date, end_date, serv
|
|||||||
EMAIL_TYPE: NOTIFICATION_STATUS_TYPES_BILLABLE,
|
EMAIL_TYPE: NOTIFICATION_STATUS_TYPES_BILLABLE,
|
||||||
LETTER_TYPE: NOTIFICATION_STATUS_TYPES_BILLABLE_FOR_LETTERS
|
LETTER_TYPE: NOTIFICATION_STATUS_TYPES_BILLABLE_FOR_LETTERS
|
||||||
}
|
}
|
||||||
|
sent_by_func = func.coalesce(table.sent_by, case(
|
||||||
|
[(table.notification_type == 'letter', 'dvla'),
|
||||||
|
(table.notification_type == 'sms', 'unknown'),
|
||||||
|
(table.notification_type == 'email', 'ses')]), )
|
||||||
|
letter_page_count = case([(table.notification_type == 'letter', table.billable_units), ])
|
||||||
|
|
||||||
query = db.session.query(
|
query = db.session.query(
|
||||||
table.template_id,
|
table.template_id,
|
||||||
table.service_id,
|
table.service_id,
|
||||||
table.notification_type,
|
table.notification_type,
|
||||||
func.coalesce(table.sent_by,
|
sent_by_func.label('sent_by'),
|
||||||
case(
|
|
||||||
[
|
|
||||||
(table.notification_type == 'letter', 'dvla'),
|
|
||||||
(table.notification_type == 'sms', 'unknown'),
|
|
||||||
(table.notification_type == 'email', 'ses')
|
|
||||||
]),
|
|
||||||
).label('sent_by'),
|
|
||||||
func.coalesce(table.rate_multiplier, 1).cast(Integer).label('rate_multiplier'),
|
func.coalesce(table.rate_multiplier, 1).cast(Integer).label('rate_multiplier'),
|
||||||
func.coalesce(table.international, False).label('international'),
|
func.coalesce(table.international, False).label('international'),
|
||||||
case(
|
letter_page_count.label('letter_page_count'),
|
||||||
[
|
|
||||||
(table.notification_type == 'letter', table.billable_units),
|
|
||||||
]
|
|
||||||
).label('letter_page_count'),
|
|
||||||
func.sum(table.billable_units).label('billable_units'),
|
func.sum(table.billable_units).label('billable_units'),
|
||||||
func.count().label('notifications_sent'),
|
func.count().label('notifications_sent'),
|
||||||
Service.crown,
|
Service.crown,
|
||||||
@@ -370,8 +365,8 @@ def _query_for_billing_data(table, notification_type, start_date, end_date, serv
|
|||||||
table.template_id,
|
table.template_id,
|
||||||
table.service_id,
|
table.service_id,
|
||||||
table.notification_type,
|
table.notification_type,
|
||||||
'sent_by',
|
sent_by_func,
|
||||||
'letter_page_count',
|
letter_page_count,
|
||||||
table.rate_multiplier,
|
table.rate_multiplier,
|
||||||
table.international,
|
table.international,
|
||||||
Service.crown,
|
Service.crown,
|
||||||
|
|||||||
@@ -221,6 +221,34 @@ def test_fetch_billing_data_for_day_groups_by_postage(notify_db_session):
|
|||||||
assert len(results) == 3
|
assert len(results) == 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_fetch_billing_data_for_day_groups_by_sent_by(notify_db_session):
|
||||||
|
service = create_service()
|
||||||
|
letter_template = create_template(service=service, template_type='letter')
|
||||||
|
email_template = create_template(service=service, template_type='email')
|
||||||
|
create_notification(template=letter_template, status='delivered', postage='second', sent_by='dvla')
|
||||||
|
create_notification(template=letter_template, status='delivered', postage='second', sent_by='dvla')
|
||||||
|
create_notification(template=letter_template, status='delivered', postage='second', sent_by=None)
|
||||||
|
create_notification(template=email_template, status='delivered')
|
||||||
|
|
||||||
|
today = convert_utc_to_bst(datetime.utcnow())
|
||||||
|
results = fetch_billing_data_for_day(today)
|
||||||
|
assert len(results) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_fetch_billing_data_for_day_groups_by_page_count(notify_db_session):
|
||||||
|
service = create_service()
|
||||||
|
letter_template = create_template(service=service, template_type='letter')
|
||||||
|
email_template = create_template(service=service, template_type='email')
|
||||||
|
create_notification(template=letter_template, status='delivered', postage='second', billable_units=1)
|
||||||
|
create_notification(template=letter_template, status='delivered', postage='second', billable_units=1)
|
||||||
|
create_notification(template=letter_template, status='delivered', postage='second', billable_units=2)
|
||||||
|
create_notification(template=email_template, status='delivered')
|
||||||
|
|
||||||
|
today = convert_utc_to_bst(datetime.utcnow())
|
||||||
|
results = fetch_billing_data_for_day(today)
|
||||||
|
assert len(results) == 3
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_billing_data_for_day_sets_postage_for_emails_and_sms_to_none(notify_db_session):
|
def test_fetch_billing_data_for_day_sets_postage_for_emails_and_sms_to_none(notify_db_session):
|
||||||
service = create_service()
|
service = create_service()
|
||||||
sms_template = create_template(service=service, template_type='sms')
|
sms_template = create_template(service=service, template_type='sms')
|
||||||
|
|||||||
Reference in New Issue
Block a user