Skip to content

Commit

Permalink
[#190] persist recipient identifiers to table
Browse files Browse the repository at this point in the history
  • Loading branch information
marisahoenig committed Oct 14, 2020
1 parent 766fe2e commit b4a528f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
17 changes: 17 additions & 0 deletions app/dao/recipient_identifiers_dao.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 0 additions & 4 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
23 changes: 15 additions & 8 deletions app/v2/notifications/post_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions tests/app/dao/test_recipient_identifiers_dao.py
Original file line number Diff line number Diff line change
@@ -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():
8 changes: 5 additions & 3 deletions tests/app/v2/notifications/test_post_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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', [
Expand All @@ -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,
Expand All @@ -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",
Expand Down

0 comments on commit b4a528f

Please sign in to comment.