'use server'; import { dailyService } from '@/services/task-management/daily'; import { UpdateDailyCheckboxData, DailyCheckbox, CreateDailyCheckboxData } from '@/lib/types'; import { revalidatePath } from 'next/cache'; import { getToday, getPreviousWorkday, parseDate, normalizeDate } from '@/lib/date-utils'; /** * Toggle l'état d'une checkbox */ export async function toggleCheckbox(checkboxId: string): Promise<{ success: boolean; data?: DailyCheckbox; error?: string; }> { try { // 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); 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 }); revalidatePath('/daily'); return { success: true, data: updatedCheckbox }; } catch (error) { console.error('Erreur toggleCheckbox:', error); return { success: false, error: error instanceof Error ? error.message : 'Erreur inconnue' }; } } /** * Ajoute une checkbox pour aujourd'hui */ export async function addTodayCheckbox(content: string, type?: 'task' | 'meeting', taskId?: string): Promise<{ success: boolean; data?: DailyCheckbox; error?: string; }> { try { const newCheckbox = await dailyService.addCheckbox({ date: getToday(), text: content, type: type || 'task', taskId }); revalidatePath('/daily'); return { success: true, data: newCheckbox }; } catch (error) { console.error('Erreur addTodayCheckbox:', error); return { success: false, error: error instanceof Error ? error.message : 'Erreur inconnue' }; } } /** * Ajoute une checkbox pour hier */ export async function addYesterdayCheckbox(content: string, type?: 'task' | 'meeting', taskId?: string): Promise<{ success: boolean; data?: DailyCheckbox; error?: string; }> { try { const yesterday = getPreviousWorkday(getToday()); const newCheckbox = await dailyService.addCheckbox({ date: yesterday, text: content, type: type || 'task', taskId }); revalidatePath('/daily'); return { success: true, data: newCheckbox }; } catch (error) { console.error('Erreur addYesterdayCheckbox:', error); return { success: false, error: error instanceof Error ? error.message : 'Erreur inconnue' }; } } /** * Met à jour une checkbox complète */ export async function updateCheckbox(checkboxId: string, data: UpdateDailyCheckboxData): Promise<{ success: boolean; data?: DailyCheckbox; error?: string; }> { try { const updatedCheckbox = await dailyService.updateCheckbox(checkboxId, data); revalidatePath('/daily'); return { success: true, data: updatedCheckbox }; } catch (error) { console.error('Erreur updateCheckbox:', error); return { success: false, error: error instanceof Error ? error.message : 'Erreur inconnue' }; } } /** * Supprime une checkbox */ export async function deleteCheckbox(checkboxId: string): Promise<{ success: boolean; error?: string; }> { try { await dailyService.deleteCheckbox(checkboxId); revalidatePath('/daily'); return { success: true }; } catch (error) { console.error('Erreur deleteCheckbox:', error); return { success: false, error: error instanceof Error ? error.message : 'Erreur inconnue' }; } } /** * Ajoute un todo lié à une tâche */ export async function addTodoToTask(taskId: string, text: string, date?: Date): Promise<{ success: boolean; data?: DailyCheckbox; error?: string; }> { try { const targetDate = normalizeDate(date || getToday()); const checkboxData: CreateDailyCheckboxData = { date: targetDate, text: text.trim(), type: 'task', taskId: taskId, isChecked: false }; const checkbox = await dailyService.addCheckbox(checkboxData); revalidatePath('/daily'); revalidatePath('/kanban'); return { success: true, data: checkbox }; } catch (error) { console.error('Erreur addTodoToTask:', error); return { success: false, error: error instanceof Error ? error.message : 'Erreur inconnue' }; } } /** * Réorganise les checkboxes d'une date */ export async function reorderCheckboxes(dailyId: string, checkboxIds: string[]): Promise<{ success: boolean; error?: string; }> { try { // Le dailyId correspond à la date au format YYYY-MM-DD const date = parseDate(dailyId); await dailyService.reorderCheckboxes(date, checkboxIds); revalidatePath('/daily'); return { success: true }; } catch (error) { console.error('Erreur reorderCheckboxes:', error); return { success: false, error: error instanceof Error ? error.message : 'Erreur inconnue' }; } } /** * Déplace une checkbox non cochée à aujourd'hui */ export async function moveCheckboxToToday(checkboxId: string): Promise<{ success: boolean; data?: DailyCheckbox; error?: string; }> { try { const updatedCheckbox = await dailyService.moveCheckboxToToday(checkboxId); revalidatePath('/daily'); return { success: true, data: updatedCheckbox }; } catch (error) { console.error('Erreur moveCheckboxToToday:', error); return { success: false, error: error instanceof Error ? error.message : 'Erreur inconnue' }; } }