- Add init migration with all 23 models including PostGIS - Add seed script for districts, plans, and sample data Co-Authored-By: Paperclip <noreply@paperclip.ing>
787 lines
20 KiB
TypeScript
787 lines
20 KiB
TypeScript
import {
|
|
PrismaClient,
|
|
UserRole,
|
|
PlanTier,
|
|
PropertyType,
|
|
TransactionType,
|
|
ListingStatus,
|
|
Direction,
|
|
} from '@prisma/client';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
// =============================================================================
|
|
// Districts & Wards — HCM & Hanoi
|
|
// =============================================================================
|
|
|
|
const HCM_DISTRICTS = [
|
|
{
|
|
district: 'Quận 1',
|
|
wards: [
|
|
'Bến Nghé',
|
|
'Bến Thành',
|
|
'Cầu Kho',
|
|
'Cầu Ông Lãnh',
|
|
'Cô Giang',
|
|
'Đa Kao',
|
|
'Nguyễn Cư Trinh',
|
|
'Nguyễn Thái Bình',
|
|
'Phạm Ngũ Lão',
|
|
'Tân Định',
|
|
],
|
|
},
|
|
{
|
|
district: 'Quận 3',
|
|
wards: [
|
|
'Phường 1',
|
|
'Phường 2',
|
|
'Phường 3',
|
|
'Phường 4',
|
|
'Phường 5',
|
|
'Phường 9',
|
|
'Phường 10',
|
|
'Phường 11',
|
|
'Phường 12',
|
|
'Phường 13',
|
|
'Phường 14',
|
|
'Võ Thị Sáu',
|
|
],
|
|
},
|
|
{
|
|
district: 'Quận 7',
|
|
wards: [
|
|
'Bình Thuận',
|
|
'Phú Mỹ',
|
|
'Phú Thuận',
|
|
'Tân Hưng',
|
|
'Tân Kiểng',
|
|
'Tân Phong',
|
|
'Tân Phú',
|
|
'Tân Quy',
|
|
'Tân Thuận Đông',
|
|
'Tân Thuận Tây',
|
|
],
|
|
},
|
|
{
|
|
district: 'Thủ Đức',
|
|
wards: [
|
|
'An Khánh',
|
|
'An Lợi Đông',
|
|
'An Phú',
|
|
'Bình Chiểu',
|
|
'Bình Thọ',
|
|
'Bình Trưng Đông',
|
|
'Bình Trưng Tây',
|
|
'Cát Lái',
|
|
'Hiệp Bình Chánh',
|
|
'Hiệp Bình Phước',
|
|
'Linh Chiểu',
|
|
'Linh Đông',
|
|
'Linh Tây',
|
|
'Linh Trung',
|
|
'Linh Xuân',
|
|
'Long Bình',
|
|
'Long Phước',
|
|
'Long Thạnh Mỹ',
|
|
'Long Trường',
|
|
'Phú Hữu',
|
|
'Phước Bình',
|
|
'Phước Long A',
|
|
'Phước Long B',
|
|
'Tam Bình',
|
|
'Tam Phú',
|
|
'Tân Phú',
|
|
'Thạnh Mỹ Lợi',
|
|
'Thảo Điền',
|
|
'Thủ Thiêm',
|
|
'Trường Thạnh',
|
|
'Trường Thọ',
|
|
],
|
|
},
|
|
{
|
|
district: 'Quận Bình Thạnh',
|
|
wards: [
|
|
'Phường 1',
|
|
'Phường 2',
|
|
'Phường 3',
|
|
'Phường 5',
|
|
'Phường 6',
|
|
'Phường 7',
|
|
'Phường 11',
|
|
'Phường 12',
|
|
'Phường 13',
|
|
'Phường 14',
|
|
'Phường 15',
|
|
'Phường 17',
|
|
'Phường 19',
|
|
'Phường 21',
|
|
'Phường 22',
|
|
'Phường 24',
|
|
'Phường 25',
|
|
'Phường 26',
|
|
'Phường 27',
|
|
'Phường 28',
|
|
],
|
|
},
|
|
{
|
|
district: 'Quận Phú Nhuận',
|
|
wards: [
|
|
'Phường 1',
|
|
'Phường 2',
|
|
'Phường 3',
|
|
'Phường 4',
|
|
'Phường 5',
|
|
'Phường 7',
|
|
'Phường 8',
|
|
'Phường 9',
|
|
'Phường 10',
|
|
'Phường 11',
|
|
'Phường 13',
|
|
'Phường 15',
|
|
'Phường 17',
|
|
],
|
|
},
|
|
{
|
|
district: 'Quận Tân Bình',
|
|
wards: [
|
|
'Phường 1',
|
|
'Phường 2',
|
|
'Phường 3',
|
|
'Phường 4',
|
|
'Phường 5',
|
|
'Phường 6',
|
|
'Phường 7',
|
|
'Phường 8',
|
|
'Phường 9',
|
|
'Phường 10',
|
|
'Phường 11',
|
|
'Phường 12',
|
|
'Phường 13',
|
|
'Phường 14',
|
|
'Phường 15',
|
|
],
|
|
},
|
|
{
|
|
district: 'Quận Gò Vấp',
|
|
wards: [
|
|
'Phường 1',
|
|
'Phường 3',
|
|
'Phường 4',
|
|
'Phường 5',
|
|
'Phường 6',
|
|
'Phường 7',
|
|
'Phường 8',
|
|
'Phường 9',
|
|
'Phường 10',
|
|
'Phường 11',
|
|
'Phường 12',
|
|
'Phường 13',
|
|
'Phường 14',
|
|
'Phường 15',
|
|
'Phường 16',
|
|
'Phường 17',
|
|
],
|
|
},
|
|
];
|
|
|
|
const HANOI_DISTRICTS = [
|
|
{
|
|
district: 'Hoàn Kiếm',
|
|
wards: [
|
|
'Chương Dương',
|
|
'Cửa Đông',
|
|
'Cửa Nam',
|
|
'Đồng Xuân',
|
|
'Hàng Bạc',
|
|
'Hàng Bài',
|
|
'Hàng Bồ',
|
|
'Hàng Bông',
|
|
'Hàng Buồm',
|
|
'Hàng Đào',
|
|
'Hàng Gai',
|
|
'Hàng Mã',
|
|
'Hàng Trống',
|
|
'Lý Thái Tổ',
|
|
'Phan Chu Trinh',
|
|
'Phúc Tân',
|
|
'Tràng Tiền',
|
|
'Trần Hưng Đạo',
|
|
],
|
|
},
|
|
{
|
|
district: 'Ba Đình',
|
|
wards: [
|
|
'Cống Vị',
|
|
'Điện Biên',
|
|
'Đội Cấn',
|
|
'Giảng Võ',
|
|
'Kim Mã',
|
|
'Liễu Giai',
|
|
'Ngọc Hà',
|
|
'Ngọc Khánh',
|
|
'Nguyễn Trung Trực',
|
|
'Phúc Xá',
|
|
'Quán Thánh',
|
|
'Thành Công',
|
|
'Trúc Bạch',
|
|
'Vĩnh Phúc',
|
|
],
|
|
},
|
|
{
|
|
district: 'Đống Đa',
|
|
wards: [
|
|
'Cát Linh',
|
|
'Hàng Bột',
|
|
'Khâm Thiên',
|
|
'Khương Thượng',
|
|
'Kim Liên',
|
|
'Láng Hạ',
|
|
'Láng Thượng',
|
|
'Nam Đồng',
|
|
'Ngã Tư Sở',
|
|
'Ô Chợ Dừa',
|
|
'Phương Liên',
|
|
'Phương Mai',
|
|
'Quang Trung',
|
|
'Quốc Tử Giám',
|
|
'Thổ Quan',
|
|
'Trung Liệt',
|
|
'Trung Phụng',
|
|
'Trung Tự',
|
|
'Văn Chương',
|
|
'Văn Miếu',
|
|
],
|
|
},
|
|
{
|
|
district: 'Hai Bà Trưng',
|
|
wards: [
|
|
'Bách Khoa',
|
|
'Bạch Đằng',
|
|
'Bạch Mai',
|
|
'Bùi Thị Xuân',
|
|
'Cầu Dền',
|
|
'Đồng Mác',
|
|
'Đồng Nhân',
|
|
'Đồng Tâm',
|
|
'Lê Đại Hành',
|
|
'Minh Khai',
|
|
'Ngô Thì Nhậm',
|
|
'Nguyễn Du',
|
|
'Phạm Đình Hổ',
|
|
'Phố Huế',
|
|
'Quỳnh Lôi',
|
|
'Quỳnh Mai',
|
|
'Thanh Lương',
|
|
'Thanh Nhàn',
|
|
'Trương Định',
|
|
'Vĩnh Tuy',
|
|
],
|
|
},
|
|
{
|
|
district: 'Cầu Giấy',
|
|
wards: [
|
|
'Dịch Vọng',
|
|
'Dịch Vọng Hậu',
|
|
'Mai Dịch',
|
|
'Nghĩa Đô',
|
|
'Nghĩa Tân',
|
|
'Quan Hoa',
|
|
'Trung Hòa',
|
|
'Yên Hòa',
|
|
],
|
|
},
|
|
{
|
|
district: 'Tây Hồ',
|
|
wards: [
|
|
'Bưởi',
|
|
'Nhật Tân',
|
|
'Phú Thượng',
|
|
'Quảng An',
|
|
'Thụy Khuê',
|
|
'Tứ Liên',
|
|
'Xuân La',
|
|
'Yên Phụ',
|
|
],
|
|
},
|
|
{
|
|
district: 'Nam Từ Liêm',
|
|
wards: [
|
|
'Cầu Diễn',
|
|
'Đại Mỗ',
|
|
'Mễ Trì',
|
|
'Mỹ Đình 1',
|
|
'Mỹ Đình 2',
|
|
'Phú Đô',
|
|
'Phương Canh',
|
|
'Tây Mỗ',
|
|
'Trung Văn',
|
|
'Xuân Phương',
|
|
],
|
|
},
|
|
];
|
|
|
|
// =============================================================================
|
|
// Subscription Plans
|
|
// =============================================================================
|
|
|
|
const PLANS = [
|
|
{
|
|
tier: PlanTier.FREE,
|
|
name: 'Miễn phí',
|
|
priceMonthlyVND: BigInt(0),
|
|
priceYearlyVND: BigInt(0),
|
|
maxListings: 3,
|
|
maxSavedSearches: 5,
|
|
features: {
|
|
basicSearch: true,
|
|
listingPost: true,
|
|
maxPhotos: 5,
|
|
analytics: false,
|
|
prioritySupport: false,
|
|
aiValuation: false,
|
|
featuredListing: false,
|
|
},
|
|
},
|
|
{
|
|
tier: PlanTier.AGENT_PRO,
|
|
name: 'Agent Pro',
|
|
priceMonthlyVND: BigInt(499_000),
|
|
priceYearlyVND: BigInt(4_990_000),
|
|
maxListings: 50,
|
|
maxSavedSearches: 30,
|
|
features: {
|
|
basicSearch: true,
|
|
listingPost: true,
|
|
maxPhotos: 30,
|
|
analytics: true,
|
|
prioritySupport: true,
|
|
aiValuation: true,
|
|
featuredListing: true,
|
|
leadManagement: true,
|
|
agentProfile: true,
|
|
},
|
|
},
|
|
{
|
|
tier: PlanTier.INVESTOR,
|
|
name: 'Investor',
|
|
priceMonthlyVND: BigInt(999_000),
|
|
priceYearlyVND: BigInt(9_990_000),
|
|
maxListings: 20,
|
|
maxSavedSearches: 100,
|
|
features: {
|
|
basicSearch: true,
|
|
listingPost: true,
|
|
maxPhotos: 15,
|
|
analytics: true,
|
|
prioritySupport: true,
|
|
aiValuation: true,
|
|
featuredListing: false,
|
|
marketReports: true,
|
|
priceAlerts: true,
|
|
portfolioTracking: true,
|
|
},
|
|
},
|
|
{
|
|
tier: PlanTier.ENTERPRISE,
|
|
name: 'Enterprise',
|
|
priceMonthlyVND: BigInt(4_990_000),
|
|
priceYearlyVND: BigInt(49_900_000),
|
|
maxListings: null,
|
|
maxSavedSearches: null,
|
|
features: {
|
|
basicSearch: true,
|
|
listingPost: true,
|
|
maxPhotos: 100,
|
|
analytics: true,
|
|
prioritySupport: true,
|
|
aiValuation: true,
|
|
featuredListing: true,
|
|
leadManagement: true,
|
|
agentProfile: true,
|
|
marketReports: true,
|
|
priceAlerts: true,
|
|
portfolioTracking: true,
|
|
apiAccess: true,
|
|
whiteLabel: true,
|
|
dedicatedSupport: true,
|
|
},
|
|
},
|
|
];
|
|
|
|
// =============================================================================
|
|
// Sample coordinates for HCM districts
|
|
// =============================================================================
|
|
|
|
const SAMPLE_LOCATIONS: Record<string, { lat: number; lng: number }> = {
|
|
'Quận 1': { lat: 10.7769, lng: 106.7009 },
|
|
'Quận 3': { lat: 10.7834, lng: 106.6867 },
|
|
'Quận 7': { lat: 10.734, lng: 106.7218 },
|
|
'Thủ Đức': { lat: 10.8544, lng: 106.7536 },
|
|
'Quận Bình Thạnh': { lat: 10.8065, lng: 106.7098 },
|
|
'Quận Phú Nhuận': { lat: 10.7993, lng: 106.6815 },
|
|
'Quận Tân Bình': { lat: 10.8016, lng: 106.6525 },
|
|
'Quận Gò Vấp': { lat: 10.8384, lng: 106.6652 },
|
|
};
|
|
|
|
// =============================================================================
|
|
// Seed functions
|
|
// =============================================================================
|
|
|
|
async function seedPlans() {
|
|
console.log('Seeding subscription plans...');
|
|
for (const plan of PLANS) {
|
|
await prisma.plan.upsert({
|
|
where: { tier: plan.tier },
|
|
update: {
|
|
name: plan.name,
|
|
priceMonthlyVND: plan.priceMonthlyVND,
|
|
priceYearlyVND: plan.priceYearlyVND,
|
|
maxListings: plan.maxListings,
|
|
maxSavedSearches: plan.maxSavedSearches,
|
|
features: plan.features,
|
|
},
|
|
create: plan,
|
|
});
|
|
}
|
|
console.log(` ✓ ${PLANS.length} plans seeded`);
|
|
}
|
|
|
|
async function seedUsers() {
|
|
console.log('Seeding sample users...');
|
|
|
|
const admin = await prisma.user.upsert({
|
|
where: { phone: '0900000001' },
|
|
update: {},
|
|
create: {
|
|
phone: '0900000001',
|
|
email: 'admin@goodgo.vn',
|
|
fullName: 'Admin GoodGo',
|
|
role: UserRole.ADMIN,
|
|
kycStatus: 'VERIFIED',
|
|
isActive: true,
|
|
},
|
|
});
|
|
|
|
const agent1 = await prisma.user.upsert({
|
|
where: { phone: '0900000002' },
|
|
update: {},
|
|
create: {
|
|
phone: '0900000002',
|
|
email: 'agent.nguyen@goodgo.vn',
|
|
fullName: 'Nguyễn Văn An',
|
|
role: UserRole.AGENT,
|
|
kycStatus: 'VERIFIED',
|
|
isActive: true,
|
|
},
|
|
});
|
|
|
|
const agent2 = await prisma.user.upsert({
|
|
where: { phone: '0900000003' },
|
|
update: {},
|
|
create: {
|
|
phone: '0900000003',
|
|
email: 'agent.tran@goodgo.vn',
|
|
fullName: 'Trần Thị Bình',
|
|
role: UserRole.AGENT,
|
|
kycStatus: 'VERIFIED',
|
|
isActive: true,
|
|
},
|
|
});
|
|
|
|
const buyer = await prisma.user.upsert({
|
|
where: { phone: '0900000004' },
|
|
update: {},
|
|
create: {
|
|
phone: '0900000004',
|
|
email: 'buyer.le@gmail.com',
|
|
fullName: 'Lê Minh Cường',
|
|
role: UserRole.BUYER,
|
|
kycStatus: 'NONE',
|
|
isActive: true,
|
|
},
|
|
});
|
|
|
|
const seller = await prisma.user.upsert({
|
|
where: { phone: '0900000005' },
|
|
update: {},
|
|
create: {
|
|
phone: '0900000005',
|
|
email: 'seller.pham@gmail.com',
|
|
fullName: 'Phạm Đức Dũng',
|
|
role: UserRole.SELLER,
|
|
kycStatus: 'VERIFIED',
|
|
isActive: true,
|
|
},
|
|
});
|
|
|
|
// Create agent profiles
|
|
await prisma.agent.upsert({
|
|
where: { userId: agent1.id },
|
|
update: {},
|
|
create: {
|
|
userId: agent1.id,
|
|
licenseNumber: 'BDS-2024-001',
|
|
agency: 'GoodGo Premium Realty',
|
|
qualityScore: 4.8,
|
|
totalDeals: 127,
|
|
responseTimeAvg: 15,
|
|
bio: 'Chuyên viên bất động sản cao cấp Quận 1, Quận 7 với 10 năm kinh nghiệm.',
|
|
serviceAreas: ['quan-1', 'quan-7', 'thu-duc'],
|
|
isVerified: true,
|
|
},
|
|
});
|
|
|
|
await prisma.agent.upsert({
|
|
where: { userId: agent2.id },
|
|
update: {},
|
|
create: {
|
|
userId: agent2.id,
|
|
licenseNumber: 'BDS-2024-002',
|
|
agency: 'Saigon Homes',
|
|
qualityScore: 4.5,
|
|
totalDeals: 89,
|
|
responseTimeAvg: 20,
|
|
bio: 'Chuyên gia bất động sản Bình Thạnh, Phú Nhuận. Tư vấn miễn phí.',
|
|
serviceAreas: ['binh-thanh', 'phu-nhuan', 'go-vap'],
|
|
isVerified: true,
|
|
},
|
|
});
|
|
|
|
console.log(' ✓ 5 users + 2 agent profiles seeded');
|
|
return { admin, agent1, agent2, buyer, seller };
|
|
}
|
|
|
|
async function seedProperties(users: Awaited<ReturnType<typeof seedUsers>>) {
|
|
console.log('Seeding sample properties and listings...');
|
|
|
|
const sampleProperties = [
|
|
{
|
|
propertyType: PropertyType.APARTMENT,
|
|
title: 'Căn hộ Vinhomes Central Park 3PN view sông',
|
|
description:
|
|
'Căn hộ 3 phòng ngủ tại Vinhomes Central Park, tầng cao view sông Sài Gòn. Full nội thất cao cấp, tiện ích đầy đủ.',
|
|
address: '208 Nguyễn Hữu Cảnh',
|
|
ward: 'Phường 22',
|
|
district: 'Quận Bình Thạnh',
|
|
city: 'Hồ Chí Minh',
|
|
lat: 10.7942,
|
|
lng: 106.7214,
|
|
areaM2: 108,
|
|
usableAreaM2: 95,
|
|
bedrooms: 3,
|
|
bathrooms: 2,
|
|
floor: 25,
|
|
totalFloors: 50,
|
|
direction: Direction.SOUTHEAST,
|
|
yearBuilt: 2018,
|
|
legalStatus: 'Sổ hồng',
|
|
priceVND: BigInt(8_500_000_000),
|
|
transactionType: TransactionType.SALE,
|
|
projectName: 'Vinhomes Central Park',
|
|
},
|
|
{
|
|
propertyType: PropertyType.APARTMENT,
|
|
title: 'Căn hộ The Sun Avenue 2PN cho thuê',
|
|
description: 'Cho thuê căn hộ 2PN The Sun Avenue, nội thất đầy đủ, gần Metro, view đẹp.',
|
|
address: '28 Mai Chí Thọ',
|
|
ward: 'An Phú',
|
|
district: 'Thủ Đức',
|
|
city: 'Hồ Chí Minh',
|
|
lat: 10.7696,
|
|
lng: 106.7511,
|
|
areaM2: 76,
|
|
usableAreaM2: 68,
|
|
bedrooms: 2,
|
|
bathrooms: 2,
|
|
floor: 15,
|
|
totalFloors: 28,
|
|
direction: Direction.NORTH,
|
|
yearBuilt: 2020,
|
|
legalStatus: 'Sổ hồng',
|
|
priceVND: BigInt(15_000_000),
|
|
transactionType: TransactionType.RENT,
|
|
projectName: 'The Sun Avenue',
|
|
},
|
|
{
|
|
propertyType: PropertyType.TOWNHOUSE,
|
|
title: 'Nhà phố Thảo Điền 1 trệt 3 lầu',
|
|
description:
|
|
'Nhà phố khu compound an ninh Thảo Điền, 1 trệt 3 lầu, sân vườn rộng, gara ô tô.',
|
|
address: '12 Nguyễn Văn Hưởng',
|
|
ward: 'Thảo Điền',
|
|
district: 'Thủ Đức',
|
|
city: 'Hồ Chí Minh',
|
|
lat: 10.8033,
|
|
lng: 106.7391,
|
|
areaM2: 200,
|
|
usableAreaM2: 350,
|
|
bedrooms: 4,
|
|
bathrooms: 5,
|
|
floors: 4,
|
|
direction: Direction.SOUTH,
|
|
yearBuilt: 2015,
|
|
legalStatus: 'Sổ hồng',
|
|
priceVND: BigInt(25_000_000_000),
|
|
transactionType: TransactionType.SALE,
|
|
projectName: null,
|
|
},
|
|
{
|
|
propertyType: PropertyType.LAND,
|
|
title: 'Đất nền Quận 7 gần Phú Mỹ Hưng',
|
|
description: 'Đất nền thổ cư 100%, sổ riêng, hẻm ô tô, gần trung tâm Phú Mỹ Hưng.',
|
|
address: '56 Huỳnh Tấn Phát',
|
|
ward: 'Phú Thuận',
|
|
district: 'Quận 7',
|
|
city: 'Hồ Chí Minh',
|
|
lat: 10.7312,
|
|
lng: 106.7283,
|
|
areaM2: 120,
|
|
usableAreaM2: null,
|
|
bedrooms: null,
|
|
bathrooms: null,
|
|
direction: Direction.EAST,
|
|
yearBuilt: null,
|
|
legalStatus: 'Sổ đỏ',
|
|
priceVND: BigInt(12_000_000_000),
|
|
transactionType: TransactionType.SALE,
|
|
projectName: null,
|
|
},
|
|
{
|
|
propertyType: PropertyType.OFFICE,
|
|
title: 'Văn phòng cho thuê Quận 1 - 200m²',
|
|
description:
|
|
'Văn phòng hạng B tại trung tâm Quận 1, full nội thất, hệ thống PCCC, thang máy.',
|
|
address: '123 Nguyễn Huệ',
|
|
ward: 'Bến Nghé',
|
|
district: 'Quận 1',
|
|
city: 'Hồ Chí Minh',
|
|
lat: 10.7731,
|
|
lng: 106.703,
|
|
areaM2: 200,
|
|
usableAreaM2: 180,
|
|
bedrooms: null,
|
|
bathrooms: 2,
|
|
floor: 8,
|
|
totalFloors: 15,
|
|
direction: Direction.WEST,
|
|
yearBuilt: 2010,
|
|
legalStatus: 'Sổ hồng',
|
|
priceVND: BigInt(80_000_000),
|
|
transactionType: TransactionType.RENT,
|
|
projectName: null,
|
|
},
|
|
];
|
|
|
|
const agents = await prisma.agent.findMany();
|
|
|
|
for (let i = 0; i < sampleProperties.length; i++) {
|
|
const p = sampleProperties[i];
|
|
const agent = agents[i % agents.length];
|
|
|
|
const property = await prisma.$executeRaw`
|
|
INSERT INTO "Property" (
|
|
"id", "propertyType", "title", "description", "address",
|
|
"ward", "district", "city", "location",
|
|
"areaM2", "usableAreaM2", "bedrooms", "bathrooms",
|
|
"floors", "floor", "totalFloors", "direction",
|
|
"yearBuilt", "legalStatus", "amenities", "nearbyPOIs",
|
|
"metroDistanceM", "projectName", "createdAt", "updatedAt"
|
|
) VALUES (
|
|
${`prop-${i + 1}`}, ${p.propertyType}::"PropertyType", ${p.title}, ${p.description}, ${p.address},
|
|
${p.ward}, ${p.district}, ${p.city}, ST_SetSRID(ST_MakePoint(${p.lng}, ${p.lat}), 4326),
|
|
${p.areaM2}, ${p.usableAreaM2 ?? null}, ${p.bedrooms ?? null}, ${p.bathrooms ?? null},
|
|
${p.floors ?? null}, ${p.floor ?? null}, ${p.totalFloors ?? null}, ${p.direction ?? null}::"Direction",
|
|
${p.yearBuilt ?? null}, ${p.legalStatus ?? null}, ${null}, ${null},
|
|
${null}, ${p.projectName ?? null}, NOW(), NOW()
|
|
)
|
|
ON CONFLICT ("id") DO NOTHING
|
|
`;
|
|
|
|
await prisma.listing.upsert({
|
|
where: { id: `listing-${i + 1}` },
|
|
update: {},
|
|
create: {
|
|
id: `listing-${i + 1}`,
|
|
propertyId: `prop-${i + 1}`,
|
|
agentId: agent.id,
|
|
sellerId: users.seller.id,
|
|
transactionType: p.transactionType,
|
|
status: ListingStatus.ACTIVE,
|
|
priceVND: p.priceVND,
|
|
pricePerM2: Number(p.priceVND) / p.areaM2,
|
|
publishedAt: new Date(),
|
|
},
|
|
});
|
|
}
|
|
|
|
console.log(` ✓ ${sampleProperties.length} properties + listings seeded`);
|
|
}
|
|
|
|
async function seedMarketIndex() {
|
|
console.log('Seeding sample market index data...');
|
|
|
|
const districts = ['Quận 1', 'Quận 3', 'Quận 7', 'Thủ Đức', 'Quận Bình Thạnh'];
|
|
const periods = ['2025-Q4', '2026-Q1'];
|
|
|
|
for (const district of districts) {
|
|
for (const period of periods) {
|
|
for (const propertyType of [PropertyType.APARTMENT, PropertyType.TOWNHOUSE]) {
|
|
const basePrice =
|
|
district === 'Quận 1' ? 120_000_000 : district === 'Quận 7' ? 65_000_000 : 55_000_000;
|
|
const randomFactor = 0.9 + Math.random() * 0.2;
|
|
|
|
await prisma.marketIndex.upsert({
|
|
where: {
|
|
district_city_propertyType_period: {
|
|
district,
|
|
city: 'Hồ Chí Minh',
|
|
propertyType,
|
|
period,
|
|
},
|
|
},
|
|
update: {},
|
|
create: {
|
|
district,
|
|
city: 'Hồ Chí Minh',
|
|
propertyType,
|
|
period,
|
|
medianPrice: BigInt(Math.round(basePrice * randomFactor * 80)),
|
|
avgPriceM2: basePrice * randomFactor,
|
|
totalListings: Math.floor(100 + Math.random() * 500),
|
|
daysOnMarket: 30 + Math.random() * 60,
|
|
inventoryLevel: Math.floor(50 + Math.random() * 200),
|
|
absorptionRate: 0.3 + Math.random() * 0.4,
|
|
yoyChange: -5 + Math.random() * 15,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(' ✓ Market index data seeded');
|
|
}
|
|
|
|
// =============================================================================
|
|
// Main seed
|
|
// =============================================================================
|
|
|
|
async function main() {
|
|
console.log('🌱 Starting seed...\n');
|
|
|
|
await seedPlans();
|
|
const users = await seedUsers();
|
|
await seedProperties(users);
|
|
await seedMarketIndex();
|
|
|
|
console.log('\n✅ Seed completed successfully!');
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error('❌ Seed failed:', e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|