-
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.
- Loading branch information
Showing
7 changed files
with
138 additions
and
26 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
9 changes: 6 additions & 3 deletions
9
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,14 @@ | ||
from openg2p_fastapi_common.models import BaseORMModel | ||
from sqlalchemy import ForeignKey, String | ||
from sqlalchemy import JSON, ForeignKey, Integer, String | ||
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()) |
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,38 @@ | ||
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 ProgramRegistrantInfoORM | ||
from .membership_service import MembershipService | ||
|
||
|
||
class FormService(BaseService): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.membership_service = MembershipService.get_component() | ||
|
||
async def create_new_form_draft( | ||
self, program_id: int, form_data, state: str, registrant_id: int | ||
): | ||
async_session_maker = async_sessionmaker(dbengine.get()) | ||
async with async_session_maker() as session: | ||
program_membership_id = await MembershipService.check_and_create_mem( | ||
self, 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=state, | ||
registrant_id=registrant_id, | ||
) | ||
|
||
try: | ||
session.add(program_registrant_info) | ||
|
||
await session.commit() | ||
except IntegrityError: | ||
return "Error: Duplicate entry or integrity violation" | ||
|
||
return "Successfully submitted the draft!!" |
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 | ||
) | ||
|
||
try: | ||
session.add(membership) | ||
|
||
await session.commit() | ||
except IntegrityError: | ||
return "Something went wrong!!" | ||
|
||
return membership.id |