Skip to content

Commit

Permalink
[Issue #3696] Update get agencies endpoint to remove extra queries (#…
Browse files Browse the repository at this point in the history
…3733)

## Summary
Fixes #3696

### Time to review: __5 mins__

## Changes proposed
Cleanup the way we setup get agencies queries to remove extra/unused
logic

## Context for reviewers
It looks like two separate queries were setup to do the same thing, but
only one is used, trim out the extra one that might actually cause
issues because it's setting the top level agency in its loop.
  • Loading branch information
chouinar authored and DavidDudas-Intuitial committed Feb 7, 2025
1 parent 92cb3ce commit 70c67f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
2 changes: 1 addition & 1 deletion api/src/db/models/user_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class UserSavedOpportunity(ApiSchemaTable, TimestampMixin):
)

last_notified_at: Mapped[datetime] = mapped_column(
default=datetime.utcnow, server_default="NOW()", nullable=False
default=datetime_util.utcnow, server_default="NOW()", nullable=False
)

user: Mapped[User] = relationship(User, back_populates="saved_opportunities")
Expand Down
28 changes: 9 additions & 19 deletions api/src/services/agencies_v1/get_agencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Sequence, Tuple

from pydantic import BaseModel, Field
from sqlalchemy import select
from sqlalchemy import asc, select
from sqlalchemy.orm import joinedload

import src.adapters.db as db
Expand All @@ -27,30 +27,20 @@ class AgencyListParams(BaseModel):
def get_agencies(
db_session: db.Session, list_params: AgencyListParams
) -> Tuple[Sequence[Agency], PaginationInfo]:
stmt = select(Agency).options(joinedload(Agency.top_level_agency), joinedload("*"))
stmt = (
select(Agency).options(joinedload(Agency.top_level_agency), joinedload("*"))
# Exclude test agencies
.where(Agency.is_test_agency.isnot(True))
)

# Exclude test agencies
stmt = stmt.where(Agency.is_test_agency != True) # noqa: E712
# TODO https://github.com/HHS/simpler-grants-gov/issues/3697
# use the sorting parameters from the request
stmt.order_by(asc("agency_code"))

if list_params.filters:
if list_params.filters.agency_name:
stmt = stmt.where(Agency.agency_name == list_params.filters.agency_name)

# Execute the query and fetch all agencies
agencies = db_session.execute(stmt).unique().scalars().all()

# Create a dictionary to map agency names to agency instances
agency_dict = {agency.agency_name: agency for agency in agencies}

# Process top-level agencies
for agency in agencies:
if "-" in agency.agency_name:
top_level_name = agency.agency_name.split("-")[0].strip()
# Find the top-level agency using the dictionary
top_level_agency = agency_dict.get(top_level_name)
if top_level_agency:
agency.top_level_agency = top_level_agency

# Apply pagination after processing
paginator: Paginator[Agency] = Paginator(
Agency, stmt, db_session, page_size=list_params.pagination.page_size
Expand Down

0 comments on commit 70c67f5

Please sign in to comment.