Menu randomizer turned on

Ecosystem added for pm2
New pug file for emails.
This commit is contained in:
Martin Donnelly 2020-07-06 14:47:22 +01:00
parent 5b4d9068a9
commit 2b127452ac
9 changed files with 110 additions and 26 deletions

8
ecosystem.config.js Normal file
View File

@ -0,0 +1,8 @@
module.exports = {
'apps' : [{
'name': 'Menu Server',
'script': './server/server.js',
'watch': './server/server.js'
}]
};

6
package-lock.json generated
View File

@ -4732,9 +4732,9 @@
}
},
"opencollective-postinstall": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.2.tgz",
"integrity": "sha512-pVOEP16TrAO2/fjej1IdOyupJY8KDUM1CvsaScRbw6oddvpQoOfGk4ywha0HKKVAD6RkW4x6Q+tNBwhf3Bgpuw=="
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.3.tgz",
"integrity": "sha512-8AV/sCtuzUeTo8gQK5qDZzARrulB3egtLzFgteqB2tcT4Mw7B8Kt7JcDHmltjz6FOAHsvTevk70gZEbhM4ZS9Q=="
},
"optimist": {
"version": "0.6.1",

View File

@ -1,6 +1,6 @@
{
"name": "menuserver",
"version": "1.0.0",
"version": "2.0.0",
"description": "",
"main": "server.js",
"scripts": {

19
pug/emailV2.pug Normal file
View File

@ -0,0 +1,19 @@
html(lang="en")
head
meta(charset='utf-8')
title
Suggestions
body
h1 Suggestions for !{ts}
h2 Mains
ol
each item in menu
li: a(href=item.url)= item.name
H2 Soup
ul
li: a(href=soup.url)= soup.name
small V2.00

View File

@ -6,6 +6,8 @@ const path = require('path');
const helmet = require('helmet');
const cors = require('cors');
const cron = require('node-cron');
const db = require('./server/lib/loginmanager');
const doJob = require('./server/lib/job');
@ -86,6 +88,11 @@ app.listen(serverPort, () => {
});
((() => {
console.log('Menuizer started');
// doJob();
console.log('Menuserver 2.0 started');
})());
cron.schedule('0 8 * * 6', function () {
doJob();
return -1;
});

View File

@ -3,6 +3,7 @@ class FoodObj {
constructor(limit) {
this.limit = limit;
this.store = [];
this.soup = {};
this.types = [0, 0, 0, 0, 0, 0, 0];
}
@ -31,9 +32,27 @@ class FoodObj {
}
}
addSoup(item) {
item.url = `https://menu.silvrtree.co.uk/view/${item.short}`;
this.soup = item;
}
soupID() {
return this.soup._id || null;
}
count() {
return (this.store.slice(0, 5).length);
}
forPug() {
const output = { 'menu':null, 'soup':null };
output.menu = [...this.getFirstFive()];
output.soup = this.soup;
return output;
}
}
module.exports = FoodObj;

View File

@ -126,6 +126,21 @@ exports.getRandom = (timestamp) => {
});
};
exports.getRandomSoup = (timestamp) => {
const sql = 'SELECT _id, short, hash, name, meat, mealtype FROM menu where mealtype =2 and lastused<? order by RANDOM() LIMIT 1';
const sqlTimestamp = ~~(timestamp / 1000);
return new Promise((resolve, reject) => {
db.get(sql, [sqlTimestamp], (err, row) => {
if (err)
reject(err);
if (!err) resolve(row);
});
});
};
exports.updateTimestamps = (newTimestamp, items) => {
const sqlTimestamp = ~~(newTimestamp / 1000);

View File

@ -16,15 +16,13 @@ const email = require('smtp-email-sender')({
});
function sendSMTP(data, newPath) {
const now = new Date();
const html = pug.renderFile(`${newPath}/` + 'pug/email.pug', data);
const html = pug.renderFile(`${newPath}/` + 'pug/emailV2.pug', data);
email({
'from': 'Aida <aida@caliban.io>',
'to': 'Aida <aida@caliban.io>',
// 'bcc': 'Martin <martind2000@gmail.com>, Jessica <ing.arvid@gmail.com>',
'bcc': 'Martin <martind2000@gmail.com>',
'subject': `Suggestions 🍽️ - ${dateFormat(now, 'dddd, mmmm dS, yyyy')}`,
'subject': `Suggestions 🍽️ - ${data.ts}`,
'html': html
});
}
@ -42,20 +40,28 @@ async function doJob() {
const fo = new FoodObj(2);
await dbmanager.getRandom(pastTS).then((r) => {
// console.log(r);
for(const item of r)
fo.add(item);
});
await dbmanager.getRandomSoup(pastTS).then((r) => {
fo.addSoup(r);
});
if (fo.count() === 5) {
sendSMTP({ 'suggestions':fo.getFirstFive() }, './');
await dbmanager.updateTimestamps(currentTS, fo.getFirstFiveIDs()).then((r) => {
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()}`);
}
else
console.log(`Not enough items, only got ${fo.count()}`);
}
module.exports = doJob;

View File

@ -5,6 +5,9 @@ const test = _test(tape); // decorate tape
const dbmanager = require('../server/lib/dbmanager');
const FoodObj = require('../server/lib/FoodObj');
const pug = require('pug');
const dateFormat = require('dateformat');
test('DB tests', async function(t) {
const now = new Date();
const currentTS = ~~(now.getTime() / 86400000) * 86400000;
@ -15,19 +18,26 @@ test('DB tests', async function(t) {
const fo = new FoodObj(2);
await dbmanager.getRandom(pastTS).then((r) => {
// console.log(r);
for(const item of r)
fo.add(item);
console.log(fo.getFirstFive());
console.log(fo.getFirstFiveIDs());
});
await dbmanager.updateTimestamps(currentTS, fo.getFirstFiveIDs()).then((r) => {
await dbmanager.getRandomSoup(pastTS).then((r) => {
fo.addSoup(r);
});
const data = fo.forPug();
data.ts = dateFormat(now, 'dddd, mmmm dS, yyyy');
const html = pug.renderFile( '../pug/emailV2.pug', data);
const ids = [...fo.getFirstFiveIDs(), fo.soupID()];
console.log(ids);
/* await dbmanager.updateTimestamps(currentTS, fo.getFirstFiveIDs()).then((r) => {
console.log(r);
});
});*/
t.end();
});