| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 | const handleError = require('../utils/handleError')const response = require('../utils/responseHandler')const { notifWA } = require('../utils/notifFunction')const sanksiModel = require('../model/sanksi.model')const {  TEMPLATE_KEBERATAN,  TEMPLATE_BANDING,  TEMPLATE_REMINDER,} = require('../utils/constanta')const moment = require('moment')exports.keberatan = handleError(async (req, res) => {  const dataSanksi = await sanksiModel    .find({      'batas_waktu.keberatan': {        $lt: new Date().toISOString(),      },      'batas_waktu.jawaban_keberatan': {        $exists: false,        $eq: null,      },    })    .populate('user')    .populate('laporan')  if (!dataSanksi.length) {    return response.success(res, {      message: 'Tidak ada notifikasi yg dikirim',    })  }  Promise.all(    dataSanksi.map(      async (sanksi) =>        await notifWA(TEMPLATE_KEBERATAN, [          {            key: '1',            value: 'nama_pt',            value_text: sanksi.laporan.pt.nama,          },          {            key: '2',            value: 'pemberi_sanksi',            value_text: sanksi.user.lembaga.nama,          },          {            key: '3',            value: 'no_laporan',            value_text: sanksi.laporan.no_laporan,          },        ])    )  )  return response.success(res, {    message: 'Notifikasi berhasil terkirim',  })})exports.banding = handleError(async (req, res) => {  const dataSanksi = await sanksiModel    .find({      'batas_waktu.banding': {        $lt: new Date().toISOString(),      },      'batas_waktu.jawaban_banding': {        $exists: false,        $eq: null,      },      'batas_waktu.jawaban_keberatan': {        $exists: true,        $ne: null,      },    })    .populate('user')    .populate('laporan')  if (!dataSanksi.length) {    return response.success(res, {      message: 'Tidak ada notifikasi yg dikirim',    })  }  Promise.all(    dataSanksi.map(      async (sanksi) =>        await notifWA(TEMPLATE_BANDING, [          {            key: '1',            value: 'nama_pt',            value_text: sanksi.laporan.pt.nama,          },          {            key: '2',            value: 'pemberi_sanksi',            value_text: sanksi.user.lembaga.nama,          },          {            key: '3',            value: 'no_laporan',            value_text: sanksi.laporan.no_laporan,          },        ])    )  )  return response.success(res, {    message: 'Notifikasi berhasil terkirim',  })})exports.reminderKeberatan = handleError(async (req, res) => {  let dataSanksi = await sanksiModel    .find({      'batas_waktu.jawaban_keberatan': {        $exists: true,        $ne: null,      },      'jawaban.keberatan': {        $exists: false,        $eq: null,      },    })    .populate('user')    .populate('laporan')  const notif = await Promise.all(    dataSanksi.map(async (e) => {      if (        e.batas_waktu.jawaban_keberatan &&        new Date() >          moment(e.batas_waktu.jawaban_keberatan).add(-7, 'days').toDate() &&        new Date() < e.batas_waktu.jawaban_keberatan      ) {        const dayLeft = moment(e.batas_waktu.jawaban_keberatan).diff(          new Date(),          'days'        )        try {          await notifWA(TEMPLATE_REMINDER, [            {              key: '1',              value: 'no_laporan',              value_text: e.laporan.no_laporan,            },            {              key: '2',              value: 'keterangan',              value_text: 'Proses Menjawab Pengajuan Keberatan',            },            {              key: '3',              value: 'pt',              value_text: e.laporan.pt.nama,            },            {              key: '4',              value: 'masa',              value_text: `menjawab pengajuan keberatan tersisa ${dayLeft} hari lagi.`,            },          ])        } catch (error) {          return response.error(res, {            message: 'Notifikasi gagal terkirim',            error: error.message,          })        }      }    })  )  let message = 'Tidak ada notifikasi yang dikirim'  if (notif.length) message = 'Notifikasi berhasil terkirim'  return response.success(res, {    message,  })})exports.reminderBanding = handleError(async (req, res) => {  let dataSanksi = await sanksiModel    .find({      'batas_waktu.jawaban_banding': {        $exists: true,        $ne: null,      },      'jawaban.banding': {        $exists: false,        $eq: null,      },    })    .populate('user')    .populate('laporan')  const notif = await Promise.all(    dataSanksi.map(async (e) => {      if (        e.batas_waktu.jawaban_banding &&        new Date() >          moment(e.batas_waktu.jawaban_banding).add(-7, 'days').toDate() &&        new Date() < e.batas_waktu.jawaban_banding      ) {        const dayLeft = moment(e.batas_waktu.jawaban_banding).diff(          new Date(),          'days'        )        try {          await notifWA(TEMPLATE_REMINDER, [            {              key: '1',              value: 'no_laporan',              value_text: e.laporan.no_laporan,            },            {              key: '2',              value: 'keterangan',              value_text: 'Proses Menjawab Pengajuan Banding',            },            {              key: '3',              value: 'pt',              value_text: e.laporan.pt.nama,            },            {              key: '4',              value: 'masa',              value_text: `menjawab pengajuan banding tersisa ${dayLeft} hari lagi.`,            },          ])        } catch (error) {          return response.error(res, {            message: 'Notifikasi gagal terkirim',            error: error.message,          })        }      }    })  )  let message = 'Tidak ada notifikasi yang dikirim'  if (notif.length) message = 'Notifikasi berhasil terkirim'  return response.success(res, {    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',  })})
 |