Skip to content

Commit

Permalink
feat: update institutions dto to include relational models instead of…
Browse files Browse the repository at this point in the history
… just the codes (#72)

closes #71
  • Loading branch information
lchen-2101 authored Dec 28, 2023
1 parent 176ad6e commit 05b9237
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/entities/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"FinancialInstitutionDao",
"FinancialInstitutionDomainDao",
"FinancialInstitutionDto",
"FinancialInstitutionWithDomainsDto",
"FinancialInstitutionWithRelationsDto",
"FinancialInsitutionDomainDto",
"FinancialInsitutionDomainCreate",
"FinanicialInstitutionAssociationDto",
Expand Down Expand Up @@ -33,7 +33,7 @@
)
from .dto import (
FinancialInstitutionDto,
FinancialInstitutionWithDomainsDto,
FinancialInstitutionWithRelationsDto,
FinancialInsitutionDomainDto,
FinancialInsitutionDomainCreate,
FinanicialInstitutionAssociationDto,
Expand Down
20 changes: 12 additions & 8 deletions src/entities/models/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ class Config:
from_attributes = True


class FinancialInstitutionWithDomainsDto(FinancialInstitutionDto):
domains: List[FinancialInsitutionDomainDto] = []


class DeniedDomainDto(BaseModel):
domain: str

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
27 changes: 5 additions & 22 deletions src/routers/institutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from entities.repos import institutions_repo as repo
from entities.models import (
FinancialInstitutionDto,
FinancialInstitutionWithDomainsDto,
FinancialInstitutionWithRelationsDto,
FinancialInsitutionDomainDto,
FinancialInsitutionDomainCreate,
FinanicialInstitutionAssociationDto,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
14 changes: 13 additions & 1 deletion tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
26 changes: 25 additions & 1 deletion tests/api/routers/test_institutions_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 05b9237

Please sign in to comment.