Browse Source

add auto update status sanksi

yazid138 3 năm trước cách đây
mục cha
commit
d7787e41a1
2 tập tin đã thay đổi với 32 bổ sung4 xóa
  1. 31 4
      controller/auto.controller.js
  2. 1 0
      routes/v1/auto.routes.js

+ 31 - 4
controller/auto.controller.js

@@ -121,7 +121,7 @@ exports.reminderKeberatan = handleError(async (req, res) => {
     })
     .populate('user')
     .populate('laporan')
-  Promise.all(
+  const notif = await Promise.all(
     dataSanksi.map(async (e) => {
       if (
         e.batas_waktu.jawaban_keberatan &&
@@ -166,8 +166,11 @@ exports.reminderKeberatan = handleError(async (req, res) => {
     })
   )
 
+  let message = 'Tidak ada notifikasi yang dikirim'
+  if (notif.length) message = 'Notifikasi berhasil terkirim'
+
   return response.success(res, {
-    message: 'Notifikasi berhasil terkirim',
+    message,
   })
 })
 
@@ -185,7 +188,7 @@ exports.reminderBanding = handleError(async (req, res) => {
     })
     .populate('user')
     .populate('laporan')
-  Promise.all(
+  const notif = await Promise.all(
     dataSanksi.map(async (e) => {
       if (
         e.batas_waktu.jawaban_banding &&
@@ -230,7 +233,31 @@ exports.reminderBanding = handleError(async (req, res) => {
     })
   )
 
+  let message = 'Tidak ada notifikasi yang dikirim'
+  if (notif.length) message = 'Notifikasi berhasil terkirim'
+
   return response.success(res, {
-    message: 'Notifikasi berhasil terkirim',
+    message,
+  })
+})
+
+exports.updateStatusSanksi = handleError(async (req, res) => {
+  const sanksi = await sanksiModel.find({
+    'masa_berlaku.to_date': {
+      $lte: new Date().toISOString(),
+    },
+    aktif: true,
+  })
+
+  Promise.all(
+    sanksi.map(async (e) =>
+      sanksiModel.findByIdAndUpdate(e._id, {
+        aktif: false,
+      })
+    )
+  )
+
+  return response.success(res, {
+    message: 'update status sanksi berhasil',
   })
 })

+ 1 - 0
routes/v1/auto.routes.js

@@ -5,5 +5,6 @@ router.get('/keberatan', auto.keberatan)
 router.get('/banding', auto.banding)
 router.get('/reminder-keberatan', auto.reminderKeberatan)
 router.get('/reminder-banding', auto.reminderBanding)
+router.get('/status-sanksi', auto.updateStatusSanksi)
 
 module.exports = router