add platform demo fam

This commit is contained in:
JCEEE
2026-09-10 18:54:38 +01:00
parent 4730ab3eb9
commit 41033914b4
10 changed files with 114 additions and 30 deletions
@@ -1,19 +1,27 @@
import { fail, redirect } from '@sveltejs/kit';
import { pbUser, createPbClient } from '$lib/server/pocketbase';
import { createServices } from '$lib/server/services';
import { getPlatformFlags } from '$lib/server/platform';
import { pbAdmin } from '$lib/server/pocketbase';
export async function load(event) {
const session = event.locals.user;
const demoMode = (await getPlatformFlags()).demo;
const isDemoParent = session?.username === 'demo';
const famSlug = event.params.fam;
const username = event.params.username;
// Demo parent visiting a child's route → load child kanban view.
const demoParentVisitingChild = demoMode && isDemoParent && session?.username !== username;
// Parent (role=parent, session auth) → admin overview.
// Branch on role, NOT presence: a child who OTP-logged-in also has a
// `users` session (locals.user) and must not hit the parent path.
if (session && session.role === 'parent') {
const famId = session.famId;
const famSlug = event.params.fam;
const username = event.params.username;
if (session.name && session.username && session.username !== username) {
throw redirect(303, `/${famSlug}/${session.username}`);
if (session && session.role === 'parent' && !demoParentVisitingChild) {
if (!(demoMode && isDemoParent)) {
if (session.name && session.username && session.username !== username) {
throw redirect(303, `/${famSlug}/${session.username}`);
}
}
const s = createServices(pbUser(event), session);
const [
@@ -27,15 +35,15 @@ export async function load(event) {
completions,
settings
] = await Promise.all([
s.crud.list('members', famId),
s.crud.list('chore-templates', famId),
s.crud.list('assigned-chores', famId),
s.fam.weeklySummary(famId),
s.fam.get(famId),
s.crud.list('rewards', famId),
s.crud.list('bonus-configs', famId),
s.crud.list('completions', famId),
s.settings.get(famId)
s.crud.list('members', session.famId),
s.crud.list('chore-templates', session.famId),
s.crud.list('assigned-chores', session.famId),
s.fam.weeklySummary(session.famId),
s.fam.get(session.famId),
s.crud.list('rewards', session.famId),
s.crud.list('bonus-configs', session.famId),
s.crud.list('completions', session.famId),
s.settings.get(session.famId)
]);
return {
role: 'parent',
@@ -52,18 +60,23 @@ export async function load(event) {
};
}
// Child (users auth, role=child) → kanban
const famSlug = event.params.fam;
const username = event.params.username;
// Child (users auth, role=child) → kanban.
// Also demo parent visiting a child's route.
// A child should always land on their own kanban route.
if (session?.username && session.username !== username) {
throw redirect(303, `/${famSlug}/${session.username}`);
if (!(demoMode && isDemoParent)) {
if (session?.username && session.username !== username) {
throw redirect(303, `/${famSlug}/${session.username}`);
}
}
const pbToken = event.cookies.get('pb_token') || '';
const famId = session?.famId || '';
const childId = session?.id || '';
// When a demo parent visits a child's route, session.id is the parent's PB
// record. Look up the actual child user by username so chores load correctly.
const childId = demoParentVisitingChild
? (await pbAdmin.getList('users', `famId='${famId}' && username='${famSlug}:${username}'`))?.[0]?.id || ''
: session?.id || '';
const empty = {
role: 'child' as const,