diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-07 19:17:45 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-07 19:17:45 +0200 |
| commit | 37573109a05a07ac50827759ea63793209baa73a (patch) | |
| tree | 62c5004bb006eb6ec03830f069b0d26a112e72ab /src/lib/server/services/routines.ts | |
| parent | 1b96084da4ea317b06d92f4d493d102059599009 (diff) | |
Add routine detail page with editable description and health history
Diffstat (limited to 'src/lib/server/services/routines.ts')
| -rw-r--r-- | src/lib/server/services/routines.ts | 42 |
1 files changed, 36 insertions, 6 deletions
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<Pick<Routine, 'name' | 'frequencyDays' | 'description'>>, db = defaultDb, ): Promise<Routine> { 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<Routine | undefined> { + 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<RoutineCompletion[]> { + return db + .select() + .from(routineCompletions) + .where(eq(routineCompletions.routineId, routineId)) + .orderBy(desc(routineCompletions.doneAt)); +} + export async function complete( routineId: number, note: string | null, |
