Get the sms sender from the notificaiton_sms_sender mapping table if that does not exist get the default sms sender to pass on to the sms provider.

This commit is contained in:
Rebecca Law
2017-10-30 14:55:44 +00:00
parent 4eec11b633
commit 0887910b1b
5 changed files with 69 additions and 9 deletions

View File

@@ -44,7 +44,9 @@ from app.models import (
NOTIFICATION_TEMPORARY_FAILURE, NOTIFICATION_TEMPORARY_FAILURE,
NOTIFICATION_PERMANENT_FAILURE, NOTIFICATION_PERMANENT_FAILURE,
NOTIFICATION_SENT, NOTIFICATION_SENT,
NotificationSmsSender) NotificationSmsSender,
ServiceSmsSender
)
from app.dao.dao_utils import transactional from app.dao.dao_utils import transactional
from app.statsd_decorators import statsd from app.statsd_decorators import statsd
@@ -659,3 +661,14 @@ def dao_create_notification_sms_sender_mapping(notification_id, sms_sender_id):
service_sms_sender_id=sms_sender_id service_sms_sender_id=sms_sender_id
) )
db.session.add(notification_to_sms_sender) db.session.add(notification_to_sms_sender)
def dao_get_notification_sms_sender_mapping(notification_id):
sms_sender = ServiceSmsSender.query.join(
NotificationSmsSender
).filter(
NotificationSmsSender.notification_id == notification_id
).first()
if sms_sender:
return sms_sender.sms_sender

View File

@@ -11,8 +11,8 @@ from notifications_utils.template import HTMLEmailTemplate, PlainTextEmailTempla
from app import clients, statsd_client, create_uuid from app import clients, statsd_client, create_uuid
from app.dao.notifications_dao import ( from app.dao.notifications_dao import (
dao_update_notification, dao_update_notification,
dao_get_notification_email_reply_for_notification dao_get_notification_email_reply_for_notification,
) dao_get_notification_sms_sender_mapping)
from app.dao.provider_details_dao import ( from app.dao.provider_details_dao import (
get_provider_details_by_notification_type, get_provider_details_by_notification_type,
dao_toggle_sms_provider dao_toggle_sms_provider
@@ -69,11 +69,15 @@ def send_sms_to_provider(notification):
raise raise
else: else:
try: try:
sms_sender = dao_get_notification_sms_sender_mapping(notification.id)
if not sms_sender:
sms_sender = service.get_default_sms_sender()
provider.send_sms( provider.send_sms(
to=validate_and_format_phone_number(notification.to, international=notification.international), to=validate_and_format_phone_number(notification.to, international=notification.international),
content=str(template), content=str(template),
reference=str(notification.id), reference=str(notification.id),
sender=service.get_default_sms_sender() sender=sms_sender
) )
except Exception as e: except Exception as e:
dao_toggle_sms_provider(provider.name) dao_toggle_sms_provider(provider.name)

View File

@@ -52,8 +52,8 @@ from app.dao.notifications_dao import (
update_notification_status_by_reference, update_notification_status_by_reference,
dao_get_last_notification_added_for_job_id, dao_get_last_notification_added_for_job_id,
dao_update_notifications_by_reference, dao_update_notifications_by_reference,
dao_create_notification_sms_sender_mapping dao_create_notification_sms_sender_mapping,
) dao_get_notification_sms_sender_mapping)
from app.dao.services_dao import dao_update_service from app.dao.services_dao import dao_update_service
from tests.app.db import ( from tests.app.db import (
@@ -2112,3 +2112,16 @@ def test_dao_create_notification_sms_sender_mapping(sample_notification):
assert len(notification_to_senders) == 1 assert len(notification_to_senders) == 1
assert notification_to_senders[0].notification_id == sample_notification.id assert notification_to_senders[0].notification_id == sample_notification.id
assert notification_to_senders[0].service_sms_sender_id == sms_sender.id assert notification_to_senders[0].service_sms_sender_id == sms_sender.id
def test_dao_get_notification_sms_sender_mapping(sample_notification):
sms_sender = create_service_sms_sender(service=sample_notification.service, sms_sender='123456')
dao_create_notification_sms_sender_mapping(notification_id=sample_notification.id,
sms_sender_id=sms_sender.id)
notification_to_sender = dao_get_notification_sms_sender_mapping(sample_notification.id)
assert notification_to_sender == '123456'
def test_dao_get_notification_sms_sender_mapping_returns_none(sample_notification):
notification_to_sender = dao_get_notification_sms_sender_mapping(sample_notification.id)
assert not notification_to_sender

View File

@@ -29,7 +29,11 @@ from app.models import (
KEY_TYPE_NORMAL KEY_TYPE_NORMAL
) )
from app.dao.users_dao import save_model_user from app.dao.users_dao import save_model_user
from app.dao.notifications_dao import dao_create_notification, dao_created_scheduled_notification from app.dao.notifications_dao import (
dao_create_notification,
dao_created_scheduled_notification,
dao_create_notification_sms_sender_mapping
)
from app.dao.templates_dao import dao_create_template from app.dao.templates_dao import dao_create_template
from app.dao.services_dao import dao_create_service from app.dao.services_dao import dao_create_service
from app.dao.service_permissions_dao import dao_add_service_permission from app.dao.service_permissions_dao import dao_add_service_permission
@@ -128,6 +132,7 @@ def create_notification(
scheduled_for=None, scheduled_for=None,
normalised_to=None, normalised_to=None,
one_off=False, one_off=False,
sms_sender_id=None
): ):
if created_at is None: if created_at is None:
created_at = datetime.utcnow() created_at = datetime.utcnow()
@@ -184,6 +189,9 @@ def create_notification(
if status != 'created': if status != 'created':
scheduled_notification.pending = False scheduled_notification.pending = False
dao_created_scheduled_notification(scheduled_notification) dao_created_scheduled_notification(scheduled_notification)
if sms_sender_id:
dao_create_notification_sms_sender_mapping(notification_id=notification.id,
sms_sender_id=sms_sender_id)
return notification return notification

View File

@@ -32,8 +32,8 @@ from tests.app.db import (
create_notification, create_notification,
create_inbound_number, create_inbound_number,
create_reply_to_email, create_reply_to_email,
create_reply_to_email_for_notification create_reply_to_email_for_notification,
) create_service_sms_sender)
def test_should_return_highest_priority_active_provider(restore_provider_details): def test_should_return_highest_priority_active_provider(restore_provider_details):
@@ -343,6 +343,28 @@ def test_should_send_sms_with_downgraded_content(notify_db_session, mocker):
) )
def test_send_sms_should_use_service_sms_sender(
sample_service,
sample_template,
mocker):
mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456', is_default=False)
db_notification = create_notification(template=sample_template, sms_sender_id=sms_sender.id)
send_to_providers.send_sms_to_provider(
db_notification,
)
app.mmg_client.send_sms.assert_called_once_with(
to=ANY,
content=ANY,
reference=ANY,
sender=sms_sender.sms_sender
)
@pytest.mark.parametrize('research_mode,key_type', [ @pytest.mark.parametrize('research_mode,key_type', [
(True, KEY_TYPE_NORMAL), (True, KEY_TYPE_NORMAL),
(False, KEY_TYPE_TEST) (False, KEY_TYPE_TEST)