migrasi.controller.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const sanksiModel = require('../../model/sanksi.model')
  2. const dokumenModel = require('../../model/dokumen.model')
  3. const laporanModel = require('../../model/laporan.model')
  4. const handleError = require('../../utils/v1/handleError')
  5. const response = require('../../utils/responseHandler')
  6. const coba = require('../../utils/coba')
  7. const migrateDokumenInBackground = async () => {
  8. const dokumen = await dokumenModel.find()
  9. const BATCH_SIZE = 10 // Proses 10 dokumen per batch
  10. const DELAY_MS = 100 // Delay 100ms antar batch
  11. if (dokumen?.length) {
  12. for (let i = 0; i < dokumen.length; i += BATCH_SIZE) {
  13. const batch = dokumen.slice(i, i + BATCH_SIZE)
  14. await Promise.all(
  15. batch.map(async (e) => {
  16. const path = `${coba.decrypt(process.env.W8A1C)}/dokumen/${e.chunk}/${e.judul}`
  17. await dokumenModel.findOneAndUpdate(
  18. {
  19. _id: e._id,
  20. },
  21. {
  22. path,
  23. }
  24. )
  25. })
  26. )
  27. // Delay antar batch untuk avoid rate limit
  28. if (i + BATCH_SIZE < dokumen.length) {
  29. await new Promise((resolve) => setTimeout(resolve, DELAY_MS))
  30. }
  31. }
  32. }
  33. }
  34. exports.pengajuan = handleError(async (req, res) => {
  35. const [keberatan, banding] = await Promise.all([
  36. (() =>
  37. sanksiModel.find({
  38. ['pengajuan.keberatan']: { $ne: null, $exists: true },
  39. is_pengajuan_keberatan: { $eq: null, $exists: false },
  40. }))(),
  41. (() =>
  42. sanksiModel.find({
  43. ['jawaban.keberatan']: { $ne: null, $exists: true },
  44. ['pengajuan.banding']: { $ne: null, $exists: true },
  45. is_pengajuan_banding: { $eq: null, $exists: false },
  46. }))(),
  47. ])
  48. await Promise.all([
  49. ...keberatan.map(async (e) => {
  50. await sanksiModel.findOneAndUpdate(
  51. { _id: e._id },
  52. { is_pengajuan_keberatan: true }
  53. )
  54. }),
  55. ...banding.map(async (e) => {
  56. await sanksiModel.findOneAndUpdate(
  57. { _id: e._id },
  58. { is_pengajuan_banding: true }
  59. )
  60. }),
  61. ])
  62. return response.success(res, {
  63. message: 'Berhasil migrasi pengajuan',
  64. })
  65. })
  66. exports.dokumen = handleError(async (req, res) => {
  67. setImmediate(() => {
  68. migrateDokumenInBackground().catch((error) => {
  69. console.error('Gagal migrasi dokumen di background:', error)
  70. })
  71. })
  72. return response.success(res, {
  73. message: 'Migrasi dokumen dijalankan di background',
  74. })
  75. })
  76. exports.pelanggaranSanksi = handleError(async (req, res) => {
  77. const sanksi = await sanksiModel
  78. .find({
  79. sanksi: {
  80. $eq: [],
  81. },
  82. })
  83. .populate('pelanggaran')
  84. if (sanksi?.length)
  85. await Promise.all(
  86. sanksi.map(async (e) => {
  87. await sanksiModel.findOneAndUpdate(
  88. {
  89. _id: e._id,
  90. },
  91. {
  92. sanksi: e.pelanggaran.map((e2) => ({
  93. label: e2.label_sanksi,
  94. description: e2.sanksi,
  95. level: e2.level_sanksi,
  96. })),
  97. }
  98. )
  99. })
  100. )
  101. return response.success(res, {
  102. message: 'Berhasil migrasi pelanggaran sanksi',
  103. })
  104. })
  105. exports.tambahStep = handleError(async (req, res) => {
  106. const laporan = await laporanModel.find()
  107. await Promise.all(
  108. laporan.map((e) => {
  109. let step = ['pelaporan']
  110. if (e.jadwal) step.push('penjadwalan')
  111. if (e.evaluasi.length) step.push('pemeriksaan')
  112. if (e.sanksi) step.push('sanksi')
  113. return laporanModel.updateOne({ _id: e._id }, { step })
  114. })
  115. )
  116. const sanksi = await sanksiModel.find()
  117. await Promise.all(
  118. sanksi.map((e) => {
  119. let step = []
  120. if (e.pengajuan?.keberatan) step.push('keberatan')
  121. if (e.pengajuan?.banding) step.push('banding')
  122. if (e.perbaikan.length) step.push('dokumen_perbaikan')
  123. if (e.pengajuan?.cabut_sanksi) step.push('cabut_sanksi')
  124. return sanksiModel.updateOne({ _id: e._id }, { step })
  125. })
  126. )
  127. return laporan
  128. })
  129. exports.backToSanksi = handleError(async (req, res) => {
  130. const sanksi = await sanksiModel.find({
  131. 'masa_berlaku.to_date': {
  132. $lte: new Date().toISOString(),
  133. },
  134. aktif: false,
  135. })
  136. await Promise.all(
  137. sanksi.map((e) =>
  138. sanksiModel.findOneAndUpdate(
  139. { _id: e._id },
  140. { aktif: true, 'masa_berlaku.berakhir': true }
  141. )
  142. )
  143. )
  144. return sanksi
  145. })