Files
2026-10-27 18:12:41 +01:00

28 lines
1.2 KiB
TypeScript

import { prisma } from "@/server/db";
import { seed } from "../../prisma/seed";
const TABLES = [
"OperationSplit", "Operation", "RecurringSplit", "Recurring", "LoanSplit", "Loan",
"Settlement", "Budget", "Category", "Setting", "User",
];
/** Vide la base de test et remet les catégories de départ. */
export async function resetDb() {
if (!process.env.DATABASE_URL?.includes("_test")) throw new Error("resetDb refuse de tourner hors base de test");
await prisma.$executeRawUnsafe("SET FOREIGN_KEY_CHECKS = 0");
for (const t of TABLES) await prisma.$executeRawUnsafe(`TRUNCATE TABLE \`${t}\``);
await prisma.$executeRawUnsafe("SET FOREIGN_KEY_CHECKS = 1");
await seed(prisma);
}
export async function makeUsers() {
const h = await prisma.user.create({ data: { firstName: "H", login: "h", passwordHash: "x", color: "#4f46e5" } });
const c = await prisma.user.create({ data: { firstName: "C", login: "c", passwordHash: "x", color: "#db2777" } });
await prisma.setting.update({ where: { id: 1 }, data: { defaultSplit: { [h.id]: 5000, [c.id]: 5000 } } });
return { h, c };
}
export async function categoryId(name: string) {
return (await prisma.category.findFirstOrThrow({ where: { name } })).id;
}