jadwal.controller.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const laporanModel = require('../model/laporan.model')
  2. const handleError = require('../utils/handleError')
  3. const response = require('../utils/responseHandler')
  4. const { validate } = require('../utils/validation')
  5. exports.update = handleError(async (req, res) => {
  6. const user = req.user
  7. const { id } = req.params
  8. const isValid = validate(res, req.body, {
  9. judul: 'string',
  10. dari_tanggal: { type: 'date', convert: true },
  11. sampai_tanggal: { type: 'date', convert: true },
  12. warna: 'string',
  13. })
  14. if (!isValid) return
  15. const { judul, dari_tanggal, sampai_tanggal, warna } = req.body
  16. let laporan = await laporanModel.findOne({ _id: id })
  17. if (
  18. laporan &&
  19. !(
  20. (user.role.id === 2021 &&
  21. laporan.role_data === 'lldikti' &&
  22. laporan.pt.pembina.id === user.lembaga.id &&
  23. laporan.aktif === true) ||
  24. (user.role.id === 2020 &&
  25. laporan.role_data === 'dikti' &&
  26. laporan.aktif === true)
  27. )
  28. ) {
  29. return response.error(res, {
  30. message: 'laporan_id tidak ada',
  31. code: 404,
  32. })
  33. }
  34. const data = await laporanModel.findByIdAndUpdate(
  35. laporan._id,
  36. {
  37. jadwal: {
  38. judul,
  39. dari_tanggal,
  40. sampai_tanggal,
  41. warna,
  42. },
  43. },
  44. {
  45. new: true,
  46. }
  47. )
  48. return response.success(res, {
  49. message: 'Berhasil ubah jadwal',
  50. data,
  51. })
  52. })