diff --git a/app/routers/guest/applications.py b/app/routers/guest/applications.py index e88b35aa..4a6245d0 100644 --- a/app/routers/guest/applications.py +++ b/app/routers/guest/applications.py @@ -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() @@ -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( @@ -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) diff --git a/app/util.py b/app/util.py index 0607f794..d7fd545a 100644 --- a/app/util.py +++ b/app/util.py @@ -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 @@ -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, + )