StatusHistoryService.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 StatusHistoryRepository = require('../../repository/admin/StatusHistoryRepository.js');
  8. exports.getAllStatusHistoryService = 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. hospital_id: req.params.id,
  21. deletedAt: null
  22. };
  23. const [status_histories, total] = await Promise.all([
  24. StatusHistoryRepository.findAll({ skip, take: limit, where, orderBy: { [sortBy]: orderBy } }),
  25. StatusHistoryRepository.countAll(where)
  26. ]);
  27. return { status_histories, total };
  28. };
  29. const validProgressStatuses = ['cari_data', 'dihubungi', 'negosiasi', 'follow_up', 'mou', 'onboarded', 'tidak_berminat'];
  30. exports.storeStatusHistoryService = async (validateData, req) => {
  31. const hospitalId = req.params.id;
  32. const userId = req.user.id;
  33. const hospital = await prisma.hospital.findFirst({
  34. where: {
  35. id: hospitalId
  36. }
  37. })
  38. if (!hospital) {
  39. throw new HttpException("Hospital not found", 404)
  40. }
  41. if (validateData.new_status && !validProgressStatuses.includes(validateData.new_status)) {
  42. throw new HttpException(
  43. `Invalid new_status. Allowed values are: ${validProgressStatuses.join(', ')}`,
  44. 422
  45. );
  46. }
  47. if (validateData.new_status === hospital.progress_status) {
  48. throw new HttpException("New status change is the same as old status", 400)
  49. }
  50. const payload = {
  51. hospital_id: hospitalId,
  52. user_id: userId,
  53. old_status: hospital.progress_status,
  54. new_status: validateData.new_status,
  55. note: validateData.note,
  56. };
  57. const data = await StatusHistoryRepository.create(payload);
  58. await prisma.hospital.update({
  59. where: { id: hospitalId },
  60. data: {
  61. progress_status: validateData.new_status
  62. }
  63. });
  64. await createLog(req, data);
  65. };