-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from mkumar-02/main
Added Form Submission and Program Form POST and PUT request.
- Loading branch information
Showing
9 changed files
with
231 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 27 additions & 3 deletions
30
src/openg2p_portal_api/models/orm/program_registrant_info_orm.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,35 @@ | ||
from openg2p_fastapi_common.context import dbengine | ||
from openg2p_fastapi_common.models import BaseORMModel | ||
from sqlalchemy import ForeignKey, String | ||
from sqlalchemy import JSON, ForeignKey, Integer, String, and_, select | ||
from sqlalchemy.ext.asyncio import async_sessionmaker | ||
from sqlalchemy.orm import Mapped, mapped_column | ||
|
||
|
||
class FormORM(BaseORMModel): | ||
class ProgramRegistrantInfoORM(BaseORMModel): | ||
__tablename__ = "g2p_program_registrant_info" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True) | ||
program_id: Mapped[int] = mapped_column(ForeignKey("g2p_program.id")) | ||
program_registrant_info: Mapped[str] = mapped_column(String()) | ||
program_registrant_info: Mapped[dict] = mapped_column(JSON) | ||
state: Mapped[str] = mapped_column(String()) | ||
program_membership_id: Mapped[int] = mapped_column(Integer()) | ||
registrant_id: Mapped[int] = mapped_column(Integer()) | ||
|
||
|
||
class ProgramRegistrantInfoDraftORM(BaseORMModel): | ||
__tablename__ = "g2p_program_registrant_info_draft" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True) | ||
program_id: Mapped[int] = mapped_column(ForeignKey("g2p_program.id")) | ||
registrant_id: Mapped[int] = mapped_column(Integer()) | ||
program_registrant_info: Mapped[dict] = mapped_column(JSON) | ||
|
||
@classmethod | ||
async def get_draft_reg_info_by_id(cls, program_id: int, registrant_id: int): | ||
async_session_maker = async_sessionmaker(dbengine.get()) | ||
async with async_session_maker() as session: | ||
stmt = select(cls).where( | ||
and_(cls.program_id == program_id, cls.registrant_id == registrant_id) | ||
) | ||
result = await session.execute(stmt) | ||
return result.scalar() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from openg2p_fastapi_common.context import dbengine | ||
from openg2p_fastapi_common.service import BaseService | ||
from sqlalchemy.exc import IntegrityError | ||
from sqlalchemy.ext.asyncio import async_sessionmaker | ||
|
||
from ..models.orm.program_registrant_info_orm import ( | ||
ProgramRegistrantInfoDraftORM, | ||
ProgramRegistrantInfoORM, | ||
) | ||
from .membership_service import MembershipService | ||
|
||
|
||
class FormService(BaseService): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.membership_service = MembershipService.get_component() | ||
|
||
async def crate_form_draft(self, program_id: int, form_data, registrant_id: int): | ||
async_session_maker = async_sessionmaker(dbengine.get()) | ||
async with async_session_maker() as session: | ||
check_if_draft_already_present = ( | ||
await ProgramRegistrantInfoDraftORM.get_draft_reg_info_by_id( | ||
program_id, registrant_id | ||
) | ||
) | ||
|
||
if check_if_draft_already_present is None: | ||
program_registrant_info = ProgramRegistrantInfoDraftORM( | ||
program_id=program_id, | ||
program_registrant_info=form_data.program_registrant_info, | ||
registrant_id=registrant_id, | ||
) | ||
|
||
try: | ||
session.add(program_registrant_info) | ||
|
||
await session.commit() | ||
except IntegrityError: | ||
return "Error: In creating the draft" | ||
|
||
else: | ||
check_if_draft_already_present.program_registrant_info = ( | ||
form_data.program_registrant_info | ||
) | ||
try: | ||
await session.commit() | ||
except IntegrityError: | ||
return "Error: In updating the draft." | ||
|
||
return "Successfully submitted the draft!!" | ||
|
||
async def submit_application_form( | ||
self, program_id: int, form_data, registrant_id: int | ||
): | ||
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 | ||
) | ||
get_draft_reg_info = ( | ||
await ProgramRegistrantInfoDraftORM.get_draft_reg_info_by_id( | ||
program_id, registrant_id | ||
) | ||
) | ||
program_registrant_info = ProgramRegistrantInfoORM( | ||
program_id=program_id, | ||
program_membership_id=program_membership_id, | ||
program_registrant_info=form_data.program_registrant_info, | ||
state="active", | ||
registrant_id=registrant_id, | ||
) | ||
|
||
try: | ||
if get_draft_reg_info: | ||
session.add(program_registrant_info) | ||
await session.delete(get_draft_reg_info) | ||
else: | ||
session.add(program_registrant_info) | ||
|
||
await session.commit() | ||
except IntegrityError: | ||
return "Error: Duplicate entry or integrity violation" | ||
|
||
return "Successfully applied into the program!!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from openg2p_fastapi_common.context import dbengine | ||
from openg2p_fastapi_common.service import BaseService | ||
from sqlalchemy.exc import IntegrityError | ||
from sqlalchemy.ext.asyncio import async_sessionmaker | ||
|
||
from ..models.orm.program_membership_orm import ProgramMembershipORM | ||
|
||
|
||
class MembershipService(BaseService): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
async def check_and_create_mem(self, programid: int, partnerid: int): | ||
async_session_maker = async_sessionmaker(dbengine.get()) | ||
async with async_session_maker() as session: | ||
membership = await ProgramMembershipORM.get_membership_by_id( | ||
programid, partnerid | ||
) | ||
|
||
if membership is None: | ||
membership = ProgramMembershipORM( | ||
program_id=programid, partner_id=partnerid, state="draft" | ||
) | ||
|
||
try: | ||
session.add(membership) | ||
|
||
await session.commit() | ||
except IntegrityError: | ||
return "Could not add to registrant to program!!" | ||
|
||
return membership.id |