From a0048ac85fe55a521011f99f2d708741a9337dcb Mon Sep 17 00:00:00 2001 From: Yohanes Krisna Yana Javista Date: Sat, 27 Jan 2024 14:03:26 +0700 Subject: [PATCH] Send mEssage ConsoleLog --- index-nota.mjs | 52 ++++++++++++++++++++++++++++++ index-payleter.mjs | 52 ++++++++++++++++++++++++++++++ index.mjs | 80 ---------------------------------------------- 3 files changed, 104 insertions(+), 80 deletions(-) create mode 100644 index-nota.mjs create mode 100644 index-payleter.mjs delete mode 100644 index.mjs diff --git a/index-nota.mjs b/index-nota.mjs new file mode 100644 index 0000000..03d36ca --- /dev/null +++ b/index-nota.mjs @@ -0,0 +1,52 @@ +import amqp from 'amqplib' +import dotenv from 'dotenv' +import sgMail from '@sendgrid/mail' + +dotenv.config() +const queueNota = process.env.QUEUE_NOTA +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 queueNotaExist = ch.assertQueue(queueNota, { durable: true }) + if (queueNotaExist) { + queueNotaExist.then(() => { + return ch.consume(queueNota, async (msg) => { + var messageBody = JSON.parse(msg.content.toString()) + console.log(`[*NOTA] 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 ' + queueNota) + }) + } +}).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}`) +} diff --git a/index-payleter.mjs b/index-payleter.mjs new file mode 100644 index 0000000..5a58e0e --- /dev/null +++ b/index-payleter.mjs @@ -0,0 +1,52 @@ +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}`) +} diff --git a/index.mjs b/index.mjs deleted file mode 100644 index fdffa56..0000000 --- a/index.mjs +++ /dev/null @@ -1,80 +0,0 @@ -import express from 'express' -import cors from 'cors' -import bodyParser from 'body-parser' -import amqp from 'amqplib' -import dotenv from 'dotenv' -import sgMail from '@sendgrid/mail' - -dotenv.config() - -const app = express() -const queuePaylater = process.env.QUEUE_PAYLATER -const queueNota = process.env.QUEUE_NOTA -const senderEmail = process.env.SENDER_EMAIL -const apiKey = process.env.SENDGRIP_API_KEY -sgMail.setApiKey(apiKey) - -app.use(cors()) -app.use(bodyParser.json()) - -app.get('/', (_req, res) => { - res.status.send(200).send("Amigo Receipt Sender Service Homepage!") -}) - -amqp.connect(process.env.AMQP_SERVER).then(async conn => { - const ch = await conn.createChannel() - const queuePaylaterExist = ch.assertQueue(queuePaylater, { durable: true }); - const queueNotaExist = ch.assertQueue(queueNota, { durable: true }) - if (queueNotaExist) { - queueNotaExist.then(() => { - return ch.consume(queueNota, async (msg) => { - var messageBody = JSON.parse(msg.content.toString()) - console.log(`[*NOTA] 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 ' + queueNota) - }) - } - 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: 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) - }) -}