notifikasi.service.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. const kontakModel = require('../../model/kontak.model')
  2. const { notifWA } = require('../../utils/v1/notifFunction')
  3. const { TEMPLATE_LAPORAN } = require('../../utils/constanta')
  4. const logModel = require('../../model/log.model')
  5. exports.sendWaCreateLaporan = async ({ user, pt, keterangan, no_laporan }) => {
  6. let contacts = await kontakModel.find()
  7. contacts = contacts.map((e) => e.nama).join(', ')
  8. try {
  9. const notif = await notifWA(TEMPLATE_LAPORAN, [
  10. {
  11. key: '1',
  12. value: 'nama',
  13. value_text: user.nama,
  14. },
  15. { key: '2', value: 'pt', value_text: pt[0].nama },
  16. { key: '3', value: 'keterangan', value_text: keterangan },
  17. { key: '4', value: 'no_laporan', value_text: no_laporan },
  18. ])
  19. if (notif[0].status === 'success') {
  20. await logModel.create({
  21. aktivitas: `Server berhasil mengirim notif wa kepada ${contacts} untuk Pembuatan Laporan`,
  22. })
  23. } else {
  24. await logModel.create({
  25. aktivitas: `Server gagal mengirim notif wa kepada ${contacts} untuk Pembuatan Laporan, Error: ${JSON.stringify(notif)}`,
  26. })
  27. }
  28. } catch (error) {
  29. await logModel.create({
  30. aktivitas: `Server gagal mengirim notif wa kepada ${contacts} untuk Pembuatan Laporan, Error: ${error.message}`,
  31. })
  32. }
  33. }