aboutsummaryrefslogtreecommitdiff
path: root/src/lib/server/services/grocery-actions.ts
blob: cedb46eba5f324c67fbb976bcc211c0b3c09fa3b (plain) (blame)
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
87
88

import { fail, redirect, type ActionFailure } from '@sveltejs/kit';
import { addOrUncheck, loadUnchecked, toggle } from './groceries';
import { add as addCategory } from './categories';
import { numericId, parseFormString, parseOptionalNumber } from './validation';
import { isUniqueViolation } from '$lib/server/db/errors';

export async function toggleGroceryAction(request: Request) {
	const data = await request.formData();
	try {
		const id = numericId(data.get('id'));
		await toggle(id);
	} catch {
		return fail(400, { error: 'ID invalide' });
	}
	return { success: true };
}

export type RowActionResult =
	ActionFailure<{ error: string }> | { success: boolean; type?: string };

/** Shared 'run fn with the id form field' action for row-based lists. */
export async function idAction(
	request: Request,
	fn: (id: number) => Promise<unknown>,
	extra?: { type: string },
): Promise<RowActionResult> {
	const data = await request.formData();
	try {
		await fn(numericId(data.get('id')));
	} catch {
		return fail(400, { error: 'ID invalide' });
	}
	return { success: true, type: extra?.type };
}

/** Parses the required 'name' field of an add form, or returns the failure. */
function requireFormName(
	data: FormData,
): string | ActionFailure<{ error: string }> {
	const name = parseFormString(data.get('name'));
	if (!name) {
		return fail(400, { error: 'Le nom est requis' });
	}
	return name;
}

export async function addCategoryAction(request: Request) {
	const data = await request.formData();
	const name = requireFormName(data);
	if (typeof name !== 'string') {
		return name;
	}

	try {
		await addCategory(name);
	} catch (error) {
		if (isUniqueViolation(error)) {
			return fail(400, { error: 'Cette catégorie existe déjà' });
		}
		throw error;
	}
	return { success: true };
}

export async function addGroceryAction(request: Request) {
	const data = await request.formData();
	const name = requireFormName(data);
	if (typeof name !== 'string') {
		return name;
	}
	const categoryId = parseOptionalNumber(data.get('category_id'));

	await addOrUncheck(name, categoryId);
	const { items } = await loadUnchecked();
	return { success: true, items };
}

export async function addGroceryAndEditAction(request: Request) {
	const data = await request.formData();
	const name = requireFormName(data);
	if (typeof name !== 'string') {
		return name;
	}
	const categoryId = parseOptionalNumber(data.get('category_id'));

	const { item } = await addOrUncheck(name, categoryId);
	throw redirect(303, `/grocery/${item.id}`);
}