1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
import { fail, type ActionFailure } from '@sveltejs/kit';
import { archive, complete, edit, loadCurrent } from './routines';
import { parseFormString, parseFrequencyDays } from './validation';
/**
* Shared implementations of the routine edit/complete form actions, used by
* both the project dashboard (/projects/[id]) and the routine page
* (/projects/[projectId]/routine/[routineId]). Guarding and failure messages
* are the strictest common denominator: a missing or archived routine is
* rejected before any write.
*/
export type EditRoutineResult =
ActionFailure<{ error: string }> | { success: boolean; type?: 'edit' };
export async function editRoutineAction(
data: FormData,
routineId: number,
extra?: { type: 'edit' },
): Promise<EditRoutineResult> {
const current = await loadCurrent(routineId);
if (!current) {
return fail(400, { error: 'Routine introuvable' });
}
if (current.archived) {
return fail(400, { error: 'Routine archivée' });
}
const name = parseFormString(data.get('name'));
if (!name) {
return fail(400, { error: 'Le nom est requis' });
}
let frequencyDays: number;
try {
frequencyDays = parseFrequencyDays(data.get('frequencyDays'));
} catch {
return fail(400, { error: 'Fréquence invalide' });
}
// The dashboard row form has no description field; only touch it when the
// form actually sends one, so list edits never wipe a description.
const fields: { name: string; frequencyDays: number; description?: string } =
{ name, frequencyDays };
const description = data.get('description');
if (description !== null) {
fields.description = description.toString();
}
try {
await edit(routineId, fields);
} catch {
return fail(400, { error: 'Routine introuvable' });
}
return { success: true, type: extra?.type };
}
export async function completeRoutineAction(data: FormData, routineId: number) {
const current = await loadCurrent(routineId);
if (!current) {
return fail(400, { error: 'Routine introuvable' });
}
if (current.archived) {
return fail(400, { error: 'Routine archivée' });
}
try {
await complete(
routineId,
parseFormString(data.get('note')),
new Date().toISOString(),
);
} catch {
return fail(400, { error: 'Routine introuvable' });
}
return { success: true };
}
export async function archiveRoutineAction(routineId: number) {
try {
await archive(routineId);
} catch {
return fail(400, { error: 'Routine introuvable' });
}
return { success: true };
}
|