HospitalSeeder.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const { PrismaClient } = require('@prisma/client');
  2. const prisma = new PrismaClient();
  3. async function seedHospitals() {
  4. try {
  5. const sales1 = await prisma.user.findFirst({ where: { username: 'sales1' } });
  6. const sales2 = await prisma.user.findFirst({ where: { username: 'sales2' } });
  7. if (!sales1 || !sales2) {
  8. throw new Error('User sales1 or sales2 not found');
  9. }
  10. // Ambil province_id dari user_areas
  11. const sales1Area = await prisma.userArea.findFirst({ where: { user_id: sales1.id } });
  12. const sales2Area = await prisma.userArea.findFirst({ where: { user_id: sales2.id } });
  13. if (!sales1Area || !sales2Area) {
  14. throw new Error('UserArea for sales1 or sales2 not found');
  15. }
  16. // Ambil city yang berada di province_id tersebut
  17. const city1 = await prisma.city.findFirst({ where: { province_id: sales1Area.province_id } });
  18. const city2 = await prisma.city.findFirst({ where: { province_id: sales2Area.province_id } });
  19. if (!city1 || !city2) {
  20. throw new Error('City not found for one of province id');
  21. }
  22. // Data rumah sakit
  23. const hospitals = [
  24. {
  25. name: 'RS Sehat Selalu',
  26. hospital_code: 'RSS001',
  27. type: 'Tipe B',
  28. ownership: 'Swasta',
  29. address: 'Jl. Kesehatan No. 1',
  30. // simrs_type: 'MediSoft',
  31. contact: '021-123456',
  32. image: null,
  33. progress_status: 'cari_data',
  34. note: 'Calon potensial',
  35. province_id: sales1Area.province_id,
  36. city_id: city1.id,
  37. created_by: sales1.id,
  38. },
  39. {
  40. name: 'RS Harapan Bangsa',
  41. hospital_code: 'RSB002',
  42. type: 'Tipe C',
  43. ownership: 'Pemerintah',
  44. address: 'Jl. Harapan No. 2',
  45. // simrs_type: 'Hospicare',
  46. contact: '021-654321',
  47. image: null,
  48. progress_status: 'cari_data',
  49. note: null,
  50. province_id: sales1Area.province_id,
  51. city_id: city1.id,
  52. created_by: sales1.id,
  53. },
  54. {
  55. name: 'RS Mitra Medika',
  56. hospital_code: 'RSM003',
  57. type: 'Tipe D',
  58. ownership: 'Swasta',
  59. address: 'Jl. Mitra No. 3',
  60. // simrs_type: 'SimRS Platinum',
  61. contact: '021-999999',
  62. image: null,
  63. progress_status: 'cari_data',
  64. note: 'Baru dihubungi',
  65. province_id: sales1Area.province_id,
  66. city_id: city1.id,
  67. created_by: sales1.id,
  68. },
  69. {
  70. name: 'RS Sembuh Bersama',
  71. hospital_code: 'RSB004',
  72. type: 'Tipe B',
  73. ownership: 'Swasta',
  74. address: 'Jl. Bersama No. 4',
  75. // simrs_type: 'MediSoft',
  76. contact: '0274-111111',
  77. image: null,
  78. progress_status: 'cari_data',
  79. note: null,
  80. province_id: sales2Area.province_id,
  81. city_id: city2.id,
  82. created_by: sales2.id,
  83. },
  84. {
  85. name: 'RS Kasih Ibu',
  86. hospital_code: 'RSK005',
  87. type: 'Tipe C',
  88. ownership: 'Yayasan',
  89. address: 'Jl. Kasih No. 5',
  90. // simrs_type: 'Hospicare',
  91. contact: '0274-222222',
  92. image: null,
  93. progress_status: 'cari_data',
  94. note: 'Perlu follow up',
  95. province_id: sales2Area.province_id,
  96. city_id: city2.id,
  97. created_by: sales2.id,
  98. },
  99. {
  100. name: 'RS Bhakti Sehat',
  101. hospital_code: 'RSB006',
  102. type: 'Tipe D',
  103. ownership: 'Swasta',
  104. address: 'Jl. Bhakti No. 6',
  105. // simrs_type: 'SimRS Platinum',
  106. contact: '0274-333333',
  107. image: null,
  108. progress_status: 'cari_data',
  109. note: null,
  110. province_id: sales2Area.province_id,
  111. city_id: city2.id,
  112. created_by: sales2.id,
  113. },
  114. ];
  115. // Masukkan ke DB
  116. for (const hospital of hospitals) {
  117. await prisma.hospital.create({ data: hospital });
  118. }
  119. console.log('✅ Hospital seeded!');
  120. } catch (err) {
  121. console.error('❌ Error seeding hospital:', err.message);
  122. }
  123. }
  124. module.exports = { seedHospitals };