feat: quick entry form and operation/settlement actions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
YugonitoandClaude Opus 5.5 committed 2026-10-27 18:17:43 +01:00
1 parent 08f23f909f
commit 2860f2f776
8 files changed
+859 -2

No files matched your search

+142
View File
@@ -0,0 +1,142 @@
import { beforeEach, describe, expect, it } from "vitest";
import { prisma } from "@/server/db";
import {
confirmOperation,
deleteOperation,
saveOperation,
saveSettlement,
skipOperation,
type OperationInput,
} from "@/server/services/operations";
import { syncRecurrings } from "@/server/sync";
import { categoryId, makeUsers, resetDb } from "./helpers";
let H: string;
let C: string;
let courses: string;
let salaire: string;
const expense = (over: Partial<OperationInput> = {}): OperationInput => ({
kind: "EXPENSE",
amount: "86,40",
label: "Carrefour",
categoryId: courses,
date: "2026-10-27",
accountUserId: C,
status: "ACTUAL",
split: { [H]: 5000, [C]: 5000 },
...over,
});
beforeEach(async () => {
await resetDb();
const { h, c } = await makeUsers();
H = h.id;
C = c.id;
courses = await categoryId("Courses");
salaire = await categoryId("Salaire");
});
describe("saveOperation", () => {
it("stores a French amount with its split", async () => {
const r = await saveOperation(expense(), H);
expect(r).toMatchObject({ ok: true });
const op = await prisma.operation.findFirstOrThrow({ include: { splits: true } });
expect(op.amount.toString()).toBe("86.4");
expect(op.createdById).toBe(H);
expect(op.splits).toHaveLength(2);
});
it("rejects a split that does not sum to 100 %", async () => {
expect(await saveOperation(expense({ split: { [H]: 3333, [C]: 3333 } }), H)).toEqual({
ok: false,
error: "La répartition doit totaliser 100 %.",
});
});
it("rejects a zero amount", async () => {
expect(await saveOperation(expense({ amount: "0" }), H)).toEqual({ ok: false, error: "Montant invalide." });
});
it("rejects an allocation on an expense", async () => {
const r = await saveOperation(expense({ allocatedToRecurringId: "x", allocationMode: "REDUCE_SHARED" }), H);
expect(r).toEqual({ ok: false, error: "Seule une entrée peut être affectée à une charge." });
});
it("replaces splits on update", async () => {
const r = await saveOperation(expense(), H);
if (!r.ok) throw new Error(r.error);
await saveOperation(expense({ id: r.id, split: { [H]: 0, [C]: 10000 } }), H);
const splits = await prisma.operationSplit.findMany({ where: { operationId: r.id }, orderBy: { basisPoints: "asc" } });
expect(splits.map((s) => s.basisPoints)).toEqual([0, 10000]);
});
it("keeps an allocated income when its target expense is deleted", async () => {
const hotel = await saveOperation(expense({ label: "Hôtel", amount: "150" }), H);
if (!hotel.ok) throw new Error(hotel.error);
const refund = await saveOperation(
{
kind: "INCOME",
amount: "150",
label: "Note de frais",
categoryId: salaire,
date: "2026-10-28",
accountUserId: H,
status: "ACTUAL",
split: { [H]: 5000, [C]: 5000 },
allocatedToOperationId: hotel.id,
allocationMode: "REDUCE_SHARED",
},
H,
);
if (!refund.ok) throw new Error(refund.error);
await deleteOperation(hotel.id);
const kept = await prisma.operation.findUniqueOrThrow({ where: { id: refund.id } });
expect(kept.allocatedToOperationId).toBeNull();
});
it("creates a recurring template with this operation as its first occurrence", async () => {
const r = await saveOperation(expense({ makeRecurring: { frequency: "MONTHLY", endDate: null } }), H);
if (!r.ok) throw new Error(r.error);
const rec = await prisma.recurring.findFirstOrThrow({ include: { splits: true } });
expect(rec.dayOfMonth).toBe(27);
expect(rec.splits).toHaveLength(2);
const op = await prisma.operation.findUniqueOrThrow({ where: { id: r.id } });
expect(op.recurringId).toBe(rec.id);
expect(op.occurrenceDate?.toISOString().slice(0, 10)).toBe("2026-10-27");
expect(await syncRecurrings("2026-10-27")).toBe(11);
});
});
describe("confirm / skip", () => {
it("confirms a planned operation with an adjusted amount and date", async () => {
const r = await saveOperation(expense({ status: "PLANNED" }), H);
if (!r.ok) throw new Error(r.error);
expect(await confirmOperation(r.id, "90", "2026-10-29")).toEqual({ ok: true });
const op = await prisma.operation.findUniqueOrThrow({ where: { id: r.id } });
expect(op.status).toBe("ACTUAL");
expect(op.amount.toString()).toBe("90");
expect(op.date.toISOString().slice(0, 10)).toBe("2026-10-29");
});
it("marks a recurring occurrence skipped but deletes a one-off", async () => {
const rec = await saveOperation(expense({ status: "PLANNED", makeRecurring: { frequency: "MONTHLY", endDate: null } }), H);
const oneOff = await saveOperation(expense({ status: "PLANNED" }), H);
if (!rec.ok || !oneOff.ok) throw new Error("setup");
await skipOperation(rec.id);
await skipOperation(oneOff.id);
expect((await prisma.operation.findUniqueOrThrow({ where: { id: rec.id } })).status).toBe("SKIPPED");
expect(await prisma.operation.findUnique({ where: { id: oneOff.id } })).toBeNull();
});
});
describe("saveSettlement", () => {
it("records a repayment and refuses one to oneself", async () => {
expect(await saveSettlement({ fromUserId: C, toUserId: H, amount: "450", date: "2026-10-27" })).toEqual({ ok: true });
expect(await prisma.settlement.count()).toBe(1);
expect(await saveSettlement({ fromUserId: H, toUserId: H, amount: "10", date: "2026-10-27" })).toEqual({
ok: false,
error: "Choisissez deux personnes différentes.",
});
});
});