|
|
|
@@ -0,0 +1,78 @@ |
|
|
|
const amqp = require("amqplib"); |
|
|
|
const nodemailer = require("nodemailer"); |
|
|
|
|
|
|
|
const { |
|
|
|
AMQP_URL, |
|
|
|
AMQP_EXCHANGE, |
|
|
|
AMQP_QUEUE = "email-forwarder", |
|
|
|
SMTP_HOST, |
|
|
|
SMTP_PORT = "25", |
|
|
|
NOTIFY_TO, |
|
|
|
NOTIFY_FROM = "hal@novox.be", |
|
|
|
} = process.env; |
|
|
|
|
|
|
|
async function main() { |
|
|
|
console.log(`Connecting to AMQP: ${AMQP_URL?.replace(/\/\/.*@/, "//***@")}`); |
|
|
|
const conn = await amqp.connect(AMQP_URL); |
|
|
|
const ch = await conn.createChannel(); |
|
|
|
|
|
|
|
await ch.assertQueue(AMQP_QUEUE, { durable: true }); |
|
|
|
await ch.bindQueue(AMQP_QUEUE, AMQP_EXCHANGE, ""); |
|
|
|
console.log(`Bound queue '${AMQP_QUEUE}' to exchange '${AMQP_EXCHANGE}'`); |
|
|
|
|
|
|
|
const transport = nodemailer.createTransport({ |
|
|
|
host: SMTP_HOST, |
|
|
|
port: parseInt(SMTP_PORT), |
|
|
|
secure: false, |
|
|
|
tls: { rejectUnauthorized: false }, |
|
|
|
}); |
|
|
|
|
|
|
|
console.log(`SMTP: ${SMTP_HOST}:${SMTP_PORT}`); |
|
|
|
console.log(`Forwarding to: ${NOTIFY_TO}`); |
|
|
|
console.log("Waiting for messages...\n"); |
|
|
|
|
|
|
|
ch.consume(AMQP_QUEUE, async (msg) => { |
|
|
|
if (!msg) return; |
|
|
|
|
|
|
|
try { |
|
|
|
const headers = {}; |
|
|
|
for (const [k, v] of Object.entries(msg.properties.headers || {})) { |
|
|
|
headers[k] = Buffer.isBuffer(v) ? v.toString() : v; |
|
|
|
} |
|
|
|
|
|
|
|
const body = JSON.parse(msg.content.toString()); |
|
|
|
const subjectType = headers.subjecttype || "unknown"; |
|
|
|
const brandCode = body.brandCode || "unknown"; |
|
|
|
const ref = body.transactionReference || "unknown"; |
|
|
|
|
|
|
|
const subject = `[CloudEvent] ${subjectType} / ${brandCode} / ${ref}`; |
|
|
|
const html = ` |
|
|
|
<h2>CloudEvent received</h2> |
|
|
|
<h3>Headers</h3> |
|
|
|
<pre>${JSON.stringify(headers, null, 2)}</pre> |
|
|
|
<h3>CDM Payload</h3> |
|
|
|
<pre>${JSON.stringify(body, null, 2)}</pre> |
|
|
|
<hr> |
|
|
|
<p><small>From: AH BE Task Connector → ${AMQP_EXCHANGE} → ${AMQP_QUEUE}</small></p> |
|
|
|
`; |
|
|
|
|
|
|
|
await transport.sendMail({ |
|
|
|
from: NOTIFY_FROM, |
|
|
|
to: NOTIFY_TO, |
|
|
|
subject, |
|
|
|
html, |
|
|
|
}); |
|
|
|
|
|
|
|
console.log(`Forwarded: ${subject} → ${NOTIFY_TO}`); |
|
|
|
ch.ack(msg); |
|
|
|
} catch (err) { |
|
|
|
console.error("Failed to process message:", err.message); |
|
|
|
ch.nack(msg, false, true); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
main().catch((err) => { |
|
|
|
console.error("Fatal:", err); |
|
|
|
process.exit(1); |
|
|
|
}); |