Files
NextChair/manager
Stefan Lazic 2d4f9c5bf0
CI / ci (push) Canceled after 0s
Email Settings
1. Architecture chosen
Manager pushes environment variables into every business's .env, rather than businesses fetching config at runtime. Manager has no database of its own today (confirmed via code — no Prisma, no ORM dependency anywhere in manager/); every piece of its state is either an env var or a flat file under STACKS_DIR. A "runtime shared-config source" design would make every single email send (including OTP login) depend on Manager being reachable — a new failure mode this app has never had, for no real benefit. Instead:

Storage: one JSON file, stacks/manager/global-settings.json (gitignored), shaped for future sections (SMS, storage, backups, branding...) to slot in as sibling keys.
Secret at rest: the SMTP password is AES-256-GCM encrypted with a new MANAGER_SETTINGS_ENCRYPTION_KEY, never returned to the browser.
Rollout: saving complete settings automatically pushes the 7 EMAIL_* keys into every business's .env via the exact same surgical upsertEnvValues helper updateBusiness() already uses for name/domain edits, then force-recreates each container (composeUp(dir, {forceRecreate:true})) — the identical mechanism that already applies domain changes today.
New businesses get the current global settings baked into their .env at provisioning time — no manual step, ever.

2. Files changed
New (Manager): global-settings.ts (storage+encryption), email-rollout.ts (push-to-all-businesses), test-email.ts (standalone nodemailer test-send), dashboard/settings/ (page.tsx, actions.ts, email-settings-form.tsx).

Modified (Manager): compose-template.ts (optional email block in generated .env), provisioning.ts (reads global settings at creation), dashboard/layout.tsx (Settings nav link), package.json/package-lock.json (added nodemailer).

Modified (main app): brevo-email-provider.ts (added encryption mode + replyTo, backward-compatible), its test file (8 new tests).

Modified (docs): root .gitignore, .env.example, .env.business.example, manager/.env.example.

3. New volumes/databases/config files
None — no new Docker volume, no database. One new gitignored file, stacks/manager/global-settings.json, living inside the STACKS_DIR bind-mount Manager already has.

4. New environment variables
MANAGER_SETTINGS_ENCRYPTION_KEY (Manager only, required to use Settings) — 32-byte hex, generate with openssl rand -hex 32.
EMAIL_SMTP_ENCRYPTION and EMAIL_REPLY_TO (main app, both optional) — unset EMAIL_SMTP_ENCRYPTION preserves today's exact port-based inference, so existing deployments are unaffected.
Reply-to is now business-specific with global fallback,

5. How settings are persisted
stacks/manager/global-settings.json, written by saveEmailSettings(). The password is encrypted before writing; every other field is plain text (host, port, username, from-address, from-name, reply-to, encryption mode, last-test result) since none of those are secrets.

6. How business containers receive global settings
Via their own .env file (Docker Compose's env_file:, unchanged mechanism) — Manager writes the 7 EMAIL_* keys into it and recreates the container. Nothing in the business app itself needed a new runtime dependency; getEmailProvider() still just reads process.env once at process start, exactly as before.

7. What happens to existing businesses when settings change
Saving complete settings (host+username+password+from-address all present) automatically loops every business, updates its .env, and force-recreates its container in one action — the UI reports "applied to N of M businesses" with per-business failure reasons. A separate "Reapply to all businesses" button retries this without re-entering anything, for recovering from a business that was down during the last save. Saving incomplete settings persists them as a draft and touches no business (verified live — this is what stops a business from being flipped into a broken "brevo with blank credentials" state).

8. Manual deployment/update steps on the VPS
Generate a key: openssl rand -hex 32, add it as MANAGER_SETTINGS_ENCRYPTION_KEY to stacks/manager/.env.
Rebuild/redeploy the Manager image and the main app image (both changed) as usual.
Open Manager → Settings, fill in real SMTP details, send a test email, then Save — this rolls out to every existing business automatically. Nothing else needs to run by hand.

9. Security considerations
Password never logged, never returned to the browser after save (only a hasPassword boolean) — confirmed live: the form re-shows a masked placeholder, not the real value.
Test-email errors are categorized (auth/connection/timeout) before display — the raw SMTP error is never forwarded.
Encrypted at rest with AES-256-GCM; losing/rotating the key makes the stored password undecryptable (documented in manager/.env.example).
Settings routes sit under manager/src/proxy.ts's existing auth gate — no new auth surface.
Validated before rollout: malformed addresses/ports are rejected before Save, and rollout only fires when configuration is actually complete.

On JSON vs. database for Manager: JSON is the right call here. Reasoning:

Manager has zero DB dependencies today, deliberately — its own package.json description says an earlier attempt at giving it business-data access "repeatedly broke." Introducing SQLite/Prisma just for one settings blob breaks that design principle for not much gain.

The access pattern is a poor fit for a DB anyway: read on page-load/provisioning/rollout, written by a human clicking Save occasionally. No queries, no joins, no need for transactions beyond "read-modify-write one small file."

Reconsider if: multiple admins need to edit concurrently with conflict detection, you want an audit history of who-changed-what-when beyond "last test result," or the settings graph grows relational (e.g., per-business overrides tied by foreign key to businesses) rather than a flat global blob. None of that applies yet.
2026-09-06 10:38:21 +01:00
..
2026-09-05 13:39:55 +01:00
2026-09-06 10:38:21 +01:00
2026-09-05 13:39:55 +01:00
2026-09-06 10:38:21 +01:00
2026-09-05 13:39:55 +01:00
2026-09-05 13:39:55 +01:00
2026-09-05 13:39:55 +01:00
2026-09-05 13:39:55 +01:00
2026-09-05 13:39:55 +01:00
2026-09-06 10:38:21 +01:00
2026-09-06 10:38:21 +01:00
2026-09-05 13:39:55 +01:00
2026-09-05 13:39:55 +01:00
2026-09-05 13:39:55 +01:00

NextChair Manager

Standalone platform-operator tool for provisioning and managing NextChair business deployments on the shared multi-business VPS setup (see ../stacks/). Not part of the main app — a genuinely separate Next.js app, own package.json, own build, own container.

Why it's a separate app

An earlier attempt at this exact feature was built as routes inside the main app (src/app/manager/...). It was reverted after repeated, never-fully-resolved bugs: the main app's src/proxy.ts, root layout, root page, and global SessionProvider all statically import Prisma / Auth.js / business branding — code a Manager (which has no business database of its own, by definition) kept transitively pulling in, causing crashes in "Manager mode."

This app avoids that class of bug structurally rather than by careful avoidance: it never imports anything from the main app's src/ at all. A few small pieces (bcrypt convention, the signed-cookie primitive) were copied and adapted, not imported — see src/lib/auth.ts and src/lib/session.ts.

What it does

  • Lists provisioned businesses and their running/stopped state.
  • Add Business: collects a name/slug/domain, then generates secrets, creates that business's database + user on the shared MySQL server, writes its stacks/<slug>/compose.yaml + .env + Caddy site file, reloads Caddy, starts the container, and runs its migrations. See src/lib/provisioning.ts for the exact sequence.
  • Per business: start / stop / restart / update (recreate the container against the current shared image tag — not a source rebuild) / view recent logs.
  • Does not duplicate the /install wizard — after provisioning, the operator is linked to https://<domain>/install to finish that business's own setup.

How auth works

A single dedicated platform-operator credential (MANAGER_EMAIL / MANAGER_PASSWORD_HASH, bcrypt cost 12) — not tied to any one business's own Super Administrator, since this tool spans every business's separate database and has no "primary" one of its own. A signed (HMAC, not encrypted) cookie holds the session; see src/lib/session.ts.

The Docker-socket trust boundary

This container has the host's Docker socket mounted (docker.sock) so it can run docker compose/docker exec against the host's dockerd — see src/lib/docker.ts. Root inside this container plus that socket effectively means root on the host. Consequences:

  • It must stay behind the login above at all times — never expose it without that gate, and don't reuse MANAGER_PASSWORD_HASH anywhere else.
  • STACKS_DIR must be bind-mounted at the identical absolute path on both host and container (see .env.example) — a docker compose -f <path> command run from inside this container is executed by the host's dockerd, so the path only means the same file on both sides if the mount doesn't change it. This is the same constraint Dockge itself has.

Setup

cp manager/.env.example stacks/manager/.env   # fill in real values
docker compose -f stacks/manager/compose.yaml --env-file stacks/manager/.env up -d --build

Requires stacks/infra/compose.yaml already running (shared MySQL, Caddy, the next-chair-edge network). Add a Caddy site for this app the same way as any business (see stacks/infra/sites/.gitkeep), pointed at next-chair-manager:3000.

Local development

cd manager
npm install
cp .env.example .env.local
npm run dev

Requires network access to a real MySQL server and docker/docker compose on the machine you run it from for the provisioning/stack actions to actually work — the dashboard's read-only parts (listing stacks) will also just show nothing if STACKS_DIR doesn't point at a real directory yet.