source: Klonkt/src/routes/admin-shows.js@ 834bcc3

main
Last change on this file since 834bcc3 was 834bcc3, checked in by Robin Genis <roboburr@…>, 3 months ago

i18n: translate Dutch code comments to English across src/

Comments in routes/services/views/config/middleware/assets translated to
English for the public repo. A few dev-facing throw/console message strings
were Englished too. No user-facing UI strings or i18n dictionary values changed
(src/services/i18n.js untouched). Logic unchanged.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 4.3 KB
Line 
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
12import express from 'express';
13import db from '../config/database.js';
14import { v4 as uuid } from 'uuid';
15import { renderPage } from '../middleware/render.js';
16import { requireSiteManager } from '../middleware/auth.js';
17import { premiumUnlocked } from '../services/PatreonService.js';
18import { mailerConfigured, sendMail } from '../config/mailer.js';
19import { confirmedFor, counts } from '../services/SubscriberService.js';
20import { getSetting, setSetting } from '../services/SettingsService.js';
21
22const router = express.Router();
23
24function esc(s) { return String(s || '').replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c])); }
25function 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}
29function premiumGate(req, res, next) {
30 if (!premiumUnlocked()) return res.status(403).send('Agenda is premium — koppel Patreon in Beheer → Instellingen.');
31 next();
32}
33function 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
44router.get('/', requireSiteManager, premiumGate, (req, res) => {
45 if (!res.locals.site) return res.status(404).send('Geen site.');
46 render(req, res);
47});
48
49router.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
87router.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
93router.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
99export default router;
Note: See TracBrowser for help on using the repository browser.