ExecutivesHistoryService.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const ExecutivesHistoryRepository = require('../../repository/sales/ExecutivesHistoryRepository.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. exports.getAllExecutivesHistoryService = async ({ page, limit, search, sortBy, orderBy }, req) => {
  8. const skip = (page - 1) * limit;
  9. const hospitalId = req.params.id;
  10. const hospital = await prisma.hospital.findFirst({
  11. where: {
  12. id: hospitalId
  13. }
  14. })
  15. if (!hospital) {
  16. throw new HttpException("Hospital not found", 404)
  17. }
  18. const where = {
  19. // ...SearchFilter(search, ['name', 'name_pt']),
  20. hospital_id: req.params.id,
  21. deletedAt: null
  22. };
  23. const [executives_histories, total] = await Promise.all([
  24. ExecutivesHistoryRepository.findAll({ skip, take: limit, where, orderBy: { [sortBy]: orderBy } }),
  25. ExecutivesHistoryRepository.countAll(where)
  26. ]);
  27. return { executives_histories, total };
  28. };
  29. exports.showExecutivesHistoryService = async (req) => {
  30. const id_hospital = req.params.id;
  31. const id_executives_history = req.params.id_executives_history;
  32. const hospital = await prisma.hospital.findFirst({
  33. where: {
  34. id: id_hospital
  35. }
  36. })
  37. if (!hospital) {
  38. throw new HttpException("Hospital not found", 404)
  39. }
  40. const executivesHistory = await ExecutivesHistoryRepository.findById(id_executives_history);
  41. if (!executivesHistory) {
  42. throw new HttpException("Executives history not found", 404);
  43. }
  44. return executivesHistory;
  45. };
  46. exports.storeExecutivesHistoryService = async (validateData, req) => {
  47. const hospitalId = req.params.id;
  48. const hospital = await prisma.hospital.findFirst({
  49. where: {
  50. id: hospitalId
  51. }
  52. })
  53. if (!hospital) {
  54. throw new HttpException("Hospital not found", 404)
  55. }
  56. if (validateData.status === "active") {
  57. await prisma.executivesHistory.updateMany({
  58. where: {
  59. hospital_id: hospitalId,
  60. status: "active"
  61. },
  62. data: {
  63. status: "inactive"
  64. }
  65. });
  66. }
  67. const payload = {
  68. ...validateData,
  69. hospital_id: hospitalId
  70. };
  71. const data = await ExecutivesHistoryRepository.create(payload);
  72. await createLog(req, data);
  73. };
  74. exports.updateExecutivesHistoryService = async (validateData, req) => {
  75. const id_hospital = req.params.id;
  76. const id_executives_history = req.params.id_executives_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 executivesHistory = await ExecutivesHistoryRepository.findById(id_executives_history);
  86. if (!executivesHistory) {
  87. throw new HttpException("Executives history not found", 404);
  88. }
  89. if (validateData.start_term && validateData.end_term) {
  90. if (validateData.start_term >= validateData.end_term) {
  91. throw new HttpException("End term must be after start_term", 400)
  92. }
  93. }
  94. if (validateData.status === "active") {
  95. await prisma.executivesHistory.updateMany({
  96. where: {
  97. hospital_id: id_hospital,
  98. status: "active"
  99. },
  100. data: {
  101. status: "inactive"
  102. }
  103. });
  104. }
  105. const payload = {
  106. ...validateData,
  107. start_term: validateData.start_term ? new Date(validateData.start_term) : executivesHistory.start_term,
  108. end_term: validateData.end_term ? new Date(validateData.end_term) : executivesHistory.end_term,
  109. };
  110. const data = await ExecutivesHistoryRepository.update(id_executives_history, payload);
  111. await updateLog(req, data);
  112. };
  113. exports.deleteExecutivesHistoryService = async (req) => {
  114. const id_hospital = req.params.id;
  115. const id_executives_history = req.params.id_executives_history;
  116. const hospital = await prisma.hospital.findFirst({
  117. where: {
  118. id: id_hospital
  119. }
  120. })
  121. if (!hospital) {
  122. throw new HttpException("Hospital not found", 404)
  123. }
  124. const executivesHistory = await ExecutivesHistoryRepository.findById(id_executives_history);
  125. if (!executivesHistory) {
  126. throw new HttpException("Executives history not found", 404);
  127. }
  128. const data = await ExecutivesHistoryRepository.update(id_executives_history, {
  129. deletedAt: timeLocal.now().toDate()
  130. });
  131. await deleteLog(req, data);
  132. };