|
- import amqp from 'amqplib'
- import dotenv from 'dotenv'
- import sgMail from '@sendgrid/mail'
-
- dotenv.config()
- const queuePaylater = process.env.QUEUE_PAYLATER
- const senderEmail = process.env.SENDER_EMAIL
- const apiKey = process.env.SENDGRIP_API_KEY
- sgMail.setApiKey(apiKey)
-
- amqp.connect(process.env.AMQP_SERVER).then(async conn => {
- const ch = await conn.createChannel()
- const queuePaylaterExist = ch.assertQueue(queuePaylater, { durable: true });
- if (queuePaylaterExist) {
- queuePaylaterExist.then(() => {
- return ch.consume(queuePaylater, async (msg) => {
- var messageBody = JSON.parse(msg.content.toString())
- console.log(`[*PayLater] Message Received! email : ${messageBody.email}`)
- sendReceipt({
- email: messageBody.email,
- subject: messageBody.subject,
- content: messageBody.content,
- })
- }, { noAck: true })
- }).then(() => {
- console.log('* Waiting for messages from queue ' + queuePaylater)
- })
- }
- }).catch(console.warn)
-
- function sendReceipt(message) {
- const now = new Date()
- const msg = {
- to: message.email,
- from: `Amigo Group Indonesia <${senderEmail}>`,
- subject: message.subject,
- html: message.content,
- }
- // sgMail
- // .send(msg)
- // .then((response) => {
- // if (response[0].statusCode == 202) {
- // console.log(`Email sent to ${message.email} at ${now}`)
- // } else {
- // console.error(`Failed to send email to ${message.email} at ${now}`)
- // }
- // })
- // .catch((error) => {
- // console.error(error)
- // })
- console.log(`Email sent to ${message.email} at ${now}`)
- }
|