From b4a528f1571d3349e27689804c40227ef23bcba7 Mon Sep 17 00:00:00 2001 From: Marisa Hoenig Date: Wed, 14 Oct 2020 11:08:36 -0400 Subject: [PATCH] [#190] persist recipient identifiers to table --- app/dao/recipient_identifiers_dao.py | 17 ++++++++++++ app/models.py | 4 --- app/v2/notifications/post_notifications.py | 23 ++++++++++------ .../app/dao/test_recipient_identifiers_dao.py | 27 +++++++++++++++++++ .../notifications/test_post_notifications.py | 8 +++--- 5 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 app/dao/recipient_identifiers_dao.py create mode 100644 tests/app/dao/test_recipient_identifiers_dao.py diff --git a/app/dao/recipient_identifiers_dao.py b/app/dao/recipient_identifiers_dao.py new file mode 100644 index 0000000000..737f279f16 --- /dev/null +++ b/app/dao/recipient_identifiers_dao.py @@ -0,0 +1,17 @@ +from notifications_utils.statsd_decorators import statsd + +from app import db +from app.dao.dao_utils import transactional +from app.models import RecipientIdentifiers + + +@statsd(namespace="dao") +@transactional +def persist_recipient_identifiers(notification_id, va_identifier_type, va_identifier_value): + recipient_identifiers = RecipientIdentifiers( + notification_id=notification_id, + va_identifier_type=va_identifier_type, + va_identifier_value=va_identifier_value + ) + db.session.add(recipient_identifiers) + db.session.commit() diff --git a/app/models.py b/app/models.py index 02bced4b81..34a0bf1cf7 100644 --- a/app/models.py +++ b/app/models.py @@ -1746,10 +1746,6 @@ class RecipientIdentifiers(db.Model): default=VA_PROFILE_ID) va_identifier_value = db.Column(db.String, primary_key=True, nullable=False) - def __init__(self, va_identifier_type, va_identifier_value): - self.va_identifier_type = va_identifier_type - self.va_identifier_value = va_identifier_value - INVITE_PENDING = 'pending' INVITE_ACCEPTED = 'accepted' diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index 958135b44d..cb7fb9e4e2 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -11,6 +11,7 @@ from app.clients.document_download import DocumentDownloadError from app.config import QueueNames, TaskNames from app.dao.notifications_dao import update_notification_status_by_reference +from app.dao.recipient_identifiers_dao import persist_recipient_identifiers from app.dao.templates_dao import get_precompiled_letter_template from app.letters.utils import upload_letter_pdf from app.models import ( @@ -216,19 +217,25 @@ def process_sms_or_email_notification(*, form, notification_type, api_key, templ reply_to_text=reply_to_text ) + if 'va_identifier' in form: + persist_recipient_identifiers(notification.id, form['va_identifier']['id_type'], form['va_identifier']['value']) + scheduled_for = form.get("scheduled_for", None) if scheduled_for: persist_scheduled_notification(notification.id, form["scheduled_for"]) else: - if not simulated: - queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None - send_notification_to_queue( - notification=notification, - research_mode=service.research_mode, - queue=queue_name - ) - else: + if simulated: current_app.logger.debug("POST simulated notification for id: {}".format(notification.id)) + else: + if notification.to: + queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None + send_notification_to_queue( + notification=notification, + research_mode=service.research_mode, + queue=queue_name + ) + else: + current_app.logger.info('No recipient. Must get contact info from a VA system.') return notification diff --git a/tests/app/dao/test_recipient_identifiers_dao.py b/tests/app/dao/test_recipient_identifiers_dao.py new file mode 100644 index 0000000000..f2526f5a07 --- /dev/null +++ b/tests/app/dao/test_recipient_identifiers_dao.py @@ -0,0 +1,27 @@ +from app.dao.recipient_identifiers_dao import persist_recipient_identifiers +from app.models import RecipientIdentifiers, VA_PROFILE_ID + +from tests.app.db import ( + create_notification +) + + +def test_should_add_recipient_identifiers_to_recipient_identifiers_table(notify_api, sample_job, sample_email_template): + notification = create_notification(to_field=None, job=sample_job, template=sample_email_template) + notification_id = notification.id + va_identifier_type = VA_PROFILE_ID + va_identifier_value = "foo" + + persist_recipient_identifiers(notification_id, va_identifier_type, va_identifier_value) + assert RecipientIdentifiers.query.count() == 1 + assert RecipientIdentifiers.query.get((notification_id, va_identifier_type, va_identifier_value))\ + .notification_id == notification_id + assert RecipientIdentifiers.query.get((notification_id, va_identifier_type, va_identifier_value))\ + .va_identifier_type == va_identifier_type + assert RecipientIdentifiers.query.get((notification_id, va_identifier_type, va_identifier_value)) \ + .va_identifier_value == va_identifier_value + + +# def test_should_add_recipient_identifiers_to_recipient_identifiers_history(): + +# def test_should_have_access_to_recipient_identifiers_dict_from_notification(): diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index c3c3e5b200..a9f3eb308d 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -12,7 +12,8 @@ SMS_TYPE, UPLOAD_DOCUMENT, INTERNATIONAL_SMS_TYPE, - VA_PROFILE_ID) + VA_PROFILE_ID, + RecipientIdentifiers) from flask import json, current_app from app.models import Notification @@ -366,6 +367,7 @@ def test_should_not_persist_or_send_notification_if_simulated_recipient( apply_async.assert_not_called() assert json.loads(response.get_data(as_text=True))["id"] assert Notification.query.count() == 0 + assert RecipientIdentifiers.query.count() == 0 @pytest.mark.parametrize('notification_type', [ @@ -379,7 +381,7 @@ def test_should_persist_notification_without_recipient( notification_type, sample_email_template_with_placeholders, mocker): - apply_async = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type)) + mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type)) data = { "va_identifier": { "id_type": VA_PROFILE_ID, @@ -398,7 +400,7 @@ def test_should_persist_notification_without_recipient( assert response.status_code == 201 assert json.loads(response.get_data(as_text=True))["id"] assert Notification.query.count() == 1 - apply_async.assert_called() + assert RecipientIdentifiers.query.count() == 1 @pytest.mark.parametrize("notification_type, key_send_to, send_to",