Files
goodgo-platform/apps/web/lib/analytics-api.ts
Ho Ngoc Hai 08c8b5e027
Some checks failed
CI / E2E Tests (push) Has been skipped
CI / Lint → Typecheck → Test → Build (22) (push) Failing after 6s
Security Scanning / Trivy Scan — API Image (push) Failing after 25s
Security Scanning / Trivy Scan — Web Image (push) Failing after 26s
Security Scanning / Trivy Scan — AI Services Image (push) Failing after 23s
CodeQL Analysis / CodeQL (javascript-typescript) (push) Failing after 56s
Deploy / Build API Image (push) Failing after 18s
Deploy / Build Web Image (push) Failing after 10s
Deploy / Build AI Services Image (push) Failing after 9s
E2E Tests / Playwright E2E (push) Failing after 7s
Security Scanning / Dependency Audit (pnpm) (push) Failing after 2s
Security Scanning / Trivy Filesystem Scan (push) Failing after 26s
Deploy / Deploy to Staging (push) Has been skipped
Deploy / Smoke Test Staging (push) Has been skipped
Deploy / Deploy to Production (push) Has been skipped
Deploy / Smoke Test Production (push) Has been skipped
Security Scanning / Security Gate (push) Failing after 0s
Deploy / Rollback Staging (push) Has been skipped
Deploy / Rollback Production (push) Has been skipped
feat(listings): phase C — nearby POIs on listing detail map
Backend
-------
- New endpoint GET /analytics/pois/nearby?lat&lng&radius&limit (public,
  no guard). Mirrors the neighborhoods/:district/score shape.
- Prisma $queryRawUnsafe with PostGIS ST_DWithin on POI.location::geography
  and ST_Distance for the ordered-by-distance result. Default radius 2km,
  max 10km; default limit 30, max 100.
- Response maps POIType enum → frontend POICategory so the existing pill
  filter in NeighborhoodPOIMap works out of the box:
    SCHOOL/UNIVERSITY → school
    HOSPITAL/CLINIC/PHARMACY → hospital
    METRO_STATION/BUS_STOP → transit
    MALL/MARKET/SUPERMARKET/BANK/ATM → shopping
    RESTAURANT/CAFE → restaurant
    PARK → park
    else → shopping (fallback, still filterable)
- New files: application/queries/get-nearby-pois/{query,handler}.ts +
  presentation/dto/get-nearby-pois.dto.ts. Registered in analytics.module.ts.

Frontend
--------
- analytics-api.ts: exports NearbyPOI, NearbyPOIsResponse, NearbyPOICategory
  and analyticsApi.getNearbyPOIs(lat, lng, radius?, limit?).
- listing-detail-client.tsx: the "Vị trí trên bản đồ" card no longer
  renders <ListingMap> for a single pin — it now renders
  <NeighborhoodPOIMap> with the property's coords as center, the nearby
  POIs as markers, and the existing category-filter pills. A small
  "Tìm thấy N điểm quan tâm trong bán kính 2 km" summary sits below.
- The neighborhood score radar card remains below, untouched.
- The spec fixture + mocks extended for the new analyticsApi dependency.

No schema change, no migration. Phase C of 4.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 14:48:09 +07:00

121 lines
2.9 KiB
TypeScript

import { apiClient } from './api-client';
export interface MarketReportDistrict {
district: string;
city: string;
propertyType: string;
period: string;
medianPrice: string;
avgPriceM2: number;
totalListings: number;
daysOnMarket: number;
inventoryLevel: number;
absorptionRate: number | null;
yoyChange: number | null;
}
export interface MarketReportResponse {
city: string;
period: string;
districts: MarketReportDistrict[];
}
export interface HeatmapDataPoint {
district: string;
city: string;
avgPriceM2: number;
totalListings: number;
medianPrice: string;
}
export interface HeatmapResponse {
city: string;
period: string;
dataPoints: HeatmapDataPoint[];
}
export interface PriceTrendPoint {
period: string;
medianPrice: string;
avgPriceM2: number;
totalListings: number;
}
export interface PriceTrendResponse {
district: string;
city: string;
propertyType: string;
trend: PriceTrendPoint[];
}
export interface DistrictStats {
district: string;
city: string;
propertyType: string;
medianPrice: string;
avgPriceM2: number;
totalListings: number;
daysOnMarket: number;
inventoryLevel: number;
absorptionRate: number | null;
yoyChange: number | null;
}
export interface DistrictStatsResponse {
city: string;
period: string;
districts: DistrictStats[];
}
export type NearbyPOICategory =
| 'school'
| 'hospital'
| 'transit'
| 'shopping'
| 'restaurant'
| 'park';
export interface NearbyPOI {
id: string;
name: string;
type: string;
category: NearbyPOICategory;
lat: number;
lng: number;
distance: number;
address: string | null;
}
export interface NearbyPOIsResponse {
pois: NearbyPOI[];
center: { lat: number; lng: number };
}
export const analyticsApi = {
getMarketReport: (city: string, period: string, propertyType?: string) => {
const params = new URLSearchParams({ city, period });
if (propertyType) params.set('propertyType', propertyType);
return apiClient.get<MarketReportResponse>(`/analytics/market-report?${params}`);
},
getHeatmap: (city: string, period: string) => {
const params = new URLSearchParams({ city, period });
return apiClient.get<HeatmapResponse>(`/analytics/heatmap?${params}`);
},
getPriceTrend: (district: string, city: string, propertyType: string, periods: string[]) => {
const params = new URLSearchParams({ district, city, propertyType, periods: periods.join(',') });
return apiClient.get<PriceTrendResponse>(`/analytics/price-trend?${params}`);
},
getDistrictStats: (city: string, period: string) => {
const params = new URLSearchParams({ city, period });
return apiClient.get<DistrictStatsResponse>(`/analytics/district-stats?${params}`);
},
getNearbyPOIs: (lat: number, lng: number, radius = 2000, limit = 30) =>
apiClient.get<NearbyPOIsResponse>(
`/analytics/pois/nearby?lat=${lat}&lng=${lng}&radius=${radius}&limit=${limit}`,
),
};