Files
token-circles/shared/profileSettings.ts
Komediruzecki 0cd26c1087 feat(guidance): one answers record per profile, kept in its settings
shared/guidance/answers.ts reads, merges and checks the record the
guidance questions write: the JSON text of one object in the profile's
settings under `guidance` (local-first: `guidance:<profile id>`). This
build reads `region` (DE, HR, EU27, UK, US, elsewhere, skipped),
`adults` and `children`. A field it does not know, or a value it cannot
read, is kept as stored, and the value reads as unanswered.
PUT /api/settings checks the record in both runtimes: the JSON text of
one object, at most 4,096 bytes, each field this build knows well
formed.

`guidance` joins PROFILE_SETTING_KEYS, and the local-first key pattern
is built from that list, so the record goes with a deleted profile,
survives "Clear profile data" and follows its profile through a backup
within and between the runtimes.

core/guidanceAnswers.ts saves through one function that re-reads the
record just before it writes and changes only the fields answered, with
this tab's saves queued. In cloud mode it refuses a save for a profile
that is no longer active.
2026-10-11 22:34:44 +02:00

40 lines
1.8 KiB
TypeScript

/**
* The settings that belong to one profile: its retirement plan, its badge record, and the answers
* it gave the guidance (shared/guidance/answers.ts).
*
* The Worker's settings table is keyed by (key, profile_id), so there each is a row of its
* profile under the plain key. Local-first keeps one settings row per key for the whole browser,
* so it names the profile in the key: `retirement_settings:<profile id>`. A backup moved from one
* runtime to the other is read through these, so that each setting stays with its profile, and
* each goes with a deleted profile in both.
*/
export const PROFILE_SETTING_KEYS = ['retirement_settings', 'achievements', 'guidance'] as const;
export type ProfileSettingKey = (typeof PROFILE_SETTING_KEYS)[number];
/** Whether `key` is one of these in the Worker's form: the plain key. */
export function isProfileSettingKey(key: string): key is ProfileSettingKey {
return (PROFILE_SETTING_KEYS as readonly string[]).includes(key);
}
/** A key in local-first's form, `<key>:<profile id>`, for any of the keys above. */
const LOCAL_FORM = new RegExp(`^(${PROFILE_SETTING_KEYS.join('|')}):(\\d+)$`);
/** Local-first's key for `key` of profile `profileId`. */
export function localProfileSettingKey(key: ProfileSettingKey, profileId: number): string {
return `${key}:${profileId}`;
}
/** A key in local-first's form split into the plain key and its profile, or null for any other. */
export function splitLocalProfileSettingKey(
key: string
): { key: ProfileSettingKey; profileId: number } | null {
const match = LOCAL_FORM.exec(key);
return match ? { key: match[1] as ProfileSettingKey, profileId: Number(match[2]) } : null;
}
/** Whether `key` is one of these in either form. */
export function belongsToOneProfile(key: string): boolean {
return isProfileSettingKey(key) || splitLocalProfileSettingKey(key) !== null;
}