Files
goodgo-platform/apps/web/lib/validations/listings.ts
Ho Ngoc Hai 88429a1e51
Some checks failed
CI / Lint → Typecheck → Test → Build (22) (push) Failing after 6s
CI / E2E Tests (push) Has been skipped
CodeQL Analysis / CodeQL (javascript-typescript) (push) Failing after 1m8s
Deploy / Build API Image (push) Failing after 29s
E2E Tests / Playwright E2E (push) Failing after 13s
Security Scanning / Dependency Audit (pnpm) (push) Failing after 2s
Security Scanning / Trivy Scan — API Image (push) Failing after 1m9s
Security Scanning / Trivy Scan — Web Image (push) Failing after 37s
Security Scanning / Trivy Scan — AI Services Image (push) Failing after 1m2s
Security Scanning / Trivy Filesystem Scan (push) Failing after 51s
Deploy / Smoke Test Staging (push) Has been skipped
Deploy / Smoke Test Production (push) Has been skipped
Security Scanning / Security Gate (push) Failing after 1s
Deploy / Rollback Staging (push) Has been skipped
Deploy / Rollback Production (push) Has been skipped
Deploy / Build Web Image (push) Failing after 14s
Deploy / Build AI Services Image (push) Failing after 12s
Deploy / Deploy to Staging (push) Has been skipped
Deploy / Deploy to Production (push) Has been skipped
feat(listings): phase B — rich property fields + admin-authored personas
Schema (prisma/migrations/20260419000000_property_rich_fields)
--------------------------------------------------------------
New Prisma enums:
- Furnishing: FULLY_FURNISHED / BASIC_FURNISHED / UNFURNISHED
- PropertyCondition: NEW / LIKE_NEW / RENOVATED / USED

New Property columns (all optional / default empty, no data loss):
- furnishing, propertyCondition — enums above
- balconyDirection — reuses existing Direction enum
- maintenanceFeeVND BigInt (phí quản lý/tháng)
- parkingSlots Int
- viewType String[] (e.g. ["Sông","Thành phố"])
- petFriendly Boolean (null = unknown)
- suitableFor String[] — admin-chosen persona labels
- whyThisLocation Text — admin narrative

Backend wiring end-to-end
-------------------------
- Create/Update DTOs: @IsEnum/@IsString/@IsNumber/@IsBoolean/@IsArray
  validators; maintenanceFeeVND accepted as a numeric string, cast to
  BigInt on the way to Prisma. whyThisLocation capped at 2000 chars.
- Introduced a small `PropertyExtras` interface on the create/update
  commands so the constructor signature stays readable instead of
  ballooning to 30+ positional args. Handlers forward it to the repo.
- Prisma property repository writes all new columns via raw SQL
  INSERT/UPDATE and reads them on findById.
- ListingDetailData + findByIdWithProperty expose the 9 new fields
  (maintenanceFeeVND serialised as decimal string to avoid BigInt JSON).

Frontend
--------
- listings-api.ts: ListingDetail.property + CreateListingPayload carry
  the 9 new fields; Furnishing + PropertyCondition exported as string
  unions.
- validations/listings.ts: zod schema extended; FURNISHING_OPTIONS,
  PROPERTY_CONDITION_OPTIONS, VIEW_TYPE_OPTIONS label arrays added in
  the existing DIRECTIONS style (Vietnamese labels).
- listing-form-steps.tsx StepDetails: new "Nội thất & điều kiện"
  fieldset with selects/inputs for each field. viewType + suitableFor
  are comma-separated text (same convention as amenities).
  petFriendly is a 3-way select (không chọn / Có / Không).
- new/page.tsx + [id]/edit/page.tsx: submit handlers split CSV inputs
  into arrays, coerce petFriendly, prune empty selects.
- listing-detail-client.tsx Details card: new rows for furnishing,
  propertyCondition, balconyDirection, maintenanceFeeVND (VND
  formatted), parkingSlots, viewType (joined · ), petFriendly
  (Cho phép / Không cho phép / hide when null).
- PersonaFitCard now takes the listing directly and MERGES admin
  suitableFor (rendered first with a "Người đăng chọn" badge in primary
  accent) with the derived personas (deduped by label). When
  whyThisLocation is non-empty it overrides the derived narrative.

Tests
-----
- listing-detail-client.spec.tsx fixture gains all 9 nullable/empty
  defaults.
- listing-form-steps.spec.tsx direction-options duplication fixed.
- pnpm --filter @goodgo/api test --run: 1975/1975 pass.
- pnpm --filter @goodgo/web test --run: 624/624 pass.

Phase B of 4. Next: Phase E AI advisor via Anthropic Opus (URL+key to
be provided by the user).

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

133 lines
5.4 KiB
TypeScript

import { z } from 'zod';
export const TRANSACTION_TYPES = [
{ value: 'SALE', label: 'Bán' },
{ value: 'RENT', label: 'Cho thuê' },
] as const;
export const PROPERTY_TYPES = [
{ value: 'APARTMENT', label: 'Căn hộ' },
{ value: 'HOUSE', label: 'Nhà riêng' },
{ value: 'VILLA', label: 'Biệt thự' },
{ value: 'LAND', label: 'Đất nền' },
{ value: 'OFFICE', label: 'Văn phòng' },
{ value: 'SHOPHOUSE', label: 'Shophouse' },
] as const;
export const DIRECTIONS = [
{ value: 'NORTH', label: 'Bắc' },
{ value: 'SOUTH', label: 'Nam' },
{ value: 'EAST', label: 'Đông' },
{ value: 'WEST', label: 'Tây' },
{ value: 'NORTHEAST', label: 'Đông Bắc' },
{ value: 'NORTHWEST', label: 'Tây Bắc' },
{ value: 'SOUTHEAST', label: 'Đông Nam' },
{ value: 'SOUTHWEST', label: 'Tây Nam' },
] as const;
export const FURNISHING_OPTIONS = [
{ value: 'FULLY_FURNISHED', label: 'Đầy đủ nội thất' },
{ value: 'BASIC_FURNISHED', label: 'Nội thất cơ bản' },
{ value: 'UNFURNISHED', label: 'Bàn giao thô' },
] as const;
export const PROPERTY_CONDITION_OPTIONS = [
{ value: 'NEW', label: 'Mới' },
{ value: 'LIKE_NEW', label: 'Như mới' },
{ value: 'RENOVATED', label: 'Đã cải tạo' },
{ value: 'USED', label: 'Đã qua sử dụng' },
] as const;
export const VIEW_TYPE_OPTIONS = [
{ value: 'Sông', label: 'Sông' },
{ value: 'Thành phố', label: 'Thành phố' },
{ value: 'Công viên', label: 'Công viên' },
{ value: 'Hồ bơi', label: 'Hồ bơi' },
{ value: 'Sân vườn', label: 'Sân vườn' },
{ value: 'Biển', label: 'Biển' },
{ value: 'Nội khu', label: 'Nội khu' },
] as const;
export const LISTING_STATUSES = {
DRAFT: { label: 'Nháp', variant: 'secondary' as const },
PENDING_REVIEW: { label: 'Chờ duyệt', variant: 'warning' as const },
ACTIVE: { label: 'Đang bán', variant: 'success' as const },
RESERVED: { label: 'Đã đặt cọc', variant: 'info' as const },
SOLD: { label: 'Đã bán', variant: 'default' as const },
RENTED: { label: 'Đã cho thuê', variant: 'default' as const },
EXPIRED: { label: 'Hết hạn', variant: 'destructive' as const },
REJECTED: { label: 'Bị từ chối', variant: 'destructive' as const },
};
// ─── Step 1: Basic Info ──────────────────────────────────
export const listingBasicSchema = z.object({
transactionType: z.enum(['SALE', 'RENT'], {
message: 'Vui lòng chọn loại giao dịch',
}),
propertyType: z.enum(['APARTMENT', 'HOUSE', 'VILLA', 'LAND', 'OFFICE', 'SHOPHOUSE'], {
message: 'Vui lòng chọn loại bất động sản',
}),
title: z.string().min(5, 'Tiêu đề tối thiểu 5 ký tự'),
description: z.string().min(10, 'Mô tả tối thiểu 10 ký tự'),
});
// ─── Step 2: Location ────────────────────────────────────
export const listingLocationSchema = z.object({
address: z.string().min(1, 'Vui lòng nhập địa chỉ'),
ward: z.string().min(1, 'Vui lòng nhập phường/xã'),
district: z.string().min(1, 'Vui lòng nhập quận/huyện'),
city: z.string().min(1, 'Vui lòng nhập tỉnh/thành phố'),
latitude: z.string().optional(),
longitude: z.string().optional(),
});
// ─── Step 3: Details ─────────────────────────────────────
export const listingDetailsSchema = z.object({
areaM2: z.string().min(1, 'Diện tích tối thiểu 1 m²'),
usableAreaM2: z.string().optional(),
bedrooms: z.string().optional(),
bathrooms: z.string().optional(),
floors: z.string().optional(),
floor: z.string().optional(),
totalFloors: z.string().optional(),
direction: z.string().optional(),
yearBuilt: z.string().optional(),
legalStatus: z.string().optional(),
amenities: z.string().optional(),
projectName: z.string().optional(),
// Rich property fields (Phase B)
furnishing: z.string().optional(),
propertyCondition: z.string().optional(),
balconyDirection: z.string().optional(),
maintenanceFeeVND: z.string().optional(),
parkingSlots: z.string().optional(),
viewType: z.string().optional(), // comma-separated
petFriendly: z.string().optional(), // '' | 'true' | 'false'
suitableFor: z.string().optional(), // comma-separated
whyThisLocation: z.string().max(2000, 'Tối đa 2000 ký tự').optional(),
});
// ─── Step 4: Pricing ─────────────────────────────────────
export const listingPricingSchema = z.object({
priceVND: z.string().min(1, 'Vui lòng nhập giá'),
rentPriceMonthly: z.string().optional(),
commissionPct: z.string().optional(),
});
// ─── Full Schema ─────────────────────────────────────────
export const createListingSchema = listingBasicSchema
.merge(listingLocationSchema)
.merge(listingDetailsSchema)
.merge(listingPricingSchema);
export type ListingBasicData = z.infer<typeof listingBasicSchema>;
export type ListingLocationData = z.infer<typeof listingLocationSchema>;
export type ListingDetailsData = z.infer<typeof listingDetailsSchema>;
export type ListingPricingData = z.infer<typeof listingPricingSchema>;
export type CreateListingFormData = z.infer<typeof createListingSchema>;