Ad a reference to the model

- used if 3rd party needs to record an ID for reconciliation purposes
This commit is contained in:
Martyn Inglis
2016-03-11 09:40:35 +00:00
parent 8d9b5b172b
commit 901d04605f
6 changed files with 94 additions and 10 deletions

View File

@@ -9,7 +9,8 @@ from app.dao.notifications_dao import (
dao_update_notification, dao_update_notification,
delete_failed_notifications_created_more_than_a_week_ago, delete_failed_notifications_created_more_than_a_week_ago,
delete_successful_notifications_created_more_than_a_day_ago, delete_successful_notifications_created_more_than_a_day_ago,
dao_get_notification_statistics_for_service_and_day dao_get_notification_statistics_for_service_and_day,
update_notification_reference_by_id
) )
from app.dao.jobs_dao import dao_update_job, dao_get_job_by_id from app.dao.jobs_dao import dao_update_job, dao_get_job_by_id
from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago
@@ -205,7 +206,7 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
client.send_sms( client.send_sms(
to=notification['to'], to=notification['to'],
content=template.replaced, content=template.replaced,
notification_id=notification_id reference=str(notification_id)
) )
except FiretextClientException as e: except FiretextClientException as e:
current_app.logger.error( current_app.logger.error(
@@ -273,12 +274,13 @@ def send_email(service_id, notification_id, subject, from_address, encrypted_not
values=notification.get('personalisation', {}) values=notification.get('personalisation', {})
) )
client.send_email( reference = client.send_email(
from_address, from_address,
notification['to'], notification['to'],
subject, subject,
template.replaced template.replaced
) )
update_notification_reference_by_id(notification_id, reference)
except AwsSesClientException as e: except AwsSesClientException as e:
current_app.logger.debug(e) current_app.logger.debug(e)
notification_db_object.status = 'failed' notification_db_object.status = 'failed'

View File

@@ -64,6 +64,7 @@ def update_job_sent_count(notification):
def dao_update_notification(notification): def dao_update_notification(notification):
notification.updated_at = datetime.utcnow()
db.session.add(notification) db.session.add(notification)
db.session.commit() db.session.commit()
@@ -90,6 +91,28 @@ def update_notification_status_by_to(to, status):
return count return count
def update_notification_status_by_reference(reference, status):
count = db.session.query(Notification).filter_by(
refernce=reference
).update({
Notification.status: status,
Notification.updated_at: datetime.utcnow()
})
db.session.commit()
return count
def update_notification_reference_by_id(id, reference):
count = db.session.query(Notification).filter_by(
id=id
).update({
Notification.reference: reference,
Notification.updated_at: datetime.utcnow()
})
db.session.commit()
return count
def get_notification_for_job(service_id, job_id, notification_id): def get_notification_for_job(service_id, job_id, notification_id):
return Notification.query.filter_by(service_id=service_id, job_id=job_id, id=notification_id).one() return Notification.query.filter_by(service_id=service_id, job_id=job_id, id=notification_id).one()

View File

@@ -265,6 +265,7 @@ class Notification(db.Model):
onupdate=datetime.datetime.now) onupdate=datetime.datetime.now)
status = db.Column( status = db.Column(
db.Enum(*NOTIFICATION_STATUS_TYPES, name='notification_status_types'), nullable=False, default='sent') db.Enum(*NOTIFICATION_STATUS_TYPES, name='notification_status_types'), nullable=False, default='sent')
reference = db.Column(db.String, nullable=True, index=True)
INVITED_USER_STATUS_TYPES = ['pending', 'accepted', 'cancelled'] INVITED_USER_STATUS_TYPES = ['pending', 'accepted', 'cancelled']

View File

@@ -0,0 +1,24 @@
"""empty message
Revision ID: 0040_add_reference
Revises: 0039_more_notification_states
Create Date: 2016-03-11 09:15:57.900192
"""
# revision identifiers, used by Alembic.
revision = '0040_add_reference'
down_revision = '0039_more_notification_states'
from alembic import op
import sqlalchemy as sa
def upgrade():
op.add_column('notifications', sa.Column('reference', sa.String(), nullable=True))
op.create_index(op.f('ix_notifications_reference'), 'notifications', ['reference'], unique=False)
def downgrade():
op.drop_index(op.f('ix_notifications_reference'), table_name='notifications')
op.drop_column('notifications', 'reference')

View File

@@ -267,7 +267,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
firetext_client.send_sms.assert_called_once_with( firetext_client.send_sms.assert_called_once_with(
to="+441234123123", to="+441234123123",
content="Sample service: Hello Jo", content="Sample service: Hello Jo",
notification_id=notification_id reference=str(notification_id)
) )
persisted_notification = notifications_dao.get_notification( persisted_notification = notifications_dao.get_notification(
sample_template_with_placeholders.service_id, notification_id sample_template_with_placeholders.service_id, notification_id
@@ -303,7 +303,7 @@ def test_should_send_sms_without_personalisation(sample_template, mocker):
firetext_client.send_sms.assert_called_once_with( firetext_client.send_sms.assert_called_once_with(
to="+441234123123", to="+441234123123",
content="Sample service: This is a template", content="Sample service: This is a template",
notification_id=notification_id reference=str(notification_id)
) )
@@ -332,7 +332,7 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif
firetext_client.send_sms.assert_called_once_with( firetext_client.send_sms.assert_called_once_with(
to="+441234123123", to="+441234123123",
content="Sample service: This is a template", content="Sample service: This is a template",
notification_id=notification_id reference=str(notification_id)
) )
@@ -413,7 +413,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
firetext_client.send_sms.assert_called_once_with( firetext_client.send_sms.assert_called_once_with(
to="+441234123123", to="+441234123123",
content="Sample service: This is a template", content="Sample service: This is a template",
notification_id=notification_id reference=str(notification_id)
) )
persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id) persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
assert persisted_notification.id == notification_id assert persisted_notification.id == notification_id
@@ -464,6 +464,31 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
assert persisted_notification.sent_by == 'ses' assert persisted_notification.sent_by == 'ses'
def test_should_use_email_template_and_persist_ses_reference(sample_email_template_with_placeholders, mocker):
notification = {
"template": sample_email_template_with_placeholders.id,
"to": "my_email@my_email.com",
"personalisation": {"name": "Jo"}
}
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.aws_ses_client.send_email', return_value='reference')
notification_id = uuid.uuid4()
now = datetime.utcnow()
send_email(
sample_email_template_with_placeholders.service_id,
notification_id,
'subject',
'email_from',
"encrypted-in-reality",
now.strftime(DATETIME_FORMAT)
)
persisted_notification = notifications_dao.get_notification(
sample_email_template_with_placeholders.service_id, notification_id
)
assert persisted_notification.reference == 'reference'
def test_should_use_email_template_and_persist_without_personalisation( def test_should_use_email_template_and_persist_without_personalisation(
sample_email_template, mocker sample_email_template, mocker
): ):
@@ -471,7 +496,7 @@ def test_should_use_email_template_and_persist_without_personalisation(
"template": sample_email_template.id, "template": sample_email_template.id,
"to": "my_email@my_email.com", "to": "my_email@my_email.com",
}) })
mocker.patch('app.aws_ses_client.send_email') mocker.patch('app.aws_ses_client.send_email', return_value="ref")
mocker.patch('app.aws_ses_client.get_name', return_value='ses') mocker.patch('app.aws_ses_client.get_name', return_value='ses')
notification_id = uuid.uuid4() notification_id = uuid.uuid4()
@@ -513,7 +538,8 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa
firetext_client.send_sms.assert_called_once_with( firetext_client.send_sms.assert_called_once_with(
to="+441234123123", to="+441234123123",
content="Sample service: This is a template", content="Sample service: This is a template",
notification_id=notification_id) reference=str(notification_id)
)
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id) persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
assert persisted_notification.id == notification_id assert persisted_notification.id == notification_id
assert persisted_notification.to == '+441234123123' assert persisted_notification.to == '+441234123123'

View File

@@ -18,12 +18,20 @@ from app.dao.notifications_dao import (
delete_failed_notifications_created_more_than_a_week_ago, delete_failed_notifications_created_more_than_a_week_ago,
dao_get_notification_statistics_for_service_and_day, dao_get_notification_statistics_for_service_and_day,
update_notification_status_by_id, update_notification_status_by_id,
update_notification_status_by_to update_notification_status_by_to,
update_notification_reference_by_id
) )
from tests.app.conftest import sample_job from tests.app.conftest import sample_job
from tests.app.conftest import sample_notification from tests.app.conftest import sample_notification
def test_should_by_able_to_update_reference_by_id(sample_notification):
assert not Notification.query.get(sample_notification.id).reference
count = update_notification_reference_by_id(sample_notification.id, 'reference')
assert count == 1
assert Notification.query.get(sample_notification.id).reference == 'reference'
def test_should_by_able_to_update_status_by_id(sample_notification): def test_should_by_able_to_update_status_by_id(sample_notification):
assert Notification.query.get(sample_notification.id).status == 'sent' assert Notification.query.get(sample_notification.id).status == 'sent'
count = update_notification_status_by_id(sample_notification.id, 'delivered') count = update_notification_status_by_id(sample_notification.id, 'delivered')