mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
feat(mood): structured tag taxonomy + note capture and timeline
Add the Category -> Tag taxonomy the mood surface was missing and the free-text note the web form could never write, then surface both on the Insights mood page. Schema (migration 0101): a global MoodTagCategory -> MoodTag catalog plus a MoodEntryTagLink join, seeded with a default five-category set (i18n-keyed labels, Lucide icons). Purely additive — the legacy flat MoodEntry.tags JSON column, the GLP-1 quick-tags, and the Telegram / MoodLog sync are untouched; an entry can carry both axes. Capture: the mood-logging form and the edit sheet gain a Textarea for the note (model + API already accepted it) and an icon-chip picker over the taxonomy catalog (GET /api/mood/tags). Create / update resolve the picked keys to links; unknown keys drop silently against the catalog. Insights: a structured-tag breakdown (icon + per-tag avg-mood lift) and a notes timeline join the existing distribution / weekday / flat-tag sections. The aggregate extension also fixes the flat-tag axis, which read the raw JSON string and so never populated. i18n across all six locales. Tests cover the taxonomy aggregation, the note round-trip, the structured-tag schema, and the notes timeline.
This commit is contained in:
1 parent
b99a015266
commit
cf649f72ce
25 files changed
+1688
-18
No files matched your search
@@ -1,5 +1,50 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { moodLogCredentialsSchema } from "../moodlog";
|
||||
import {
|
||||
moodLogCredentialsSchema,
|
||||
createMoodEntrySchema,
|
||||
updateMoodEntrySchema,
|
||||
} from "../moodlog";
|
||||
|
||||
describe("createMoodEntrySchema — note + structured tagKeys (v1.8.5)", () => {
|
||||
const base = { mood: "GUT", moodLoggedAt: "2026-06-01T12:00:00.000Z" };
|
||||
|
||||
it("round-trips the note field", () => {
|
||||
const r = createMoodEntrySchema.safeParse({ ...base, note: "felt great" });
|
||||
expect(r.success).toBe(true);
|
||||
if (r.success) expect(r.data.note).toBe("felt great");
|
||||
});
|
||||
|
||||
it("accepts structured tagKeys alongside flat tags", () => {
|
||||
const r = createMoodEntrySchema.safeParse({
|
||||
...base,
|
||||
tags: ["nausea"],
|
||||
tagKeys: ["happy", "worked_out"],
|
||||
});
|
||||
expect(r.success).toBe(true);
|
||||
if (r.success) {
|
||||
expect(r.data.tagKeys).toEqual(["happy", "worked_out"]);
|
||||
expect(r.data.tags).toEqual(["nausea"]);
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects an over-long note and an over-large tagKeys set", () => {
|
||||
expect(
|
||||
createMoodEntrySchema.safeParse({ ...base, note: "x".repeat(501) }).success,
|
||||
).toBe(false);
|
||||
expect(
|
||||
createMoodEntrySchema.safeParse({
|
||||
...base,
|
||||
tagKeys: Array.from({ length: 31 }, (_, i) => `t${i}`),
|
||||
}).success,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("allows update to clear tagKeys with null", () => {
|
||||
const r = updateMoodEntrySchema.safeParse({ tagKeys: null });
|
||||
expect(r.success).toBe(true);
|
||||
if (r.success) expect(r.data.tagKeys).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("moodLogCredentialsSchema SSRF guard", () => {
|
||||
it("accepts a public moodLog URL", () => {
|
||||
|
||||
@@ -78,9 +78,19 @@ export function getScoreForMood(mood: string): number {
|
||||
return MOOD_SCORE_MAP[mood] ?? 3;
|
||||
}
|
||||
|
||||
// v1.8.5 — structured-tag keys picked from the catalog (`mood_tags.key`).
|
||||
// Additive alongside the flat free-text `tags`: an entry can carry both.
|
||||
// Bounded so a single create can't fan out an unbounded link set.
|
||||
const structuredTagKeys = z.array(z.string().max(60)).max(30);
|
||||
|
||||
export const createMoodEntrySchema = z.object({
|
||||
mood: moodLevelEnum,
|
||||
tags: z.array(z.string().max(50)).max(20).optional(),
|
||||
// v1.8.5 — structured-tag keys from the taxonomy. Server resolves each
|
||||
// key to a `MoodTag` row and writes the `MoodEntryTagLink` join;
|
||||
// unknown keys are dropped silently (the catalog is the source of
|
||||
// truth, a stale client can't mint a tag).
|
||||
tagKeys: structuredTagKeys.optional(),
|
||||
// v1.4.30 H-5 — first-class free-text note. Replaces the
|
||||
// `tags: ["note:<text>"]` workaround. Capped at 500 chars so the
|
||||
// Coach evidence shelf renders cleanly without truncating chips.
|
||||
@@ -92,6 +102,9 @@ export const createMoodEntrySchema = z.object({
|
||||
export const updateMoodEntrySchema = z.object({
|
||||
mood: moodLevelEnum.optional(),
|
||||
tags: z.array(z.string().max(50)).max(20).nullable().optional(),
|
||||
// v1.8.5 — full replacement of the structured-tag set when present.
|
||||
// `null` clears every link; omit to leave links untouched.
|
||||
tagKeys: structuredTagKeys.nullable().optional(),
|
||||
note: z.string().max(500).nullable().optional(),
|
||||
moodLoggedAt: z.iso
|
||||
.datetime({ offset: true })
|
||||
|
||||
Reference in new issue
Block a user