Skip to content

Commit

Permalink
feat: application endpoint for FI now only brings all based on user l…
Browse files Browse the repository at this point in the history
…ender id
  • Loading branch information
Lombardoh committed Jun 14, 2023
1 parent 0fe32e2 commit b50098c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 42 deletions.
4 changes: 3 additions & 1 deletion app/routers/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ async def get_applications(
user: core.User = Depends(get_user),
session: Session = Depends(get_db),
):
return utils.get_all_FI_user_applications(page, page_size, session, user.id)
# validar lender id en user y app
print(user.lender.id)
return utils.get_all_FI_user_applications(page, page_size, session, user.lender.id)


@router.get(
Expand Down
5 changes: 1 addition & 4 deletions app/schema/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@

class Pagination(BaseModel):
items: List[core.Application]
total: int
count: int
page: int
page_size: int
total_pages: int
next_page: Optional[int]
previous_page: Optional[int]


class AwardUpdate(BaseModel):
Expand Down
18 changes: 11 additions & 7 deletions app/schema/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Application(SQLModel, table=True):
sa_column=Column(SAEnum(ApplicationStatus, name="application_status")),
default=ApplicationStatus.PENDING,
)
award_borrowed_identifier: str = Field(default="", unique=True, nullable=False)
award_borrower_identifier: str = Field(default="", unique=True, nullable=False)
borrower_id: Optional[int] = Field(foreign_key="borrower.id")
borrower: "Borrower" = Relationship(back_populates="applications")
lender_id: Optional[int] = Field(foreign_key="lender.id", nullable=True)
Expand Down Expand Up @@ -280,37 +280,41 @@ class Lender(SQLModel, table=True):
class Award(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
applications: Optional[List["Application"]] = Relationship(back_populates="award")
borrower_id: int = Field(foreign_key="borrower.id")
borrower_id: Optional[int] = Field(foreign_key="borrower.id", nullable=True)
borrower: Borrower = Relationship(back_populates="awards")
source_contract_id: str = Field(default="")
title: str = Field(default="")
description: str = Field(default="")
award_date: Optional[datetime] = Field(
sa_column=Column(DateTime(timezone=True), nullable=True)
sa_column=Column(DateTime(timezone=False), nullable=True)
)
award_amount: Optional[Decimal] = Field(
sa_column=Column(DECIMAL(precision=16, scale=2), nullable=False)
)
award_currency: str = Field(default="COP", description="ISO 4217 currency code")
contractperiod_startdate: Optional[datetime] = Field(
sa_column=Column(DateTime(timezone=True), nullable=True)
sa_column=Column(DateTime(timezone=False), nullable=True)
)
contractperiod_enddate: Optional[datetime] = Field(
sa_column=Column(DateTime(timezone=True), nullable=True)
sa_column=Column(DateTime(timezone=False), nullable=True)
)
payment_method: dict = Field(default={}, sa_column=Column(JSON), nullable=False)
buyer_name: str = Field(default="")
source_url: str = Field(default="")
entity_code: str = Field(default="")
contract_status: str = Field(default="")
source_last_updated_at: Optional[datetime] = Field(
sa_column=Column(DateTime(timezone=True), nullable=True)
sa_column=Column(DateTime(timezone=False), nullable=True)
)
previous: bool = Field(default=False)
procurement_method: str = Field(default="")
contracting_process_id: str = Field(default="")
procurement_category: str = Field(default="")
source_data: dict = Field(default={}, sa_column=Column(JSON), nullable=False)
source_data_contracts: dict = Field(
default={}, sa_column=Column(JSON), nullable=False
)
source_data_awards: dict = Field(default={}, sa_column=Column(JSON), nullable=False)
missing_data: dict = Field(default={}, sa_column=Column(JSON), nullable=False)
created_at: Optional[datetime] = Field(
sa_column=Column(
DateTime(timezone=True),
Expand Down
2 changes: 1 addition & 1 deletion app/schema/diagram
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Table Application {
uuid text
primary_email text
status text
award_borrowed_identifier text
award_borrower_identifier text
borrower_id int [ref: > Borrower.id]
lender_id int [ref: > Lender.id]
contract_amount_submitted decimal
Expand Down
69 changes: 43 additions & 26 deletions app/utils/applications.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import datetime
from math import ceil

from fastapi import HTTPException, status
from fastapi.encoders import jsonable_encoder
Expand All @@ -24,6 +23,24 @@
]


def update_models(payload, model):
update_dict = jsonable_encoder(payload, exclude_unset=True)
for field, value in update_dict.items():
setattr(model, field, value)


def update_models_with_validation(payload, model):
update_dict = jsonable_encoder(payload, exclude_unset=True)
for field, value in update_dict.items():
if model.missing_data[field]:
setattr(model, field, value)
else:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="This column cannot be updated",
)


def create_application_action(
session: Session,
user_id: int,
Expand All @@ -40,6 +57,7 @@ def create_application_action(
user_id=user_id,
)
session.add(new_action)
session.flush()

return new_action

Expand All @@ -54,23 +72,27 @@ def update_application_borrower(
.first()
)
if not application or not application.borrower:
raise HTTPException(status_code=404, detail="Application or borrower not found")
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Application or borrower not found",
)

if application.status == core.ApplicationStatus.APPROVED:
raise HTTPException(
status_code=409, detail="Approved applications cannot be updated"
status_code=status.HTTP_409_CONFLICT,
detail="Approved applications cannot be updated",
)

if user.is_OCP() and application.status not in OCP_can_modify:
raise HTTPException(
status_code=409, detail="This application cannot be updated by OCP Admins"
status_code=status.HTTP_409_CONFLICT,
detail="This application cannot be updated by OCP Admins",
)

update_dict = jsonable_encoder(payload, exclude_unset=True)
for field, value in update_dict.items():
setattr(application.borrower, field, value)
update_models(payload, application.borrower)

session.add(application)
session.flush()

return application

Expand All @@ -85,24 +107,27 @@ def update_application_award(
.first()
)
if not application or not application.award:
raise HTTPException(status_code=404, detail="Application or award not found")
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Application or award not found",
)

if application.status == core.ApplicationStatus.APPROVED:
raise HTTPException(
status_code=409, detail="Approved applications cannot be updated"
status_code=status.HTTP_409_CONFLICT,
detail="Approved applications cannot be updated",
)

if user.is_OCP() and application.status not in OCP_can_modify:
raise HTTPException(
status_code=409, detail="This application cannot be updated by OCP Admins"
status_code=status.HTTP_409_CONFLICT,
detail="This application cannot be updated by OCP Admins",
)

update_dict = jsonable_encoder(payload)
for field, value in update_dict.items():
setattr(application.award, field, value)
update_models_with_validation(payload, application.award)

session.add(application)
session.refresh(application)
session.flush()

return application

Expand All @@ -115,44 +140,36 @@ def get_all_active_applications(
)

total_count = applications_query.count()
total_pages = ceil(total_count / page_size)

applications = (
applications_query.offset((page - 1) * page_size).limit(page_size).all()
)

return Pagination(
items=applications,
total=total_count,
count=total_count,
page=page,
page_size=page_size,
total_pages=total_pages,
next_page=page + 1 if page < total_pages else None,
previous_page=page - 1 if page > 1 else None,
)


def get_all_FI_user_applications(
page: int, page_size: int, session: Session, user_id
page: int, page_size: int, session: Session, lender_id
) -> Pagination:
applications_query = session.query(core.Application).filter(
core.Application.lender_id == user_id
core.Application.lender_id == lender_id
)
total_count = applications_query.count()
total_pages = ceil(total_count / page_size)

applications = (
applications_query.offset((page - 1) * page_size).limit(page_size).all()
)

return Pagination(
items=applications,
total=total_count,
count=total_count,
page=page,
page_size=page_size,
total_pages=total_pages,
next_page=page + 1 if page < total_pages else None,
previous_page=page - 1 if page > 1 else None,
)


Expand Down
6 changes: 3 additions & 3 deletions background_processes/application_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def insert_application(application: Application):
def create_application(
award_id, borrower_id, email, legal_identifier, source_contract_id
) -> str:
award_borrowed_identifier: str = get_secret_hash(
award_borrower_identifier: str = get_secret_hash(
legal_identifier + source_contract_id
)
new_uuid: str = generate_uuid(award_borrowed_identifier)
new_uuid: str = generate_uuid(award_borrower_identifier)
application = {
"award_id": award_id,
"borrower_id": borrower_id,
"primary_email": email,
"award_borrowed_identifier": award_borrowed_identifier,
"award_borrower_identifier": award_borrower_identifier,
"uuid": new_uuid,
"expired_at": datetime.utcnow() + timedelta(days=DAYS_UNTIL_EXPIRED),
}
Expand Down

0 comments on commit b50098c

Please sign in to comment.