From b26f23cebc214091638c56d965954916e76c3b53 Mon Sep 17 00:00:00 2001 From: le king fu Date: Sat, 24 Oct 2026 14:06:27 -0400 Subject: [PATCH] fix(sync): store completed and completedAt on task create taskCreateData now accepts completed and completedAt with the same rules as taskUpdateData, and the task create insert writes them. initialMerge sends completed without completedAt: a completed task without a date gets the server time, an open task gets no date (same rule as PATCH /api/tasks/[id]). Without completed the column default applies. Route tests cover the five cases on the real handler (PGlite), schema tests cover the create payloads. REVIEW.md Sync zone records the rule. Refs #146 Co-Authored-By: Claude Opus 5.5 --- REVIEW.md | 1 + web/src/app/api/sync/route.test.ts | 46 ++++++++++++++++++++++++++++++ web/src/app/api/sync/route.ts | 5 ++++ web/src/lib/syncData.test.ts | 19 ++++++++++-- web/src/lib/syncData.ts | 10 +++++-- 5 files changed, 75 insertions(+), 6 deletions(-) diff --git a/REVIEW.md b/REVIEW.md index c01a0a4..f54cf8e 100644 --- a/REVIEW.md +++ b/REVIEW.md @@ -101,6 +101,7 @@ Chaque règle cite sa source. Elles valent quel que soit le niveau : une PR norm - Toute écriture locale d'une liste, d'une tâche, d'une étiquette ou d'un lien tâche-étiquette passe par `writeOutboxEntry` quand la sync est active ; l'identifiant de l'entrée sert de clé d'idempotence côté serveur. — `src/db/repository/outbox.ts`, `src/db/repository/{lists,tasks,tags}.ts`, `web/src/app/api/sync/route.ts` - Les formes de données sont croisées : le client pousse en camelCase (`listId`, `dueDate`), le serveur renvoie en snake_case (`list_id`, `due_date`). Un champ ajouté l'est des deux côtés, avec la conversion des dates. — `src/services/syncClient.ts`, `web/src/app/api/sync/route.ts` - L'Inbox locale a l'identifiant fixe `00000000-0000-0000-0000-000000000001`, remappé vers un UUID serveur à la première sync (`initialMerge`). Une Inbox créée par la sync absorbe toutes les autres Inbox actives du compte (tâches déplacées, Inbox archivées), et elle est insérée avant le déplacement, car la clé étrangère `sl_tasks.list_id` n'est pas différable. Un id déjà présent dans le compte ne fusionne rien. Une seule Inbox est créée par POST, la suivante revient `skipped`. Une tâche envoyée vers une Inbox archivée, à la création comme à la mise à jour, va dans l'Inbox active. — `src/services/syncClient.ts`, `web/src/app/api/sync/route.ts`, PR #58, #64, #145, `web/src/db/migrations/0002_cleanup_duplicate_inboxes.sql` +- `task create` écrit `completed` et `completedAt` ; une tâche terminée sans date reçoit la date du serveur, une tâche non terminée n'a pas de date (#146). — `web/src/lib/syncData.ts`, `web/src/app/api/sync/route.ts` - `initialReset` efface toutes les données locales avant de tirer celles du serveur : un appel ajouté se justifie, et il reste derrière une confirmation de l'utilisateur. — `src/services/syncClient.ts`, `app/(tabs)/settings.tsx` - Une suppression côté serveur est un soft-delete (`deletedAt`), relayé à l'app par le pull. — `web/src/app/api/sync/route.ts` - Après un push ou un pull qui applique des changements, le widget est rafraîchi (`syncWidgetData`). — PR #66, `src/services/syncClient.ts` diff --git a/web/src/app/api/sync/route.test.ts b/web/src/app/api/sync/route.test.ts index e7704b2..bd5a494 100644 --- a/web/src/app/api/sync/route.test.ts +++ b/web/src/app/api/sync/route.test.ts @@ -390,3 +390,49 @@ describe('POST /api/sync: inbox merge', () => { expect(await listOf(B.task)).toBe(N1); }); }); + +describe('POST /api/sync: completion on task create (#146)', () => { + const D2 = '2026-09-01T08:30:00.000Z'; + + async function createTask(data: Record) { + const { B } = await seed(); + const id = u(); + expect(await push(B.userId, [['task', 'create', id, { title: 'done', listId: B.list, ...data }]])).toEqual(['applied']); + return (await row<{ completed: boolean; completed_at: Date | null }>( + 'select completed, completed_at from sl_tasks where id=$1', [id]))!; + } + + it('(a) stamps the server time on a completed task sent without completedAt (initialMerge)', async () => { + const before = Date.now(); + const { B } = await seed(); + const id = u(); + expect(await push(B.userId, [['task', 'create', id, { title: 'done', listId: B.list, completed: true }]])).toEqual(['applied']); + const after = Date.now(); + const r = (await row<{ completed: boolean; completed_at: Date | null }>( + 'select completed, completed_at from sl_tasks where id=$1', [id]))!; + expect(r.completed).toBe(true); + expect(r.completed_at).not.toBeNull(); + const t = new Date(r.completed_at!).getTime(); + expect(t).toBeGreaterThanOrEqual(before); + expect(t).toBeLessThanOrEqual(after); + }); + + it('(b) keeps the completedAt sent with a completed task', async () => { + const r = await createTask({ completed: true, completedAt: D2 }); + expect(r.completed).toBe(true); + expect(new Date(r.completed_at!).toISOString()).toBe(D2); + }); + + it('(c) stores an open task with no date (createTask payload)', async () => { + expect(await createTask({ notes: null, completed: false, completedAt: null, priority: 0, dueDate: null, parentId: null, recurrence: null })) + .toEqual({ completed: false, completed_at: null }); + }); + + it('(d) drops the completedAt of an open task', async () => { + expect(await createTask({ completed: false, completedAt: D2 })).toEqual({ completed: false, completed_at: null }); + }); + + it('(e) falls back to the column default without completed', async () => { + expect(await createTask({})).toEqual({ completed: false, completed_at: null }); + }); +}); diff --git a/web/src/app/api/sync/route.ts b/web/src/app/api/sync/route.ts index 7dcaeae..909331a 100644 --- a/web/src/app/api/sync/route.ts +++ b/web/src/app/api/sync/route.ts @@ -307,6 +307,11 @@ async function processOperation(op: SyncOperation, userId: string, ctx: RequestC parentId: d.parentId, recurrence: d.recurrence, position: d.position, + // initialMerge sends `completed` without `completedAt`: a completed + // task without a date gets the server time, an open task gets none + // (same rule as PATCH /api/tasks/[id]). + completed: d.completed, + completedAt: d.completed ? (d.completedAt ?? now) : null, }).onConflictDoNothing(); } else if (action === 'update') { const d = taskUpdateData.parse(data); diff --git a/web/src/lib/syncData.test.ts b/web/src/lib/syncData.test.ts index 3d54154..ff7998e 100644 --- a/web/src/lib/syncData.test.ts +++ b/web/src/lib/syncData.test.ts @@ -91,8 +91,8 @@ describe('task data', () => { dueDate: null, listId: LIST_ID, parentId: null, recurrence: null, })); expect(d).toEqual({ - title: 'Lait', notes: null, priority: 0, dueDate: null, listId: LIST_ID, - parentId: null, recurrence: null, + title: 'Lait', notes: null, completed: false, completedAt: null, priority: 0, + dueDate: null, listId: LIST_ID, parentId: null, recurrence: null, }); }); @@ -102,11 +102,24 @@ describe('task data', () => { dueDate: DATE, listId: LIST_ID, parentId: PARENT_ID, position: 4, recurrence: 'weekly', })); expect(d).toEqual({ - title: 'Sous-tâche', notes: 'n', priority: 2, dueDate: new Date(DATE), + title: 'Sous-tâche', notes: 'n', completed: true, priority: 2, dueDate: new Date(DATE), listId: LIST_ID, parentId: PARENT_ID, position: 4, recurrence: 'weekly', }); }); + it('converts an ISO completedAt to a Date on create', () => { + const d = taskCreateData.parse({ listId: LIST_ID, completed: true, completedAt: DATE }); + expect(d.completedAt).toEqual(new Date(DATE)); + }); + + it('rejects a non-boolean completed on create', () => { + expect(() => taskCreateData.parse({ listId: LIST_ID, completed: 'true' })).toThrow(); + }); + + it('turns a null completed into undefined on create (column default)', () => { + expect(taskCreateData.parse({ listId: LIST_ID, completed: null }).completed).toBeUndefined(); + }); + it('accepts the updateTask payload', () => { const d = taskUpdateData.parse(wire({ id: ID, title: 'Lait', notes: 'bio', completed: true, completedAt: DATE, priority: 3, diff --git a/web/src/lib/syncData.ts b/web/src/lib/syncData.ts index e1e8907..85d67e7 100644 --- a/web/src/lib/syncData.ts +++ b/web/src/lib/syncData.ts @@ -51,17 +51,21 @@ const taskFields = { position: notNull(z.number().int()), }; -// completed / completedAt are sent by the app on create but not stored (#142, out of scope). +const completionFields = { + completed: notNull(z.boolean()), + completedAt: nullable(isoDate), +}; + export const taskCreateData = z.object({ ...taskFields, + ...completionFields, listId: ref, }); export const taskUpdateData = z.object({ ...taskFields, + ...completionFields, listId: notNull(ref), - completed: notNull(z.boolean()), - completedAt: nullable(isoDate), }); export const tagCreateData = z.object({