Skip to content

Commit

Permalink
chore: Reduce repetition between external onboarding routers
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Oct 30, 2024
1 parent 707ef8f commit a2a7ac7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 40 deletions.
33 changes: 2 additions & 31 deletions app/routers/guest/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from app import aws, dependencies, mail, models, parsers, serializers, util
from app.db import get_db, rollback_on_error
from app.i18n import _
from app.settings import app_settings

router = APIRouter()

Expand Down Expand Up @@ -733,22 +732,7 @@ async def access_external_onboarding(
:return: A redirect to the lender.external_onboarding_url.
:raise: HTTPException if the application has a lender without an external_onboarding_url.
"""
with rollback_on_error(session):
if not application.lender.external_onboarding_url:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=_("The lender has no external onboarding URL"),
)

if not application.borrower_accessed_external_onboarding_at:
util.set_borrower_accessed_external_onboarding(application, session)

return RedirectResponse(application.lender.external_onboarding_url, status_code=status.HTTP_303_SEE_OTHER)

return RedirectResponse(
f"{app_settings.frontend_url}/application/{application.uuid}/external-onboarding-completed",
status_code=status.HTTP_303_SEE_OTHER,
)
return util.handle_external_onboarding(session, application, forward=True)


@router.get(
Expand All @@ -767,17 +751,4 @@ async def accessed_external_onboarding(
:return: A redirect to the frontend's external-onboarding-completed page.
:raise: HTTPException if the application has a lender without an external_onboarding_url.
"""
with rollback_on_error(session):
if not application.lender.external_onboarding_url:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=_("The lender has no external onboarding URL"),
)

if not application.borrower_accessed_external_onboarding_at:
util.set_borrower_accessed_external_onboarding(application, session)

return RedirectResponse(
f"{app_settings.frontend_url}/application/{application.uuid}/external-onboarding-completed",
status_code=status.HTTP_303_SEE_OTHER,
)
return util.handle_external_onboarding(session, application)
39 changes: 30 additions & 9 deletions app/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
from fastapi import File, HTTPException, UploadFile, status
from sqlalchemy.orm import Session
from sqlmodel import col
from starlette.responses import RedirectResponse

from app import models
from app.db import get_db, handle_skipped_award
from app.db import get_db, handle_skipped_award, rollback_on_error
from app.exceptions import SkippedAwardError
from app.i18n import _
from app.settings import app_settings
Expand Down Expand Up @@ -266,13 +267,33 @@ def create_or_update_borrower_document(
)


def set_borrower_accessed_external_onboarding(application: models.Application, session: Session) -> None:
application.borrower_accessed_external_onboarding_at = datetime.now(application.created_at.tzinfo)
def handle_external_onboarding(
session: Session, application: models.Application, *, forward: bool = False
) -> RedirectResponse:
with rollback_on_error(session):
external_onboarding_url = application.lender.external_onboarding_url

models.ApplicationAction.create(
session,
type=models.ApplicationActionType.MSME_ACCESS_EXTERNAL_ONBOARDING,
application_id=application.id,
)
if not external_onboarding_url:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=_("The lender has no external onboarding URL"),
)

if not application.borrower_accessed_external_onboarding_at:
application.borrower_accessed_external_onboarding_at = datetime.now(application.created_at.tzinfo)

models.ApplicationAction.create(
session,
type=models.ApplicationActionType.MSME_ACCESS_EXTERNAL_ONBOARDING,
application_id=application.id,
)

session.commit()
session.commit()

if forward:
return RedirectResponse(external_onboarding_url, status_code=status.HTTP_303_SEE_OTHER)

return RedirectResponse(
f"{app_settings.frontend_url}/application/{application.uuid}/external-onboarding-completed",
status_code=status.HTTP_303_SEE_OTHER,
)

0 comments on commit a2a7ac7

Please sign in to comment.