diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/server/services/routine-actions.ts | 16 | ||||
| -rw-r--r-- | src/lib/shared/temporal.test.ts | 35 | ||||
| -rw-r--r-- | src/lib/shared/temporal.ts | 26 | ||||
| -rw-r--r-- | src/routes/projects/[projectId]/routine/[routineId]/+page.svelte | 29 | ||||
| -rw-r--r-- | src/temporal-ambient.d.ts | 10 |
5 files changed, 105 insertions, 11 deletions
diff --git a/src/lib/server/services/routine-actions.ts b/src/lib/server/services/routine-actions.ts index 28a25cc..44ba2b8 100644 --- a/src/lib/server/services/routine-actions.ts +++ b/src/lib/server/services/routine-actions.ts @@ -1,6 +1,7 @@ import { fail, type ActionFailure } from '@sveltejs/kit'; import { archive, complete, edit, loadCurrent } from './routines'; import { parseFormString, parseFrequencyDays } from './validation'; +import { instantFromDay } from '$lib/shared/temporal'; /** Shared routine edit/complete actions; rejects missing/archived routines. */ @@ -57,12 +58,17 @@ export async function completeRoutineAction(data: FormData, routineId: number) { return fail(400, { error: 'Routine archivée' }); } + const now = Temporal.Now.instant(); + const day = parseFormString(data.get('doneAt')); + const doneAt = day + ? instantFromDay(day, now) + : now.toString({ smallestUnit: 'millisecond' }); + if (doneAt === null) { + return fail(400, { error: 'Date invalide' }); + } + try { - await complete( - routineId, - parseFormString(data.get('note')), - Temporal.Now.instant().toString({ smallestUnit: 'millisecond' }), - ); + await complete(routineId, parseFormString(data.get('note')), doneAt); } catch { return fail(400, { error: 'Routine introuvable' }); } diff --git a/src/lib/shared/temporal.test.ts b/src/lib/shared/temporal.test.ts new file mode 100644 index 0000000..ef3477a --- /dev/null +++ b/src/lib/shared/temporal.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from 'vitest'; +import { instantFromDay } from './temporal'; + +// A weekday well inside the year so noon UTC never collides with a clamp. +const NOW = Temporal.Instant.from('2026-09-20T08:00:00.000Z'); + +describe('instantFromDay', () => { + it('stamps a past day at noon UTC', () => { + expect(instantFromDay('2026-09-13', NOW)).toBe('2026-09-13T12:00:00.000Z'); + }); + + it('clamps today to the current instant', () => { + expect(instantFromDay('2026-09-20', NOW)).toBe('2026-09-20T08:00:00.000Z'); + }); + + it('keeps a past day that is only hours old as-is', () => { + // 21:00 the day before: noon of that day is still in the past. + expect(instantFromDay('2026-09-19', NOW)).toBe('2026-09-19T12:00:00.000Z'); + }); + + it('accepts a day a few hours ahead and clamps it to now', () => { + const late = Temporal.Instant.from('2026-09-20T22:00:00.000Z'); + expect(instantFromDay('2026-09-21', late)).toBe('2026-09-20T22:00:00.000Z'); + }); + + it('rejects a day more than a day ahead', () => { + expect(instantFromDay('2026-09-22', NOW)).toBeNull(); + }); + + it('rejects malformed and impossible dates', () => { + expect(instantFromDay('', NOW)).toBeNull(); + expect(instantFromDay('hier', NOW)).toBeNull(); + expect(instantFromDay('2026-02-30', NOW)).toBeNull(); + }); +}); diff --git a/src/lib/shared/temporal.ts b/src/lib/shared/temporal.ts index 103f209..6fd085d 100644 --- a/src/lib/shared/temporal.ts +++ b/src/lib/shared/temporal.ts @@ -24,3 +24,29 @@ export function formatDayMonthYear(iso: string): string { year: 'numeric', }); } + +const ONE_DAY_MS = 86_400_000; + +/** + * Stamp to store for a `YYYY-MM-DD` form day: noon UTC — a midnight stamp can + * read back as the wrong day in a timezone ahead of UTC. Clamped to `now` when + * the day is still ahead of the clock, and null for a malformed day or one + * more than a day in the future (timezones ahead of UTC make "today" fuzzy). + */ +export function instantFromDay( + day: string, + now: Temporal.Instant, +): string | null { + let midnight: Temporal.Instant; + try { + midnight = Temporal.PlainDate.from(day.trim()) + .toZonedDateTime('UTC') + .toInstant(); + } catch { + return null; + } + const noon = midnight.add({ hours: 12 }); + if (noon.epochMilliseconds - now.epochMilliseconds > ONE_DAY_MS) return null; + const stamp = Temporal.Instant.compare(noon, now) > 0 ? now : noon; + return stamp.toString({ smallestUnit: 'millisecond' }); +} diff --git a/src/routes/projects/[projectId]/routine/[routineId]/+page.svelte b/src/routes/projects/[projectId]/routine/[routineId]/+page.svelte index ea292ae..359fd5b 100644 --- a/src/routes/projects/[projectId]/routine/[routineId]/+page.svelte +++ b/src/routes/projects/[projectId]/routine/[routineId]/+page.svelte @@ -17,7 +17,18 @@ let frequency = $state(''); let description = $state(''); let note = $state(''); + let today = $state(''); + let day = $state(''); let saved = $state(false); + // Filled after mount: the server's timezone is not the viewer's. + $effect(() => { + if (today) return; + today = Temporal.Now.instant() + .toZonedDateTimeISO(Temporal.Now.timeZoneId()) + .toPlainDate() + .toString(); + day = today; + }); $effect(() => { name = data.routine.name; frequency = `${data.routine.frequencyDays}d`; @@ -160,6 +171,7 @@ use:enhance={() => { return async ({ update }) => { note = ''; + day = today; await update({ reset: false }); }; }} @@ -172,6 +184,14 @@ placeholder="Note (optionnel)" autocomplete="off" /> + <input + class="form-input done-date" + type="date" + name="doneAt" + bind:value={day} + max={today} + aria-label="Date de réalisation" + /> <Button variant="done" size="lg">Fait</Button> </form> {/if} @@ -244,10 +264,17 @@ .add-completion { flex-direction: row; align-items: center; + flex-wrap: wrap; } + /* Basis wider than the phone width so the note wraps instead of collapsing. */ .add-completion .form-input { - flex: 1; + flex: 1 1 12rem; + } + + .add-completion .done-date { + flex: 0 0 auto; + width: 11rem; } .completion-date { diff --git a/src/temporal-ambient.d.ts b/src/temporal-ambient.d.ts index 87933b9..c5ec34f 100644 --- a/src/temporal-ambient.d.ts +++ b/src/temporal-ambient.d.ts @@ -36,6 +36,7 @@ declare global { add(durationLike: DurationLike): ZonedDateTime; startOfDay(): ZonedDateTime; toInstant(): Instant; + toPlainDate(): PlainDate; toLocaleString( locales?: string | string[], options?: Intl.DateTimeFormatOptions, @@ -47,13 +48,12 @@ declare global { add(durationLike: DurationLike): PlainDate; subtract(durationLike: DurationLike): PlainDate; toZonedDateTime(timeZone: TimeZoneId): ZonedDateTime; + toString(): string; } namespace PlainDate { - function from(plainDateLike: { - year: number; - month: number; - day: number; - }): PlainDate; + function from( + plainDateLike: { year: number; month: number; day: number } | string, + ): PlainDate; } namespace Now { function instant(): Instant; |
