feat: settings and CSV export

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
YugonitoandClaude Opus 5.5 committed 2026-10-27 18:33:37 +01:00
1 parent 9cb7fe8c81
commit 8201496bb6
8 files changed
+528

No files matched your search

+62
View File
@@ -0,0 +1,62 @@
import { Prisma } from "@prisma/client";
import { validateSplit } from "@/domain/money";
import type { OpKind, Split } from "@/domain/types";
import { hashPassword, validatePassword, verifyPassword } from "../auth";
import { prisma } from "../db";
import type { Result } from "./operations";
const isUniqueViolation = (e: unknown) => e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2002";
export async function updateUser(id: string, input: { firstName: string; color: string; login: string }): Promise<Result> {
const firstName = input.firstName.trim();
const login = input.login.trim().toLowerCase();
if (!firstName) return { ok: false, error: "Le prénom est obligatoire." };
if (!/^[a-z0-9._-]+$/.test(login)) return { ok: false, error: "Identifiant invalide (lettres, chiffres, . _ -)." };
if (!/^#[0-9a-f]{6}$/i.test(input.color)) return { ok: false, error: "Couleur invalide." };
try {
await prisma.user.update({ where: { id }, data: { firstName, login, color: input.color } });
} catch (e) {
if (isUniqueViolation(e)) return { ok: false, error: "Cet identifiant est déjà utilisé." };
throw e;
}
return { ok: true };
}
export async function changePassword(id: string, current: string, next: string): Promise<Result> {
const user = await prisma.user.findUniqueOrThrow({ where: { id } });
if (!(await verifyPassword(current, user.passwordHash))) return { ok: false, error: "Mot de passe actuel incorrect." };
const error = validatePassword(next, next);
if (error) return { ok: false, error };
await prisma.user.update({ where: { id }, data: { passwordHash: await hashPassword(next) } });
return { ok: true };
}
export async function setDefaultSplit(split: Split): Promise<Result> {
const error = validateSplit(split);
if (error) return { ok: false, error };
await prisma.setting.upsert({ where: { id: 1 }, update: { defaultSplit: split }, create: { id: 1, defaultSplit: split } });
return { ok: true };
}
export async function saveCategory(input: { id?: string; name: string; emoji: string; kind: OpKind }): Promise<Result<{ id: string }>> {
const name = input.name.trim();
if (!name) return { ok: false, error: "Le nom est obligatoire." };
const data = { name, emoji: input.emoji.trim() || "•", kind: input.kind };
try {
if (input.id) {
await prisma.category.update({ where: { id: input.id }, data });
return { ok: true, id: input.id };
}
const last = await prisma.category.aggregate({ _max: { sortOrder: true } });
const created = await prisma.category.create({ data: { ...data, sortOrder: (last._max.sortOrder ?? 0) + 1 } });
return { ok: true, id: created.id };
} catch (e) {
if (isUniqueViolation(e)) return { ok: false, error: "Cette catégorie existe déjà." };
throw e;
}
}
export async function archiveCategory(id: string, archived: boolean): Promise<Result> {
await prisma.category.update({ where: { id }, data: { archived } });
return { ok: true };
}