From 8c80ef90bfe01897ea7d6702846504d53a579afc Mon Sep 17 00:00:00 2001 From: shivamg9 Date: Tue, 26 Mar 2024 17:56:36 +0530 Subject: [PATCH 1/2] Modified get program by keyword api --- .../models/orm/program_orm.py | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/openg2p_portal_api/models/orm/program_orm.py b/src/openg2p_portal_api/models/orm/program_orm.py index 8580b30..1e80426 100644 --- a/src/openg2p_portal_api/models/orm/program_orm.py +++ b/src/openg2p_portal_api/models/orm/program_orm.py @@ -64,14 +64,32 @@ async def get_all_by_program_id(cls, programid: int): async def get_all_program_by_keyword(cls, keyword: str): response = [] async_session_maker = async_sessionmaker(dbengine.get()) - async with async_session_maker() as session: + async with async_session_maker() as session: + # Create a case sensitive match condition + case_sensitive_match = cls.name.like(f"%{keyword}%") & cls.name.ilike(f"%{keyword}%") + # Create a case insensitive match condition + case_insensitive_match = cls.name.ilike(f"%{keyword}%") + + # First, select entries that match the keyword with the same case + stmt = ( + select(cls) + .filter(case_sensitive_match) + .options(selectinload(cls.membership)) + ) + result = await session.execute(stmt) + response.extend(list(result.scalars().all())) + + # Next, select entries that match the keyword regardless of case, excluding already selected ones stmt = ( select(cls) - .filter(cls.name.like(f"%{keyword}%")) + .filter(case_insensitive_match) + .filter(~case_sensitive_match) .options(selectinload(cls.membership)) ) + result = await session.execute(stmt) - response = list(result.scalars().all()) if result.scalars() else [] + # response = list(result.scalars().all()) if result.scalars() else [] + response.extend(list(result.scalars().all())) return response @classmethod From 3bd552b263cf226e2456d39c12c4f82f06efa071 Mon Sep 17 00:00:00 2001 From: shivamg9 Date: Wed, 27 Mar 2024 13:53:39 +0530 Subject: [PATCH 2/2] Modified queries: Added filter conditions, order by date desc --- .../models/orm/entitlement_orm.py | 2 ++ src/openg2p_portal_api/models/orm/program_orm.py | 16 +++++++++++++--- src/openg2p_portal_api/models/program.py | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/openg2p_portal_api/models/orm/entitlement_orm.py b/src/openg2p_portal_api/models/orm/entitlement_orm.py index f6e8654..d669458 100644 --- a/src/openg2p_portal_api/models/orm/entitlement_orm.py +++ b/src/openg2p_portal_api/models/orm/entitlement_orm.py @@ -1,3 +1,4 @@ +from datetime import datetime from openg2p_fastapi_common.models import BaseORMModel from sqlalchemy.orm import Mapped, mapped_column @@ -11,3 +12,4 @@ class EntitlementORM(BaseORMModel): state: Mapped[str] = mapped_column() initial_amount: Mapped[int] = mapped_column() ern: Mapped[int] = mapped_column() + date_approved: Mapped[datetime]=mapped_column() diff --git a/src/openg2p_portal_api/models/orm/program_orm.py b/src/openg2p_portal_api/models/orm/program_orm.py index 1e80426..23f90e3 100644 --- a/src/openg2p_portal_api/models/orm/program_orm.py +++ b/src/openg2p_portal_api/models/orm/program_orm.py @@ -2,7 +2,7 @@ from openg2p_fastapi_common.context import dbengine from openg2p_fastapi_common.models import BaseORMModelWithId -from sqlalchemy import ForeignKey, String, and_, func, select +from sqlalchemy import ForeignKey, String, and_, func, select, or_ from sqlalchemy.ext.asyncio import async_sessionmaker from sqlalchemy.orm import Mapped, mapped_column, relationship, selectinload @@ -176,6 +176,7 @@ async def get_application_details(partner_id: int) -> List["ProgramORM"]: ) .outerjoin(ProgramORM, ProgramMembershipORM.program_id == ProgramORM.id) .where(ProgramMembershipORM.partner_id == partner_id) + .order_by(ProgramRegistrantInfoORM.create_date.desc()) ) # print("####################################") # print(stmt) @@ -188,7 +189,7 @@ async def get_benefit_details(cls, partner_id: int) -> List["ProgramORM"]: stmt = ( select( ProgramORM.name.label("program_name"), - ProgramMembershipORM.state.label("enrollment_status"), + EntitlementORM.date_approved.label("date_approved"), func.coalesce(EntitlementORM.initial_amount, 0).label( "funds_awaited" ), @@ -224,7 +225,16 @@ async def get_benefit_details(cls, partner_id: int) -> List["ProgramORM"]: PaymentORM.status == "paid", ), ) - .where(ProgramMembershipORM.partner_id == partner_id) + .where( + and_( + ProgramMembershipORM.partner_id == partner_id, + or_( + EntitlementORM.ern.isnot(None), + EntitlementORM.initial_amount != 0, + PaymentORM.amount_paid != 0, + ), + ) + ) .order_by(EntitlementORM.date_approved.desc()) ) result = await session.execute(stmt) return result.all() diff --git a/src/openg2p_portal_api/models/program.py b/src/openg2p_portal_api/models/program.py index 01df11e..5d3b8b8 100644 --- a/src/openg2p_portal_api/models/program.py +++ b/src/openg2p_portal_api/models/program.py @@ -57,7 +57,7 @@ class BenefitDetails(BaseModel): model_config = ConfigDict(from_attributes=True) program_name: Optional[str] = None - enrollment_status: Optional[str] = None + date_approved: Optional[datetime] = None funds_awaited: Optional[float] = None funds_received: Optional[float] = None entitlement_reference_number: Optional[int] = None