| 1 | /**
|
|---|
| 2 | * Show agenda (premium feature #8) — admin side.
|
|---|
| 3 | *
|
|---|
| 4 | * GET /admin/shows -> list + add form
|
|---|
| 5 | * POST /admin/shows -> add show (optional notify email to subscribers)
|
|---|
| 6 | * POST /admin/shows/:id/delete
|
|---|
| 7 | *
|
|---|
| 8 | * Premium + site manager. Notify email requires SMTP; without SMTP the show is
|
|---|
| 9 | * simply saved (no email sent).
|
|---|
| 10 | */
|
|---|
| 11 |
|
|---|
| 12 | import express from 'express';
|
|---|
| 13 | import db from '../config/database.js';
|
|---|
| 14 | import { v4 as uuid } from 'uuid';
|
|---|
| 15 | import { renderPage } from '../middleware/render.js';
|
|---|
| 16 | import { requireSiteManager } from '../middleware/auth.js';
|
|---|
| 17 | import { premiumUnlocked } from '../services/PatreonService.js';
|
|---|
| 18 | import { mailerConfigured, sendMail } from '../config/mailer.js';
|
|---|
| 19 | import { confirmedFor, counts } from '../services/SubscriberService.js';
|
|---|
| 20 | import { getSetting, setSetting } from '../services/SettingsService.js';
|
|---|
| 21 |
|
|---|
| 22 | const router = express.Router();
|
|---|
| 23 |
|
|---|
| 24 | function esc(s) { return String(s || '').replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])); }
|
|---|
| 25 | function fullUrl(req, p) {
|
|---|
| 26 | const base = (process.env.PUBLIC_BASE_URL || ('https://' + (req.get('host') || ''))).replace(/\/$/, '');
|
|---|
| 27 | return base + (req.res.locals.siteUrlBase || '') + p;
|
|---|
| 28 | }
|
|---|
| 29 | function premiumGate(req, res, next) {
|
|---|
| 30 | if (!premiumUnlocked()) return res.status(403).send('Agenda is premium — koppel Patreon in Beheer → Instellingen.');
|
|---|
| 31 | next();
|
|---|
| 32 | }
|
|---|
| 33 | function render(req, res, extra = {}) {
|
|---|
| 34 | const site = res.locals.site;
|
|---|
| 35 | const shows = db.prepare('SELECT * FROM shows WHERE site_id = ? ORDER BY date DESC, time DESC').all(site.id);
|
|---|
| 36 | renderPage(req, res, 'pages/admin-shows', {
|
|---|
| 37 | pageTitle: 'Agenda', bodyClass: 'on-admin',
|
|---|
| 38 | shows, smtp: mailerConfigured(), notifyCount: confirmedFor(site.id, 'notify').length,
|
|---|
| 39 | agendaEnabled: getSetting('agenda_enabled') === '1',
|
|---|
| 40 | ...extra,
|
|---|
| 41 | });
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | router.get('/', requireSiteManager, premiumGate, (req, res) => {
|
|---|
| 45 | if (!res.locals.site) return res.status(404).send('Geen site.');
|
|---|
| 46 | render(req, res);
|
|---|
| 47 | });
|
|---|
| 48 |
|
|---|
| 49 | router.post('/', requireSiteManager, premiumGate, async (req, res) => {
|
|---|
| 50 | const site = res.locals.site;
|
|---|
| 51 | if (!site) return res.status(404).send('Geen site.');
|
|---|
| 52 | const b = req.body || {};
|
|---|
| 53 | const date = (b.date || '').trim();
|
|---|
| 54 | const city = (b.city || '').trim();
|
|---|
| 55 | if (!date || !city) return render(req, res, { msg: 'Datum en plaats zijn verplicht.', msgKind: 'bad' });
|
|---|
| 56 | let ticket = (b.ticket_url || '').trim();
|
|---|
| 57 | if (ticket && !/^https?:\/\//i.test(ticket)) ticket = '';
|
|---|
| 58 |
|
|---|
| 59 | db.prepare(`INSERT INTO shows (id, site_id, date, time, city, venue, country, ticket_url, notes)
|
|---|
| 60 | VALUES (?,?,?,?,?,?,?,?,?)`).run(
|
|---|
| 61 | uuid(), site.id, date, (b.time || '').trim() || null, city, (b.venue || '').trim() || null,
|
|---|
| 62 | (b.country || '').trim() || null, ticket || null, (b.notes || '').trim() || null,
|
|---|
| 63 | );
|
|---|
| 64 |
|
|---|
| 65 | let sent = 0;
|
|---|
| 66 | if (b.notify && mailerConfigured()) {
|
|---|
| 67 | const subs = confirmedFor(site.id, 'notify');
|
|---|
| 68 | const where = city + (b.venue ? ' — ' + b.venue : '');
|
|---|
| 69 | for (const s of subs) {
|
|---|
| 70 | const unsub = fullUrl(req, '/nieuwsbrief/uitschrijven/' + s.token);
|
|---|
| 71 | try {
|
|---|
| 72 | await sendMail({
|
|---|
| 73 | to: s.email,
|
|---|
| 74 | subject: 'Nieuwe show: ' + where + ' (' + date + ')',
|
|---|
| 75 | text: (site.title || '') + ' speelt op ' + date + ' in ' + where + '.' + (ticket ? ('\nTickets: ' + ticket) : '') + '\n\nUitschrijven: ' + unsub,
|
|---|
| 76 | html: '<p><strong>' + esc(site.title) + '</strong> speelt op <strong>' + esc(date) + '</strong> in ' + esc(where) + '.</p>' +
|
|---|
| 77 | (ticket ? ('<p><a href="' + ticket + '">Tickets</a></p>') : '') +
|
|---|
| 78 | '<p style="color:#888;font-size:12px"><a href="' + unsub + '">Uitschrijven</a></p>',
|
|---|
| 79 | });
|
|---|
| 80 | sent++;
|
|---|
| 81 | } catch { /* skip */ }
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | render(req, res, { msg: 'Show toegevoegd.' + (sent ? (' Notify gestuurd naar ' + sent + ' abonnee(s).') : ''), msgKind: 'ok' });
|
|---|
| 85 | });
|
|---|
| 86 |
|
|---|
| 87 | router.post('/toggle', requireSiteManager, premiumGate, (req, res) => {
|
|---|
| 88 | // Show the agenda on the site (Agenda button in the pill + /shows page).
|
|---|
| 89 | setSetting('agenda_enabled', req.body.enabled ? '1' : '0');
|
|---|
| 90 | res.redirect((res.locals.siteUrlBase || '') + '/admin/shows');
|
|---|
| 91 | });
|
|---|
| 92 |
|
|---|
| 93 | router.post('/:id/delete', requireSiteManager, premiumGate, (req, res) => {
|
|---|
| 94 | const site = res.locals.site;
|
|---|
| 95 | if (site) db.prepare('DELETE FROM shows WHERE id = ? AND site_id = ?').run(req.params.id, site.id);
|
|---|
| 96 | res.redirect((res.locals.siteUrlBase || '') + '/admin/shows');
|
|---|
| 97 | });
|
|---|
| 98 |
|
|---|
| 99 | export default router;
|
|---|