Skip to content

Commit

Permalink
G2P-2185 G2P-2404 Restrict submission from beneficiary portal api
Browse files Browse the repository at this point in the history
  • Loading branch information
shivamg9 committed May 29, 2024
1 parent 0031cda commit 6b0779d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/openg2p_portal_api/services/form_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from openg2p_fastapi_common.context import dbengine
from openg2p_fastapi_common.service import BaseService
from sqlalchemy import select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import async_sessionmaker

Expand Down Expand Up @@ -95,9 +96,26 @@ async def submit_application_form(
):
async_session_maker = async_sessionmaker(dbengine.get())
async with async_session_maker() as session:
program_membership_id = await self.membership_service.check_and_create_mem(
program_id, registrant_id
existing_application = await session.execute(
select(ProgramRegistrantInfoORM).filter(
ProgramRegistrantInfoORM.program_id == program_id,
ProgramRegistrantInfoORM.registrant_id == registrant_id,
ProgramRegistrantInfoORM.state.in_(
["active", "inprogress", "applied"]
),
)
)
if existing_application.scalars().first():
return "Error: There is already an active or in-progress application for this program."

try:
program_membership_id = (
await self.membership_service.check_and_create_mem(
program_id, registrant_id
)
)
except ValueError as e:
return str(e)
get_draft_reg_info = (
await ProgramRegistrantInfoDraftORM.get_draft_reg_info_by_id(
program_id, registrant_id
Expand Down
3 changes: 2 additions & 1 deletion src/openg2p_portal_api/services/membership_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MembershipService(BaseService):
def __init__(self, **kwargs):
super().__init__(**kwargs)

async def check_and_create_mem(self, programid: int, partnerid: int):
async def check_and_create_mem(self, programid: int, partnerid: int) -> int:
async_session_maker = async_sessionmaker(dbengine.get())
async with async_session_maker() as session:
membership = await ProgramMembershipORM.get_membership_by_id(
Expand All @@ -25,6 +25,7 @@ async def check_and_create_mem(self, programid: int, partnerid: int):
try:
session.add(membership)
await session.commit()
await session.refresh(membership)
except IntegrityError:
return "Could not add to registrant to program!!"

Expand Down

0 comments on commit 6b0779d

Please sign in to comment.