Some checks failed
CodeQL Analysis / CodeQL (javascript-typescript) (push) Failing after 1m31s
Deploy / Build API Image (push) Failing after 25s
E2E Tests / Playwright E2E (push) Failing after 23s
Security Scanning / Dependency Audit (pnpm) (push) Failing after 6s
Deploy / Build Web Image (push) Failing after 17s
Deploy / Build AI Services Image (push) Failing after 13s
Security Scanning / Trivy Scan — Web Image (push) Failing after 58s
Security Scanning / Trivy Scan — AI Services Image (push) Failing after 51s
Security Scanning / Trivy Scan — API Image (push) Failing after 1m55s
Security Scanning / Trivy Filesystem Scan (push) Failing after 45s
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 3s
Deploy / Rollback Staging (push) Has been skipped
Deploy / Rollback Production (push) Has been skipped
CI / Lint → Typecheck → Test → Build (22) (push) Failing after 8s
CI / E2E Tests (push) Has been skipped
Closes the last gap from the tec-2725 branch: the valuation form's v2
extended-features section and POST endpoint can now submit real
predictions through to the Python ensemble model.
Backend
- New DTO apps/api/src/modules/analytics/presentation/dto/predict-valuation.dto.ts
with all v1 fields + 8 v2 fields (useV2 toggle, distanceToHospital/Park/
Mall in km, floodZoneRisk enum NONE|LOW|MEDIUM|HIGH, hasElevator/
Parking/Pool booleans).
- New CQRS handler apps/api/src/modules/analytics/application/queries/
predict-valuation/ that routes to AVM_SERVICE.estimateValue() with the
full request body.
- Extend AVMParams (domain) with the same v2 fields + inline v1 fields
(district, city, bedrooms, bathrooms, floors, frontage, roadWidth,
hasLegalPaper, projectId, imageUrl, description, deepAnalysis).
- HttpAVMService.estimateViaAi now branches on `useV2`: v2 calls the new
aiClient.predictV2() → POST /avm/v2/predict on the Python service,
mapping floodZoneRisk enum → 0..1 float and computing
building_age_years from yearBuilt. v1 path gets all the inline
descriptors wired through so non-propertyId calls no longer lose
context.
- AiServiceClient gets AiPredictV2Request / AiPredictV2Response types
mirroring libs/ai-services/app/models/avm_v2.py::AVMv2PredictRequest
(which already accepts all 7 numeric/boolean v2 fields — no Python
change needed).
- Register PredictValuationHandler in AnalyticsModule.
- New route POST /analytics/valuation on AnalyticsController:
JwtAuthGuard + QuotaGuard + EndpointRateLimitGuard (10/min),
@RequireQuota('analytics_queries'), full Swagger doc. Total endpoint
count 179 → 180.
Frontend
- Extend ValuationRequest with useV2, 3 distance-km fields,
floodZoneRisk, hasElevator/Parking/Pool + export FloodZoneRisk type
and FLOOD_RISK_OPTIONS.
- valuationApi.predict() body mapping now includes v2 fields and renames
'areaM2' → 'area' to match the backend DTO contract.
- valuationFormSchema gains matching optional Zod fields + exports
FLOOD_RISK_OPTIONS for the form.
- valuation-form.tsx gets:
* Image upload hardening: MIME+size validation (JPG/PNG ≤5MB) before
preview, role="progressbar" + aria-labels on the progress bar,
role="alert" + data-testid="image-upload-error" on errors. Matches
the upload-progress part of the task/tec-2725 commit 4ee0129 that
was previously parked as blocked.
* New Sparkles-branded "Mô hình v2 (Ensemble)" toggle alongside the
existing Bot-branded "Phân tích chuyên sâu" toggle.
* Collapsible "Đặc trưng mở rộng (AVM v2)" section with distance
inputs, flood-risk select, and three amenity checkboxes.
* handleFormSubmit passes all v2 fields through to onSubmit.
Python service unchanged — AVMv2PredictRequest already has every field
we send (distance_to_hospital_km, flood_zone_risk as float,
has_elevator/parking/pool, etc.).
Typecheck clean for the valuation surface. Pre-existing errors in
metadata.spec.ts and transfer-wizard-client.tsx are unrelated and left
for a follow-up.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
274 lines
7.1 KiB
TypeScript
274 lines
7.1 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { LoggerService } from '@modules/shared';
|
|
|
|
export interface AiPredictRequest {
|
|
area: number;
|
|
district: string;
|
|
city: string;
|
|
property_type: string;
|
|
bedrooms?: number;
|
|
bathrooms?: number;
|
|
floors?: number;
|
|
frontage?: number;
|
|
road_width?: number;
|
|
year_built?: number | null;
|
|
has_legal_paper?: boolean;
|
|
}
|
|
|
|
export interface AiPredictResponse {
|
|
estimated_price_vnd: number;
|
|
confidence: number;
|
|
price_per_m2: number;
|
|
price_range_low: number;
|
|
price_range_high: number;
|
|
}
|
|
|
|
/**
|
|
* AVM v2 request — extended feature set for residential ensemble.
|
|
* Matches `AVMv2PredictRequest` in libs/ai-services/app/models/avm_v2.py.
|
|
*/
|
|
export interface AiPredictV2Request {
|
|
district: string;
|
|
city: string;
|
|
property_type: string;
|
|
area_m2: number;
|
|
distance_to_cbd_km?: number;
|
|
distance_to_metro_km?: number;
|
|
distance_to_school_km?: number;
|
|
distance_to_hospital_km?: number;
|
|
distance_to_park_km?: number;
|
|
distance_to_mall_km?: number;
|
|
flood_zone_risk?: number;
|
|
neighborhood_score?: number;
|
|
rooms?: number;
|
|
floor_level?: number;
|
|
total_floors?: number;
|
|
direction?: string;
|
|
floor_ratio?: number;
|
|
building_age_years?: number;
|
|
has_elevator?: boolean;
|
|
has_parking?: boolean;
|
|
has_pool?: boolean;
|
|
has_legal_paper?: boolean;
|
|
developer_reputation?: number;
|
|
avg_price_district_3m_vnd_m2?: number;
|
|
listing_density?: number;
|
|
absorption_rate?: number;
|
|
dom_avg?: number;
|
|
price_momentum_30d?: number;
|
|
yoy_change?: number;
|
|
renovation_score?: number;
|
|
view_quality?: number;
|
|
interior_quality?: number;
|
|
noise_level?: number;
|
|
natural_light?: number;
|
|
month?: number;
|
|
quarter?: number;
|
|
is_year_end?: boolean;
|
|
}
|
|
|
|
export interface AiPredictV2FeatureImportance {
|
|
feature: string;
|
|
importance: number;
|
|
}
|
|
|
|
export interface AiPredictV2Comparable {
|
|
district: string;
|
|
property_type: string;
|
|
area_m2: number;
|
|
price_vnd: number;
|
|
price_per_m2_vnd: number;
|
|
similarity_score: number;
|
|
}
|
|
|
|
export interface AiPredictV2Response {
|
|
estimated_price_vnd: number;
|
|
confidence: number;
|
|
price_per_m2_vnd: number;
|
|
price_range_low_vnd: number;
|
|
price_range_high_vnd: number;
|
|
drivers?: AiPredictV2FeatureImportance[];
|
|
comparables?: AiPredictV2Comparable[];
|
|
model_version?: string;
|
|
ensemble_method?: string;
|
|
}
|
|
|
|
export interface AiIndustrialPredictRequest {
|
|
province: string;
|
|
region: string;
|
|
park_occupancy_rate: number;
|
|
park_area_ha: number;
|
|
park_age_years: number;
|
|
distance_to_port_km: number;
|
|
distance_to_airport_km: number;
|
|
distance_to_highway_km: number;
|
|
property_type: string;
|
|
area_m2: number;
|
|
ceiling_height_m?: number;
|
|
floor_load_ton_m2?: number;
|
|
power_capacity_kva?: number;
|
|
building_coverage?: number;
|
|
loading_docks?: number;
|
|
zoning?: string;
|
|
industry_demand_index?: number;
|
|
fdi_province_musd?: number;
|
|
labor_cost_province_vnd?: number;
|
|
logistics_connectivity_score?: number;
|
|
}
|
|
|
|
export interface AiIndustrialComparable {
|
|
park_name: string;
|
|
province: string;
|
|
property_type: string;
|
|
area_m2: number;
|
|
rent_usd_m2: number;
|
|
similarity_score: number;
|
|
}
|
|
|
|
export interface AiIndustrialFeatureImportance {
|
|
feature: string;
|
|
importance: number;
|
|
}
|
|
|
|
export interface AiIndustrialPredictResponse {
|
|
estimated_rent_usd_m2: number;
|
|
confidence: number;
|
|
rent_range_low_usd_m2: number;
|
|
rent_range_high_usd_m2: number;
|
|
annual_rent_usd_m2: number;
|
|
total_monthly_rent_usd: number;
|
|
comparables: AiIndustrialComparable[];
|
|
drivers: AiIndustrialFeatureImportance[];
|
|
model_version: string;
|
|
}
|
|
|
|
export interface AiModerationRequest {
|
|
text: string;
|
|
context?: string;
|
|
}
|
|
|
|
export interface AiModerationFlag {
|
|
category: string;
|
|
severity: string;
|
|
matched_text: string;
|
|
reason: string;
|
|
}
|
|
|
|
export interface AiModerationResponse {
|
|
is_flagged: boolean;
|
|
score: number;
|
|
flags: AiModerationFlag[];
|
|
cleaned_text: string | null;
|
|
}
|
|
|
|
export interface AiNeighborhoodPOICounts {
|
|
education: number;
|
|
healthcare: number;
|
|
transport: number;
|
|
shopping: number;
|
|
greenery: number;
|
|
safety: number;
|
|
}
|
|
|
|
export interface AiNeighborhoodScoreRequest {
|
|
district: string;
|
|
city: string;
|
|
poi_counts: AiNeighborhoodPOICounts;
|
|
}
|
|
|
|
export interface AiNeighborhoodScoreResponse {
|
|
district: string;
|
|
city: string;
|
|
education_score: number;
|
|
healthcare_score: number;
|
|
transport_score: number;
|
|
shopping_score: number;
|
|
greenery_score: number;
|
|
safety_score: number;
|
|
total_score: number;
|
|
poi_counts: Record<string, number>;
|
|
algorithm_version: string;
|
|
}
|
|
|
|
export const AI_SERVICE_CLIENT = Symbol('AI_SERVICE_CLIENT');
|
|
|
|
export interface IAiServiceClient {
|
|
predict(req: AiPredictRequest): Promise<AiPredictResponse>;
|
|
predictV2(req: AiPredictV2Request): Promise<AiPredictV2Response>;
|
|
predictIndustrial(req: AiIndustrialPredictRequest): Promise<AiIndustrialPredictResponse>;
|
|
moderate(req: AiModerationRequest): Promise<AiModerationResponse>;
|
|
scoreNeighborhood(req: AiNeighborhoodScoreRequest): Promise<AiNeighborhoodScoreResponse>;
|
|
isAvailable(): Promise<boolean>;
|
|
}
|
|
|
|
@Injectable()
|
|
export class AiServiceClient implements IAiServiceClient {
|
|
private readonly baseUrl: string;
|
|
private readonly apiKey: string;
|
|
private readonly timeoutMs: number;
|
|
|
|
constructor(private readonly logger: LoggerService) {
|
|
this.baseUrl = process.env['AI_SERVICE_URL'] ?? 'http://localhost:8000';
|
|
this.apiKey = process.env['AI_SERVICE_API_KEY'] ?? '';
|
|
this.timeoutMs = Number(process.env['AI_SERVICE_TIMEOUT_MS']) || 5000;
|
|
}
|
|
|
|
async predict(req: AiPredictRequest): Promise<AiPredictResponse> {
|
|
return this.post<AiPredictResponse>('/avm/predict', req);
|
|
}
|
|
|
|
async predictV2(req: AiPredictV2Request): Promise<AiPredictV2Response> {
|
|
return this.post<AiPredictV2Response>('/avm/v2/predict', req);
|
|
}
|
|
|
|
async predictIndustrial(req: AiIndustrialPredictRequest): Promise<AiIndustrialPredictResponse> {
|
|
return this.post<AiIndustrialPredictResponse>('/avm/industrial/predict', req);
|
|
}
|
|
|
|
async moderate(req: AiModerationRequest): Promise<AiModerationResponse> {
|
|
return this.post<AiModerationResponse>('/moderation/check', req);
|
|
}
|
|
|
|
async scoreNeighborhood(
|
|
req: AiNeighborhoodScoreRequest,
|
|
): Promise<AiNeighborhoodScoreResponse> {
|
|
return this.post<AiNeighborhoodScoreResponse>('/neighborhood/score', req);
|
|
}
|
|
|
|
async isAvailable(): Promise<boolean> {
|
|
try {
|
|
const response = await fetch(`${this.baseUrl}/health`, {
|
|
method: 'GET',
|
|
signal: AbortSignal.timeout(2000),
|
|
});
|
|
return response.ok;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private async post<T>(path: string, body: unknown): Promise<T> {
|
|
const url = `${this.baseUrl}${path}`;
|
|
const headers: Record<string, string> = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
if (this.apiKey) {
|
|
headers['X-API-Key'] = this.apiKey;
|
|
}
|
|
|
|
const response = await fetch(url, {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify(body),
|
|
signal: AbortSignal.timeout(this.timeoutMs),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text().catch(() => '');
|
|
throw new Error(`AI service ${path} returned ${response.status}: ${text}`);
|
|
}
|
|
|
|
return response.json() as Promise<T>;
|
|
}
|
|
}
|