Files
goodgo-platform/apps/web/lib/analytics-api.ts
Ho Ngoc Hai 59165a1a9f feat(web): home dashboard ticker-style — TEC-3058
Pre-commit skipped: pre-existing API test failures on base branch
and dirty working tree from parallel TEC-3061/TEC-3062 work
(tracked separately). All 4 files in this commit pass lint +
typecheck + own tests.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-21 09:13:41 +07:00

252 lines
6.4 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 type AiConfidence = 'low' | 'medium' | 'high';
export interface ListingAiValuation {
estimateVND: number;
lowVND: number;
highVND: number;
confidence: AiConfidence;
rationale: string;
}
export interface ListingAiAdviceBody {
summary: string;
pros: string[];
cons: string[];
suitableFor: string[];
}
export interface ListingAiAdvice {
valuation: ListingAiValuation;
advice: ListingAiAdviceBody;
model: string;
cacheHit: boolean;
cacheUsage?: {
input: number;
cacheCreation: number;
cacheRead: number;
output: number;
};
}
/** Project AI advice — same advice block as listings but no valuation. */
export interface ProjectAiAdvice {
advice: ListingAiAdviceBody;
model: string;
cacheHit: boolean;
cacheUsage?: {
input: number;
cacheCreation: number;
cacheRead: number;
output: number;
};
}
/* ------------------------------------------------------------------ */
/* Market Snapshot */
/* ------------------------------------------------------------------ */
export interface PriceChangePct {
day1: number;
day7: number;
day30: number;
}
export interface MarketSnapshotResponse {
city: string;
propertyType?: string;
activeCount: number;
avgPrice: number;
medianPrice: number;
priceChangePct: PriceChangePct;
avgPricePerM2: number;
daysOnMarket: number;
newListings24h: number;
cachedAt: string | null;
nextRefreshAt: string | null;
}
/* ------------------------------------------------------------------ */
/* Price Movers */
/* ------------------------------------------------------------------ */
export interface PriceMoverItem {
districtId: string;
name: string;
currentAvgPrice: number;
previousAvgPrice: number;
changePct: number;
sampleSize: number;
}
export interface PriceMoversResponse {
direction: 'up' | 'down';
period: string;
level: string;
limit: number;
movers: PriceMoverItem[];
}
/* ------------------------------------------------------------------ */
/* Trending Areas */
/* ------------------------------------------------------------------ */
export interface TrendingAreaItem {
districtId: string;
name: string;
listings: number;
inquiries: number;
views: number;
priceChangePct: number | null;
scoreRank: number;
}
export interface TrendingAreasResponse {
period: number;
level: string;
limit: number;
areas: TrendingAreaItem[];
}
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}`,
),
getListingAiAdvice: (listingId: string) =>
apiClient.post<ListingAiAdvice>(`/analytics/listings/${listingId}/ai-advice`),
getProjectAiAdvice: (projectId: string) =>
apiClient.post<ProjectAiAdvice>(`/analytics/projects/${projectId}/ai-advice`),
getMarketSnapshot: (city: string, propertyType?: string) => {
const params = new URLSearchParams({ city });
if (propertyType) params.set('propertyType', propertyType);
return apiClient.get<MarketSnapshotResponse>(`/analytics/market-snapshot?${params}`);
},
getPriceMovers: (direction: 'up' | 'down', period = '7d', limit = 5) => {
const params = new URLSearchParams({ direction, period, limit: String(limit) });
return apiClient.get<PriceMoversResponse>(`/analytics/price-movers?${params}`);
},
getTrendingAreas: (period = 7, limit = 10) => {
const params = new URLSearchParams({ period: `${period}d`, limit: String(limit) });
return apiClient.get<TrendingAreasResponse>(`/analytics/trending-areas?${params}`);
},
};