diff --git a/src/app.js b/src/app.js index ade0554..c9f296e 100644 --- a/src/app.js +++ b/src/app.js @@ -123,9 +123,10 @@ async function start() { // Home page app.get('/', (req, res) => { - const { Hunts } = require('./models'); + const { Hunts, Scans } = require('./models'); const hunts = Hunts.getAll(); - res.render('home', { title: 'Loot Hunt', hunts }); + const recentActivity = Scans.getRecentActivity(5); + res.render('home', { title: 'Loot Hunt', hunts, recentActivity }); }); // 404 handler diff --git a/src/models/index.js b/src/models/index.js index 69d9a10..13f181a 100644 --- a/src/models/index.js +++ b/src/models/index.js @@ -329,6 +329,22 @@ const Scans = { GROUP BY u.id ORDER BY total_points DESC `).all(); + }, + + getRecentActivity(limit = 5) { + return db.prepare(` + SELECT s.points_awarded, s.scanned_at, + u.username, + p.card_number, + h.name as hunt_name, h.short_name as hunt_short_name, h.package_count + FROM scans s + JOIN users u ON s.user_id = u.id + JOIN packages p ON s.package_id = p.id + JOIN hunts h ON p.hunt_id = h.id + WHERE s.points_awarded > 0 + ORDER BY s.scanned_at DESC + LIMIT ? + `).all(limit); } }; diff --git a/src/views/home.ejs b/src/views/home.ejs index 7feab56..59e90cf 100644 --- a/src/views/home.ejs +++ b/src/views/home.ejs @@ -47,6 +47,21 @@ <% }) %> <% } %> + + <% if (typeof recentActivity !== 'undefined' && recentActivity && recentActivity.length > 0) { %> +