From 7dd8002e1b1014870f4151a08a259cb3109f7e26 Mon Sep 17 00:00:00 2001 From: shivamg9 Date: Wed, 15 May 2024 17:55:22 +0530 Subject: [PATCH] Field mapping of form to res partner --- .../models/orm/partner_orm.py | 24 +++++++++++-- .../services/form_service.py | 36 ++++++++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/openg2p_portal_api/models/orm/partner_orm.py b/src/openg2p_portal_api/models/orm/partner_orm.py index 720400c..05a10f0 100644 --- a/src/openg2p_portal_api/models/orm/partner_orm.py +++ b/src/openg2p_portal_api/models/orm/partner_orm.py @@ -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 @@ -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) diff --git a/src/openg2p_portal_api/services/form_service.py b/src/openg2p_portal_api/services/form_service.py index 97261e0..ca0927d 100644 --- a/src/openg2p_portal_api/services/form_service.py +++ b/src/openg2p_portal_api/services/form_service.py @@ -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, @@ -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, @@ -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