瀏覽代碼

udah aman

yazid138 3 年之前
父節點
當前提交
28e50de74f
共有 2 個文件被更改,包括 90 次插入16 次删除
  1. 89 15
      controller/laporan.controller.js
  2. 1 1
      middleware/role.js

+ 89 - 15
controller/laporan.controller.js

@@ -444,28 +444,102 @@ exports.jumlahLaporan = handleError(async (req, res) => {
 
 exports.laporanByPembina = handleError(async (req, res) => {
   const { idPembina } = req.params
+  const {
+    penjadwalan,
+    pemeriksaa,
+    sanksi,
+    keberatan,
+    banding,
+    perbaikan,
+    cabutSanksi,
+  } = req.query
   const user = req.user
+  const where = {}
+  let isLaporan = true
+  let isSanksi = true
+
+  if (penjadwalan === 'true') {
+    where.jadwal = {
+      $exists: true,
+      $ne: null,
+    }
+    isLaporan = true
+    isSanksi = false
+  }
+  if (pemeriksaa === 'true') {
+    where.evaluasi = {
+      $exists: true,
+      $ne: null,
+      $not: {
+        $size: 0,
+      },
+    }
+    isLaporan = true
+    isSanksi = false
+  }
+  if (sanksi === 'true') {
+    where.sanksi = {
+      $exists: true,
+      $ne: null,
+    }
+    isLaporan = false
+    isSanksi = true
+  }
+  if (keberatan === 'true') {
+    where['pengajuan.keberatan'] = { $exists: true, $ne: null }
+    isLaporan = false
+    isSanksi = true
+  }
+  if (banding === 'true') {
+    where.banding = true
+    where['pengajuan.keberatan'] = { $exists: true, $ne: null }
+    where['jawaban.keberatan'] = { $exists: true, $ne: null }
+    where['pengajuan.banding'] = { $exists: true, $ne: null }
+    isLaporan = false
+    isSanksi = true
+  }
+  if (cabutSanksi === 'true') {
+    where.perbaikan = {
+      $exists: true,
+      $ne: null,
+      $not: {
+        $size: 0,
+      },
+    }
+    isLaporan = false
+    isSanksi = true
+  }
+  if (perbaikan === 'true') {
+    where['jawaban.banding'] = { $exists: true, $ne: null }
+    isLaporan = false
+    isSanksi = true
+  }
 
-  const [laporan, sanksi] = await Promise.all([
+  const [laporan, dataSanksi] = await Promise.all([
     (async () =>
-      await cekBanyakDataLaporan(user, {
-        'pt.pembina.id': idPembina,
-        all: true,
-      }))(),
+      isLaporan
+        ? await cekBanyakDataLaporan(user, {
+            'pt.pembina.id': idPembina,
+            all: true,
+            ...where,
+          })
+        : [])(),
     (async () =>
-      (
-        await cekBanyakDataSanksi(
-          user,
-          { all: true },
-          {
-            ['pt.pembina.id']: idPembina,
-          }
-        )
-      ).filter((e) => e.laporan != null))(),
+      isSanksi
+        ? (
+            await cekBanyakDataSanksi(
+              user,
+              { all: true, ...where },
+              {
+                ['pt.pembina.id']: idPembina,
+              }
+            )
+          ).filter((e) => e.laporan != null)
+        : [])(),
   ])
 
   return response.success(res, {
     message: 'berhasil get laporan by pembina',
-    data: { laporan, sanksi },
+    data: { laporan, sanksi: dataSanksi },
   })
 })

+ 1 - 1
middleware/role.js

@@ -7,7 +7,7 @@ module.exports = (role) => async (req, res, next) => {
     (typeof role == 'number' && user.role.id !== role) ||
     (typeof role == 'object' && !role.includes(user.role.id))
   ) {
-    response.error(res, {
+    return response.error(res, {
       message: 'Forbidden',
       code: 403,
     })