diff --git a/app/commands.py b/app/commands.py index 2a2cee9f6..e194ec453 100644 --- a/app/commands.py +++ b/app/commands.py @@ -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")