menuserver/server/lib/job.js

68 lines
1.7 KiB
JavaScript
Raw Normal View History

const dbmanager = require('./dbmanager');
const FoodObj = require('./FoodObj');
const pug = require('pug');
const dateFormat = require('dateformat');
const email = require('smtp-email-sender')({
'host': 'mail.caliban.io',
'port': '465',
'auth': {
'user': 'aida@caliban.io',
'pass': 'WaF#E+5am7.)\\csD',
'type': 'LOGIN' // PLAIN, LOGIN, MD5 etc...
},
'secure': 'secure'
});
function sendSMTP(data, newPath) {
const html = pug.renderFile(`${newPath}/` + 'pug/emailV2.pug', data);
email({
'from': 'Aida <aida@caliban.io>',
'to': 'Aida <aida@caliban.io>',
2020-07-06 14:01:42 +00:00
'bcc': 'Martin <martind2000@gmail.com>, Jessica <ing.arvid@gmail.com>',
// 'bcc': 'Martin <martind2000@gmail.com>',
'subject': `Suggestions 🍽️ - ${data.ts}`,
'html': html
});
}
function pugTest(data, newpath) {
console.log(pug.renderFile(`${newpath}/` + 'pug/email.pug', data));
}
async function doJob() {
console.log('Doing job...');
const now = new Date();
const currentTS = ~~(now.getTime() / 86400000) * 86400000;
const pastTS = currentTS - (86400000 * 28);
const fo = new FoodObj(2);
await dbmanager.getRandom(pastTS).then((r) => {
for(const item of r)
fo.add(item);
});
await dbmanager.getRandomSoup(pastTS).then((r) => {
fo.addSoup(r);
});
if (fo.count() === 5) {
const data = fo.forPug();
data.ts = dateFormat(now, 'dddd, mmmm dS, yyyy');
sendSMTP(data, './');
const ids = [...fo.getFirstFiveIDs(), fo.soupID()];
await dbmanager.updateTimestamps(currentTS, ids).then((r) => {
console.log(r);
});
}
else
console.log(`Not enough items, only got ${fo.count()}`);
}
module.exports = doJob;