Files
RFP_Finanzen/prisma/seed.ts
2026-04-08 16:30:44 +02:00

251 lines
5.8 KiB
TypeScript

import { ApprovalStatus, ApprovalType, ExpenseRecurrence, PrismaClient, Role } from "@prisma/client";
import bcrypt from "bcryptjs";
const prisma = new PrismaClient();
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
) {
return prisma.budget.upsert({
where: {
workingGroupId_periodId_name: {
workingGroupId,
periodId,
name
}
},
update: {
totalBudget,
colorCode
},
create: {
name,
totalBudget,
colorCode,
workingGroupId,
periodId
}
});
}
async function main() {
const passwordHash = await bcrypt.hash("demo123!", 12);
const currentPeriod = await upsertCurrentPeriod();
const deko = await upsertWorkingGroup("AG Deko");
const awareness = await upsertWorkingGroup("AG Awareness");
const technik = await upsertWorkingGroup("AG Technik");
await upsertBudget(deko.id, currentPeriod.id, "Deko Hauptbudget", 0, "#FFB94A");
await upsertBudget(awareness.id, currentPeriod.id, "Awareness Hauptbudget", 800, "#68A35D");
const technikBudget = await upsertBudget(technik.id, currentPeriod.id, "Technik Infrastruktur", 1500, "#5677F6");
await prisma.user.upsert({
where: { username: "vorstand-a" },
update: {
name: "Admin 1",
username: "vorstand-a",
email: null,
passwordHash,
role: Role.ADMIN,
approvalPreference: ApprovalType.CHAIR_A,
workingGroupId: null
},
create: {
name: "Admin 1",
username: "vorstand-a",
email: null,
passwordHash,
role: Role.ADMIN,
approvalPreference: ApprovalType.CHAIR_A
}
});
await prisma.user.upsert({
where: { username: "vorstand-b" },
update: {
name: "Admin 2",
username: "vorstand-b",
email: null,
passwordHash,
role: Role.ADMIN,
approvalPreference: ApprovalType.CHAIR_B,
workingGroupId: null
},
create: {
name: "Admin 2",
username: "vorstand-b",
email: null,
passwordHash,
role: Role.ADMIN,
approvalPreference: ApprovalType.CHAIR_B
}
});
await prisma.user.upsert({
where: { username: "finanzen" },
update: {
name: "Finanz-AG",
username: "finanzen",
email: null,
passwordHash,
role: Role.FINANCE,
approvalPreference: ApprovalType.FINANCE,
workingGroupId: null
},
create: {
name: "Finanz-AG",
username: "finanzen",
email: null,
passwordHash,
role: Role.FINANCE,
approvalPreference: ApprovalType.FINANCE
}
});
await prisma.user.upsert({
where: { username: "deko" },
update: {
name: "Deko Mitglied",
username: "deko",
email: null,
passwordHash,
role: Role.MEMBER,
approvalPreference: null,
workingGroupId: deko.id
},
create: {
name: "Deko Mitglied",
username: "deko",
email: null,
passwordHash,
role: Role.MEMBER,
workingGroupId: deko.id
}
});
await prisma.user.upsert({
where: { username: "technik" },
update: {
name: "Technik Mitglied",
username: "technik",
email: null,
passwordHash,
role: Role.MEMBER,
approvalPreference: null,
workingGroupId: technik.id
},
create: {
name: "Technik Mitglied",
username: "technik",
email: null,
passwordHash,
role: Role.MEMBER,
workingGroupId: technik.id
}
});
const existingExpense = await prisma.expense.findFirst({
where: { title: "Muster: Kabelbinder" }
});
if (!existingExpense) {
const technikUser = await prisma.user.findUniqueOrThrow({
where: { username: "technik" }
});
await prisma.expense.create({
data: {
title: "Muster: Kabelbinder",
description: "Beispiel fuer einen automatisch freigegebenen Infrastrukturposten.",
amount: 24.5,
creatorId: technikUser.id,
agId: technik.id,
budgetId: technikBudget.id,
periodId: currentPeriod.id,
approvalStatus: ApprovalStatus.APPROVED,
proofUrl: null,
recurrence: ExpenseRecurrence.NONE
}
});
}
const existingSubscription = await prisma.expense.findFirst({
where: { title: "Muster: Vereinssoftware Abo" }
});
if (!existingSubscription) {
const financeUser = await prisma.user.findUniqueOrThrow({
where: { username: "finanzen" }
});
await prisma.expense.create({
data: {
title: "Muster: Vereinssoftware Abo",
description: "Monatlich wiederkehrendes Beispielabo fuer die Uebersicht.",
amount: 19,
creatorId: financeUser.id,
agId: technik.id,
budgetId: technikBudget.id,
periodId: currentPeriod.id,
approvalStatus: ApprovalStatus.APPROVED,
proofUrl: null,
recurrence: ExpenseRecurrence.MONTHLY
}
});
}
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (error) => {
console.error(error);
await prisma.$disconnect();
process.exit(1);
});