Skip to content

Commit

Permalink
Field mapping of form to res partner
Browse files Browse the repository at this point in the history
  • Loading branch information
shivamg9 committed May 15, 2024
1 parent e54c5c8 commit 7dd8002
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/openg2p_portal_api/models/orm/partner_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@

from openg2p_fastapi_common.context import dbengine
from openg2p_fastapi_common.models import BaseORMModel, BaseORMModelWithId
from sqlalchemy import Boolean, Date, DateTime, ForeignKey, String, select
from sqlalchemy import (
Boolean,
CheckConstraint,
Date,
DateTime,
ForeignKey,
String,
select,
)
from sqlalchemy.ext.asyncio import async_sessionmaker
from sqlalchemy.orm import Mapped, mapped_column, relationship

Expand All @@ -24,7 +32,19 @@ class PartnerORM(BaseORMModelWithId):
birth_place: Mapped[str] = mapped_column()
phone: Mapped[str] = mapped_column()
company_id: Mapped[Optional[int]] = mapped_column()

income: Mapped[Optional[int]] = mapped_column()
display_name: Mapped[str] = mapped_column()
area_id: Mapped[Optional[int]] = mapped_column()
civil_status: Mapped[str] = mapped_column()
occupation: Mapped[str] = mapped_column()
district: Mapped[str] = mapped_column()
birthdate_not_exact: Mapped[bool] = mapped_column(Boolean(), default=False)
registration_date: Mapped[date] = mapped_column(Date())
notification_preference: Mapped[str] = mapped_column(
String,
CheckConstraint("notification_preference IN ('none', 'email', 'sms', 'both')"),
default="none",
)
reg_ids: Mapped[Optional[List[RegIDORM]]] = relationship(back_populates="partner")

create_date: Mapped[datetime] = mapped_column(DateTime(), default=datetime.utcnow)
Expand Down
36 changes: 35 additions & 1 deletion src/openg2p_portal_api/services/form_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sqlalchemy.ext.asyncio import async_sessionmaker

from ..models.form import ProgramForm
from ..models.orm.partner_orm import PartnerORM
from ..models.orm.program_orm import ProgramORM
from ..models.orm.program_registrant_info_orm import (
ProgramRegistrantInfoDraftORM,
Expand Down Expand Up @@ -103,10 +104,23 @@ async def submit_application_form(
)
application_id = self._compute_application_id()
create_date = datetime.now()

partner_info = await PartnerORM.get_partner_data(registrant_id)
if partner_info:
updated_partner_info = await self.update_partner_info(
session, partner_info, form_data.program_registrant_info
)

cleaned_program_registrant_info = self.clean_program_registrant_info(
form_data.program_registrant_info, updated_partner_info
)
else:
cleaned_program_registrant_info = form_data.program_registrant_info

program_registrant_info = ProgramRegistrantInfoORM(
program_id=program_id,
program_membership_id=program_membership_id,
program_registrant_info=form_data.program_registrant_info,
program_registrant_info=cleaned_program_registrant_info,
state="active",
registrant_id=registrant_id,
application_id=application_id,
Expand Down Expand Up @@ -134,3 +148,23 @@ def _compute_application_id(self):
y = datetime.today().strftime("%y")
random_number = str(random.randint(1, 100000))
return d + m + y + random_number.zfill(5)

async def update_partner_info(self, session, partner_info, program_registrant_info):
# Update partner_info with fields from program_registrant_info
updated_fields = {}
for key, value in program_registrant_info.items():
if hasattr(partner_info, key) and getattr(partner_info, key) != value:
setattr(partner_info, key, value)
updated_fields[key] = value
session.add(partner_info)
await session.commit()
return updated_fields

def clean_program_registrant_info(self, program_registrant_info, updated_fields):
# Remove updated fields from program_registrant_info
cleaned_info = {
key: value
for key, value in program_registrant_info.items()
if key not in updated_fields
}
return cleaned_info

0 comments on commit 7dd8002

Please sign in to comment.