notifFunction.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const axios = require('../utils/axios')
  2. const kontakModel = require('../model/kontak.model')
  3. const { logInfo, logError } = require('./logger')
  4. exports.notifWA = async (templateId, data, where = {}) => {
  5. const kontak = await kontakModel.find(where)
  6. const contacts = kontak.map((e) => ({ name: e.nama, number: e.no_hp }))
  7. const url = 'https://api2.kemdiktisaintek.go.id/qontak/1.0/send'
  8. logInfo('WA_SEND_V1', 'Sending WA via notifWA v1', { templateId, contactsCount: contacts.length, contacts })
  9. try {
  10. const send = await axios.post(url, { templateId, contacts, body: data })
  11. logInfo('WA_SEND_V1', 'WA sent successfully v1', { templateId, response: send })
  12. return send
  13. } catch (error) {
  14. logError('WA_SEND_V1', 'Failed to send WA v1', {
  15. templateId,
  16. error: error.message,
  17. response: error.response?.data || null
  18. })
  19. throw error
  20. }
  21. }
  22. exports.notifWA2 = async (templateId, { nama, no_hp }, data) => {
  23. const url = 'https://api2.kemdiktisaintek.go.id/qontak/1.0/send'
  24. const contacts = [{ name: nama, number: no_hp }]
  25. logInfo('WA_SEND_V1', 'Sending WA via notifWA2 v1', { templateId, contacts })
  26. try {
  27. const send = await axios.post(url, { templateId, contacts, body: data })
  28. logInfo('WA_SEND_V1', 'WA2 sent successfully v1', { templateId, response: send })
  29. return send
  30. } catch (error) {
  31. logError('WA_SEND_V1', 'Failed to send WA2 v1', {
  32. templateId,
  33. error: error.message,
  34. response: error.response?.data || null
  35. })
  36. throw error
  37. }
  38. }