VendorHistoryService.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const VendorHistoryRepository = require('../../repository/admin/VendorHistoryRepository.js');
  2. const HttpException = require('../../utils/HttpException.js');
  3. const prisma = require('../../prisma/PrismaClient.js');
  4. const { SearchFilter } = require('../../utils/SearchFilter.js');
  5. const timeLocal = require('../../utils/TimeLocal.js');
  6. const { createLog, updateLog, deleteLog } = require('../../utils/LogActivity.js');
  7. const { formatDateOnly, formatISOWithoutTimezone } = require('../../utils/FormatDate.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. VendorHistoryRepository.findAll({ skip, take: limit, where, orderBy: { [sortBy]: orderBy } }),
  26. VendorHistoryRepository.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_history = req.params.id_vendor_history;
  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 VendorHistoryRepository.findById(id_vendor_history);
  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. await prisma.hospital.update({
  58. where: { id: hospitalId },
  59. data: {
  60. simrs_type: validateData.simrs_type
  61. }
  62. });
  63. const payload = {
  64. vendor_id: validateData.vendor_id,
  65. vendor_impression: validateData.vendor_impression,
  66. status: validateData.status,
  67. contract_date: validateData.contract_date,
  68. contract_expired_date: validateData.contract_expired_date,
  69. hospital_id: hospitalId
  70. };
  71. const data = await VendorHistoryRepository.create(payload);
  72. await createLog(req, data);
  73. };
  74. exports.updateVendorHistoryService = async (validateData, req) => {
  75. const id_hospital = req.params.id;
  76. const id_vendor_history = req.params.id_vendor_history;
  77. const hospital = await prisma.hospital.findFirst({
  78. where: {
  79. id: id_hospital
  80. }
  81. })
  82. if (!hospital) {
  83. throw new HttpException("Hospital not found", 404)
  84. }
  85. const vendor = await VendorHistoryRepository.findById(id_vendor_history);
  86. if (!vendor) {
  87. throw new HttpException("Vendor history not found", 404);
  88. }
  89. await prisma.hospital.update({
  90. where: { id: id_hospital },
  91. data: {
  92. simrs_type: validateData.simrs_type
  93. }
  94. });
  95. const payload = {
  96. vendor_id: validateData.vendor_id,
  97. vendor_impression: validateData.vendor_impression,
  98. status: validateData.status,
  99. contract_date: validateData.contract_date,
  100. contract_expired_date: validateData.contract_expired_date
  101. };
  102. const data = await VendorHistoryRepository.update(id_vendor_history, payload);
  103. await updateLog(req, data);
  104. };
  105. exports.deleteVendorHistoryService = async (req) => {
  106. const id_hospital = req.params.id;
  107. const id_vendor_history = req.params.id_vendor_history;
  108. const hospital = await prisma.hospital.findFirst({
  109. where: {
  110. id: id_hospital
  111. }
  112. })
  113. if (!hospital) {
  114. throw new HttpException("Hospital not found", 404)
  115. }
  116. const vendor = await VendorHistoryRepository.findById(id_vendor_history);
  117. if (!vendor) {
  118. throw new HttpException("Vendor history not found", 404);
  119. }
  120. const data = await VendorHistoryRepository.update(id_vendor_history, {
  121. deletedAt: timeLocal.now().toDate()
  122. });
  123. await deleteLog(req, data);
  124. };