From 2b127452ac0115849b3de71a298e4ddc1ff04119 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Mon, 6 Jul 2020 14:47:22 +0100 Subject: [PATCH] Menu randomizer turned on Ecosystem added for pm2 New pug file for emails. --- ecosystem.config.js | 8 ++++++++ package-lock.json | 6 +++--- package.json | 2 +- pug/emailV2.pug | 19 +++++++++++++++++++ server.js | 11 +++++++++-- server/lib/FoodObj.js | 19 +++++++++++++++++++ server/lib/dbmanager.js | 15 +++++++++++++++ server/lib/job.js | 26 ++++++++++++++++---------- tests/db.js | 30 ++++++++++++++++++++---------- 9 files changed, 110 insertions(+), 26 deletions(-) create mode 100644 ecosystem.config.js create mode 100644 pug/emailV2.pug diff --git a/ecosystem.config.js b/ecosystem.config.js new file mode 100644 index 0000000..18604a8 --- /dev/null +++ b/ecosystem.config.js @@ -0,0 +1,8 @@ +module.exports = { + 'apps' : [{ + 'name': 'Menu Server', + 'script': './server/server.js', + 'watch': './server/server.js' + }] + +}; diff --git a/package-lock.json b/package-lock.json index 10c911d..aefe528 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 87e191b..4cc1e9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "menuserver", - "version": "1.0.0", + "version": "2.0.0", "description": "", "main": "server.js", "scripts": { diff --git a/pug/emailV2.pug b/pug/emailV2.pug new file mode 100644 index 0000000..ae81161 --- /dev/null +++ b/pug/emailV2.pug @@ -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 diff --git a/server.js b/server.js index c08a14a..cfdf21e 100644 --- a/server.js +++ b/server.js @@ -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; +}); diff --git a/server/lib/FoodObj.js b/server/lib/FoodObj.js index 7fe8684..51d40ba 100644 --- a/server/lib/FoodObj.js +++ b/server/lib/FoodObj.js @@ -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; diff --git a/server/lib/dbmanager.js b/server/lib/dbmanager.js index 56c16cf..48281bc 100644 --- a/server/lib/dbmanager.js +++ b/server/lib/dbmanager.js @@ -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 { + db.get(sql, [sqlTimestamp], (err, row) => { + if (err) + reject(err); + + if (!err) resolve(row); + }); + }); +}; + exports.updateTimestamps = (newTimestamp, items) => { const sqlTimestamp = ~~(newTimestamp / 1000); diff --git a/server/lib/job.js b/server/lib/job.js index fdfdbf8..0100289 100644 --- a/server/lib/job.js +++ b/server/lib/job.js @@ -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 ', 'to': 'Aida ', // 'bcc': 'Martin , Jessica ', 'bcc': 'Martin ', - '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; diff --git a/tests/db.js b/tests/db.js index 00bd486..15ebcbc 100644 --- a/tests/db.js +++ b/tests/db.js @@ -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) + 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(); });