diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index dc3e685..99cfbfd 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -39,16 +39,22 @@ fastify.post<{ Body: { name: string; doesRepeat: boolean; repeatsEvery?: string; return; } - // Calculate lastCompletedOn based on toDoToday + // Calculate lastCompletedOn so the next due date lands correctly let lastCompletedOn: Date | null = null; - if (!toDoToday && doesRepeat && repeatsEvery) { - // Set it in the future so it doesn't appear as due today + if (doesRepeat && repeatsEvery) { const [amount, unit] = repeatsEvery.split('_'); - const days = unit === 'DAYS' ? parseInt(amount) : - unit === 'WEEKS' ? parseInt(amount) * 7 : - unit === 'MONTHS' ? parseInt(amount) * 30 : + const days = unit === 'DAYS' ? parseInt(amount) : + unit === 'WEEKS' ? parseInt(amount) * 7 : + unit === 'MONTHS' ? parseInt(amount) * 30 : parseInt(amount) * 365; - lastCompletedOn = new Date(Date.now() - days * 24 * 60 * 60 * 1000 + 24 * 60 * 60 * 1000); + const msInterval = days * 24 * 60 * 60 * 1000; + if (toDoToday) { + // next due = today: set lastCompletedOn = now - interval + lastCompletedOn = new Date(Date.now() - msInterval); + } else { + // next due = tomorrow: set lastCompletedOn = now - interval + 1 day + lastCompletedOn = new Date(Date.now() - msInterval + 24 * 60 * 60 * 1000); + } } const [newTask] = await db.insert(tasks).values({ diff --git a/apps/web/src/components/CreateTaskForm.tsx b/apps/web/src/components/CreateTaskForm.tsx index f081f19..436defa 100644 --- a/apps/web/src/components/CreateTaskForm.tsx +++ b/apps/web/src/components/CreateTaskForm.tsx @@ -112,7 +112,7 @@ export default function CreateTaskForm({ onCreateTask }: CreateTaskFormProps) { )}
- +