HospitalService.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const HospitalRepository = require('../../repository/admin/HospitalRepository.js');
  2. const HttpException = require('../../utils/HttpException.js');
  3. const { SearchFilter } = require('../../utils/SearchFilter.js');
  4. const timeLocal = require('../../utils/TimeLocal.js');
  5. const { createLog, updateLog, deleteLog } = require('../../utils/LogActivity.js');
  6. const ProvinceRepository = require('../../repository/admin/ProvinceRepository.js');
  7. const CityRepository = require('../../repository/admin/CityRepository.js');
  8. const { BASE_URL } = require('../../../config/config.js');
  9. const prisma = require('../../prisma/PrismaClient.js');
  10. const { formatISOWithoutTimezone } = require('../../utils/FormatDate.js');
  11. exports.getAllHospitalService = async ({ page, limit, search, sortBy, orderBy, province, city, type, ownership, progress_status }) => {
  12. const skip = (page - 1) * limit;
  13. const where = {
  14. ...SearchFilter(search, ['name', 'province.id', 'city.id', 'type', 'ownership', 'simrs_type']),
  15. ...(province ? { province_id: province } : {}),
  16. ...(city ? { city_id: city } : {}),
  17. ...(type ? { type: type } : {}),
  18. ...(ownership ? { ownership: ownership } : {}),
  19. ...(progress_status ? { progress_status: progress_status } : {}),
  20. deletedAt: null
  21. };
  22. const [hospitals, total] = await Promise.all([
  23. HospitalRepository.findAll({ skip, take: limit, where, orderBy: { [sortBy]: orderBy } }),
  24. HospitalRepository.countAll(where)
  25. ]);
  26. return { hospitals, total };
  27. };
  28. exports.showHospitalService = async (id) => {
  29. const hospital = await HospitalRepository.findById(id);
  30. if (!hospital) {
  31. throw new HttpException("Data hospital not found", 404);
  32. }
  33. return hospital;
  34. };
  35. exports.storeHospitalService = async (validateData, req) => {
  36. const creatorId = req.user.id;
  37. const province = await ProvinceRepository.findById(validateData.province_id);
  38. if (!province) {
  39. throw new HttpException('Province not found', 404);
  40. }
  41. const city = await CityRepository.findById(validateData.city_id);
  42. if (!city) {
  43. throw new HttpException('City not found', 404);
  44. }
  45. if (!req.file) {
  46. throw new HttpException({ image: ['image file is required'] }, 422);
  47. }
  48. const existingHospital = await prisma.hospital.findFirst({
  49. where: {
  50. name: validateData.name,
  51. city_id: validateData.city_id,
  52. deletedAt: null
  53. }
  54. });
  55. if (existingHospital) {
  56. throw new HttpException('Hospital with same name in this city already exists', 400);
  57. }
  58. const imagePath = req.file ? `/storage/img/${req.file.filename}` : null;
  59. const payload = {
  60. ...validateData,
  61. image: imagePath,
  62. progress_status: "cari_data",
  63. // simrs_type: "-",
  64. created_by: creatorId
  65. };
  66. const data = await HospitalRepository.create(payload);
  67. await createLog(req, data);
  68. };
  69. const validProgressStatuses = ['cari_data', 'dihubungi', 'negosiasi', 'follow_up', 'mou', 'onboarded', 'tidak_berminat'];
  70. exports.updateHospitalService = async (validateData, id, req) => {
  71. const hospital = await HospitalRepository.findById(id);
  72. if (!hospital) {
  73. throw new HttpException("Hospital data not found", 404);
  74. }
  75. const province = await ProvinceRepository.findById(validateData.province_id);
  76. if (!province) {
  77. throw new HttpException('Province not found', 404);
  78. }
  79. const city = await CityRepository.findById(validateData.city_id);
  80. if (!city) {
  81. throw new HttpException('City not found', 404);
  82. }
  83. if (validateData.progress_status && !validProgressStatuses.includes(validateData.progress_status)) {
  84. throw new HttpException(
  85. `Invalid progress_status. Allowed values are: ${validProgressStatuses.join(', ')}`,
  86. 422
  87. );
  88. }
  89. const existingHospital = await prisma.hospital.findFirst({
  90. where: {
  91. name: validateData.name,
  92. city_id: validateData.city_id,
  93. deletedAt: null
  94. }
  95. });
  96. if (existingHospital) {
  97. throw new HttpException('Hospital with same name in this city already exists', 400);
  98. }
  99. // Jika ada file baru, replace image
  100. let imagePath = hospital.image; // pakai yang lama
  101. if (req.file) {
  102. imagePath = `/storage/img/${req.file.filename}`; // path relatif
  103. }
  104. const payload = {
  105. ...validateData,
  106. image: imagePath,
  107. // created_by: req.user.id,
  108. };
  109. const data = await HospitalRepository.update(id, payload);
  110. await updateLog(req, data);
  111. };
  112. exports.deleteHospitalService = async (id, req) => {
  113. const hospital = await HospitalRepository.findById(id);
  114. if (!hospital) {
  115. throw new HttpException('Hospital not found', 404);
  116. }
  117. const data = await HospitalRepository.update(id, {
  118. deletedAt: timeLocal.now().toDate()
  119. });
  120. await deleteLog(req, data);
  121. };