laporan.controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. const axios = require('../utils/axios')
  2. const handleError = require('../utils/handleError')
  3. const response = require('../utils/responseHandler')
  4. const laporanModel = require('../model/laporan.model')
  5. const pelanggaranModel = require('../model/pelanggaran.model')
  6. const pemantauanModel = require('../model/pemantauan.model')
  7. const { validate } = require('../utils/validation')
  8. const { notifWA } = require('../utils/notifFunction')
  9. const { addManyDokumen } = require('../utils/dokumenFunction')
  10. const userModel = require('../model/user.model')
  11. const {
  12. cekSatuDataLaporan,
  13. cekBanyakDataLaporan,
  14. cekBanyakDataSanksi,
  15. } = require('../utils/cekData')
  16. const { TEMPLATE_LAPORAN } = require('../utils/constanta')
  17. const logModel = require('../model/log.model')
  18. const kontakModel = require('../model/kontak.model')
  19. exports.create = handleError(async (req, res) => {
  20. const user = req.user
  21. const files = req.files
  22. const isValid = validate(res, req.body, {
  23. no_laporan: 'string',
  24. pt_id: 'string',
  25. pelanggaran_id: 'string',
  26. keterangan: 'string',
  27. })
  28. if (!isValid) return
  29. const { no_laporan, pt_id, keterangan } = req.body
  30. let { pelanggaran_id } = req.body
  31. const pt = await axios.get(
  32. `https://api.kemdikbud.go.id:8243/pddikti/1.2/pt/${pt_id}`
  33. )
  34. if (pt.length === 0)
  35. return response.error(res, {
  36. message: 'pt_id tidak ditemukan',
  37. })
  38. let dokumen_id = []
  39. if (files.length) {
  40. const dokumen = await addManyDokumen(files)
  41. dokumen_id = dokumen.map((e) => e._id)
  42. }
  43. pelanggaran_id = pelanggaran_id.split(',')
  44. const pelanggaran = await pelanggaranModel.find({
  45. _id: {
  46. $in: pelanggaran_id,
  47. },
  48. })
  49. if (!pelanggaran.length)
  50. return response.error(res, { message: 'pelanggaran_id tidak ada' })
  51. let data = {
  52. no_laporan,
  53. user: user._id,
  54. dokumen: dokumen_id,
  55. pt: pt[0],
  56. pelanggaran: pelanggaran_id,
  57. keterangan,
  58. role_data: user.role.id === 2020 ? 'dikti' : 'lldikti',
  59. role_asal: user.role.id === 2020 ? 'dikti' : 'lldikti',
  60. level: 2,
  61. }
  62. data = await laporanModel.create(data)
  63. const notif = await notifWA(TEMPLATE_LAPORAN, [
  64. {
  65. key: '1',
  66. value: 'nama',
  67. value_text: user.nama,
  68. },
  69. { key: '2', value: 'pt', value_text: pt[0].nama },
  70. { key: '3', value: 'keterangan', value_text: keterangan },
  71. { key: '4', value: 'no_laporan', value_text: no_laporan },
  72. ])
  73. let contacts = await kontakModel.find()
  74. contacts = contacts.map((e) => e.nama).join(', ')
  75. if (notif[0].status == 'success') {
  76. await logModel.create({
  77. aktivitas: `Server berhasil mengirim notif wa kepada ${contacts} untuk Pembuatan Laporan`,
  78. })
  79. }
  80. await pemantauanModel.create({
  81. laporan: data._id,
  82. action: 'CREATE LAPORAN',
  83. pt_id: pt[0].id,
  84. user: user._id,
  85. keterangan: 'Membuat Laporan',
  86. dokumen: dokumen_id,
  87. for_pt: false,
  88. })
  89. return response.success(res, {
  90. message: 'Berhasil menambah laporan',
  91. data,
  92. })
  93. })
  94. // exports.public = handleError(async (req, res) => {
  95. // const isValid = validate(res, req.body, {
  96. // nama: 'string',
  97. // email: 'email',
  98. // alamat: 'string',
  99. // no_hp: 'string',
  100. // no_laporan: 'string',
  101. // pt_id: 'string',
  102. // pelanggaran_id: 'string',
  103. // keterangan: 'string',
  104. // is_private: { type: 'string', enum: ['true', 'false'] },
  105. // })
  106. // if (!isValid) return
  107. // const {
  108. // no_laporan,
  109. // pt_id,
  110. // keterangan,
  111. // nama,
  112. // email,
  113. // alamat,
  114. // no_hp,
  115. // is_private,
  116. // } = req.body
  117. // let { pelanggaran_id } = req.body
  118. // const pt = await axios.get(
  119. // `https://api.kemdikbud.go.id:8243/pddikti/1.2/pt/${pt_id}`
  120. // )
  121. // if (!pt) {
  122. // return response.error(res, {
  123. // message: 'pt_id tidak ditemukan',
  124. // })
  125. // }
  126. // const { dokumen, foto } = req.files
  127. // if (!foto.length) {
  128. // return response.error(res, {
  129. // message: 'foto harus ada',
  130. // })
  131. // }
  132. // const foto_id = await addDokumen(foto[0])
  133. // const user = await userModel.create({
  134. // nama,
  135. // email,
  136. // no_hp,
  137. // alamat,
  138. // isPublic: true,
  139. // isPrivate: is_private === 'true',
  140. // foto: foto_id,
  141. // })
  142. // let dokumen_id = []
  143. // if (dokumen?.length) {
  144. // const dataDokumen = await addManyDokumen(dokumen)
  145. // dokumen_id = dataDokumen.map((e) => e._id)
  146. // }
  147. // pelanggaran_id = pelanggaran_id.split(',')
  148. // const pelanggaran = await pelanggaranModel.find({
  149. // _id: {
  150. // $in: pelanggaran_id,
  151. // },
  152. // })
  153. // if (!pelanggaran.length)
  154. // return response.error(res, { message: 'pelanggaran_id tidak ada' })
  155. // let data = {
  156. // no_laporan,
  157. // user: user._id,
  158. // dokumen: dokumen_id,
  159. // pt: pt[0],
  160. // pelanggaran: pelanggaran_id,
  161. // keterangan,
  162. // role_data: 'dikti',
  163. // }
  164. // data = await laporanModel.create(data)
  165. // await pemantauanModel.create({
  166. // laporan: data._id,
  167. // pt_id: pt[0].id,
  168. // user: user._id,
  169. // keterangan: 'Mengajukan Laporan',
  170. // dokumen: dokumen_id,
  171. // for_pt: false,
  172. // })
  173. // await notifWA('d5609c3c-e9e9-4dbe-9a4e-e8fa772d6770', [
  174. // { key: '1', value: 'nama', value_text: nama },
  175. // { key: '2', value: 'pt', value_text: pt[0].nama },
  176. // { key: '3', value: 'keterangan', value_text: keterangan },
  177. // { key: '4', value: 'no_laporan', value_text: no_laporan },
  178. // ])
  179. // return response.success(res, {
  180. // message: 'Berhasil menambah laporan',
  181. // data,
  182. // })
  183. // })
  184. exports.public = handleError(async (req, res) => {
  185. const user = req.user
  186. const no_laporan = req.no_laporan
  187. let level = req.level
  188. const files = req.files
  189. const isValid = validate(res, req.body, {
  190. pt_id: 'string',
  191. pelanggaran_id: 'string',
  192. keterangan: 'string',
  193. // no_verifikasi: 'string',
  194. })
  195. if (!isValid) return
  196. const { pt_id, keterangan, no_verifikasi } = req.body
  197. let { pelanggaran_id } = req.body
  198. if (no_verifikasi && user.no_verifikasi !== no_verifikasi) {
  199. return response.error(res, {
  200. message: 'no_verifikasi tidak sesuai',
  201. error: { no_verifikasi: 'No. Verifikasi tidak sesuai' },
  202. })
  203. } else if (no_verifikasi && user.no_verifikasi === no_verifikasi) {
  204. level = 3
  205. }
  206. const pt = await axios.get(
  207. `https://api.kemdikbud.go.id:8243/pddikti/1.2/pt/${pt_id}`
  208. )
  209. if (pt.length === 0)
  210. return response.error(res, {
  211. message: 'pt_id tidak ditemukan',
  212. })
  213. let dokumen_id = []
  214. if (files.length) {
  215. const dokumen = await addManyDokumen(files)
  216. dokumen_id = dokumen.map((e) => e._id)
  217. }
  218. pelanggaran_id = pelanggaran_id.split(',')
  219. const pelanggaran = await pelanggaranModel.find({
  220. _id: {
  221. $in: pelanggaran_id,
  222. },
  223. })
  224. if (!pelanggaran.length)
  225. return response.error(res, { message: 'pelanggaran_id tidak ada' })
  226. let data = {
  227. no_laporan,
  228. user: user._id,
  229. dokumen: dokumen_id,
  230. pt: pt[0],
  231. pelanggaran: pelanggaran_id,
  232. keterangan,
  233. role_data: 'dikti',
  234. role_asal: 'dikti',
  235. level,
  236. }
  237. data = await laporanModel.create(data)
  238. await pemantauanModel.create({
  239. laporan: data._id,
  240. action: 'CREATE LAPORAN',
  241. pt_id: pt[0].id,
  242. user: user._id,
  243. keterangan: 'Membuat Laporan',
  244. dokumen: dokumen_id,
  245. for_pt: false,
  246. })
  247. if (no_verifikasi)
  248. await userModel.findByIdAndUpdate(user._id, { verified: true })
  249. const notif = await notifWA(TEMPLATE_LAPORAN, [
  250. {
  251. key: '1',
  252. value: 'nama',
  253. value_text: user.isPrivate || !user.nama ? 'rahasia' : user.nama,
  254. },
  255. { key: '2', value: 'pt', value_text: pt[0].nama },
  256. { key: '3', value: 'keterangan', value_text: keterangan },
  257. { key: '4', value: 'no_laporan', value_text: no_laporan },
  258. ])
  259. let contacts = await kontakModel.find()
  260. contacts = contacts.map((e) => e.nama).join(', ')
  261. if (notif[0].status == 'success') {
  262. await logModel.create({
  263. aktivitas: `Server berhasil mengirim notif wa kepada ${contacts} untuk Pembuatan Laporan`,
  264. })
  265. }
  266. return response.success(res, {
  267. message: 'Berhasil menambah laporan',
  268. data,
  269. })
  270. })
  271. exports.getAll = handleError(async (req, res) => {
  272. const user = req.user
  273. const where = {}
  274. const { no_laporan, pt_id, jadwal, evaluasi, aktif, delegasi, all, sanksi } =
  275. req.query
  276. if (no_laporan) where.no_laporan = no_laporan
  277. if (pt_id) where['pt.id'] = pt_id
  278. if (aktif) where.aktif = aktif === 'true'
  279. if (all) where.all = true
  280. else if (delegasi) where.delegasi = delegasi === 'true'
  281. if (jadwal === 'true') {
  282. where.jadwal = {
  283. $exists: true,
  284. $ne: null,
  285. }
  286. } else if (evaluasi === 'true') {
  287. where.evaluasi = {
  288. $exists: true,
  289. $ne: null,
  290. $not: {
  291. $size: 0,
  292. },
  293. }
  294. } else if (sanksi === 'true') {
  295. where.sanksi = {
  296. $exists: true,
  297. $ne: null,
  298. }
  299. }
  300. let data = await cekBanyakDataLaporan(user, where)
  301. return response.success(res, {
  302. message: 'Berhasil ambil data laporan',
  303. data,
  304. })
  305. })
  306. exports.getOne = handleError(async (req, res) => {
  307. const { id } = req.params
  308. const user = req.user
  309. const { aktif, delegasi, all } = req.query
  310. const where = {}
  311. if (aktif) where.aktif = aktif === 'true'
  312. if (all) where.all = true
  313. else if (delegasi) where.delegasi = delegasi === 'true'
  314. const data = await cekSatuDataLaporan(res, user, id, where)
  315. if (!data) return
  316. return response.success(res, {
  317. message: 'Berhasil ambil data Laporan',
  318. data,
  319. })
  320. })
  321. exports.update = handleError(async (req, res) => {
  322. const { id } = req.params
  323. const user = req.user
  324. const laporan = await cekSatuDataLaporan(res, user, id)
  325. if (!laporan) return
  326. const isValid = validate(res, req.body, {
  327. change_role: { type: 'string', optional: true, enum: ['true', 'false'] },
  328. aktif: { type: 'string', optional: true, enum: ['true', 'false'] },
  329. keterangan: 'string',
  330. })
  331. if (!isValid) return
  332. const data = {}
  333. let keterangan = ''
  334. let alasan = ''
  335. const { change_role, aktif } = req.body
  336. const keterangan2 = req.body.keterangan
  337. if (change_role === 'true') {
  338. data.role_data = user.role.id === 2020 ? 'lldikti' : 'dikti'
  339. keterangan = `Laporan didelegasi ke ${
  340. user.role.id === 2020 ? 'LLDIKTI' : 'DIKTI'
  341. }`
  342. alasan = keterangan2
  343. data.alasan_delegasi = keterangan2
  344. // if (laporan.jadwal) {
  345. // await laporanModel.findByIdAndUpdate(laporan._id, {
  346. // $unset: { jadwal: 1 },
  347. // })
  348. // }
  349. }
  350. if (aktif) {
  351. data.aktif = aktif === 'true'
  352. if (aktif === 'true') {
  353. keterangan = 'Laporan dibuka'
  354. } else {
  355. keterangan = `Laporan ditutup`
  356. alasan = keterangan2
  357. }
  358. }
  359. const update = await laporanModel.findByIdAndUpdate(laporan._id, data)
  360. if (change_role || aktif) {
  361. await pemantauanModel.create({
  362. action: 'UPDATE LAPORAN',
  363. laporan: laporan._id,
  364. pt_id: laporan.pt.id,
  365. user: user._id,
  366. keterangan,
  367. alasan,
  368. for_pt: false,
  369. })
  370. }
  371. return response.success(res, {
  372. message: 'Berhasil update laporan',
  373. data: update,
  374. })
  375. })
  376. exports.jumlahLaporan = handleError(async (req, res) => {
  377. const laporan = await laporanModel.aggregate([
  378. {
  379. $match: {
  380. aktif: true,
  381. },
  382. },
  383. {
  384. $group: {
  385. _id: '$pt.pembina.nama',
  386. jumlah_laporan: {
  387. $sum: 1,
  388. },
  389. propinsi: {
  390. $addToSet: '$pt.propinsi.nama',
  391. },
  392. },
  393. },
  394. {
  395. $sort: {
  396. _id: 1,
  397. },
  398. },
  399. ])
  400. return response.success(res, {
  401. message: 'Jumlah Laporan',
  402. data: laporan,
  403. })
  404. })
  405. exports.laporanByPembina = handleError(async (req, res) => {
  406. const { idPembina } = req.params
  407. const {
  408. penjadwalan,
  409. pemeriksaan,
  410. sanksi,
  411. keberatan,
  412. banding,
  413. perbaikan,
  414. cabutSanksi,
  415. } = req.query
  416. const user = req.user
  417. const where = {}
  418. let isLaporan = true
  419. let isSanksi = true
  420. if (penjadwalan === 'true') {
  421. where.jadwal = {
  422. $exists: true,
  423. $ne: null,
  424. }
  425. isLaporan = true
  426. isSanksi = false
  427. }
  428. if (pemeriksaan === 'true') {
  429. where.evaluasi = {
  430. $exists: true,
  431. $ne: null,
  432. $not: {
  433. $size: 0,
  434. },
  435. }
  436. isLaporan = true
  437. isSanksi = false
  438. }
  439. if (sanksi === 'true') {
  440. where.sanksi = {
  441. $exists: true,
  442. $ne: null,
  443. }
  444. isLaporan = false
  445. isSanksi = true
  446. }
  447. if (keberatan === 'true') {
  448. where['pengajuan.keberatan'] = { $exists: true, $ne: null }
  449. isLaporan = false
  450. isSanksi = true
  451. }
  452. if (banding === 'true') {
  453. where.banding = true
  454. where['pengajuan.keberatan'] = { $exists: true, $ne: null }
  455. where['jawaban.keberatan'] = { $exists: true, $ne: null }
  456. where['pengajuan.banding'] = { $exists: true, $ne: null }
  457. isLaporan = false
  458. isSanksi = true
  459. }
  460. if (cabutSanksi === 'true') {
  461. where.perbaikan = {
  462. $exists: true,
  463. $ne: null,
  464. $not: {
  465. $size: 0,
  466. },
  467. }
  468. isLaporan = false
  469. isSanksi = true
  470. }
  471. if (perbaikan === 'true') {
  472. where['jawaban.banding'] = { $exists: true, $ne: null }
  473. isLaporan = false
  474. isSanksi = true
  475. }
  476. const [laporan, dataSanksi] = await Promise.all([
  477. (async () =>
  478. isLaporan
  479. ? await cekBanyakDataLaporan(user, {
  480. 'pt.pembina.id': idPembina,
  481. all: true,
  482. ...where,
  483. })
  484. : [])(),
  485. (async () =>
  486. isSanksi
  487. ? (
  488. await cekBanyakDataSanksi(
  489. user,
  490. { all: true, ...where },
  491. {
  492. ['pt.pembina.id']: idPembina,
  493. }
  494. )
  495. ).filter((e) => e.laporan != null)
  496. : [])(),
  497. ])
  498. return response.success(res, {
  499. message: 'berhasil get laporan by pembina',
  500. data: { laporan, sanksi: dataSanksi },
  501. })
  502. })