Files
SkyMoney/api/prisma/seed.ts

85 lines
2.7 KiB
TypeScript

/// <reference types="node" />
import { PrismaClient } from "@prisma/client";
import { allocateIncome } from "../src/allocator.ts"; // adjust if your path differs
const prisma = new PrismaClient();
const cents = (dollars: number) => BigInt(Math.round(dollars * 100));
async function main() {
const userId = "demo-user-1"; // dev-only, string id per schema
// 1) User
await prisma.user.upsert({
where: { id: userId },
create: { id: userId, email: "demo@example.com" },
update: {},
});
// 2) Variable categories (sum = 100)
const categories = [
{ name: "Savings", percent: 40, isSavings: true, priority: 10 },
{ name: "Needs", percent: 40, isSavings: false, priority: 20 },
{ name: "Wants", percent: 20, isSavings: false, priority: 30 },
];
for (const c of categories) {
await prisma.variableCategory.upsert({
where: { userId_name: { userId, name: c.name } },
create: { userId, name: c.name, percent: c.percent, isSavings: c.isSavings, priority: c.priority, balanceCents: 0n },
update: { percent: c.percent, isSavings: c.isSavings, priority: c.priority },
});
}
// 3) Fixed plans
const today = new Date();
const monthStart = new Date(today.getFullYear(), today.getMonth(), 1);
const dueNext = new Date(today.getFullYear(), today.getMonth(), 1);
dueNext.setMonth(dueNext.getMonth() + 1);
const plans = [
{ name: "Rent", total: cents(1200), priority: 10, cycleStart: monthStart, dueOn: dueNext },
{ name: "Utilities", total: cents(300), priority: 20, cycleStart: monthStart, dueOn: dueNext },
];
for (const p of plans) {
await prisma.fixedPlan.upsert({
where: { userId_name: { userId, name: p.name } },
create: {
userId, name: p.name,
totalCents: p.total, fundedCents: 0n,
priority: p.priority, cycleStart: p.cycleStart, dueOn: p.dueOn,
fundingMode: "auto-on-deposit"
},
update: {
totalCents: p.total, priority: p.priority,
cycleStart: p.cycleStart, dueOn: p.dueOn
},
});
}
// 4) Seed income + allocate
const deposit = 2500; // dollars
const nowISO = new Date().toISOString();
const income = await prisma.incomeEvent.create({
data: { userId, postedAt: new Date(nowISO), amountCents: cents(deposit) },
select: { id: true },
});
const result = await allocateIncome(
prisma, // db
userId, // user id (string)
Math.round(deposit * 100), // depositCentsNum (number is fine)
nowISO, // ISO timestamp
income.id // incomeEventId (string)
);
console.log("Seed complete\n", JSON.stringify(result, null, 2));
}
main().catch((e) => {
console.error(e);
process.exit(1);
}).finally(async () => {
await prisma.$disconnect();
});