From e431eaf2aa50b6486ebcbee1b6894e8562ffbc16 Mon Sep 17 00:00:00 2001 From: Viktor Barzin Date: Sun, 8 Feb 2026 19:10:32 +0000 Subject: [PATCH] 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. --- repositories/listing_repository.py | 16 +++++++++++++--- services/poi_distance_calculator.py | 5 ++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/repositories/listing_repository.py b/repositories/listing_repository.py index 3a8a2d8..ac74200 100644 --- a/repositories/listing_repository.py +++ b/repositories/listing_repository.py @@ -40,13 +40,14 @@ class ListingRepository: query_parameters: QueryParameters | None = None, only_ids: list[int] | None = None, limit: int | None = None, + listing_type: ListingType | None = None, ) -> list[modelListing]: """ Get all listings from the database. """ 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) if only_ids: @@ -87,9 +88,18 @@ class ListingRepository: yield listing def _get_model_for_query( - self, query_parameters: QueryParameters | None + self, + query_parameters: QueryParameters | None, + listing_type: ListingType | None = None, ) -> 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: return BuyListing return RentListing diff --git a/services/poi_distance_calculator.py b/services/poi_distance_calculator.py index 7b1378f..e38fe5a 100644 --- a/services/poi_distance_calculator.py +++ b/services/poi_distance_calculator.py @@ -7,7 +7,7 @@ from typing import Callable import aiohttp 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_distance import POIDistance from rec.osrm_client import osrm_table @@ -53,10 +53,9 @@ async def calculate_poi_distances( config = RoutingConfig.from_env() # Load listings with coordinates - model = RentListing if listing_type == ListingType.RENT else BuyListing listings = await listing_repo.get_listings( only_ids=listing_ids, - query_parameters=None, + listing_type=listing_type, ) if not listings: logger.info("No listings found for distance calculation")