-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber-multi.js
101 lines (78 loc) · 2.84 KB
/
subscriber-multi.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import amqp from 'amqplib';
import hbs from 'nodemailer-express-handlebars';
import { transport, hbSetup } from './transport';
import { getUserList } from "./users";
import { getDACs } from "./dacs";
const queues = [ process.env.RABBITMQ_QUEUE_DAC_PORTAL,
process.env.RABBITMQ_QUEUE_DAC_MANAGEMENT,
process.env.RABBITMQ_QUEUE_PERMISSIONS_API]
const sendEmail = async (msg, hbSetup) => {
transport.use('compile', hbs(hbSetup));
return await transport.sendMail(msg)
}
const createEmail = async (msg, additionalFields) => {
let list = await getUserList();
let dacMembersIds = await getDACs(msg.dataset);
const mask = list.map(element => dacMembersIds[0]["members"].some(item => element.id.includes(item) === true));
const dacInfo = list.filter((item, i) => mask[i]);
const dacEmail = dacInfo.map(el => el.email).join(",")
const message = {
from: `"iPC Data Acess Framework" <[email protected]>`,
to: msg.userEmail,
cc: dacEmail,
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()
queues.map(async (q) => await channel.assertQueue(q))
queues.map(q => channel.consume(q, async (message) => {
const msg = JSON.parse(message.content.toString())
console.log(`Received message: Queue - '${q}'`)
console.log(msg)
let fields;
switch (q) {
case queues[0]:
fields = {
subject: "Data Access Request",
template: "dac-portal",
dataset: msg.dataset,
user: msg.userId,
comments: msg.comments
}
break;
case queues[1]:
fields = {
subject: "Data Access Committee has been created",
template: "dac-management",
dac: msg.dacId
}
break;
case queues[2]:
fields = {
subject: "File permissions updated",
template: "permissions-api",
dataset: msg.dataset
}
break;
}
try {
const email = await createEmail(msg, fields);
const status = await sendEmail(email, hbSetup);
console.log('Email sent successfully: ', status.messageId);
channel.ack(message);
} catch {
console.error(err);
channel.nack(message);
}
}));
}
subscriber().catch((error) => {
console.error(error)
})