HospitalSeeder.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const prisma = require('../../src/prisma/PrismaClient.js');
  2. const { faker } = require('@faker-js/faker');
  3. const timeLocal = require('../../src/utils/TimeLocal.js');
  4. exports.seedHospital = async () => {
  5. // 1. Ambil semua provinsi
  6. const allProvinces = await prisma.province.findMany();
  7. const shuffled = allProvinces.sort(() => 0.5 - Math.random());
  8. const selectedProvinces = shuffled.slice(0, 5); // ambil 5 random
  9. for (const province of selectedProvinces) {
  10. const cities = await prisma.city.findMany({
  11. where: { province_id: province.id }
  12. });
  13. if (cities.length === 0) continue; // skip kalau tidak ada kota
  14. const randomCity = cities[Math.floor(Math.random() * cities.length)];
  15. await prisma.hospital.create({
  16. data: {
  17. name: `RS ${faker.company.name()}`,
  18. hospital_code: faker.string.alphanumeric(6).toUpperCase(),
  19. type: faker.helpers.arrayElement(['Tipe A', 'Tipe B', 'Tipe C']),
  20. ownership: faker.helpers.arrayElement(['Pemerintah', 'Swasta']),
  21. address: faker.location.streetAddress(),
  22. simrs_type: faker.helpers.arrayElement(['SIMRS A', 'SIMRS B', 'SIMRS C']),
  23. contact: faker.phone.number('08##########'),
  24. image: '/storage/img/dummy.jpg',
  25. note: faker.lorem.sentence(),
  26. progress_status: 'cari_data',
  27. createdAt: timeLocal.now().toDate(),
  28. province: { connect: { id: province.id } },
  29. city: { connect: { id: randomCity.id } },
  30. // user: { connect: { id: await getRandomUserId() } }, // opsional: assign user
  31. user: null
  32. }
  33. });
  34. }
  35. console.log('✅ Hospital seeder done!');
  36. };
  37. // Fungsi ambil 1 user id random dari tabel user
  38. async function getRandomUserId() {
  39. const users = await prisma.user.findMany({ where: { deletedAt: null } });
  40. if (users.length === 0) throw new Error('No users found for seeding hospital.');
  41. return users[Math.floor(Math.random() * users.length)].id;
  42. }