Changeset 7881080 in Klonkt for src/routes


Ignore:
Timestamp:
06/18/2026 03:28:34 AM (3 months ago)
Author:
roboburr <roboburr@…>
Branches:
main
Children:
8398935
Parents:
98ecf51
Message:

Hub #3: explicit is_primary flag + shared getPrimarySite() (DRY)

The "oldest site = primary site" assumption was duplicated independently in
resolveSite, hub.js, account.js and admin.js (fragile: if the oldest happened
to be an artist site, the hub home would be wrong). Now:

  • sites.is_primary column + backfill (marks the oldest if none is primary yet; ensurePrimarySite sets it on fresh installs) → existing behaviour exactly preserved.
  • one getPrimarySite() helper (is_primary, fallback oldest) replaces the 4 copies.
  • god can CHOOSE the primary/main site: ★ button + "primary" badge on /admin/sites (exactly one primary via a transaction).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@…>

Location:
src/routes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/routes/account.js

    r98ecf51 r7881080  
    2121import { v4 as uuid } from 'uuid';
    2222import db from '../config/database.js';
     23import { getPrimarySite } from '../middleware/site.js';
    2324import { renderPage } from '../middleware/render.js';
    2425import { requireAuth } from '../middleware/auth.js';
     
    8081  let site = db.prepare('SELECT id, title, tagline, slug, owner_id FROM sites WHERE owner_id = ? ORDER BY created_at LIMIT 1').get(user.id);
    8182  if (!site && user.role === 'god') {
    82     site = db.prepare('SELECT id, title, tagline, slug, owner_id FROM sites ORDER BY created_at LIMIT 1').get();
     83    site = getPrimarySite(); // primaire/hoofd-site als fallback
    8384  }
    8485  return site || null;
  • src/routes/admin-sites.js

    r98ecf51 r7881080  
    161161  const sites = db.prepare(`
    162162    SELECT s.id, s.slug, s.title, s.description, s.created_at,
    163            s.is_public, s.robots_index,
     163           s.is_public, s.robots_index, s.is_primary,
    164164           u.username AS owner_username,
    165165           (SELECT COUNT(*) FROM posts WHERE site_id = s.id) AS post_count
    166166    FROM sites s LEFT JOIN users u ON u.id = s.owner_id
    167     ORDER BY s.created_at DESC
     167    ORDER BY s.is_primary DESC, s.created_at DESC
    168168  `).all();
    169169
     
    354354});
    355355
     356// ==================== MAAK PRIMAIR ====================
     357// God kiest welke site de primaire/hoofd-site is (de label-/bedrijfssite in hub;
     358// in solo dé site). Precies één site is primair → eerst alles uit, dan deze aan.
     359router.post('/:slug/make-primary', requireGod, (req, res) => {
     360  const site = db.prepare('SELECT id FROM sites WHERE slug = ?').get(req.params.slug);
     361  if (!site) return res.redirect('/admin/sites?error=Niet+gevonden');
     362  db.transaction(() => {
     363    db.prepare('UPDATE sites SET is_primary = 0').run();
     364    db.prepare('UPDATE sites SET is_primary = 1 WHERE id = ?').run(site.id);
     365  })();
     366  res.redirect('/admin/sites?success=' + encodeURIComponent('Primaire site bijgewerkt'));
     367});
     368
    356369// ==================== DELETE ====================
    357370router.post('/:slug/delete', requireGod, (req, res) => {
  • src/routes/admin.js

    r98ecf51 r7881080  
    1010import { requireAuth } from '../middleware/auth.js';
    1111import { getTenancy } from '../services/SettingsService.js';
     12import { getPrimarySite } from '../middleware/site.js';
    1213
    1314const router = express.Router();
     
    5960  const tenancy = getTenancy();
    6061
    61   // De primaire/owner-site — in solo dé site, in hub de hoofdsite. Geeft de
     62  // De primaire/hoofd-site — in solo dé site, in hub de hoofdsite. Geeft de
    6263  // "Uiterlijk"-tegel z'n edit-link + de posts/concepten-lijst.
    63   const primarySite = db.prepare(
    64     'SELECT id, slug, title FROM sites ORDER BY created_at ASC LIMIT 1'
    65   ).get() || null;
     64  const primarySite = getPrimarySite();
    6665
    6766  const stats = {
  • src/routes/hub.js

    r98ecf51 r7881080  
    3535  `).all();
    3636
    37   // De hoofd-/labelsite (oudste = de bedrijfs-/hoofdaccount) is GEEN artiest;
    38   // die tonen we apart bovenaan, niet in de Artiesten-roster.
     37  // De hoofd-/labelsite (de expliciet primaire = de bedrijfs-/hoofdaccount) is
     38  // GEEN artiest; die tonen we apart bovenaan, niet in de Artiesten-roster.
    3939  const mainSite = db.prepare(`
    4040    SELECT s.id, s.slug, s.title, s.tagline, s.profile_photo, s.accent,
     
    4343    FROM sites s
    4444    LEFT JOIN users u ON u.id = s.owner_id
    45     ORDER BY s.created_at ASC
     45    WHERE s.is_primary = 1
    4646    LIMIT 1
    4747  `).get() || null;
Note: See TracChangeset for help on using the changeset viewer.