import { PrismaClient } from '@prisma/client'; import { PrismaPg } from '@prisma/adapter-pg'; import { Pool } from 'pg'; import * as dotenv from 'dotenv'; import * as path from 'path'; import bcrypt from 'bcrypt'; const envPath = path.resolve(process.cwd(), '..', '.env'); dotenv.config({ path: envPath }); const pool = new Pool({ connectionString: process.env.DATABASE_URL }); const adapter = new PrismaPg(pool); const prisma = new PrismaClient({ adapter }); async function main() { console.log('--- Đang xóa dữ liệu cũ... ---'); // Xóa các bảng phụ thuộc (nhiều bản ghi) trước await prisma.expense.deleteMany(); await prisma.photo.deleteMany(); await prisma.tour.deleteMany(); await prisma.user.deleteMany(); console.log('--- Đang tạo người dùng mẫu... ---'); const owner = await prisma.user.create({ data: { email: 'owner@travel.com', name: 'Lộc Phạm (Chủ Tour)', // Hash mật khẩu '123456' để có thể đăng nhập thực tế passwordHash: await bcrypt.hash('123456', 10), isAdmin: true, }, }); const photoMember = await prisma.user.create({ data: { email: 'photomember@travel.com', name: 'Nguyễn Văn Ảnh (Chỉ xem ảnh)', passwordHash: await bcrypt.hash('123456', 10), }, }); console.log('--- Đang tạo Tour và phân quyền... ---'); const tour = await prisma.tour.create({ data: { title: 'Hành trình khám phá TP.HCM', startDate: new Date('2023-11-20'), endDate: new Date('2023-11-21'), createdById: owner.id, participants: { create: [ { userId: owner.id, role: 'OWNER' }, { userId: photoMember.id, role: 'VIEWER_ONLY' }, ], }, }, }); console.log('--- Đang tạo chặng và địa điểm... ---'); const leg1 = await prisma.leg.create({ data: { tourId: tour.id, sequence: 1, note: 'Khám phá lịch sử trung tâm', locations: { create: [ { name: 'Dinh Độc Lập', address: '135 Nam Kỳ Khởi Nghĩa, Quận 1', latitude: 10.777, longitude: 106.695, plannedStart: new Date('2023-11-20T08:00:00Z'), }, { name: 'Bưu điện Thành phố', address: '02 Công xã Paris, Quận 1', latitude: 10.779, longitude: 106.699, plannedStart: new Date('2023-11-20T10:00:00Z'), }, ], }, }, }); console.log('--- Đang tạo chi phí mẫu... ---'); const expense1 = await prisma.expense.create({ data: { legId: leg1.id, category: 'FOOD', amount: 500000, description: 'Ăn trưa đặc sản Quận 1', note: 'Đặt trước cho 3 người', paidById: owner.id, }, }); console.log('--- Đang tạo bình luận mẫu... ---'); const dinhDocLap = await prisma.location.findFirst({ where: { name: 'Dinh Độc Lập' } }); if (dinhDocLap) { await prisma.comment.createMany({ data: [ { content: 'Chỗ này rất đẹp, giàu giá trị lịch sử!', locationId: dinhDocLap.id, userId: owner.id, }, { content: 'Nên đi vào buổi sáng cho mát mẻ mọi người nhé.', locationId: dinhDocLap.id, userId: photoMember.id, } ], }); } console.log('--- Seed dữ liệu hoàn tất! ---'); console.log(`Email đăng nhập Owner: ${owner.email}`); console.log(`Email đăng nhập Photo Only: ${photoMember.email}`); console.log(`Tour ID để test: ${tour.id}`); } main() .catch((e) => { console.error(e); process.exit(1); }) .finally(async () => { await pool.end(); });