notifFunction.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const kontakModel = require('../../model/kontak.model')
  2. const { PTB_DIKTI, SUCCESS } = require('../constanta')
  3. const { enqueueWa } = require('../../services/waJob.service')
  4. const { logError } = require('../logger')
  5. /**
  6. * Enqueue WA job ke background worker, bukan kirim langsung.
  7. * Return shape tetap [{ status }] supaya caller lama (notif[0].status === SUCCESS) tidak perlu berubah.
  8. *
  9. * @param templateId
  10. * @param data
  11. * @param where {object?}
  12. * @returns {Promise<Array<{status: string, jobId?: string}>>}
  13. */
  14. exports.notifWA = async (templateId, data, where = {}) => {
  15. const kontak = await kontakModel.find({ ...where, 'role.id': PTB_DIKTI })
  16. const contacts = kontak.map((e) => ({ name: e.nama, number: e.no_hp }))
  17. try {
  18. const job = await enqueueWa(templateId, contacts, data, 'notifWA')
  19. return [{ status: SUCCESS, jobId: job._id }]
  20. } catch (error) {
  21. logError('WA_SEND_V1', 'Failed to enqueue notifWA', { templateId, error: error.message })
  22. return [{ status: 'failed', error: error.message }]
  23. }
  24. }
  25. /**
  26. * @param templateId
  27. * @param contacts
  28. * @param data
  29. * @returns {Promise<Array<{status: string, jobId?: string}>>}
  30. */
  31. exports.notifWA2 = async (templateId, contacts, data) => {
  32. try {
  33. const job = await enqueueWa(templateId, contacts, data, 'notifWA2')
  34. return [{ status: SUCCESS, jobId: job._id }]
  35. } catch (error) {
  36. logError('WA_SEND_V1', 'Failed to enqueue notifWA2', { templateId, error: error.message })
  37. return [{ status: 'failed', error: error.message }]
  38. }
  39. }