Changeset 7881080 in Klonkt for src/middleware


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@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/middleware/site.js

    r98ecf51 r7881080  
    1212import db from '../config/database.js';
    1313import { getTenancy } from '../services/SettingsService.js';
     14
     15/**
     16 * De primaire/hoofd-site — ÉÉN bron van waarheid (vervangt de "oudste site ="
     17 * hoofd"-aanname die voorheen los in resolveSite/hub/account/admin stond).
     18 * Leest de expliciete is_primary-vlag; valt terug op de oudste als die (nog)
     19 * nergens staat, zodat bestaand gedrag exact behouden blijft.
     20 */
     21export function getPrimarySite() {
     22  return db.prepare('SELECT * FROM sites WHERE is_primary = 1 LIMIT 1').get()
     23      || db.prepare('SELECT * FROM sites ORDER BY created_at ASC LIMIT 1').get()
     24      || null;
     25}
    1426
    1527export function resolveSite(req, res, next) {
     
    4355  }
    4456
    45   // Solo (of hub zonder match): de primaire/owner-site (eerste aangemaakte).
    46   const defaultSite = db.prepare('SELECT * FROM sites ORDER BY created_at ASC LIMIT 1').get();
     57  // Solo (of hub zonder match): pin op de primaire/hoofd-site.
     58  const defaultSite = getPrimarySite();
    4759  if (defaultSite) {
    4860    res.locals.site = defaultSite;
Note: See TracChangeset for help on using the changeset viewer.