notifikasi.service.js 1.1 KB

1234567891011121314151617181920212223242526272829
  1. const kontakModel = require('../../model/kontak.model')
  2. const { TEMPLATE_LAPORAN } = require('../../utils/constanta')
  3. const logModel = require('../../model/log.model')
  4. const { enqueueWa } = require('../waJob.service')
  5. exports.sendWaCreateLaporan = async ({ user, pt, keterangan, no_laporan }) => {
  6. let contacts = await kontakModel.find()
  7. const contactNames = contacts.map((e) => e.nama).join(', ')
  8. const formattedContacts = contacts.map((e) => ({ name: e.nama, number: e.no_hp }))
  9. try {
  10. const body = [
  11. { key: '1', value: 'nama', value_text: user.nama },
  12. { key: '2', value: 'pt', value_text: pt[0].nama },
  13. { key: '3', value: 'keterangan', value_text: keterangan },
  14. { key: '4', value: 'no_laporan', value_text: no_laporan },
  15. ]
  16. await enqueueWa(TEMPLATE_LAPORAN, formattedContacts, body, 'CREATE_LAPORAN')
  17. await logModel.create({
  18. aktivitas: `Job notif wa Pembuatan Laporan berhasil dijadwalkan untuk ${contactNames}`,
  19. })
  20. } catch (error) {
  21. await logModel.create({
  22. aktivitas: `Gagal menjadwalkan job notif wa Pembuatan Laporan untuk ${contactNames}, Error: ${error.message}`,
  23. })
  24. }
  25. }