From 0bf9802e71d48cbe32c0b377904155cd023fa359 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Thu, 30 Oct 2025 08:15:01 +0100 Subject: [PATCH] feat(DailyService): implement toggleCheckbox method for direct checkbox state updates --- src/actions/daily.ts | 37 ++++++++------------------- src/hooks/useDaily.ts | 14 ++++++++-- src/services/task-management/daily.ts | 21 +++++++++++++++ 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/actions/daily.ts b/src/actions/daily.ts index ff245ff..e11df64 100644 --- a/src/actions/daily.ts +++ b/src/actions/daily.ts @@ -30,28 +30,8 @@ export async function toggleCheckbox(checkboxId: string): Promise<{ return { success: false, error: 'Non authentifié' }; } - // Nous devons d'abord récupérer la checkbox pour connaître son état actuel - // En absence de getCheckboxById, nous allons essayer de la trouver via une vue daily - // Pour l'instant, nous allons simplement toggle via updateCheckbox - // (le front-end gère déjà l'état optimiste) - - // Récupérer toutes les checkboxes d'aujourd'hui et hier pour trouver celle à toggle - const today = getToday(); - const dailyView = await dailyService.getDailyView(today, session.user.id); - - let checkbox = dailyView.today.find((cb) => cb.id === checkboxId); - if (!checkbox) { - checkbox = dailyView.yesterday.find((cb) => cb.id === checkboxId); - } - - if (!checkbox) { - return { success: false, error: 'Checkbox non trouvée' }; - } - - // Toggle l'état - const updatedCheckbox = await dailyService.updateCheckbox(checkboxId, { - isChecked: !checkbox.isChecked, - }); + // Toggle direct côté service par ID (indépendant de la date) + const updatedCheckbox = await dailyService.toggleCheckbox(checkboxId); revalidatePath('/daily'); return { success: true, data: updatedCheckbox }; @@ -70,7 +50,8 @@ export async function toggleCheckbox(checkboxId: string): Promise<{ export async function addTodayCheckbox( content: string, type?: 'task' | 'meeting', - taskId?: string + taskId?: string, + date?: Date ): Promise<{ success: boolean; data?: DailyCheckbox; @@ -82,8 +63,10 @@ export async function addTodayCheckbox( return { success: false, error: 'Non authentifié' }; } + const targetDate = normalizeDate(date || getToday()); + const newCheckbox = await dailyService.addCheckbox({ - date: getToday(), + date: targetDate, userId: session.user.id, text: content, type: type || 'task', @@ -107,7 +90,8 @@ export async function addTodayCheckbox( export async function addYesterdayCheckbox( content: string, type?: 'task' | 'meeting', - taskId?: string + taskId?: string, + baseDate?: Date ): Promise<{ success: boolean; data?: DailyCheckbox; @@ -119,7 +103,8 @@ export async function addYesterdayCheckbox( return { success: false, error: 'Non authentifié' }; } - const yesterday = getPreviousWorkday(getToday()); + const base = normalizeDate(baseDate || getToday()); + const yesterday = getPreviousWorkday(base); const newCheckbox = await dailyService.addCheckbox({ date: yesterday, diff --git a/src/hooks/useDaily.ts b/src/hooks/useDaily.ts index 7572415..c3e98d6 100644 --- a/src/hooks/useDaily.ts +++ b/src/hooks/useDaily.ts @@ -120,7 +120,12 @@ export function useDaily( try { setError(null); - const result = await addTodayCheckboxAction(text, type, taskId); + const result = await addTodayCheckboxAction( + text, + type, + taskId, + currentDate + ); if (result.success && result.data) { // Mise à jour optimiste @@ -167,7 +172,12 @@ export function useDaily( try { setError(null); - const result = await addYesterdayCheckboxAction(text, type, taskId); + const result = await addYesterdayCheckboxAction( + text, + type, + taskId, + currentDate + ); if (result.success && result.data) { // Mise à jour optimiste diff --git a/src/services/task-management/daily.ts b/src/services/task-management/daily.ts index d92af09..84e8ee8 100644 --- a/src/services/task-management/daily.ts +++ b/src/services/task-management/daily.ts @@ -130,6 +130,27 @@ export class DailyService { return this.mapPrismaCheckbox(checkbox); } + /** + * Toggle l'état d'une checkbox par son ID (indépendant de la date) + */ + async toggleCheckbox(checkboxId: string): Promise { + const existing = await prisma.dailyCheckbox.findUnique({ + where: { id: checkboxId }, + }); + + if (!existing) { + throw new BusinessError('Checkbox non trouvée'); + } + + const updated = await prisma.dailyCheckbox.update({ + where: { id: checkboxId }, + data: { isChecked: !existing.isChecked, updatedAt: new Date() }, + include: { task: true, user: true }, + }); + + return this.mapPrismaCheckbox(updated); + } + /** * Supprime une checkbox */