From 37573109a05a07ac50827759ea63793209baa73a Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Mon, 7 Sep 2026 19:17:45 +0200 Subject: Add routine detail page with editable description and health history --- src/lib/server/services/routines.ts | 42 +++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'src/lib/server/services/routines.ts') diff --git a/src/lib/server/services/routines.ts b/src/lib/server/services/routines.ts index e9e0c45..c3a5eff 100644 --- a/src/lib/server/services/routines.ts +++ b/src/lib/server/services/routines.ts @@ -1,6 +1,6 @@ import { db as defaultDb } from '$lib/server/db'; import { routines, routineCompletions, projects } from '$lib/server/db/schema'; -import { and, eq, inArray, sql } from 'drizzle-orm'; +import { eq, inArray, desc, sql } from 'drizzle-orm'; import type { AppDatabase } from './groceries'; import type { Routine, RoutineCompletion } from './types'; import { @@ -40,11 +40,15 @@ export async function add( return inserted; } -/** Edit = insert a new snapshot row with version+1; history stays intact. */ +/** + * Edit = insert a new snapshot row with version+1; history stays intact. + * Only the given fields change; the others are copied from the current + * snapshot so a name/frequency edit never touches the description and + * vice-versa. + */ export async function edit( id: number, - name: string, - frequencyDays: number, + fields: Partial>, db = defaultDb, ): Promise { const [current] = await db @@ -61,8 +65,9 @@ export async function edit( id: current.id, version: current.version + 1, projectId: current.projectId, - name, - frequencyDays, + name: fields.name ?? current.name, + frequencyDays: fields.frequencyDays ?? current.frequencyDays, + description: fields.description ?? current.description, archived: current.archived, createdAt: current.createdAt, }) @@ -97,6 +102,31 @@ export async function loadAllVersions( .orderBy(routines.version); } +/** Current (max-version) snapshot of a routine, or undefined when unknown. */ +export async function loadCurrent( + routineId: number, + db = defaultDb, +): Promise { + const [row] = await db + .select() + .from(routines) + .where(eq(routines.id, routineId)) + .orderBy(sql`${routines.version} DESC`) + .limit(1); + return row; +} + +export async function loadCompletions( + routineId: number, + db = defaultDb, +): Promise { + return db + .select() + .from(routineCompletions) + .where(eq(routineCompletions.routineId, routineId)) + .orderBy(desc(routineCompletions.doneAt)); +} + export async function complete( routineId: number, note: string | null, -- cgit v1.3