VendorExperienceService.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. const HttpException = require('../../utils/HttpException.js');
  2. const prisma = require('../../prisma/PrismaClient.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 { formatDateOnly, formatISOWithoutTimezone } = require('../../utils/FormatDate.js');
  7. const VendorExperienceRepository = require('../../repository/admin/VendorExperienceRepository.js');
  8. exports.getAllVendorHistoryService = async ({ page, limit, search, sortBy, orderBy }, req) => {
  9. const skip = (page - 1) * limit;
  10. const hospitalId = req.params.id;
  11. const hospital = await prisma.hospital.findFirst({
  12. where: {
  13. id: hospitalId
  14. }
  15. })
  16. if (!hospital) {
  17. throw new HttpException("Hospital not found", 404)
  18. }
  19. const where = {
  20. // ...SearchFilter(search, ['name', 'name_pt']),
  21. hospital_id: req.params.id,
  22. deletedAt: null
  23. };
  24. const [vendor_histories, total] = await Promise.all([
  25. VendorExperienceRepository.findAll({ skip, take: limit, where, orderBy: { [sortBy]: orderBy } }),
  26. VendorExperienceRepository.countAll(where)
  27. ]);
  28. return { vendor_histories, total };
  29. };
  30. exports.showVendorHistoryService = async (req) => {
  31. const id_hospital = req.params.id;
  32. const id_vendor_experience = req.params.id_vendor_experience;
  33. const hospital = await prisma.hospital.findFirst({
  34. where: {
  35. id: id_hospital
  36. }
  37. })
  38. if (!hospital) {
  39. throw new HttpException("Hospital not found", 404)
  40. }
  41. const vendorHistory = await VendorExperienceRepository.findById(id_vendor_experience);
  42. if (!vendorHistory) {
  43. throw new HttpException("Vendor history not found", 404);
  44. }
  45. return vendorHistory;
  46. };
  47. exports.storeVendorHistoryService = async (validateData, req) => {
  48. const hospitalId = req.params.id;
  49. const hospital = await prisma.hospital.findFirst({
  50. where: {
  51. id: hospitalId
  52. }
  53. });
  54. if (!hospital) {
  55. throw new HttpException("Hospital not found", 404)
  56. }
  57. if (validateData.vendor_id) {
  58. const vendor = await prisma.vendor.findFirst({
  59. where: {
  60. id: validateData.vendor_id
  61. }
  62. });
  63. if (!vendor) {
  64. throw new HttpException("Vendor not found", 404)
  65. }
  66. }
  67. const SimrsType = ["vendor", "in house", "gratis"];
  68. if (validateData.simrs_type && !SimrsType.includes(validateData.simrs_type)) {
  69. throw new HttpException("Simrs type must be vendor, in house, or gratis", 400);
  70. }
  71. if (validateData.contract_start_date && validateData.contract_expired_date) {
  72. if (validateData.contract_start_date >= validateData.contract_expired_date) {
  73. throw new HttpException("Contract expired date must be after contract date", 400)
  74. }
  75. }
  76. if (validateData.contract_value_min && validateData.contract_value_max) {
  77. if (validateData.contract_value_min >= validateData.contract_value_max) {
  78. throw new HttpException("Contract value max must be after contract value min", 400)
  79. }
  80. }
  81. // await prisma.hospital.update({
  82. // where: { id: hospitalId },
  83. // data: {
  84. // simrs_type: validateData.simrs_type
  85. // }
  86. // });
  87. if (validateData.simrs_type) {
  88. const existingActiveVendors = await prisma.vendorExperience.findMany({
  89. where: {
  90. hospital_id: hospitalId,
  91. status: "active",
  92. deletedAt: null
  93. }
  94. });
  95. if (existingActiveVendors.length > 0) {
  96. await prisma.vendorExperience.updateMany({
  97. where: {
  98. hospital_id: hospitalId,
  99. status: "active",
  100. deletedAt: null
  101. },
  102. data: {
  103. status: "inactive"
  104. }
  105. });
  106. }
  107. validateData.status = "active";
  108. }
  109. const payload = {
  110. vendor_id: validateData.vendor_id,
  111. simrs_type: validateData.simrs_type,
  112. status: validateData.status,
  113. contract_start_date: validateData.contract_start_date ? new Date(validateData.contract_start_date) : null,
  114. contract_expired_date: validateData.contract_expired_date ? new Date(validateData.contract_expired_date) : null,
  115. contract_value_min: validateData.contract_value_min ? Number(validateData.contract_value_min) : null,
  116. contract_value_max: validateData.contract_value_max ? Number(validateData.contract_value_max) : null,
  117. positive_notes: validateData.positive_notes,
  118. negative_notes: validateData.negative_notes,
  119. hospital_id: hospitalId
  120. };
  121. const data = await VendorExperienceRepository.create(payload);
  122. await createLog(req, data);
  123. };
  124. exports.updateVendorHistoryService = async (validateData, req) => {
  125. const id_hospital = req.params.id;
  126. const id_vendor_experience = req.params.id_vendor_experience;
  127. const hospital = await prisma.hospital.findFirst({
  128. where: {
  129. id: id_hospital
  130. }
  131. })
  132. if (!hospital) {
  133. throw new HttpException("Hospital not found", 404)
  134. }
  135. const vendorHistory = await VendorExperienceRepository.findById(id_vendor_experience);
  136. if (!vendorHistory) {
  137. throw new HttpException("Vendor history not found", 404);
  138. }
  139. if (validateData.vendor_id) {
  140. const existVendor = await prisma.vendor.findFirst({
  141. where: {
  142. id: validateData.vendor_id
  143. }
  144. });
  145. if (!existVendor) {
  146. throw new HttpException("Vendor not found", 404)
  147. }
  148. }
  149. const SimrsType = ["vendor", "in house", "gratis"];
  150. if (validateData.simrs_type && !SimrsType.includes(validateData.simrs_type)) {
  151. throw new HttpException("Simrs type must be vendor, in house, or gratis", 400);
  152. }
  153. if (
  154. validateData.simrs_type &&
  155. vendorHistory.simrs_type === "vendor" &&
  156. vendorHistory.vendor_id !== null &&
  157. validateData.simrs_type !== "vendor"
  158. ) {
  159. await prisma.vendorExperience.update({
  160. where: {
  161. id: id_vendor_experience,
  162. deletedAt: null
  163. },
  164. data: {
  165. vendor_id: null
  166. }
  167. });
  168. }
  169. if (validateData.contract_start_date && validateData.contract_expired_date) {
  170. if (validateData.contract_start_date >= validateData.contract_expired_date) {
  171. throw new HttpException("Contract expired date must be after contract date", 400)
  172. }
  173. }
  174. if (validateData.contract_value_min && validateData.contract_value_max) {
  175. if (validateData.contract_value_min >= validateData.contract_value_max) {
  176. throw new HttpException("Contract value max must be after contract value min", 400)
  177. }
  178. }
  179. if (validateData.status === "active") {
  180. await prisma.vendorExperience.updateMany({
  181. where: {
  182. hospital_id: id_hospital,
  183. status: "active",
  184. deletedAt: null,
  185. NOT: { id: id_vendor_experience },
  186. },
  187. data: {
  188. status: "inactive",
  189. },
  190. });
  191. }
  192. const payload = {
  193. status: validateData.status,
  194. contract_start_date: validateData.contract_start_date ? new Date(validateData.contract_start_date) : vendorHistory.contract_start_date,
  195. contract_expired_date: validateData.contract_expired_date ? new Date(validateData.contract_expired_date) : vendorHistory.contract_expired_date,
  196. contract_value_min: validateData.contract_value_min ? Number(validateData.contract_value_min) : vendorHistory.contract_value_min,
  197. contract_value_max: validateData.contract_value_max ? Number(validateData.contract_value_max) : vendorHistory.contract_value_max,
  198. positive_notes: validateData.positive_notes,
  199. negative_notes: validateData.negative_notes,
  200. simrs_type: validateData.simrs_type,
  201. vendor_id: validateData.vendor_id,
  202. };
  203. const data = await VendorExperienceRepository.update(id_vendor_experience, payload);
  204. await updateLog(req, data);
  205. };
  206. exports.deleteVendorHistoryService = async (req) => {
  207. const id_hospital = req.params.id;
  208. const id_vendor_experience = req.params.id_vendor_experience;
  209. const hospital = await prisma.hospital.findFirst({
  210. where: {
  211. id: id_hospital
  212. }
  213. })
  214. if (!hospital) {
  215. throw new HttpException("Hospital not found", 404)
  216. }
  217. const vendor = await VendorExperienceRepository.findById(id_vendor_experience);
  218. if (!vendor) {
  219. throw new HttpException("Vendor history not found", 404);
  220. }
  221. const data = await VendorExperienceRepository.update(id_vendor_experience, {
  222. deletedAt: timeLocal.now().toDate()
  223. });
  224. await deleteLog(req, data);
  225. };