No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

53 líneas
1.6 KiB

  1. import amqp from 'amqplib'
  2. import dotenv from 'dotenv'
  3. import sgMail from '@sendgrid/mail'
  4. dotenv.config()
  5. const queuePaylater = process.env.QUEUE_PAYLATER
  6. const senderEmail = process.env.SENDER_EMAIL
  7. const apiKey = process.env.SENDGRIP_API_KEY
  8. sgMail.setApiKey(apiKey)
  9. amqp.connect(process.env.AMQP_SERVER).then(async conn => {
  10. const ch = await conn.createChannel()
  11. const queuePaylaterExist = ch.assertQueue(queuePaylater, { durable: true });
  12. if (queuePaylaterExist) {
  13. queuePaylaterExist.then(() => {
  14. return ch.consume(queuePaylater, async (msg) => {
  15. var messageBody = JSON.parse(msg.content.toString())
  16. console.log(`[*PayLater] Message Received! email : ${messageBody.email}`)
  17. sendReceipt({
  18. email: messageBody.email,
  19. subject: messageBody.subject,
  20. content: messageBody.content,
  21. })
  22. }, { noAck: true })
  23. }).then(() => {
  24. console.log('* Waiting for messages from queue ' + queuePaylater)
  25. })
  26. }
  27. }).catch(console.warn)
  28. function sendReceipt(message) {
  29. const now = new Date()
  30. const msg = {
  31. to: message.email,
  32. from: `Amigo Group Indonesia <${senderEmail}>`,
  33. subject: message.subject,
  34. html: message.content,
  35. }
  36. // sgMail
  37. // .send(msg)
  38. // .then((response) => {
  39. // if (response[0].statusCode == 202) {
  40. // console.log(`Email sent to ${message.email} at ${now}`)
  41. // } else {
  42. // console.error(`Failed to send email to ${message.email} at ${now}`)
  43. // }
  44. // })
  45. // .catch((error) => {
  46. // console.error(error)
  47. // })
  48. console.log(`Email sent to ${message.email} at ${now}`)
  49. }