From 8c63f6da1d484a9dae9158d5430bfa775f15493d Mon Sep 17 00:00:00 2001 From: Adam <41971533+jcadam14@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:55:47 -0500 Subject: [PATCH] Updated FinancialInstitutionWithRelationsDto and institution_repo Fixed FinancialInstitutionWithRelationsDto to container a list of sbl_institution_types Updated institution_repo to correct merge issues with main --- src/entities/models/dto.py | 2 +- src/entities/repos/institutions_repo.py | 13 +++---------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/entities/models/dto.py b/src/entities/models/dto.py index 315b6b8..275cbef 100644 --- a/src/entities/models/dto.py +++ b/src/entities/models/dto.py @@ -95,7 +95,7 @@ class Config: class FinancialInstitutionWithRelationsDto(FinancialInstitutionDto): primary_federal_regulator: FederalRegulatorDto | None = None hmda_institution_type: InstitutionTypeDto | None = None - sbl_institution_type: InstitutionTypeDto | None = None + sbl_institution_types: List[InstitutionTypeDto] = [] hq_address_state: AddressStateDto domains: List[FinancialInsitutionDomainDto] = [] diff --git a/src/entities/repos/institutions_repo.py b/src/entities/repos/institutions_repo.py index 5b0318e..679c131 100644 --- a/src/entities/repos/institutions_repo.py +++ b/src/entities/repos/institutions_repo.py @@ -69,9 +69,6 @@ async def get_federal_regulators(session: AsyncSession) -> List[FederalRegulator async def upsert_institution(session: AsyncSession, fi: FinancialInstitutionDto) -> FinancialInstitutionDao: async with session.begin(): - stmt = select(FinancialInstitutionDao).filter(FinancialInstitutionDao.lei == fi.lei) - res = await session.execute(stmt) - db_fi = res.scalar_one_or_none() fi_data = fi.__dict__.copy() fi_data.pop("_sa_instance_state", None) @@ -85,13 +82,9 @@ async def upsert_institution(session: AsyncSession, fi: FinancialInstitutionDto) fi_data["sbl_institution_types"] = sbl_types.all() del fi_data["sbl_institution_type_ids"] - if db_fi is None: - db_fi = FinancialInstitutionDao(**fi_data) - session.add(db_fi) - else: - for key, value in fi_data.items(): - setattr(db_fi, key, value) - await session.commit() + db_fi = await session.merge(FinancialInstitutionDao(**fi_data)) + await session.flush([db_fi]) + await session.refresh(db_fi) return db_fi