source: Klonkt/src/routes/admin.js@ 2ab99c0

main
Last change on this file since 2ab99c0 was 3487567, checked in by Robin Genis <roboburr@…>, 2 months ago

fix(i18n): translate admin page titles (pageTitleKey instead of hardcoded strings)

Admin page/tab titles were hardcoded (mostly Dutch: Beheer, Instellingen, Nieuwsbrief, …).
renderPage now accepts pageTitleKey (+ pageTitleVars) and translates it with the resolved
language; the 16 admin routes pass keys. Adds admin.t_* keys in nl/en/de.

  • middleware/render.js — pageTitleKey support
  • services/i18n.js — admin.t_* title keys (nl/en/de)
  • routes/admin*.js — pageTitle string -> pageTitleKey
  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[7bc636b]1/**
2 * Admin routes — Phase B stub.
3 * Read-only god-only overview of users / sites / posts.
4 * Real admin dashboard (create/delete sites, manage users, etc.) comes later.
5 */
6
7import express from 'express';
8import db from '../config/database.js';
9import { renderPage } from '../middleware/render.js';
[8cb1dc7]10import { requireAuth } from '../middleware/auth.js';
[6351545]11import { getTenancy } from '../services/SettingsService.js';
[7881080]12import { getPrimarySite } from '../middleware/site.js';
[7bc636b]13
14const router = express.Router();
15
[834bcc3]16// Recent posts from one site, DRAFTS ON TOP, with mode-aware edit/view URLs.
17// Solves the problem that drafts (status != published) were not findable anywhere:
18// the timeline shows only published posts.
[f45fc82]19function sitePosts(siteId, siteSlug, tenancy, limit = 60) {
20 const base = tenancy === 'hub' ? `/user/${siteSlug}` : '';
21 return db.prepare(`
22 SELECT slug, title, status, published_at, created_at, updated_at
23 FROM posts WHERE site_id = ?
24 ORDER BY (status != 'published') DESC, COALESCE(updated_at, published_at, created_at) DESC
25 LIMIT ?
26 `).all(siteId, limit).map((p) => ({
27 ...p,
28 isDraft: p.status !== 'published',
29 editUrl: `${base}/posts/${p.slug}/edit`,
30 viewUrl: `${base}/${p.slug}`,
31 }));
32}
33
[8cb1dc7]34router.get('/', requireAuth, (req, res) => {
35 const user = req.session.user;
36
[834bcc3]37 // A kijker may view the full (god) admin panel read-only — same as god,
38 // but writing is globally blocked. A regular artist who owns a site gets
39 // a "My Klonkt Hub" dashboard, scoped to their own site. No site -> no admin.
[8afbdd6]40 if (user.role !== 'god' && user.role !== 'kijker') {
[8cb1dc7]41 const mySite = db.prepare(
42 'SELECT * FROM sites WHERE owner_id = ? ORDER BY created_at ASC LIMIT 1'
43 ).get(user.id);
44 if (!mySite) return res.status(403).send('Geen beheer beschikbaar voor dit account.');
45
46 const mine = {
47 posts: db.prepare("SELECT COUNT(*) AS c FROM posts WHERE site_id = ?").get(mySite.id).c,
48 published: db.prepare("SELECT COUNT(*) AS c FROM posts WHERE site_id = ? AND status = 'published'").get(mySite.id).c,
49 };
50 return renderPage(req, res, 'pages/my-site', {
[3487567]51 pageTitleKey: 'admin.t_hub',
[8cb1dc7]52 bodyClass: 'on-admin',
53 mySite,
54 mine,
[f45fc82]55 posts: sitePosts(mySite.id, mySite.slug, 'hub'), // my-site is hub-only
[8cb1dc7]56 });
57 }
58
[6351545]59 const tenancy = getTenancy();
60
[834bcc3]61 // The primary/main site — in solo THE site, in hub the main site. Provides the
62 // "Appearance" tile with its edit link + the posts/drafts list.
[7881080]63 const primarySite = getPrimarySite();
[6351545]64
[7bc636b]65 const stats = {
66 users: db.prepare('SELECT COUNT(*) AS c FROM users').get().c,
67 sites: db.prepare('SELECT COUNT(*) AS c FROM sites').get().c,
68 posts: db.prepare('SELECT COUNT(*) AS c FROM posts').get().c,
69 published: db.prepare(
70 "SELECT COUNT(*) AS c FROM posts WHERE status = 'published'"
71 ).get().c,
72 };
73
[834bcc3]74 // Sites/users tables are only relevant in hub mode; in solo we skip the query.
[6351545]75 const sites = tenancy === 'hub' ? db.prepare(`
[7bc636b]76 SELECT s.slug, s.title, s.created_at, u.username AS owner_username
77 FROM sites s
78 LEFT JOIN users u ON u.id = s.owner_id
79 ORDER BY s.created_at DESC
80 LIMIT 50
[6351545]81 `).all() : [];
[7bc636b]82
[6351545]83 const users = tenancy === 'hub' ? db.prepare(`
[7bc636b]84 SELECT username, email, role, created_at
85 FROM users
86 ORDER BY created_at DESC
87 LIMIT 50
[6351545]88 `).all() : [];
[7bc636b]89
[834bcc3]90 // Posts/drafts of the primary site (in solo = the site; in hub = the admin's
91 // main site). Drafts are listed first so they are easy to find.
[f45fc82]92 const posts = primarySite ? sitePosts(primarySite.id, primarySite.slug, tenancy) : [];
93
[7bc636b]94 renderPage(req, res, 'pages/admin', {
[3487567]95 pageTitleKey: 'admin.t_admin',
[7bc636b]96 bodyClass: 'on-admin',
[6351545]97 tenancy,
98 primarySite,
[7bc636b]99 stats,
100 sites,
[f45fc82]101 posts,
[7bc636b]102 users,
103 });
104});
105
[834bcc3]106// Handleiding — searchable explanation of all admin features. Visible to anyone
107// who may view the admin panel (logged in); purely static help text, nothing sensitive.
[d0f4c10]108router.get('/handleiding', requireAuth, (req, res) => {
109 renderPage(req, res, 'pages/admin-help', {
[3487567]110 pageTitleKey: 'admin.t_manual',
[d0f4c10]111 bodyClass: 'on-admin',
112 tenancy: getTenancy(),
113 });
114});
115
[7bc636b]116export default router;
Note: See TracBrowser for help on using the repository browser.