mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 00:11:16 -05:00
Add service_email_reply_to DAO with an upsert method
This commit is contained in:
32
app/dao/service_email_reply_to_dao.py
Normal file
32
app/dao/service_email_reply_to_dao.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
from app import db
|
||||||
|
from app.dao.dao_utils import transactional
|
||||||
|
from app.models import ServiceEmailReplyTo
|
||||||
|
|
||||||
|
|
||||||
|
def create_or_update_email_reply_to(service_id, email_address):
|
||||||
|
reply_to = dao_get_reply_to_by_service_id(service_id)
|
||||||
|
if reply_to:
|
||||||
|
reply_to.email_address = email_address
|
||||||
|
dao_update_reply_to_email(reply_to)
|
||||||
|
else:
|
||||||
|
reply_to = ServiceEmailReplyTo(service_id=service_id, email_address=email_address)
|
||||||
|
dao_create_reply_to_email_address(reply_to)
|
||||||
|
|
||||||
|
|
||||||
|
@transactional
|
||||||
|
def dao_create_reply_to_email_address(reply_to_email):
|
||||||
|
db.session.add(reply_to_email)
|
||||||
|
|
||||||
|
|
||||||
|
def dao_get_reply_to_by_service_id(service_id):
|
||||||
|
reply_to = db.session.query(
|
||||||
|
ServiceEmailReplyTo
|
||||||
|
).filter(
|
||||||
|
ServiceEmailReplyTo.service_id == service_id
|
||||||
|
).first()
|
||||||
|
return reply_to
|
||||||
|
|
||||||
|
|
||||||
|
@transactional
|
||||||
|
def dao_update_reply_to_email(reply_to):
|
||||||
|
db.session.add(reply_to)
|
||||||
41
tests/app/dao/test_service_email_reply_to_dao.py
Normal file
41
tests/app/dao/test_service_email_reply_to_dao.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
from app.dao.service_email_reply_to_dao import (
|
||||||
|
create_or_update_email_reply_to,
|
||||||
|
dao_get_reply_to_by_service_id
|
||||||
|
)
|
||||||
|
from app.models import ServiceEmailReplyTo
|
||||||
|
from tests.app.db import create_reply_to_email, create_service
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_or_update_email_reply_to_does_not_create_another_entry(notify_db_session):
|
||||||
|
service = create_service()
|
||||||
|
create_reply_to_email(service, 'test@mail.com')
|
||||||
|
|
||||||
|
create_or_update_email_reply_to(service.id, 'different@mail.com')
|
||||||
|
|
||||||
|
reply_to = dao_get_reply_to_by_service_id(service.id)
|
||||||
|
|
||||||
|
assert ServiceEmailReplyTo.query.count() == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_or_update_email_reply_to_updates_existing_entry(notify_db_session):
|
||||||
|
service = create_service()
|
||||||
|
create_reply_to_email(service, 'test@mail.com')
|
||||||
|
|
||||||
|
create_or_update_email_reply_to(service.id, 'different@mail.com')
|
||||||
|
|
||||||
|
reply_to = dao_get_reply_to_by_service_id(service.id)
|
||||||
|
|
||||||
|
assert reply_to.service.id == service.id
|
||||||
|
assert reply_to.email_address == 'different@mail.com'
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_or_update_email_reply_to_creates_new_entry(notify_db_session):
|
||||||
|
service = create_service()
|
||||||
|
|
||||||
|
create_or_update_email_reply_to(service.id, 'test@mail.com')
|
||||||
|
|
||||||
|
reply_to = dao_get_reply_to_by_service_id(service.id)
|
||||||
|
|
||||||
|
assert ServiceEmailReplyTo.query.count() == 1
|
||||||
|
assert reply_to.service.id == service.id
|
||||||
|
assert reply_to.email_address == 'test@mail.com'
|
||||||
@@ -25,7 +25,9 @@ from app.models import (
|
|||||||
SMS_TYPE,
|
SMS_TYPE,
|
||||||
INBOUND_SMS_TYPE,
|
INBOUND_SMS_TYPE,
|
||||||
KEY_TYPE_NORMAL,
|
KEY_TYPE_NORMAL,
|
||||||
ServiceInboundApi)
|
ServiceInboundApi,
|
||||||
|
ServiceEmailReplyTo
|
||||||
|
)
|
||||||
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
|
||||||
from app.dao.templates_dao import dao_create_template
|
from app.dao.templates_dao import dao_create_template
|
||||||
@@ -327,3 +329,19 @@ def create_monthly_billing_entry(
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return entry
|
return entry
|
||||||
|
|
||||||
|
|
||||||
|
def create_reply_to_email(
|
||||||
|
service,
|
||||||
|
email_address
|
||||||
|
):
|
||||||
|
data = {
|
||||||
|
'service': service,
|
||||||
|
'email_address': email_address,
|
||||||
|
}
|
||||||
|
reply_to = ServiceEmailReplyTo(**data)
|
||||||
|
|
||||||
|
db.session.add(reply_to)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return reply_to
|
||||||
|
|||||||
Reference in New Issue
Block a user