mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
feat(onboarding): persist goal selection and seed the dashboard
Wire the goals step to actually keep its promise. The picker now posts the chosen slugs on the step-2 submit; the step endpoint validates them against the closed slug enum (422 on an unknown slug), persists them to User.onboardingGoals field-by-field, and on completion seeds dashboardWidgetsJson from the selection — promoting the goal-mapped tiles to the top and forcing them visible. The seed is one-time, gated on a null dashboardWidgetsJson via the updateMany WHERE clause, so a user who already arranged tiles is never clobbered. An empty or general-wellness-only selection leaves the column untouched (default layout). The resulting layout is what both the web dashboard and the iOS widgets contract read — no client recompute. Skip posts an explicit empty array (deliberate no preference).
This commit is contained in:
1 parent
5b756f01f2
commit
5f601426a9
5 files changed
+431
-15
No files matched your search
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* v1.17.1 — onboarding goal slug enum + dashboard seeding.
|
||||
*
|
||||
* Goals are a personalization seed, not a clinical target store: the
|
||||
* only leverage they have is which dashboard tiles get promoted and
|
||||
* made visible. These tests pin (a) the closed slug guard and (b) the
|
||||
* one-time seed builder's promote/forces-visible/no-op contracts.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
import {
|
||||
ONBOARDING_GOAL_SLUGS,
|
||||
GOAL_WIDGET_SEED_MAP,
|
||||
isOnboardingGoalSlug,
|
||||
buildGoalSeededDashboardLayout,
|
||||
} from "../goals";
|
||||
import {
|
||||
DEFAULT_DASHBOARD_LAYOUT,
|
||||
type DashboardWidgetId,
|
||||
} from "@/lib/dashboard-layout";
|
||||
|
||||
describe("isOnboardingGoalSlug", () => {
|
||||
it("accepts every member of the closed slug set", () => {
|
||||
for (const slug of ONBOARDING_GOAL_SLUGS) {
|
||||
expect(isOnboardingGoalSlug(slug)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects unknown / non-string values", () => {
|
||||
expect(isOnboardingGoalSlug("not-a-goal")).toBe(false);
|
||||
expect(isOnboardingGoalSlug("")).toBe(false);
|
||||
expect(isOnboardingGoalSlug(null)).toBe(false);
|
||||
expect(isOnboardingGoalSlug(42)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("GOAL_WIDGET_SEED_MAP", () => {
|
||||
it("only maps to ids the dashboard layout actually knows", () => {
|
||||
const known = new Set<DashboardWidgetId>(
|
||||
DEFAULT_DASHBOARD_LAYOUT.widgets.map((w) => w.id as DashboardWidgetId),
|
||||
);
|
||||
for (const ids of Object.values(GOAL_WIDGET_SEED_MAP)) {
|
||||
for (const id of ids) expect(known.has(id)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("leaves general-wellness without a tile preference", () => {
|
||||
expect(GOAL_WIDGET_SEED_MAP["general-wellness"]).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildGoalSeededDashboardLayout", () => {
|
||||
it("returns null for an empty selection (no seed)", () => {
|
||||
expect(buildGoalSeededDashboardLayout([])).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for a general-wellness-only selection", () => {
|
||||
expect(buildGoalSeededDashboardLayout(["general-wellness"])).toBeNull();
|
||||
});
|
||||
|
||||
it("ignores unknown slugs and seeds nothing when only unknowns are passed", () => {
|
||||
expect(buildGoalSeededDashboardLayout(["bogus", "nope"])).toBeNull();
|
||||
});
|
||||
|
||||
it("promotes glucose to the top and forces it visible", () => {
|
||||
const layout = buildGoalSeededDashboardLayout(["glucose-tracking"]);
|
||||
expect(layout).not.toBeNull();
|
||||
const widgets = layout!.widgets;
|
||||
// glucose is default-invisible + ordered low; the seed lifts it to
|
||||
// order 0 and turns both surfaces on.
|
||||
const glucose = widgets.find((w) => w.id === "glucose");
|
||||
expect(glucose).toMatchObject({
|
||||
order: 0,
|
||||
visible: true,
|
||||
tileVisible: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("promotes every mapped tile for a multi-goal selection", () => {
|
||||
const layout = buildGoalSeededDashboardLayout([
|
||||
"weight-management",
|
||||
"bp-tracking",
|
||||
]);
|
||||
expect(layout).not.toBeNull();
|
||||
const widgets = layout!.widgets;
|
||||
const promotedIds = new Set(["weight", "bodyFat", "bp", "bpInTarget"]);
|
||||
// Every promoted id sits in the leading block and is visible.
|
||||
for (const id of promotedIds) {
|
||||
const w = widgets.find((x) => x.id === id);
|
||||
expect(w?.visible).toBe(true);
|
||||
expect(w?.tileVisible).toBe(true);
|
||||
expect(w!.order).toBeLessThan(promotedIds.size);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps a dense, complete widget set (no tiles dropped)", () => {
|
||||
const layout = buildGoalSeededDashboardLayout(["sleep-improvement"]);
|
||||
expect(layout!.widgets.length).toBe(
|
||||
DEFAULT_DASHBOARD_LAYOUT.widgets.length,
|
||||
);
|
||||
const orders = layout!.widgets.map((w) => w.order).sort((a, b) => a - b);
|
||||
expect(orders).toEqual(orders.map((_, i) => i));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* v1.17.1 — onboarding goal slugs + dashboard seeding.
|
||||
*
|
||||
* The closed set of goal slugs the GoalsChipPicker offers, plus the
|
||||
* server-authoritative seeding that turns a goal selection into a
|
||||
* personalization signal: which dashboard tiles get promoted to the
|
||||
* top and made visible. Goals are NOT a clinical target store — the
|
||||
* targets surface (`/api/insights/targets`) is fully derived from
|
||||
* height/age/gender + clinical references. Their only leverage is
|
||||
* (a) dashboard tile pinning (here) and (b) reminder suggestions
|
||||
* (read by the reminders panel from `User.onboardingGoals`).
|
||||
*
|
||||
* One home for the slug enum so the API validation
|
||||
* (`/api/onboarding/step`), the client picker, and the seeding all
|
||||
* read the same source — same-key drift was the v1.4.16 dashboard-enum
|
||||
* bug class.
|
||||
*/
|
||||
import {
|
||||
DEFAULT_DASHBOARD_LAYOUT,
|
||||
serializeDashboardLayout,
|
||||
type DashboardLayout,
|
||||
type DashboardWidgetId,
|
||||
} from "@/lib/dashboard-layout";
|
||||
|
||||
/** Closed slug set — mirrors `OnboardingGoalSlug` in GoalsChipPicker. */
|
||||
export const ONBOARDING_GOAL_SLUGS = [
|
||||
"weight-management",
|
||||
"bp-tracking",
|
||||
"glucose-tracking",
|
||||
"sleep-improvement",
|
||||
"medication-compliance",
|
||||
"general-wellness",
|
||||
] as const;
|
||||
|
||||
export type OnboardingGoalSlug = (typeof ONBOARDING_GOAL_SLUGS)[number];
|
||||
|
||||
export function isOnboardingGoalSlug(
|
||||
value: unknown,
|
||||
): value is OnboardingGoalSlug {
|
||||
return (
|
||||
typeof value === "string" &&
|
||||
(ONBOARDING_GOAL_SLUGS as readonly string[]).includes(value)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Slug → dashboard widget ids to promote. `general-wellness` maps to
|
||||
* nothing — it carries no tile preference, so a general-wellness-only
|
||||
* selection leaves the default layout untouched. Ids must be members
|
||||
* of `DASHBOARD_WIDGET_IDS`; the seed never invents an id.
|
||||
*/
|
||||
export const GOAL_WIDGET_SEED_MAP: Record<
|
||||
OnboardingGoalSlug,
|
||||
readonly DashboardWidgetId[]
|
||||
> = {
|
||||
"weight-management": ["weight", "bodyFat"],
|
||||
"bp-tracking": ["bp", "bpInTarget"],
|
||||
"glucose-tracking": ["glucose"],
|
||||
"sleep-improvement": ["sleep"],
|
||||
"medication-compliance": ["medications"],
|
||||
"general-wellness": [],
|
||||
};
|
||||
|
||||
/**
|
||||
* Build the seeded dashboard layout for a goal selection.
|
||||
*
|
||||
* Returns a layout where the goal-mapped tiles are promoted to the top
|
||||
* of the order (preserving their relative order within the default
|
||||
* layout) and forced visible on both surfaces; every other tile keeps
|
||||
* its default config, shifted down. Returns `null` when there is
|
||||
* nothing to promote (empty selection or general-wellness only) so the
|
||||
* caller can skip the write entirely and leave the column null →
|
||||
* default layout.
|
||||
*
|
||||
* This is a ONE-TIME seed: the caller MUST gate it on
|
||||
* `User.dashboardWidgetsJson == null` so it never clobbers a user who
|
||||
* already arranged tiles.
|
||||
*/
|
||||
export function buildGoalSeededDashboardLayout(
|
||||
goals: readonly string[] | null | undefined,
|
||||
): DashboardLayout | null {
|
||||
if (!goals || goals.length === 0) return null;
|
||||
const promoted = new Set<DashboardWidgetId>();
|
||||
for (const slug of goals) {
|
||||
if (!isOnboardingGoalSlug(slug)) continue;
|
||||
for (const id of GOAL_WIDGET_SEED_MAP[slug]) promoted.add(id);
|
||||
}
|
||||
if (promoted.size === 0) return null;
|
||||
|
||||
const base = DEFAULT_DASHBOARD_LAYOUT.widgets;
|
||||
const promotedWidgets = base.filter((w) => promoted.has(w.id as DashboardWidgetId));
|
||||
const rest = base.filter((w) => !promoted.has(w.id as DashboardWidgetId));
|
||||
|
||||
const ordered = [...promotedWidgets, ...rest].map((w, index) => ({
|
||||
...w,
|
||||
// Force the goal-mapped tiles visible on both surfaces; leave the
|
||||
// rest exactly as the default layout had them.
|
||||
...(promoted.has(w.id as DashboardWidgetId)
|
||||
? { visible: true, tileVisible: true }
|
||||
: {}),
|
||||
order: index,
|
||||
}));
|
||||
|
||||
return serializeDashboardLayout({
|
||||
...DEFAULT_DASHBOARD_LAYOUT,
|
||||
widgets: ordered,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user