mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-11 09:27:56 -04:00
calculate billable units when sending an sms
don't calculate it if we're in research mode * added tests to prove this * removed last code referring to content_char_count
This commit is contained in:
@@ -20,7 +20,7 @@ from notifications_utils.recipients import (
|
||||
)
|
||||
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
from notifications_utils.template import Template
|
||||
from notifications_utils.template import Template, get_sms_fragment_count
|
||||
from notifications_utils.renderers import HTMLEmail, PlainTextEmail, SMSMessage
|
||||
|
||||
from app.models import SMS_TYPE, EMAIL_TYPE, KEY_TYPE_TEST
|
||||
@@ -68,6 +68,7 @@ def send_sms_to_provider(self, service_id, notification_id):
|
||||
send_sms_response.apply_async(
|
||||
(provider.get_name(), str(notification_id), notification.to), queue='research-mode'
|
||||
)
|
||||
notification.billable_units = 0
|
||||
else:
|
||||
provider.send_sms(
|
||||
to=validate_and_format_phone_number(notification.to),
|
||||
@@ -75,6 +76,7 @@ def send_sms_to_provider(self, service_id, notification_id):
|
||||
reference=str(notification_id),
|
||||
sender=service.sms_sender
|
||||
)
|
||||
notification.billable_units = get_sms_fragment_count(template.replaced_content_count)
|
||||
|
||||
update_provider_stats(
|
||||
notification_id,
|
||||
@@ -84,8 +86,7 @@ def send_sms_to_provider(self, service_id, notification_id):
|
||||
)
|
||||
|
||||
notification.sent_at = datetime.utcnow()
|
||||
notification.sent_by = provider.get_name(),
|
||||
notification.content_char_count = template.replaced_content_count
|
||||
notification.sent_by = provider.get_name()
|
||||
notification.status = 'sending'
|
||||
dao_update_notification(notification)
|
||||
except SmsClientException as e:
|
||||
|
||||
@@ -29,17 +29,7 @@ def get_fragment_count(service_id):
|
||||
]
|
||||
|
||||
sms_count = db.session.query(
|
||||
func.sum(
|
||||
case(
|
||||
[
|
||||
(
|
||||
NotificationHistory.content_char_count <= 160,
|
||||
func.ceil(cast(NotificationHistory.content_char_count, Float) / 153)
|
||||
)
|
||||
],
|
||||
else_=1
|
||||
)
|
||||
)
|
||||
func.sum(NotificationHistory.billable_units)
|
||||
).filter(
|
||||
NotificationHistory.notification_type == SMS_TYPE,
|
||||
*shared_filters
|
||||
|
||||
@@ -377,7 +377,6 @@ class Notification(db.Model):
|
||||
api_key_id = db.Column(UUID(as_uuid=True), db.ForeignKey('api_keys.id'), index=True, unique=False)
|
||||
api_key = db.relationship('ApiKey')
|
||||
key_type = db.Column(db.String, db.ForeignKey('key_types.name'), index=True, unique=False, nullable=False)
|
||||
content_char_count = db.Column(db.Integer, nullable=True)
|
||||
billable_units = db.Column(db.Integer, nullable=False, default=0)
|
||||
notification_type = db.Column(notification_types, index=True, nullable=False)
|
||||
created_at = db.Column(
|
||||
@@ -428,7 +427,6 @@ class NotificationHistory(db.Model):
|
||||
api_key_id = db.Column(UUID(as_uuid=True), db.ForeignKey('api_keys.id'), index=True, unique=False)
|
||||
api_key = db.relationship('ApiKey')
|
||||
key_type = db.Column(db.String, db.ForeignKey('key_types.name'), index=True, unique=False, nullable=False)
|
||||
content_char_count = db.Column(db.Integer, nullable=True)
|
||||
billable_units = db.Column(db.Integer, nullable=False, default=0)
|
||||
notification_type = db.Column(notification_types, index=True, nullable=False)
|
||||
created_at = db.Column(db.DateTime, index=True, unique=False, nullable=False)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from flask import jsonify, current_app, Blueprint
|
||||
from flask import jsonify, Blueprint
|
||||
from apispec import APISpec
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ api_spec = APISpec(
|
||||
)
|
||||
|
||||
api_spec.definition('NotificationWithTemplateSchema', properties={
|
||||
"content_char_count": {
|
||||
"billable_units": {
|
||||
"format": "int32",
|
||||
"type": "integer"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user