const prisma = require('../../src/prisma/PrismaClient.js'); const { faker } = require('@faker-js/faker'); const timeLocal = require('../../src/utils/TimeLocal.js'); exports.seedHospital = async () => { // 1. Ambil semua provinsi const allProvinces = await prisma.province.findMany(); const shuffled = allProvinces.sort(() => 0.5 - Math.random()); const selectedProvinces = shuffled.slice(0, 5); // ambil 5 random for (const province of selectedProvinces) { const cities = await prisma.city.findMany({ where: { province_id: province.id } }); if (cities.length === 0) continue; // skip kalau tidak ada kota const randomCity = cities[Math.floor(Math.random() * cities.length)]; await prisma.hospital.create({ data: { name: `RS ${faker.company.name()}`, hospital_code: faker.string.alphanumeric(6).toUpperCase(), type: faker.helpers.arrayElement(['Tipe A', 'Tipe B', 'Tipe C']), ownership: faker.helpers.arrayElement(['Pemerintah', 'Swasta']), address: faker.location.streetAddress(), simrs_type: faker.helpers.arrayElement(['SIMRS A', 'SIMRS B', 'SIMRS C']), contact: faker.phone.number('08##########'), image: '/storage/img/dummy.jpg', note: faker.lorem.sentence(), progress_status: 'cari_data', createdAt: timeLocal.now().toDate(), province: { connect: { id: province.id } }, city: { connect: { id: randomCity.id } }, // user: { connect: { id: await getRandomUserId() } }, // opsional: assign user user: null } }); } console.log('✅ Hospital seeder done!'); }; // Fungsi ambil 1 user id random dari tabel user async function getRandomUserId() { const users = await prisma.user.findMany({ where: { deletedAt: null } }); if (users.length === 0) throw new Error('No users found for seeding hospital.'); return users[Math.floor(Math.random() * users.length)].id; }