Added email subjects, and favourites
This commit is contained in:
62
utils.js
62
utils.js
@@ -43,8 +43,8 @@ const loadDefaultTemplates = () => {
|
||||
} else {
|
||||
let newMsg = Message();
|
||||
newMsg.name = name;
|
||||
newMsg.subject = "Email From Kno-Logic" //TODO: Should load these from config.js
|
||||
newMsg.body = data;
|
||||
newMsg.subject = data.substring(0, data.indexOf("\n"));
|
||||
newMsg.body = data.substring(data.indexOf("\n") + 1);
|
||||
newMsg.save()
|
||||
.then(() => {
|
||||
console.log("Loaded " + name + " message");
|
||||
@@ -68,37 +68,40 @@ const sendMail = async (user, message, replacements) => {
|
||||
let transporter = nodemailer.createTransport({
|
||||
host: config.mail.host,
|
||||
port: config.mail.port,
|
||||
secure: config.mail.secure,
|
||||
secure: config.mail.secure,
|
||||
auth: {
|
||||
user: config.mail.user,
|
||||
pass: config.mail.pass,
|
||||
user: config.mail.user,
|
||||
pass: config.mail.pass,
|
||||
},
|
||||
});
|
||||
Message.findOne({ name: message }, (err, message) => {
|
||||
let msgBody = message.body;
|
||||
|
||||
let msgBody = message.body;
|
||||
for (let i = 0; i < replacements.length; i++) {
|
||||
msgBody = msgBody.replace(replacements[i].from, replacements[i].to);
|
||||
}
|
||||
|
||||
for (let i = 0; i < replacements.length; i++) {
|
||||
msgBody = msgBody.replace(replacements[i].from, replacements[i].to);
|
||||
}
|
||||
console.log(message);
|
||||
|
||||
try {
|
||||
await transporter.sendMail({
|
||||
from: config.mail.from,
|
||||
to: user.email,
|
||||
subject: message.subject,
|
||||
text: msgBody,
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.log("Error sending mail: ")
|
||||
console.error(error);
|
||||
}
|
||||
try {
|
||||
transporter.sendMail({
|
||||
from: config.mail.from,
|
||||
to: user.email,
|
||||
subject: message.subject,
|
||||
text: msgBody,
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.log("Error sending mail: ")
|
||||
console.error(error);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// checkSession(userId, sessionId) checks if the sessionId is valid for the user
|
||||
const checkSession = (userId, sessionId, f) => {
|
||||
Session.find({userId: userId, sessionId: sessionId }, (err, res) => {
|
||||
Session.find({ userId: userId, sessionId: sessionId }, (err, res) => {
|
||||
if (res) {
|
||||
f(true);
|
||||
return;
|
||||
@@ -110,7 +113,7 @@ const checkSession = (userId, sessionId, f) => {
|
||||
// isAdmin(userId) checks if the user with userId is an administrator
|
||||
const isAdmin = (userId, f) => {
|
||||
User.findById(userId, (err, res) => {
|
||||
if(res.permission == 1) {
|
||||
if (res.permission == 1) {
|
||||
f(true);
|
||||
return;
|
||||
}
|
||||
@@ -124,11 +127,19 @@ function dateToEpoch(d) {
|
||||
if (d) {
|
||||
// When comparing js dates, the timezone does not matter
|
||||
// ex. May 17 EDT == May 17 GMT, May 17 EDT != May 18 GMT
|
||||
return d.setHours(0,0,0,0);
|
||||
return d.setHours(0, 0, 0, 0);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// removeValue(array, item) remove item from the array
|
||||
function removeValue(array, item) {
|
||||
var index = array.indexOf(item);
|
||||
if (index !== -1) {
|
||||
array.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.purgeSessions = purgeSessions;
|
||||
module.exports.loadDefaultTemplates = loadDefaultTemplates;
|
||||
@@ -136,3 +147,4 @@ module.exports.sendMail = sendMail;
|
||||
module.exports.checkSession = checkSession;
|
||||
module.exports.isAdmin = isAdmin;
|
||||
module.exports.dateToEpoch = dateToEpoch;
|
||||
module.exports.removeValue = removeValue;
|
||||
|
||||
Reference in New Issue
Block a user