laporan.controller.js 15 KB

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