auto.controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. const handleError = require('../utils/handleError')
  2. const response = require('../utils/responseHandler')
  3. const { notifWA } = require('../utils/notifFunction')
  4. const sanksiModel = require('../model/sanksi.model')
  5. const laporanModel = require('../model/laporan.model')
  6. const backupModel = require('../model/backup.model')
  7. const fs = require('fs')
  8. const mime = require('mime')
  9. const {
  10. TEMPLATE_KEBERATAN,
  11. TEMPLATE_BANDING,
  12. TEMPLATE_REMINDER,
  13. } = require('../utils/constanta')
  14. const moment = require('moment')
  15. const autoSaveModel = require('../model/autoSave.model')
  16. const { cekSatuDataSanksi, cekSatuDataLaporan } = require('../utils/cekData')
  17. const userModel = require('../model/user.model')
  18. const { addManyDokumen } = require('../utils/dokumenFunction')
  19. exports.keberatan = handleError(async (req, res) => {
  20. const dataSanksi = await sanksiModel
  21. .find({
  22. 'batas_waktu.keberatan': {
  23. $lt: new Date().toISOString(),
  24. },
  25. 'batas_waktu.jawaban_keberatan': {
  26. $exists: false,
  27. $eq: null,
  28. },
  29. })
  30. .populate('user')
  31. .populate('laporan')
  32. if (!dataSanksi.length) {
  33. return response.success(res, {
  34. message: 'Tidak ada notifikasi yg dikirim',
  35. })
  36. }
  37. Promise.all(
  38. dataSanksi.map(
  39. async (sanksi) =>
  40. await notifWA(TEMPLATE_KEBERATAN, [
  41. {
  42. key: '1',
  43. value: 'nama_pt',
  44. value_text: sanksi.laporan.pt.nama,
  45. },
  46. {
  47. key: '2',
  48. value: 'pemberi_sanksi',
  49. value_text: sanksi.user.lembaga.nama,
  50. },
  51. {
  52. key: '3',
  53. value: 'no_laporan',
  54. value_text: sanksi.laporan.no_laporan,
  55. },
  56. ])
  57. )
  58. )
  59. return response.success(res, {
  60. message: 'Notifikasi berhasil terkirim',
  61. })
  62. })
  63. exports.banding = handleError(async (req, res) => {
  64. const dataSanksi = await sanksiModel
  65. .find({
  66. 'batas_waktu.banding': {
  67. $lt: new Date().toISOString(),
  68. },
  69. 'batas_waktu.jawaban_banding': {
  70. $exists: false,
  71. $eq: null,
  72. },
  73. 'batas_waktu.jawaban_keberatan': {
  74. $exists: true,
  75. $ne: null,
  76. },
  77. })
  78. .populate('user')
  79. .populate('laporan')
  80. if (!dataSanksi.length) {
  81. return response.success(res, {
  82. message: 'Tidak ada notifikasi yg dikirim',
  83. })
  84. }
  85. Promise.all(
  86. dataSanksi.map(
  87. async (sanksi) =>
  88. await notifWA(TEMPLATE_BANDING, [
  89. {
  90. key: '1',
  91. value: 'nama_pt',
  92. value_text: sanksi.laporan.pt.nama,
  93. },
  94. {
  95. key: '2',
  96. value: 'pemberi_sanksi',
  97. value_text: sanksi.user.lembaga.nama,
  98. },
  99. {
  100. key: '3',
  101. value: 'no_laporan',
  102. value_text: sanksi.laporan.no_laporan,
  103. },
  104. ])
  105. )
  106. )
  107. return response.success(res, {
  108. message: 'Notifikasi berhasil terkirim',
  109. })
  110. })
  111. exports.reminderKeberatan = handleError(async (req, res) => {
  112. let dataSanksi = await sanksiModel
  113. .find({
  114. 'batas_waktu.jawaban_keberatan': {
  115. $exists: true,
  116. $ne: null,
  117. },
  118. 'jawaban.keberatan': {
  119. $exists: false,
  120. $eq: null,
  121. },
  122. })
  123. .populate('user')
  124. .populate('laporan')
  125. const notif = await Promise.all(
  126. dataSanksi.map(async (e) => {
  127. if (
  128. e.batas_waktu.jawaban_keberatan &&
  129. new Date() >
  130. moment(e.batas_waktu.jawaban_keberatan).add(-7, 'days').toDate() &&
  131. new Date() < e.batas_waktu.jawaban_keberatan
  132. ) {
  133. const dayLeft = moment(e.batas_waktu.jawaban_keberatan).diff(
  134. new Date(),
  135. 'days'
  136. )
  137. try {
  138. await notifWA(TEMPLATE_REMINDER, [
  139. {
  140. key: '1',
  141. value: 'no_laporan',
  142. value_text: e.laporan.no_laporan,
  143. },
  144. {
  145. key: '2',
  146. value: 'keterangan',
  147. value_text: 'Proses Menjawab Pengajuan Keberatan',
  148. },
  149. {
  150. key: '3',
  151. value: 'pt',
  152. value_text: e.laporan.pt.nama,
  153. },
  154. {
  155. key: '4',
  156. value: 'masa',
  157. value_text: `menjawab pengajuan keberatan tersisa ${dayLeft} hari lagi.`,
  158. },
  159. ])
  160. } catch (error) {
  161. return response.error(res, {
  162. message: 'Notifikasi gagal terkirim',
  163. error: error.message,
  164. })
  165. }
  166. }
  167. })
  168. )
  169. let message = 'Tidak ada notifikasi yang dikirim'
  170. if (notif.length) message = 'Notifikasi berhasil terkirim'
  171. return response.success(res, {
  172. message,
  173. })
  174. })
  175. exports.reminderBanding = handleError(async (req, res) => {
  176. let dataSanksi = await sanksiModel
  177. .find({
  178. 'batas_waktu.jawaban_banding': {
  179. $exists: true,
  180. $ne: null,
  181. },
  182. 'jawaban.banding': {
  183. $exists: false,
  184. $eq: null,
  185. },
  186. })
  187. .populate('user')
  188. .populate('laporan')
  189. const notif = await Promise.all(
  190. dataSanksi.map(async (e) => {
  191. if (
  192. e.batas_waktu.jawaban_banding &&
  193. new Date() >
  194. moment(e.batas_waktu.jawaban_banding).add(-7, 'days').toDate() &&
  195. new Date() < e.batas_waktu.jawaban_banding
  196. ) {
  197. const dayLeft = moment(e.batas_waktu.jawaban_banding).diff(
  198. new Date(),
  199. 'days'
  200. )
  201. try {
  202. await notifWA(TEMPLATE_REMINDER, [
  203. {
  204. key: '1',
  205. value: 'no_laporan',
  206. value_text: e.laporan.no_laporan,
  207. },
  208. {
  209. key: '2',
  210. value: 'keterangan',
  211. value_text: 'Proses Menjawab Pengajuan Banding',
  212. },
  213. {
  214. key: '3',
  215. value: 'pt',
  216. value_text: e.laporan.pt.nama,
  217. },
  218. {
  219. key: '4',
  220. value: 'masa',
  221. value_text: `menjawab pengajuan banding tersisa ${dayLeft} hari lagi.`,
  222. },
  223. ])
  224. } catch (error) {
  225. return response.error(res, {
  226. message: 'Notifikasi gagal terkirim',
  227. error: error.message,
  228. })
  229. }
  230. }
  231. })
  232. )
  233. let message = 'Tidak ada notifikasi yang dikirim'
  234. if (notif.length) message = 'Notifikasi berhasil terkirim'
  235. return response.success(res, {
  236. message,
  237. })
  238. })
  239. exports.updateStatusSanksi = handleError(async (req, res) => {
  240. const sanksi = await sanksiModel.find({
  241. 'masa_berlaku.to_date': {
  242. $lte: new Date().toISOString(),
  243. },
  244. aktif: true,
  245. })
  246. Promise.all(
  247. sanksi.map(async (e) =>
  248. sanksiModel.findByIdAndUpdate(e._id, {
  249. aktif: false,
  250. })
  251. )
  252. )
  253. return response.success(res, {
  254. message: 'update status sanksi berhasil',
  255. })
  256. })
  257. exports.save = handleError(async (req, res) => {
  258. const { id } = req.params
  259. const { laporan: isLaporan, sanksi: isSanksi } = req.query
  260. const user = req.user
  261. let autoData = null;
  262. let laporan = null;
  263. let sanksi = null;
  264. if (isLaporan === 'true') {
  265. laporan = await cekSatuDataLaporan(res, user, id);
  266. if (!laporan) return
  267. autoData = await autoSaveModel.findOne({ laporan_id: laporan._id });
  268. } else if (isSanksi === 'true') {
  269. sanksi = await cekSatuDataSanksi(res, user, id);
  270. if (!sanksi) return
  271. autoData = await autoSaveModel.findOne({ sanksi_id: sanksi._id });
  272. } else {
  273. return response.error(res, {
  274. message: 'query harus sanksi atau laporan yg bernilai true',
  275. })
  276. }
  277. if (autoData) {
  278. if (isLaporan) {
  279. const dataSave = await autoSaveModel.findOne({ laporan_id: laporan._id })
  280. await autoSaveModel.updateOne({ laporan_id: laporan._id }, {
  281. laporan: {
  282. ...req.body, PenetapanSanksi: {
  283. dataSuratBA: req.body?.PenetapanSanksi?.dataSuratBA || dataSave.laporan.PenetapanSanksi?.dataSuratBA,
  284. dataUpload: req.body?.PenetapanSanksi?.dataUpload || dataSave.laporan.PenetapanSanksi?.dataUpload,
  285. dataPelanggaran: req.body?.PenetapanSanksi?.dataPelanggaran || dataSave.laporan.PenetapanSanksi?.dataPelanggaran,
  286. }
  287. }
  288. })
  289. } else {
  290. await autoSaveModel.updateOne({ sanksi_id: sanksi._id }, { sanksi: req.body })
  291. }
  292. } else {
  293. if (isLaporan) {
  294. await autoSaveModel.create({ laporan_id: laporan._id, laporan: req.body })
  295. } else {
  296. await autoSaveModel.create({ sanksi_id: sanksi._id, sanksi: req.body })
  297. }
  298. }
  299. return response.success(res, {
  300. message: 'Berhasil menyimpan data auto save',
  301. })
  302. })
  303. exports.getSave = handleError(async (req, res) => {
  304. const { id } = req.params
  305. const { laporan: isLaporan, sanksi: isSanksi } = req.query
  306. let data = null;
  307. let laporan = null;
  308. let sanksi = null;
  309. if (isLaporan === 'true') {
  310. laporan = await laporanModel.findById(id)
  311. if (!laporan) return response.error(res, {
  312. code: 404,
  313. message: 'laporan_id tidak ada'
  314. })
  315. data = (await autoSaveModel.findOne({ laporan_id: laporan._id })).laporan;
  316. } else if (isSanksi === 'true') {
  317. sanksi = await sanksiModel.findById(id)
  318. if (!sanksi) return response.error(res, {
  319. code: 404,
  320. message: 'sanksi_id tidak ada'
  321. })
  322. data = (await autoSaveModel.findOne({ sanksi_id: sanksi._id })).sanksi;
  323. } else {
  324. return response.error(res, {
  325. message: 'harus terdapat query sanksi atau laporan yg bernilai true',
  326. })
  327. }
  328. return response.success(res, {
  329. message: 'Berhasil mengambil data auto save',
  330. data: data
  331. })
  332. })
  333. exports.backup = handleError(async (req, res) => {
  334. const dataBakup = await Promise.all([
  335. (async () => {
  336. const path = 'backup/' + new Date().getTime() + '-sanksi.json'
  337. const sanksi = await sanksiModel.find()
  338. fs.writeFileSync(path, JSON.stringify(sanksi))
  339. const { size } = fs.statSync(path)
  340. const mimetype = 'application/json'
  341. const buffer = fs.readFileSync(path)
  342. return { buffer, mimetype, size, originalname: 'sanksi.json' }
  343. })(),
  344. (async () => {
  345. const path = 'backup/' + new Date().getTime() + '-laporan.json'
  346. const laporan = await laporanModel.find()
  347. fs.writeFileSync(path, JSON.stringify(laporan))
  348. const { size } = fs.statSync(path)
  349. const mimetype = 'application/json'
  350. const buffer = fs.readFileSync(path)
  351. return { buffer, mimetype, size, originalname: 'laporan.json' }
  352. })(),
  353. (async () => {
  354. const path = 'backup/' + new Date().getTime() + '-user.json'
  355. const user = await userModel.find()
  356. fs.writeFileSync(path, JSON.stringify(user))
  357. const { size } = fs.statSync(path)
  358. const mimetype = 'application/json'
  359. const buffer = fs.readFileSync(path)
  360. return { buffer, mimetype, size, originalname: 'user.json' }
  361. })()
  362. ])
  363. const dokumen = await addManyDokumen(dataBakup)
  364. const tes = await backupModel.create({
  365. dokumen: dokumen.map(e => e._id)
  366. })
  367. return response.success(res, {
  368. message: 'berhasil membuat backup',
  369. data: tes
  370. })
  371. })
  372. exports.getBackup = handleError(async (req, res) => {
  373. const { id } = req.params
  374. const data = await backupModel.findById(id).populate('dokumen')
  375. if (!data) return response.error(res, {
  376. code: 404,
  377. message: 'Not Found',
  378. })
  379. return response.success(res, {
  380. message: 'berhasil mengambil data backup',
  381. data: data.dokumen
  382. })
  383. })