feat(DailyService): implement toggleCheckbox method for direct checkbox state updates

This commit is contained in:
Julien Froidefond
2025-10-30 08:15:01 +01:00
parent cd391506ce
commit 0bf9802e71
3 changed files with 44 additions and 28 deletions

View File

@@ -30,28 +30,8 @@ export async function toggleCheckbox(checkboxId: string): Promise<{
return { success: false, error: 'Non authentifié' }; return { success: false, error: 'Non authentifié' };
} }
// Nous devons d'abord récupérer la checkbox pour connaître son état actuel // Toggle direct côté service par ID (indépendant de la date)
// En absence de getCheckboxById, nous allons essayer de la trouver via une vue daily const updatedCheckbox = await dailyService.toggleCheckbox(checkboxId);
// 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,
});
revalidatePath('/daily'); revalidatePath('/daily');
return { success: true, data: updatedCheckbox }; return { success: true, data: updatedCheckbox };
@@ -70,7 +50,8 @@ export async function toggleCheckbox(checkboxId: string): Promise<{
export async function addTodayCheckbox( export async function addTodayCheckbox(
content: string, content: string,
type?: 'task' | 'meeting', type?: 'task' | 'meeting',
taskId?: string taskId?: string,
date?: Date
): Promise<{ ): Promise<{
success: boolean; success: boolean;
data?: DailyCheckbox; data?: DailyCheckbox;
@@ -82,8 +63,10 @@ export async function addTodayCheckbox(
return { success: false, error: 'Non authentifié' }; return { success: false, error: 'Non authentifié' };
} }
const targetDate = normalizeDate(date || getToday());
const newCheckbox = await dailyService.addCheckbox({ const newCheckbox = await dailyService.addCheckbox({
date: getToday(), date: targetDate,
userId: session.user.id, userId: session.user.id,
text: content, text: content,
type: type || 'task', type: type || 'task',
@@ -107,7 +90,8 @@ export async function addTodayCheckbox(
export async function addYesterdayCheckbox( export async function addYesterdayCheckbox(
content: string, content: string,
type?: 'task' | 'meeting', type?: 'task' | 'meeting',
taskId?: string taskId?: string,
baseDate?: Date
): Promise<{ ): Promise<{
success: boolean; success: boolean;
data?: DailyCheckbox; data?: DailyCheckbox;
@@ -119,7 +103,8 @@ export async function addYesterdayCheckbox(
return { success: false, error: 'Non authentifié' }; return { success: false, error: 'Non authentifié' };
} }
const yesterday = getPreviousWorkday(getToday()); const base = normalizeDate(baseDate || getToday());
const yesterday = getPreviousWorkday(base);
const newCheckbox = await dailyService.addCheckbox({ const newCheckbox = await dailyService.addCheckbox({
date: yesterday, date: yesterday,

View File

@@ -120,7 +120,12 @@ export function useDaily(
try { try {
setError(null); setError(null);
const result = await addTodayCheckboxAction(text, type, taskId); const result = await addTodayCheckboxAction(
text,
type,
taskId,
currentDate
);
if (result.success && result.data) { if (result.success && result.data) {
// Mise à jour optimiste // Mise à jour optimiste
@@ -167,7 +172,12 @@ export function useDaily(
try { try {
setError(null); setError(null);
const result = await addYesterdayCheckboxAction(text, type, taskId); const result = await addYesterdayCheckboxAction(
text,
type,
taskId,
currentDate
);
if (result.success && result.data) { if (result.success && result.data) {
// Mise à jour optimiste // Mise à jour optimiste

View File

@@ -130,6 +130,27 @@ export class DailyService {
return this.mapPrismaCheckbox(checkbox); return this.mapPrismaCheckbox(checkbox);
} }
/**
* Toggle l'état d'une checkbox par son ID (indépendant de la date)
*/
async toggleCheckbox(checkboxId: string): Promise<DailyCheckbox> {
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 * Supprime une checkbox
*/ */