StatusHistoryService.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const HttpException = require('../../utils/HttpException.js');
  2. const prisma = require('../../prisma/PrismaClient.js');
  3. const { createLog } = require('../../utils/LogActivity.js');
  4. const StatusHistoryRepository = require('../../repository/sales/StatusHistoryRepository.js');
  5. exports.getAllStatusHistoryService = async ({ page, limit, search, sortBy, orderBy }, req) => {
  6. const skip = (page - 1) * limit;
  7. const userId = req.tokenData.sub;
  8. const hospitalId = req.params.id;
  9. const hospital = await prisma.hospital.findFirst({
  10. where: {
  11. id: hospitalId
  12. }
  13. })
  14. if (!hospital) {
  15. throw new HttpException("Hospital not found", 404)
  16. }
  17. const userAreas = await prisma.userArea.findMany({
  18. where: { user_id: userId },
  19. select: { province_id: true }
  20. });
  21. const userProvinceIds = userAreas.map(ua => ua.province_id);
  22. if (!userProvinceIds.includes(hospital.province_id)) {
  23. throw new HttpException("This hospital is not your area", 403);
  24. }
  25. const where = {
  26. hospital_id: req.params.id,
  27. deletedAt: null
  28. };
  29. const [status_histories, total] = await Promise.all([
  30. StatusHistoryRepository.findAll({ skip, take: limit, where, orderBy: { [sortBy]: orderBy } }),
  31. StatusHistoryRepository.countAll(where)
  32. ]);
  33. return { status_histories, total };
  34. };
  35. const validProgressStatuses = ['cari_data', 'dihubungi', 'negosiasi', 'follow_up', 'mou', 'onboarded', 'tidak_berminat'];
  36. exports.storeStatusHistoryService = async (validateData, req) => {
  37. const hospitalId = req.params.id;
  38. const userId = req.tokenData.sub;
  39. const hospital = await prisma.hospital.findFirst({
  40. where: {
  41. id: hospitalId
  42. }
  43. })
  44. if (!hospital) {
  45. throw new HttpException("Hospital not found", 404)
  46. }
  47. const userAreas = await prisma.userArea.findMany({
  48. where: { user_id: userId },
  49. select: { province_id: true }
  50. });
  51. const userProvinceIds = userAreas.map(ua => ua.province_id);
  52. if (!userProvinceIds.includes(hospital.province_id)) {
  53. throw new HttpException("This hospital is not your area", 403);
  54. }
  55. if (validateData.new_status && !validProgressStatuses.includes(validateData.new_status)) {
  56. throw new HttpException(
  57. `Invalid new_status. Allowed values are: ${validProgressStatuses.join(', ')}`,
  58. 422
  59. );
  60. }
  61. if (validateData.new_status === hospital.progress_status) {
  62. throw new HttpException("New status change is the same as old status", 400)
  63. }
  64. const payload = {
  65. hospital_id: hospitalId,
  66. user_id: userId,
  67. old_status: hospital.progress_status,
  68. new_status: validateData.new_status,
  69. note: validateData.note,
  70. };
  71. const data = await StatusHistoryRepository.create(payload);
  72. await prisma.hospital.update({
  73. where: { id: hospitalId },
  74. data: {
  75. progress_status: validateData.new_status
  76. }
  77. });
  78. await createLog(req, data);
  79. };