mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-06 03:02:50 -05:00
Add get letter contact by id endpoint
Added the GET /<uuid:service_id>/letter-contact/<uuid:letter_contact_id> endpoint for returning a letter contact for a service with the letter contact id Added the DAO for returning a letter contact by id
This commit is contained in:
@@ -19,6 +19,16 @@ def dao_get_letter_contacts_by_service_id(service_id):
|
||||
return letter_contacts
|
||||
|
||||
|
||||
def dao_get_letter_contact_by_id(service_id, letter_contact_id):
|
||||
letter_contact = db.session.query(
|
||||
ServiceLetterContact
|
||||
).filter(
|
||||
ServiceLetterContact.service_id == service_id,
|
||||
ServiceLetterContact.id == letter_contact_id
|
||||
).one()
|
||||
return letter_contact
|
||||
|
||||
|
||||
def create_or_update_letter_contact(service_id, contact_block):
|
||||
letter_contacts = dao_get_letter_contacts_by_service_id(service_id)
|
||||
if len(letter_contacts) == 0:
|
||||
|
||||
@@ -53,7 +53,11 @@ from app.dao.service_email_reply_to_dao import (
|
||||
dao_get_reply_to_by_service_id,
|
||||
update_reply_to_email_address
|
||||
)
|
||||
from app.dao.service_letter_contact_dao import dao_get_letter_contacts_by_service_id, create_or_update_letter_contact
|
||||
from app.dao.service_letter_contact_dao import (
|
||||
dao_get_letter_contacts_by_service_id,
|
||||
create_or_update_letter_contact,
|
||||
dao_get_letter_contact_by_id
|
||||
)
|
||||
from app.dao.provider_statistics_dao import get_fragment_count
|
||||
from app.dao.users_dao import get_user_by_id
|
||||
from app.errors import (
|
||||
@@ -577,6 +581,12 @@ def get_letter_contacts(service_id):
|
||||
return jsonify([i.serialize() for i in result]), 200
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/letter-contact/<uuid:letter_contact_id>', methods=["GET"])
|
||||
def get_letter_contact_by_id(service_id, letter_contact_id):
|
||||
result = dao_get_letter_contact_by_id(service_id=service_id, letter_contact_id=letter_contact_id)
|
||||
return jsonify(result.serialize()), 200
|
||||
|
||||
|
||||
@service_blueprint.route('/unique', methods=["GET"])
|
||||
def is_service_name_unique():
|
||||
name, email_from = check_request_args(request)
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import uuid
|
||||
import pytest
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app.dao.service_letter_contact_dao import (
|
||||
add_letter_contact_for_service,
|
||||
create_or_update_letter_contact,
|
||||
dao_get_letter_contacts_by_service_id,
|
||||
dao_get_letter_contact_by_id,
|
||||
update_letter_contact
|
||||
)
|
||||
from app.errors import InvalidRequest
|
||||
@@ -202,3 +205,20 @@ def test_update_letter_contact_unset_default_for_only_letter_contact_raises_exce
|
||||
contact_block='Warwick, W14 TSR',
|
||||
is_default=False
|
||||
)
|
||||
|
||||
|
||||
def test_dao_get_letter_contact_by_id(sample_service):
|
||||
letter_contact = create_letter_contact(service=sample_service, contact_block='Aberdeen, AB12 23X')
|
||||
result = dao_get_letter_contact_by_id(service_id=sample_service.id, letter_contact_id=letter_contact.id)
|
||||
assert result == letter_contact
|
||||
|
||||
|
||||
def test_dao_get_letter_contact_by_id_raises_sqlalchemy_error_when_letter_contact_does_not_exist(sample_service):
|
||||
with pytest.raises(SQLAlchemyError):
|
||||
dao_get_letter_contact_by_id(service_id=sample_service.id, letter_contact_id=uuid.uuid4())
|
||||
|
||||
|
||||
def test_dao_get_letter_contact_by_id_raises_sqlalchemy_error_when_service_does_not_exist(sample_service):
|
||||
letter_contact = create_letter_contact(service=sample_service, contact_block='Some address')
|
||||
with pytest.raises(SQLAlchemyError):
|
||||
dao_get_letter_contact_by_id(service_id=uuid.uuid4(), letter_contact_id=letter_contact.id)
|
||||
|
||||
@@ -2377,3 +2377,23 @@ def test_get_letter_contacts_with_multiple_letter_contacts(client, notify_db, no
|
||||
assert not json_response[1]['is_default']
|
||||
assert json_response[1]['created_at']
|
||||
assert not json_response[1]['updated_at']
|
||||
|
||||
|
||||
def test_get_letter_contact_by_id(client, notify_db, notify_db_session):
|
||||
service = create_service(notify_db=notify_db, notify_db_session=notify_db_session)
|
||||
letter_contact = create_letter_contact(service, 'London, E1 8QS')
|
||||
|
||||
response = client.get('/service/{}/letter-contact/{}'.format(service.id, letter_contact.id),
|
||||
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
||||
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.get_data(as_text=True)) == letter_contact.serialize()
|
||||
|
||||
|
||||
def test_get_letter_contact_return_404_when_invalid_contact_id(client, notify_db, notify_db_session):
|
||||
service = create_service(notify_db=notify_db, notify_db_session=notify_db_session)
|
||||
|
||||
response = client.get('/service/{}/letter-contact/{}'.format(service.id, '93d59f88-4aa1-453c-9900-f61e2fc8a2de'),
|
||||
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
Reference in New Issue
Block a user