Fix billable units

There was a situation where the SMS was sent, the request timed out we got a 503 so the notification was put on the retry queue. However, the provider got the notification and sent the message. This means we didn't set the billable_units on the notification and the service will not be billed.
This PR is to fix the notifications that we can accurrately update.
This commit is contained in:
Rebecca Law
2019-05-07 16:31:12 +01:00
parent b89ab0cd87
commit ef2bbbe683

View File

@@ -9,6 +9,7 @@ import flask
import itertools
from click_datetime import Datetime as click_dt
from flask import current_app, json
from notifications_utils.template import SMSMessageTemplate
from psycopg2._psycopg import IntegrityError
from sqlalchemy.orm.exc import NoResultFound
from notifications_utils.statsd_decorators import statsd
@@ -26,6 +27,7 @@ from app.dao.fact_billing_dao import (
get_service_ids_that_need_billing_populated,
update_fact_billing,
)
from app.dao.notifications_dao import dao_update_notification
from app.dao.organisation_dao import dao_get_organisation_by_email_address, dao_add_service_to_organisation
from app.dao.provider_rates_dao import create_provider_rates as dao_create_provider_rates
@@ -36,8 +38,9 @@ from app.dao.services_dao import (
dao_fetch_service_by_id,
dao_update_service
)
from app.dao.templates_dao import dao_get_template_by_id
from app.dao.users_dao import delete_model_user, delete_user_verify_codes, get_user_by_email
from app.models import PROVIDERS, User, Notification, Organisation, Domain, Service
from app.models import PROVIDERS, User, Notification, Organisation, Domain, Service, KEY_TYPE_NORMAL
from app.performance_platform.processing_time import send_processing_time_for_start_and_end
from app.utils import get_london_midnight_in_utc, get_midnight_for_day_before
@@ -842,3 +845,31 @@ def populate_go_live(file_name):
service.go_live_user = go_live_user
service.go_live_at = go_live_date
dao_update_service(service)
@notify_command(name='fix-billable-units')
def fix_billable_units():
query = Notification.query.filter(
Notification.sent_at == None, # noqa
Notification.billable_units == 0,
Notification.key_type == KEY_TYPE_NORMAL
)
for notification in query.all():
template_model = dao_get_template_by_id(notification.template_id, notification.template_version)
template = SMSMessageTemplate(
template_model.__dict__,
values=notification.personalisation,
prefix=notification.service.name,
show_prefix=notification.service.prefix_sms,
)
print("Updating notification: {} with {} billable_units".format(notification.id, template.fragment_count))
Notification.query.filter(
Notification.id == notification.id
).update(
{"billable_units": template.fragment_count}
)
print("End fix_billable_units")