import { ApprovalStatus, ApprovalType, ExpenseRecurrence, PrismaClient, Role } from "@prisma/client"; import bcrypt from "bcryptjs"; const prisma = new PrismaClient(); const APPROVAL_THRESHOLD = 50; async function upsertAppSettings() { await prisma.appSettings.upsert({ where: { id: "global" }, update: { approvalThreshold: APPROVAL_THRESHOLD }, create: { id: "global", approvalThreshold: APPROVAL_THRESHOLD } }); } async function upsertCurrentPeriod() { const year = new Date().getFullYear(); const startsAt = new Date(Date.UTC(year, 0, 1)); const endsAt = new Date(Date.UTC(year, 11, 31, 23, 59, 59)); await prisma.accountingPeriod.updateMany({ data: { isCurrent: false } }); return prisma.accountingPeriod.upsert({ where: { name: `Haushalt ${year}` }, update: { startsAt, endsAt, isCurrent: true }, create: { name: `Haushalt ${year}`, startsAt, endsAt, isCurrent: true } }); } async function upsertWorkingGroup(name: string) { return prisma.workingGroup.upsert({ where: { name }, update: {}, create: { name } }); } async function upsertBudget( workingGroupId: string, periodId: string, name: string, totalBudget: number, colorCode: string, releasedAmount = 0 ) { return prisma.budget.upsert({ where: { workingGroupId_periodId_name: { workingGroupId, periodId, name } }, update: { totalBudget, releasedAmount, colorCode }, create: { name, totalBudget, releasedAmount, colorCode, workingGroupId, periodId } }); } async function upsertUser(input: { username: string; role: Role; passwordHash: string; workingGroupId?: string | null; approvalPermissions: ApprovalType[]; }) { const approvalPreference = input.approvalPermissions[0] ?? null; await prisma.user.upsert({ where: { username: input.username }, update: { name: input.username, username: input.username, email: null, passwordHash: input.passwordHash, role: input.role, approvalPreference, approvalPermissions: input.approvalPermissions, workingGroupId: input.workingGroupId ?? null }, create: { name: input.username, username: input.username, email: null, passwordHash: input.passwordHash, role: input.role, approvalPreference, approvalPermissions: input.approvalPermissions, workingGroupId: input.workingGroupId ?? null } }); } async function main() { const initialPassword = process.env.SEED_INITIAL_PASSWORD?.trim() || "Bitte-sofort-aendern-2026!"; const passwordHash = await bcrypt.hash(initialPassword, 12); await upsertAppSettings(); const currentPeriod = await upsertCurrentPeriod(); const deko = await upsertWorkingGroup("AG Deko"); const awareness = await upsertWorkingGroup("AG Awareness"); const technik = await upsertWorkingGroup("AG Technik"); const orga = await upsertWorkingGroup("AG Orga"); const finanzen = await upsertWorkingGroup("AG Finanzen"); await upsertBudget(deko.id, currentPeriod.id, "Deko Hauptbudget", 0, "#FFB94A", 0); await upsertBudget(awareness.id, currentPeriod.id, "Awareness Hauptbudget", 800, "#68A35D", 250); await upsertBudget(technik.id, currentPeriod.id, "Technik Infrastruktur", 1500, "#5677F6", 500); await upsertUser({ username: "vorstand", role: Role.BOARD, passwordHash, approvalPermissions: [ApprovalType.CHAIR_B] }); await upsertUser({ username: "orga", role: Role.ORGA, passwordHash, workingGroupId: orga.id, approvalPermissions: [ApprovalType.CHAIR_A] }); await upsertUser({ username: "finanzen", role: Role.FINANCE, passwordHash, workingGroupId: finanzen.id, approvalPermissions: [ApprovalType.FINANCE] }); await upsertUser({ username: "deko", role: Role.MEMBER, passwordHash, workingGroupId: deko.id, approvalPermissions: [] }); await upsertUser({ username: "technik", role: Role.MEMBER, passwordHash, workingGroupId: technik.id, approvalPermissions: [] }); } main() .then(async () => { await prisma.$disconnect(); }) .catch(async (error) => { console.error(error); await prisma.$disconnect(); process.exit(1); });