synt_backend
Version:
Synt light-weight node backend service
234 lines (221 loc) • 7.15 kB
JavaScript
var nodemailer = require("nodemailer");
var sesTransport = require("nodemailer-ses-transport");
const Email = require("email-templates");
const db = require("../mysql/models");
const Pusher = require("./push-notifications");
var SES_CREDENTIALS = {
accessKeyId: process.env.SES_ACCESS_KEY_ID,
secretAccessKey: process.env.SES_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION,
};
var transporter = nodemailer.createTransport(
sesTransport({
accessKeyId: SES_CREDENTIALS.accessKeyId,
secretAccessKey: SES_CREDENTIALS.secretAccessKey,
rateLimit: 5,
region: "eu-west-1",
})
);
/*
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'adriaandebolle@gmail.com',
pass: 'xxxxx'
}
});
*/
module.exports = {
sendMail,
sendNotification,
notify,
notifyUsers,
};
function sendAll(User, template, params) {
console.log("Sending " + template + " for " + JSON.stringify(User));
sendMail(User.email, template, params);
sendNotification(User, template, params);
}
function notify(User, template, params = {}) {
if (params.VME) {
db.UserVME.findOne({
where: { UserId: User.id, VMEId: params.VME.id },
}).then((UserVME) => {
if (!UserVME.is_disabled) {
// user is not disabled within VME
sendAll(User, template, { ...params, User });
}
});
} else {
// User notification without VME
sendAll(User, template, { ...params, User });
}
}
function notifyUsers({ VME, Users }, template, params = {}) {
Users.forEach((User) => {
sendAll(User, template, {
...params,
User,
VME,
});
});
}
function truncate(input) {
if (input.length > 15) {
return input.substring(0, 15) + "...";
}
return input;
}
function getAppMetaData(template, params) {
let title, description, path;
switch (template) {
case "new_incident":
title = `Nieuwe melding`;
description = `Nieuwe melding toegevoegd door ${params.Notifier.full_name}: "${params.Incident.report_description}".`;
path = `/app/incident/${params.Incident.id}`;
break;
case "keypoint_case_status_changed":
title = "Statuswijziging melding";
description = `Betreffende ${params.Incident.reference} (${truncate(
params.Incident.report_description
)}): ${params.Incident.case?.caseStatus?.description}`;
path = `/app/incident/${params.Incident.id}`;
break;
case "keypoint_case_invoice_created":
title = "Nieuwe factuur beschikbaar bij een melding";
description = `Betreffende ${params.Incident.reference} (${truncate(
params.Incident.report_description
)}): nieuwe factuur`;
path = `/app/incident/${params.Incident.id}`;
break;
case "update_lot":
title = "Kavel aangepast";
description = `Je kavel ${params.Lot.name} is gewijzigd.`;
path = `/app/profile/lot/${params.Lot.id}`;
break;
// FIXME: date string EUROPEES
case "new_meeting":
title = "Nieuwe vergadering toegevoegd";
description = `Er vindt een nieuwe vergadering, ${
params.Meeting.name
}, plaats op ${new Date(params.Meeting.starts_at).toLocaleDateString()}.`;
path = `/app/meeting/${params.Meeting.id}`;
break;
case "update_meeting":
title = "Details van vergadering gewijzigd";
description = `Bekijk de wijzigingen van je vergadering, ${params.Meeting.name}.`;
path = `/app/meeting/${params.Meeting.id}`;
break;
case "new_document":
title = "Nieuw document toegevoegd";
description = `Er is een nieuw document beschikbaar: ${params.Document.name}.`;
path = `/app/documents/document/${params.Document.id}`;
break;
case "update_document":
title = "Wijziging aan document";
description = `Er is een wijziging in documentgroep: ${params.Document.name}.`;
path = `/app/documents/document/${params.Document.id}`;
break;
case "new_user":
title = "Nieuwe gebruiker";
description = `Je bent nieuwe gebruiker voor VME ${params.VME.alias}.`;
path = `/app/profile`;
break;
case "new_synt":
title = "Nieuwe Synt";
description = `Je bent de synt van VME, ${params.VME.alias}.`;
path = `/app/profile`;
break;
case "new_commissioner":
title = "Nieuwe Rekencommissaris";
description = `Je bent de rekencommissaris van VME, ${params.VME.alias}.`;
path = `/app/profile`;
break;
case "update_user":
title = "Aanpassingen gebruikersprofiel";
description = `Je gegevens zijn aangepast in VME ${params.VME.alias}.`;
path = `/app/profile`;
break;
case "reminder":
title = "Reminder";
description = `Reminder ${params.Reminder.message}`;
path = `/app/profile`;
break;
case "new_provision":
title = "Nieuwe Provisie";
description = `Provisie ${new Date(
params.Provision.invoice_date
).toLocaleDateString()} ${params.Provision.Lot.name} in VME ${
params.VME.alias
}`;
path = `/app/documents/provision/${params.Provision?.id}`;
break;
case "remind_provision":
title = "Herinnering Provisie";
description = `Provisie ${new Date(
params.Provision.invoice_date
).toLocaleDateString()} ${params.Provision.Lot.name} in VME ${
params.VME.alias
}`;
path = `/app/documents/provision/${params.Provision?.id}`;
break;
}
return { title, description, path };
}
function sendNotification(User, template, params = {}) {
console.log(`params`, params);
const { title, description, path } = getAppMetaData(template, params);
db.Notification.create({
UserId: User.id,
VMEId: params.VME?.id,
title,
description,
path,
})
.then(() => {
db.Device.findAll({ where: { UserId: User.id } }).then((Devices) => {
let ids = [];
for (const Device of Devices) {
ids = [Device.apn_token, Device.fcm_token, ...ids];
}
console.log(
"Trying to send notifications:",
JSON.stringify(ids),
"(" + User.email + ")"
);
Pusher.createPushService(ids).sendNotification(
title,
description,
path,
params?.VME?.id
);
});
//console.log("Notification created:", Notification);
})
.catch((err) => console.log(err));
}
function sendMail(recipient, template, params = {}) {
const { path } = getAppMetaData(template, params);
const email = new Email({
message: {
from: "Synt Beheer <app@syntbeheer.be>",
list: {
unsubscribe: "https://synt.be/unsubscribe", //TODO: Make unsubscribe url
},
},
// uncomment below to send emails in development/test env:
// send: true,
transport: transporter,
});
email
.send({
template: template,
message: {
to: recipient,
...(params.attachments && { attachments: params.attachments }),
},
locals: { ...params, path: path ? "synt://" + path : path },
})
.then(console.log)
.catch(console.error);
}