diff --git a/app/celery/reporting_tasks.py b/app/celery/reporting_tasks.py index 960264fc6..f3cf640cf 100644 --- a/app/celery/reporting_tasks.py +++ b/app/celery/reporting_tasks.py @@ -84,15 +84,18 @@ def create_nightly_billing(day_start=None): for data in transit_data: update_count = FactBilling.query.filter( - FactBilling.bst_date == process_day, + FactBilling.bst_date == datetime.date(process_day), FactBilling.template_id == data.template_id, + FactBilling.service_id == data.service_id, FactBilling.provider == data.sent_by, # This could be zero - this is a bug that needs to be fixed. FactBilling.rate_multiplier == data.rate_multiplier, FactBilling.notification_type == data.notification_type, + FactBilling.international == data.international ).update( {"notifications_sent": data.notifications_sent, "billable_units": data.billable_units}, synchronize_session=False) + if update_count == 0: billing_record = FactBilling( bst_date=process_day, diff --git a/tests/app/celery/test_reporting_tasks.py b/tests/app/celery/test_reporting_tasks.py index b9ce112ec..9bf2daa99 100644 --- a/tests/app/celery/test_reporting_tasks.py +++ b/tests/app/celery/test_reporting_tasks.py @@ -404,3 +404,40 @@ def test_create_nightly_billing_use_BST( assert len(records) == 2 assert records[0].bst_date == date(2018, 3, 25) assert records[-1].bst_date == date(2018, 3, 26) + + +@freeze_time('2018-01-15T03:30:00') +def test_create_nightly_billing_update_when_record_exists( + notify_db, + notify_db_session, + sample_service, + sample_template, + mocker): + + mocker.patch('app.celery.reporting_tasks.get_rate', side_effect=mocker_get_rate) + + sample_notification( + notify_db, + notify_db_session, + created_at=datetime.now() - timedelta(days=1), + service=sample_service, + template=sample_template, + status='delivered', + sent_by=None, + international=False, + rate_multiplier=1.0, + billable_units=1, + ) + + records = FactBilling.query.all() + assert len(records) == 0 + + create_nightly_billing() + records = FactBilling.query.order_by(FactBilling.bst_date).all() + + assert len(records) == 1 + assert records[0].bst_date == date(2018, 1, 14) + + # run again, make sure create_nightly_billing() updates with no error + create_nightly_billing() + assert len(records) == 1