Fix POI distance calculation for buy listings

The distance calculator always queried the rentlisting table regardless of
listing type because get_listings() defaulted to RentListing when called
without query_parameters. Added a listing_type parameter to get_listings()
and _get_model_for_query() so callers can select the correct table directly.
This commit is contained in:
Viktor Barzin 2026-02-08 19:10:32 +00:00
parent 54bdcac14a
commit e431eaf2aa
No known key found for this signature in database
GPG key ID: 0EB088298288D958
2 changed files with 15 additions and 6 deletions

View file

@ -40,13 +40,14 @@ class ListingRepository:
query_parameters: QueryParameters | None = None, query_parameters: QueryParameters | None = None,
only_ids: list[int] | None = None, only_ids: list[int] | None = None,
limit: int | None = None, limit: int | None = None,
listing_type: ListingType | None = None,
) -> list[modelListing]: ) -> list[modelListing]:
""" """
Get all listings from the database. Get all listings from the database.
""" """
only_ids = only_ids or [] only_ids = only_ids or []
model = self._get_model_for_query(query_parameters) model = self._get_model_for_query(query_parameters, listing_type=listing_type)
query = select(model) query = select(model)
if only_ids: if only_ids:
@ -87,9 +88,18 @@ class ListingRepository:
yield listing yield listing
def _get_model_for_query( def _get_model_for_query(
self, query_parameters: QueryParameters | None self,
query_parameters: QueryParameters | None,
listing_type: ListingType | None = None,
) -> type[RentListing] | type[BuyListing]: ) -> type[RentListing] | type[BuyListing]:
"""Get the appropriate model class based on query parameters.""" """Get the appropriate model class based on query parameters or explicit listing type.
The explicit listing_type parameter takes precedence over query_parameters.
"""
if listing_type == ListingType.BUY:
return BuyListing
if listing_type is not None:
return RentListing
if query_parameters and query_parameters.listing_type == ListingType.BUY: if query_parameters and query_parameters.listing_type == ListingType.BUY:
return BuyListing return BuyListing
return RentListing return RentListing

View file

@ -7,7 +7,7 @@ from typing import Callable
import aiohttp import aiohttp
from config.routing_config import RoutingConfig from config.routing_config import RoutingConfig
from models.listing import BuyListing, ListingType, RentListing from models.listing import ListingType
from models.poi import PointOfInterest from models.poi import PointOfInterest
from models.poi_distance import POIDistance from models.poi_distance import POIDistance
from rec.osrm_client import osrm_table from rec.osrm_client import osrm_table
@ -53,10 +53,9 @@ async def calculate_poi_distances(
config = RoutingConfig.from_env() config = RoutingConfig.from_env()
# Load listings with coordinates # Load listings with coordinates
model = RentListing if listing_type == ListingType.RENT else BuyListing
listings = await listing_repo.get_listings( listings = await listing_repo.get_listings(
only_ids=listing_ids, only_ids=listing_ids,
query_parameters=None, listing_type=listing_type,
) )
if not listings: if not listings:
logger.info("No listings found for distance calculation") logger.info("No listings found for distance calculation")