-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber-users.js
More file actions
76 lines (57 loc) · 1.92 KB
/
subscriber-users.js
File metadata and controls
76 lines (57 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import amqp from 'amqplib';
import hbs from 'nodemailer-express-handlebars';
import { transport, hbSetup } from './transport';
const sendEmail = async (msg, hbSetup) => {
transport.use('compile', hbs(hbSetup));
return await transport.sendMail(msg)
}
const createEmail = async (msg, additionalFields) => {
const message = {
from: `"iPC Data Acess Framework" <ipc-project.no-reply@bsc.es>`,
to: msg.dacsEmail,
subject: additionalFields.subject,
template: additionalFields.template,
context: {
...additionalFields
}
};
return message
}
const subscriber = async () => {
const connection = await amqp.connect(process.env.RABBITMQ_SERVER)
const channel = await connection.createChannel()
await channel.assertQueue(process.env.RABBITMQ_QUEUE_USERS)
channel.consume(process.env.RABBITMQ_QUEUE_USERS, async (message) => {
const msg = JSON.parse(message.content.toString())
console.log(`Received message: Source - '${msg.source}'`)
let fields;
switch (msg.source) {
case "dac-portal":
fields = {
// Needs to be defined
}
break;
case "dac-management":
fields = {
subject: "DAC info updated",
template: "dac-management-info",
dac: msg.dacId
}
break;
}
console.log("FIELDS:")
console.log(fields)
try {
const email = await createEmail(msg, fields);
const status = await sendEmail(email, hbSetup);
channel.ack(message);
console.log('Email delivered: ', status.messageId);
} catch (err) {
console.error(err);
//channel.nack(message);
}
});
}
subscriber().catch((err) => {
console.error(err)
})