From 4406a57cb0befc6a76396d34b990552cab1f9841 Mon Sep 17 00:00:00 2001 From: lchen-2101 <73617864+lchen-2101@users.noreply.github.com> Date: Wed, 27 Dec 2023 14:12:33 -0500 Subject: [PATCH] feat: update institutions dto to include relational models instead of just the codes --- src/entities/models/__init__.py | 4 ++-- src/entities/models/dto.py | 20 +++++++++------- src/routers/institutions.py | 27 ++++------------------ tests/api/conftest.py | 14 ++++++++++- tests/api/routers/test_institutions_api.py | 26 ++++++++++++++++++++- 5 files changed, 57 insertions(+), 34 deletions(-) diff --git a/src/entities/models/__init__.py b/src/entities/models/__init__.py index ace01c3..27a17bb 100644 --- a/src/entities/models/__init__.py +++ b/src/entities/models/__init__.py @@ -3,7 +3,7 @@ "FinancialInstitutionDao", "FinancialInstitutionDomainDao", "FinancialInstitutionDto", - "FinancialInstitutionWithDomainsDto", + "FinancialInstitutionWithRelationsDto", "FinancialInsitutionDomainDto", "FinancialInsitutionDomainCreate", "FinanicialInstitutionAssociationDto", @@ -33,7 +33,7 @@ ) from .dto import ( FinancialInstitutionDto, - FinancialInstitutionWithDomainsDto, + FinancialInstitutionWithRelationsDto, FinancialInsitutionDomainDto, FinancialInsitutionDomainCreate, FinanicialInstitutionAssociationDto, diff --git a/src/entities/models/dto.py b/src/entities/models/dto.py index ee05131..626a56b 100644 --- a/src/entities/models/dto.py +++ b/src/entities/models/dto.py @@ -45,10 +45,6 @@ class Config: from_attributes = True -class FinancialInstitutionWithDomainsDto(FinancialInstitutionDto): - domains: List[FinancialInsitutionDomainDto] = [] - - class DeniedDomainDto(BaseModel): domain: str @@ -65,10 +61,6 @@ def to_keycloak_user(self): return {"firstName": self.first_name, "lastName": self.last_name} -class FinanicialInstitutionAssociationDto(FinancialInstitutionDto): - approved: bool - - class FederalRegulatorBase(BaseModel): id: str @@ -113,6 +105,18 @@ class Config: from_attributes = True +class FinancialInstitutionWithRelationsDto(FinancialInstitutionDto): + primary_federal_regulator: FederalRegulatorDto | None = None + hmda_institution_type: HMDAInstitutionTypeDto | None = None + sbl_institution_type: SBLInstitutionTypeDto | None = None + hq_address_state: AddressStateDto + domains: List[FinancialInsitutionDomainDto] = [] + + +class FinanicialInstitutionAssociationDto(FinancialInstitutionWithRelationsDto): + approved: bool + + class AuthenticatedUser(BaseUser, BaseModel): claims: Dict[str, Any] name: str diff --git a/src/routers/institutions.py b/src/routers/institutions.py index 371c2ee..a190b05 100644 --- a/src/routers/institutions.py +++ b/src/routers/institutions.py @@ -8,7 +8,7 @@ from entities.repos import institutions_repo as repo from entities.models import ( FinancialInstitutionDto, - FinancialInstitutionWithDomainsDto, + FinancialInstitutionWithRelationsDto, FinancialInsitutionDomainDto, FinancialInsitutionDomainCreate, FinanicialInstitutionAssociationDto, @@ -25,7 +25,7 @@ async def set_db(request: Request, session: Annotated[AsyncSession, Depends(get_ router = Router(dependencies=[Depends(set_db)]) -@router.get("/", response_model=List[FinancialInstitutionWithDomainsDto]) +@router.get("/", response_model=List[FinancialInstitutionWithRelationsDto]) @requires("authenticated") async def get_institutions( request: Request, @@ -37,7 +37,7 @@ async def get_institutions( return await repo.get_institutions(request.state.db_session, leis, domain, page, count) -@router.post("/", response_model=Tuple[str, FinancialInstitutionDto], dependencies=[Depends(check_domain)]) +@router.post("/", response_model=Tuple[str, FinancialInstitutionWithRelationsDto], dependencies=[Depends(check_domain)]) @requires(["query-groups", "manage-users"]) async def create_institution( request: Request, @@ -56,31 +56,14 @@ async def get_associated_institutions(request: Request): associated_institutions = await repo.get_institutions(request.state.db_session, user.institutions) return [ FinanicialInstitutionAssociationDto( - name=institution.name, - lei=institution.lei, - tax_id=institution.tax_id, - rssd_id=institution.rssd_id, - primary_federal_regulator_id=institution.primary_federal_regulator_id, - hmda_institution_type_id=institution.hmda_institution_type_id, - sbl_institution_type_id=institution.sbl_institution_type_id, - hq_address_street_1=institution.hq_address_street_1, - hq_address_street_2=institution.hq_address_street_2, - hq_address_city=institution.hq_address_city, - hq_address_state_code=institution.hq_address_state_code, - hq_address_zip=institution.hq_address_zip, - parent_lei=institution.parent_lei, - parent_legal_name=institution.parent_legal_name, - parent_rssd_id=institution.parent_rssd_id, - top_holder_lei=institution.top_holder_lei, - top_holder_legal_name=institution.top_holder_legal_name, - top_holder_rssd_id=institution.top_holder_rssd_id, + **institution.__dict__, approved=email_domain in [inst_domain.domain for inst_domain in institution.domains], ) for institution in associated_institutions ] -@router.get("/{lei}", response_model=FinancialInstitutionWithDomainsDto) +@router.get("/{lei}", response_model=FinancialInstitutionWithRelationsDto) @requires("authenticated") async def get_institution( request: Request, diff --git a/tests/api/conftest.py b/tests/api/conftest.py index 73577f4..468ccdc 100644 --- a/tests/api/conftest.py +++ b/tests/api/conftest.py @@ -5,7 +5,15 @@ from pytest_mock import MockerFixture from starlette.authentication import AuthCredentials, UnauthenticatedUser -from entities.models import AuthenticatedUser, FinancialInstitutionDao, FinancialInstitutionDomainDao +from entities.models import ( + AuthenticatedUser, + FinancialInstitutionDao, + FinancialInstitutionDomainDao, + FederalRegulatorDao, + AddressStateDao, + HMDAInstitutionTypeDao, + SBLInstitutionTypeDao, +) @pytest.fixture @@ -58,12 +66,16 @@ def get_institutions_mock(mocker: MockerFixture) -> Mock: tax_id="123456789", rssd_id=1234, primary_federal_regulator_id="FRI1", + primary_federal_regulator=FederalRegulatorDao(id="FRI1", name="FRI1"), hmda_institution_type_id="HIT1", + hmda_institution_type=HMDAInstitutionTypeDao(id="HIT1", name="HIT1"), sbl_institution_type_id="SIT1", + sbl_institution_type=SBLInstitutionTypeDao(id="SIT1", name="SIT1"), hq_address_street_1="Test Address Street 1", hq_address_street_2="", hq_address_city="Test City 1", hq_address_state_code="GA", + hq_address_state=AddressStateDao(code="GA", name="Georgia"), hq_address_zip="00000", parent_lei="PARENTTESTBANK123", parent_legal_name="PARENT TEST BANK 123", diff --git a/tests/api/routers/test_institutions_api.py b/tests/api/routers/test_institutions_api.py index a65545a..b4ddf08 100644 --- a/tests/api/routers/test_institutions_api.py +++ b/tests/api/routers/test_institutions_api.py @@ -5,7 +5,14 @@ from pytest_mock import MockerFixture from starlette.authentication import AuthCredentials from oauth2.oauth2_backend import AuthenticatedUser -from entities.models import FinancialInstitutionDao, FinancialInstitutionDomainDao +from entities.models import ( + FinancialInstitutionDao, + FinancialInstitutionDomainDao, + FederalRegulatorDao, + AddressStateDao, + HMDAInstitutionTypeDao, + SBLInstitutionTypeDao, +) class TestInstitutionsApi: @@ -36,12 +43,16 @@ def test_create_institution_authed(self, mocker: MockerFixture, app_fixture: Fas tax_id="123456789", rssd_id=1234, primary_federal_regulator_id="FRI2", + primary_federal_regulator=FederalRegulatorDao(id="FRI2", name="FRI2"), hmda_institution_type_id="HIT2", + hmda_institution_type=HMDAInstitutionTypeDao(id="HIT2", name="HIT2"), sbl_institution_type_id="SIT2", + sbl_institution_type=SBLInstitutionTypeDao(id="SIT2", name="SIT2"), hq_address_street_1="Test Address Street 1", hq_address_street_2="", hq_address_city="Test City 1", hq_address_state_code="VA", + hq_address_state=AddressStateDao(code="VA", name="Virginia"), hq_address_zip="00000", parent_lei="PARENTTESTBANK123", parent_legal_name="PARENT TEST BANK 123", @@ -89,6 +100,7 @@ def test_create_institution_only_required_fields( hq_address_street_1="Test Address Street 1", hq_address_city="Test City 1", hq_address_state_code="VA", + hq_address_state=AddressStateDao(code="VA", name="Virginia"), hq_address_zip="00000", ) upsert_group_mock = mocker.patch("oauth2.oauth2_admin.OAuth2Admin.upsert_group") @@ -177,12 +189,16 @@ def test_get_institution_authed(self, mocker: MockerFixture, app_fixture: FastAP tax_id="123456789", rssd_id=1234, primary_federal_regulator_id="FRI1", + primary_federal_regulator=FederalRegulatorDao(id="FRI1", name="FRI1"), hmda_institution_type_id="HIT1", + hmda_institution_type=HMDAInstitutionTypeDao(id="HIT1", name="HIT1"), sbl_institution_type_id="SIT1", + sbl_institution_type=SBLInstitutionTypeDao(id="SIT1", name="SIT1"), hq_address_street_1="Test Address Street 1", hq_address_street_2="", hq_address_city="Test City 1", hq_address_state_code="GA", + hq_address_state=AddressStateDao(code="GA", name="Georgia"), hq_address_zip="00000", parent_lei="PARENTTESTBANK123", parent_legal_name="PARENT TEST BANK 123", @@ -270,12 +286,16 @@ def test_get_associated_institutions( tax_id="123456789", rssd_id=1234, primary_federal_regulator_id="FRI1", + primary_federal_regulator=FederalRegulatorDao(id="FRI1", name="FRI1"), hmda_institution_type_id="HIT1", + hmda_institution_type=HMDAInstitutionTypeDao(id="HIT1", name="HIT1"), sbl_institution_type_id="SIT1", + sbl_institution_type=SBLInstitutionTypeDao(id="SIT1", name="SIT1"), hq_address_street_1="Test Address Street 1", hq_address_street_2="", hq_address_city="Test City 1", hq_address_state_code="GA", + hq_address_state=AddressStateDao(code="GA", name="Georgia"), hq_address_zip="00000", parent_lei="PARENTTESTBANK123", parent_legal_name="PARENT TEST BANK 123", @@ -291,12 +311,16 @@ def test_get_associated_institutions( tax_id="123456879", rssd_id=6879, primary_federal_regulator_id="FRI1", + primary_federal_regulator=FederalRegulatorDao(id="FRI1", name="FRI1"), hmda_institution_type_id="HIT1", + hmda_institution_type=HMDAInstitutionTypeDao(id="HIT1", name="HIT1"), sbl_institution_type_id="SIT1", + sbl_institution_type=SBLInstitutionTypeDao(id="SIT1", name="SIT1"), hq_address_street_1="Test Address Street 2", hq_address_street_2="", hq_address_city="Test City 2", hq_address_state_code="GA", + hq_address_state=AddressStateDao(code="GA", name="Georgia"), hq_address_zip="00000", parent_lei="PARENTTESTBANK123", parent_legal_name="PARENT TEST BANK 123",