mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 08:21:13 -05:00
Avoid call to database to get template in persist_notifications
This commit is contained in:
@@ -307,6 +307,7 @@ def save_letter(
|
|||||||
saved_notification = persist_notification(
|
saved_notification = persist_notification(
|
||||||
template_id=notification['template'],
|
template_id=notification['template'],
|
||||||
template_version=notification['template_version'],
|
template_version=notification['template_version'],
|
||||||
|
template_postage=template.postage,
|
||||||
recipient=recipient,
|
recipient=recipient,
|
||||||
service=service,
|
service=service,
|
||||||
personalisation=notification['personalisation'],
|
personalisation=notification['personalisation'],
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ def create_letter_notification(letter_data, template, api_key, status, reply_to_
|
|||||||
notification = persist_notification(
|
notification = persist_notification(
|
||||||
template_id=template.id,
|
template_id=template.id,
|
||||||
template_version=template.version,
|
template_version=template.version,
|
||||||
|
template_postage=template.postage,
|
||||||
# we only accept addresses_with_underscores from the API (from CSV we also accept dashes, spaces etc)
|
# we only accept addresses_with_underscores from the API (from CSV we also accept dashes, spaces etc)
|
||||||
recipient=letter_data['personalisation']['address_line_1'],
|
recipient=letter_data['personalisation']['address_line_1'],
|
||||||
service=template.service,
|
service=template.service,
|
||||||
|
|||||||
@@ -32,8 +32,6 @@ from app.dao.notifications_dao import (
|
|||||||
dao_created_scheduled_notification
|
dao_created_scheduled_notification
|
||||||
)
|
)
|
||||||
|
|
||||||
from app.dao.templates_dao import dao_get_template_by_id
|
|
||||||
|
|
||||||
from app.v2.errors import BadRequestError
|
from app.v2.errors import BadRequestError
|
||||||
from app.utils import (
|
from app.utils import (
|
||||||
cache_key_for_service_template_counter,
|
cache_key_for_service_template_counter,
|
||||||
@@ -76,7 +74,8 @@ def persist_notification(
|
|||||||
status=NOTIFICATION_CREATED,
|
status=NOTIFICATION_CREATED,
|
||||||
reply_to_text=None,
|
reply_to_text=None,
|
||||||
billable_units=None,
|
billable_units=None,
|
||||||
postage=None
|
postage=None,
|
||||||
|
template_postage=None
|
||||||
):
|
):
|
||||||
notification_created_at = created_at or datetime.utcnow()
|
notification_created_at = created_at or datetime.utcnow()
|
||||||
if not notification_id:
|
if not notification_id:
|
||||||
@@ -116,9 +115,8 @@ def persist_notification(
|
|||||||
if postage:
|
if postage:
|
||||||
notification.postage = postage
|
notification.postage = postage
|
||||||
else:
|
else:
|
||||||
template = dao_get_template_by_id(template_id, template_version)
|
if service.has_permission(CHOOSE_POSTAGE) and template_postage:
|
||||||
if service.has_permission(CHOOSE_POSTAGE) and template.postage:
|
notification.postage = template_postage
|
||||||
notification.postage = template.postage
|
|
||||||
else:
|
else:
|
||||||
notification.postage = service.postage
|
notification.postage = service.postage
|
||||||
|
|
||||||
|
|||||||
@@ -124,6 +124,7 @@ def send_notification(notification_type):
|
|||||||
simulated = simulated_recipient(notification_form['to'], notification_type)
|
simulated = simulated_recipient(notification_form['to'], notification_type)
|
||||||
notification_model = persist_notification(template_id=template.id,
|
notification_model = persist_notification(template_id=template.id,
|
||||||
template_version=template.version,
|
template_version=template.version,
|
||||||
|
template_postage=template.postage,
|
||||||
recipient=request.get_json()['to'],
|
recipient=request.get_json()['to'],
|
||||||
service=authenticated_service,
|
service=authenticated_service,
|
||||||
personalisation=notification_form.get('personalisation', None),
|
personalisation=notification_form.get('personalisation', None),
|
||||||
|
|||||||
@@ -77,6 +77,7 @@ def send_one_off_notification(service_id, post_data):
|
|||||||
notification = persist_notification(
|
notification = persist_notification(
|
||||||
template_id=template.id,
|
template_id=template.id,
|
||||||
template_version=template.version,
|
template_version=template.version,
|
||||||
|
template_postage=template.postage,
|
||||||
recipient=post_data['to'],
|
recipient=post_data['to'],
|
||||||
service=service,
|
service=service,
|
||||||
personalisation=personalisation,
|
personalisation=personalisation,
|
||||||
|
|||||||
@@ -504,6 +504,7 @@ def test_persist_letter_notification_finds_correct_postage(
|
|||||||
persist_notification(
|
persist_notification(
|
||||||
template_id=template.id,
|
template_id=template.id,
|
||||||
template_version=template.version,
|
template_version=template.version,
|
||||||
|
template_postage=template.postage,
|
||||||
recipient="Jane Doe, 10 Downing Street, London",
|
recipient="Jane Doe, 10 Downing Street, London",
|
||||||
service=service,
|
service=service,
|
||||||
personalisation=None,
|
personalisation=None,
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_sms(
|
|||||||
persist_mock.assert_called_once_with(
|
persist_mock.assert_called_once_with(
|
||||||
template_id=template.id,
|
template_id=template.id,
|
||||||
template_version=template.version,
|
template_version=template.version,
|
||||||
|
template_postage=None,
|
||||||
recipient=post_data['to'],
|
recipient=post_data['to'],
|
||||||
service=template.service,
|
service=template.service,
|
||||||
personalisation={'name': 'foo'},
|
personalisation={'name': 'foo'},
|
||||||
@@ -127,6 +128,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_email(
|
|||||||
persist_mock.assert_called_once_with(
|
persist_mock.assert_called_once_with(
|
||||||
template_id=template.id,
|
template_id=template.id,
|
||||||
template_version=template.version,
|
template_version=template.version,
|
||||||
|
template_postage=None,
|
||||||
recipient=post_data['to'],
|
recipient=post_data['to'],
|
||||||
service=template.service,
|
service=template.service,
|
||||||
personalisation={'name': 'foo'},
|
personalisation={'name': 'foo'},
|
||||||
@@ -153,6 +155,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_letter(
|
|||||||
template = create_template(
|
template = create_template(
|
||||||
service=service,
|
service=service,
|
||||||
template_type=LETTER_TYPE,
|
template_type=LETTER_TYPE,
|
||||||
|
postage='first',
|
||||||
subject="Test subject",
|
subject="Test subject",
|
||||||
content="Hello (( Name))\nYour thing is due soon",
|
content="Hello (( Name))\nYour thing is due soon",
|
||||||
)
|
)
|
||||||
@@ -174,6 +177,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_letter(
|
|||||||
persist_mock.assert_called_once_with(
|
persist_mock.assert_called_once_with(
|
||||||
template_id=template.id,
|
template_id=template.id,
|
||||||
template_version=template.version,
|
template_version=template.version,
|
||||||
|
template_postage='first',
|
||||||
recipient=post_data['to'],
|
recipient=post_data['to'],
|
||||||
service=template.service,
|
service=template.service,
|
||||||
personalisation=post_data['personalisation'],
|
personalisation=post_data['personalisation'],
|
||||||
|
|||||||
Reference in New Issue
Block a user