|
|
|
@ -2,20 +2,17 @@ import express from 'express'
|
|
|
|
import cors from 'cors'
|
|
|
|
import cors from 'cors'
|
|
|
|
import bodyParser from 'body-parser'
|
|
|
|
import bodyParser from 'body-parser'
|
|
|
|
import amqp from 'amqplib'
|
|
|
|
import amqp from 'amqplib'
|
|
|
|
// import nodemailer from 'nodemailer'
|
|
|
|
|
|
|
|
import SibApiV3Sdk from 'sib-api-v3-sdk'
|
|
|
|
|
|
|
|
import dotenv from 'dotenv'
|
|
|
|
import dotenv from 'dotenv'
|
|
|
|
|
|
|
|
import sgMail from '@sendgrid/mail'
|
|
|
|
|
|
|
|
|
|
|
|
dotenv.config()
|
|
|
|
dotenv.config()
|
|
|
|
|
|
|
|
|
|
|
|
const app = express()
|
|
|
|
const app = express()
|
|
|
|
const queuePaylater = process.env.QUEUE_PAYLATER
|
|
|
|
const queuePaylater = process.env.QUEUE_PAYLATER
|
|
|
|
const queueNota = process.env.QUEUE_NOTA
|
|
|
|
const queueNota = process.env.QUEUE_NOTA
|
|
|
|
const senderEmail = process.env.SENDER_EMAIL
|
|
|
|
const senderEmail = process.env.SENDER_EMAIL
|
|
|
|
var defaultClient = SibApiV3Sdk.ApiClient.instance
|
|
|
|
const apiKey = process.env.SENDGRIP_API_KEY
|
|
|
|
var apiKey = defaultClient.authentications['api-key']
|
|
|
|
sgMail.setApiKey(apiKey)
|
|
|
|
apiKey.apiKey = process.env.SENDING_BLUE_API_KEY
|
|
|
|
|
|
|
|
var apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()
|
|
|
|
|
|
|
|
var sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.use(cors())
|
|
|
|
app.use(cors())
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
@ -24,16 +21,35 @@ app.get('/', (_req, res) => {
|
|
|
|
res.status.send(200).send("Amigo Receipt Sender Service Homepage!")
|
|
|
|
res.status.send(200).send("Amigo Receipt Sender Service Homepage!")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
amqp.connect(process.env.AMQP_SERVER).then(async conn=> {
|
|
|
|
amqp.connect(process.env.AMQP_SERVER).then(async conn => {
|
|
|
|
const ch = await conn.createChannel()
|
|
|
|
const ch = await conn.createChannel()
|
|
|
|
const queuePaylaterExist = ch.assertQueue(queuePaylater, { durable : true });
|
|
|
|
const queuePaylaterExist = ch.assertQueue(queuePaylater, { durable: true });
|
|
|
|
const queueNotaExist = ch.assertQueue(queueNota, { durable: true })
|
|
|
|
const queueNotaExist = ch.assertQueue(queueNota, { durable: true })
|
|
|
|
if (queueNotaExist) {
|
|
|
|
if (queueNotaExist) {
|
|
|
|
queueNotaExist.then(() => {
|
|
|
|
queueNotaExist.then(() => {
|
|
|
|
return ch.consume(queueNota, async (msg) => {
|
|
|
|
return ch.consume(queueNota, async (msg) => {
|
|
|
|
var messageBody = JSON.parse(msg.content.toString())
|
|
|
|
var messageBody = JSON.parse(msg.content.toString())
|
|
|
|
console.log(`[*] Message Received! email : ${messageBody.email} and pesan : ${messageBody.html}`)
|
|
|
|
console.log(`[*NOTA] Message Received! email : ${messageBody.email}`)
|
|
|
|
sendReceipt(messageBody.email, messageBody.html)
|
|
|
|
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(queueNota, 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 })
|
|
|
|
}, { noAck: true })
|
|
|
|
}).then(() => {
|
|
|
|
}).then(() => {
|
|
|
|
console.log('* Waiting for messages from queue ' + queueNota)
|
|
|
|
console.log('* Waiting for messages from queue ' + queueNota)
|
|
|
|
@ -41,19 +57,24 @@ amqp.connect(process.env.AMQP_SERVER).then(async conn=> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch(console.warn)
|
|
|
|
}).catch(console.warn)
|
|
|
|
|
|
|
|
|
|
|
|
function sendReceipt(recepientEmail, receipt) {
|
|
|
|
function sendReceipt(message) {
|
|
|
|
var date = new Date()
|
|
|
|
const now = new Date()
|
|
|
|
sendSmtpEmail = {
|
|
|
|
const msg = {
|
|
|
|
sender: {email: senderEmail, name: process.env.SENDER_NAME},
|
|
|
|
to: message.email,
|
|
|
|
to: [{email: recepientEmail, name: 'Customer'}],
|
|
|
|
from: senderEmail,
|
|
|
|
subject: "test",
|
|
|
|
subject: message.subject,
|
|
|
|
textContent: "testing",
|
|
|
|
html: message.content,
|
|
|
|
htmlContent: receipt
|
|
|
|
}
|
|
|
|
|
|
|
|
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}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
apiInstance.sendTransacEmail(sendSmtpEmail).then(function(data) {
|
|
|
|
})
|
|
|
|
console.log('API called successfully. Returned data: ' + data)
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(date.toLocaleString())
|
|
|
|
|
|
|
|
}, function(error) {
|
|
|
|
|
|
|
|
console.error(error)
|
|
|
|
console.error(error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|