source: Klonkt/src/routes/admin-epk.js@ e25438a

main
Last change on this file since e25438a was e6ad948, checked in by roboburr <roboburr@…>, 3 months ago

feat(epk): choose up to 5 tracks for the press kit (instead of auto top-5)

Press kit editor now has a track picker (checkboxes, max 5). /pers shows the
chosen tracks in that order; no selection → automatically the top 5 most
played. Stored as epk_tracks_<siteId> (validated on site + max 5).

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

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/**
2 * Admin: Perskit (EPK) bewerken — per-site bio + pers-contact.
3 *
4 * GET /admin/epk -> formulier met huidige bio + contact
5 * POST /admin/epk -> opslaan (app_settings: epk_bio_<siteId> / epk_contact_<siteId>)
6 *
7 * De perskit-pagina zelf (/pers) leest deze waarden; tracks + recente posts komen
8 * automatisch. Perskit is premium + solo (zie routes/epk.js).
9 */
10
11import express from 'express';
12import db from '../config/database.js';
13import { renderPage } from '../middleware/render.js';
14import { requireGod } from '../middleware/auth.js';
15import { getSetting, setSetting } from '../services/SettingsService.js';
16import { premiumUnlocked } from '../services/PatreonService.js';
17
18const router = express.Router();
19
20const MAX_EPK_TRACKS = 5;
21
22router.get('/', requireGod, (req, res) => {
23 const site = res.locals.site;
24 if (!site) return res.status(404).send('Geen site');
25 if (!premiumUnlocked()) {
26 return res.status(403).send('Perskit is een premium-functie.');
27 }
28 const allTracks = db.prepare(
29 `SELECT id, title, artist FROM audio_tracks WHERE site_id = ? ORDER BY position ASC, created_at ASC`
30 ).all(site.id);
31 let chosen = [];
32 try { const r = JSON.parse(getSetting('epk_tracks_' + site.id, '') || '[]'); if (Array.isArray(r)) chosen = r; } catch (e) {}
33 renderPage(req, res, 'pages/admin-epk', {
34 pageTitle: 'Perskit bewerken',
35 bodyClass: 'on-admin',
36 site,
37 epkBio: getSetting('epk_bio_' + site.id, '') || '',
38 epkContact: getSetting('epk_contact_' + site.id, '') || '',
39 allTracks,
40 chosenTracks: chosen,
41 maxEpkTracks: MAX_EPK_TRACKS,
42 success: req.query.success || null,
43 });
44});
45
46router.post('/', requireGod, (req, res) => {
47 const site = res.locals.site;
48 if (!site) return res.status(404).send('Geen site');
49 setSetting('epk_bio_' + site.id, (req.body.epk_bio || '').toString().slice(0, 1000).trim());
50 setSetting('epk_contact_' + site.id, (req.body.epk_contact || '').toString().slice(0, 300).trim());
51 // Gekozen nummers: alleen ids van DEZE site, max 5, in de aangeleverde volgorde.
52 let ids = req.body.epk_tracks;
53 if (!Array.isArray(ids)) ids = ids ? [ids] : [];
54 const valid = new Set(db.prepare('SELECT id FROM audio_tracks WHERE site_id = ?').all(site.id).map((r) => r.id));
55 ids = ids.map(String).filter((id) => valid.has(id)).slice(0, MAX_EPK_TRACKS);
56 setSetting('epk_tracks_' + site.id, JSON.stringify(ids));
57 res.redirect('/admin/epk?success=' + encodeURIComponent('Perskit opgeslagen'));
58});
59
60export default router;
Note: See TracBrowser for help on using the repository browser.