Skip to content

Commit

Permalink
Merge pull request #14 from shivamg9/develop
Browse files Browse the repository at this point in the history
Modified get program by keyword api, Modified queries order by date desc
  • Loading branch information
shibu-narayanan authored Mar 28, 2024
2 parents 6ced2e7 + 3bd552b commit 677175e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/openg2p_portal_api/models/orm/entitlement_orm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from openg2p_fastapi_common.models import BaseORMModel
from sqlalchemy.orm import Mapped, mapped_column

Expand All @@ -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()
40 changes: 34 additions & 6 deletions src/openg2p_portal_api/models/orm/program_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -158,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)
Expand All @@ -170,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"
),
Expand Down Expand Up @@ -206,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()
2 changes: 1 addition & 1 deletion src/openg2p_portal_api/models/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 677175e

Please sign in to comment.