feat: gate a switched browser on the record's modules, not the actor's

Turning a module off for a profile you look after wrote a column nothing
on screen was reading. GET /api/auth/me published the ACTOR's module map
while switched, and the navigation, the dashboard tiles and the insights
sections all gate on it — so a guardian who turned Cycle off for the child
kept seeing a Cycle destination inside the child's record, and a guardian
who tracks it saw one there whether or not the child did (#939).

The map and the resolved cycle flag now answer for the record the session
is inside. They are not display preferences: every surface they gate shows
the record's data, so the record is what decides. Locale, theme and layout
stay the actor's. The record is taken from the grant-validated active
entry rather than the session's own selector, so a revoked switch cannot
publish somebody's configuration, and with no switch the payload is what
it was.

Cycle also gets a row on the guardian's Modules surface, which it never
had. It is a delegated module, so its state is the record's own cycle
profile and modulePreferences.cycle is refused — which left a guardian no
way to turn it off for a record whose sex says it should be on. The family
names the real column instead of a blob entry the gate ignores.

Refs #939
This commit is contained in:
Marc-André Bombeck committed 2026-09-10 00:02:03 +02:00
1 parent f521b03c35
commit fb1cb4e3ed
9 files changed
+556 -46

No files matched your search

+50 -7
View File
@@ -30,6 +30,8 @@ import {
parseManagedRecordSettingsPatch,
resolveGuardianRecordSettingsAccess,
} from "@/lib/record-settings";
import { isCycleEnabled } from "@/lib/cycle/gate";
import { getOrCreateCycleProfile } from "@/lib/cycle/profile";
import {
resolveInsightsLayout,
serializeInsightsLayout,
@@ -101,13 +103,23 @@ export const GET = apiHandler(
dateFormat: record.dateFormat,
};
break;
case "modules":
case "modules": {
// The delegated key needs its real source, which is not the module
// blob. `isCycleEnabled` resolves the record's own opt-in against its
// sex exactly as `/api/auth/me` does for the account itself, so the
// switch a guardian sees is the state the gate will answer with.
const cycleProfile = await prisma.cycleProfile.findUnique({
where: { userId: access.recordId },
select: { cycleTrackingEnabled: true },
});
settings = {
modulePreferences: managedModulePreferencesFrom(
record.modulePreferencesJson,
),
cycleTrackingEnabled: isCycleEnabled(record.gender, cycleProfile),
};
break;
}
case "notifications": {
const preferences = parseNotificationPrefs(record.notificationPrefs);
settings = {
@@ -233,19 +245,50 @@ export const PATCH = apiHandler(
const patch = parseManagedRecordSettingsPatch("modules", body);
const current = await prisma.user.findUnique({
where: { id: access.recordId },
select: { modulePreferencesJson: true },
select: { modulePreferencesJson: true, gender: true },
});
const modulePreferences = {
...managedModulePreferencesFrom(current?.modulePreferencesJson),
...patch.modulePreferences,
};
await prisma.user.update({
where: { id: access.recordId },
data: { modulePreferencesJson: toJson(modulePreferences) },
if (patch.modulePreferences !== undefined) {
await prisma.user.update({
where: { id: access.recordId },
data: { modulePreferencesJson: toJson(modulePreferences) },
});
}
// The delegated key writes its own column. An explicit boolean is
// what the cycle gate honours over the sex-derived default, so a
// guardian turning Cycle off for a record whose sex says otherwise
// is recorded as the deliberate answer it is.
if (patch.cycleTrackingEnabled !== undefined) {
await getOrCreateCycleProfile(access.recordId);
await prisma.cycleProfile.update({
where: { userId: access.recordId },
data: { cycleTrackingEnabled: patch.cycleTrackingEnabled },
});
}
const cycleProfile = await prisma.cycleProfile.findUnique({
where: { userId: access.recordId },
select: { cycleTrackingEnabled: true },
});
invalidateUserHealthScore(access.recordId);
settings = { modulePreferences };
changed = Object.keys(patch.modulePreferences);
settings = {
modulePreferences,
cycleTrackingEnabled: isCycleEnabled(
current?.gender ?? null,
cycleProfile,
),
};
// The module keys themselves, not the wrapper field: the record's
// trail should say which module moved. `cycleTrackingEnabled` is
// named as itself because that is the column it writes.
changed = [
...Object.keys(patch.modulePreferences ?? {}),
...(patch.cycleTrackingEnabled !== undefined
? ["cycleTrackingEnabled"]
: []),
];
break;
}
case "notifications": {