mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
feat(mood): add rated mood factors (ingestion + write contract)
Extend the structured mood taxonomy so a tag can carry a magnitude. A
rated factor is a MoodTag of kind RATED that the user scores per entry;
the score persists on MoodEntryTagLink.rating. Binary tags keep
kind BINARY and a null rating, so every existing row and read path stays
byte-identical.
Migration 0119 is additive: four marker columns on mood_tags (kind,
scale_min, scale_max, inverse), a nullable rating on mood_entry_tag_links,
and a seeded factors category with five MVP factors (work, social, sleep
quality, stress, conflict). Stress and conflict are inverse-scaled
(higher means worse); conflict is a 1..2 Yes/No factor.
Write contract: POST /api/mood-entries and the bulk route accept an
optional ratedFactors: [{ key, rating }] array parallel to the binary
tagKeys. The resolver builds the join rows field-by-field, drops unknown
and non-RATED keys silently, and rejects a rating outside the factor's
own scaleMin..scaleMax (422 on the single-entry path, per-entry skipped
on bulk). Create and list responses surface the persisted factors split
from binary keys so a client hydrates without a refetch. GET
/api/mood/tags now emits kind, scaleMin, scaleMax, and inverse so a
client can render factors versus tags.
This is the ingestion half; analytics over rated factors follow later.
Factor labels ship in all six locales.
This commit is contained in:
1 parent
793602f464
commit
81096b9553
20 files changed
+1100
-43
No files matched your search
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* v1.12.0 — `GET /api/mood/tags` must expose the rated-factor metadata
|
||||
* (`kind` / `scaleMin` / `scaleMax` / `inverse`) so the client can branch
|
||||
* between binary toggle chips and rated segmented controls.
|
||||
*/
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const categoryFindMany = vi.fn();
|
||||
|
||||
vi.mock("@/lib/db", () => ({
|
||||
prisma: {
|
||||
moodTagCategory: { findMany: (...a: unknown[]) => categoryFindMany(...a) },
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/auth/session", () => ({ getSession: vi.fn() }));
|
||||
vi.mock("@/lib/auth/audit", () => ({
|
||||
auditLog: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
vi.mock("@/lib/logging/transports", () => ({ emitIfSampled: vi.fn() }));
|
||||
vi.mock("@/lib/db-compat", () => ({
|
||||
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
vi.mock("next/headers", () => ({
|
||||
headers: vi.fn(async () => ({ get: () => null })),
|
||||
cookies: vi.fn(async () => ({
|
||||
get: () => undefined,
|
||||
set: () => {},
|
||||
delete: () => {},
|
||||
})),
|
||||
}));
|
||||
|
||||
import { GET } from "../route";
|
||||
import { getSession } from "@/lib/auth/session";
|
||||
|
||||
const SESSION_OK = {
|
||||
session: { id: "sess-1", expiresAt: new Date(Date.now() + 3_600_000) },
|
||||
user: { id: "user-1", username: "tester", role: "USER" as const },
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
|
||||
});
|
||||
|
||||
describe("GET /api/mood/tags — rated-factor catalog metadata (v1.12.0)", () => {
|
||||
it("selects + emits kind / scaleMin / scaleMax / inverse per tag", async () => {
|
||||
categoryFindMany.mockResolvedValue([
|
||||
{
|
||||
key: "factors",
|
||||
labelKey: "mood.tagCategory.factors",
|
||||
icon: "SlidersHorizontal",
|
||||
tags: [
|
||||
{
|
||||
key: "factor_work",
|
||||
labelKey: "mood.tag.factorWork",
|
||||
icon: "Briefcase",
|
||||
kind: "RATED",
|
||||
scaleMin: 1,
|
||||
scaleMax: 5,
|
||||
inverse: false,
|
||||
},
|
||||
{
|
||||
key: "factor_conflict",
|
||||
labelKey: "mood.tag.factorConflict",
|
||||
icon: "Swords",
|
||||
kind: "RATED",
|
||||
scaleMin: 1,
|
||||
scaleMax: 2,
|
||||
inverse: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
const res = await GET();
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as {
|
||||
data: {
|
||||
categories: Array<{
|
||||
key: string;
|
||||
tags: Array<{
|
||||
key: string;
|
||||
kind: string;
|
||||
scaleMin: number;
|
||||
scaleMax: number;
|
||||
inverse: boolean;
|
||||
}>;
|
||||
}>;
|
||||
};
|
||||
};
|
||||
|
||||
// The Prisma select must request the new columns.
|
||||
const selectArg = categoryFindMany.mock.calls[0]?.[0] as {
|
||||
select: { tags: { select: Record<string, boolean> } };
|
||||
};
|
||||
expect(selectArg.select.tags.select).toMatchObject({
|
||||
kind: true,
|
||||
scaleMin: true,
|
||||
scaleMax: true,
|
||||
inverse: true,
|
||||
});
|
||||
|
||||
const factor = body.data.categories[0].tags[0];
|
||||
expect(factor.kind).toBe("RATED");
|
||||
expect(factor.scaleMin).toBe(1);
|
||||
expect(factor.scaleMax).toBe(5);
|
||||
expect(factor.inverse).toBe(false);
|
||||
|
||||
const conflict = body.data.categories[0].tags[1];
|
||||
expect(conflict.scaleMax).toBe(2);
|
||||
expect(conflict.inverse).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -29,7 +29,19 @@ export const GET = apiHandler(async () => {
|
||||
tags: {
|
||||
where: { isActive: true },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
select: { key: true, labelKey: true, icon: true },
|
||||
// v1.12.0 — emit the rated-factor metadata so the client can
|
||||
// branch rendering: `kind = 'BINARY'` → a toggle chip; `'RATED'`
|
||||
// → a 1..scaleMax segmented control (Yes/No when scaleMax = 2).
|
||||
// `inverse` flags factors where a higher score means a worse day.
|
||||
select: {
|
||||
key: true,
|
||||
labelKey: true,
|
||||
icon: true,
|
||||
kind: true,
|
||||
scaleMin: true,
|
||||
scaleMax: true,
|
||||
inverse: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user