removed unneccesary files
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { FastifyPluginAsync, FastifyInstance } from "fastify";
|
||||
import type { FastifyPluginAsync } from "fastify";
|
||||
import type { Prisma, PrismaClient } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
import { ensureBudgetSessionAvailableSynced } from "../services/budget-session.js";
|
||||
|
||||
type RateLimitRouteOptions = {
|
||||
config: {
|
||||
@@ -76,52 +77,6 @@ async function assertPercentTotal(
|
||||
}
|
||||
}
|
||||
|
||||
async function getLatestBudgetSession(app: FastifyInstance, userId: string) {
|
||||
return app.prisma.budgetSession.findFirst({
|
||||
where: { userId },
|
||||
orderBy: { periodStart: "desc" },
|
||||
});
|
||||
}
|
||||
|
||||
async function ensureBudgetSession(
|
||||
app: FastifyInstance,
|
||||
userId: string,
|
||||
fallbackAvailableCents = 0
|
||||
) {
|
||||
const existing = await getLatestBudgetSession(app, userId);
|
||||
if (existing) return existing;
|
||||
|
||||
const now = new Date();
|
||||
const start = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1, 0, 0, 0, 0));
|
||||
const end = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() + 1, 1, 0, 0, 0, 0));
|
||||
|
||||
return app.prisma.budgetSession.create({
|
||||
data: {
|
||||
userId,
|
||||
periodStart: start,
|
||||
periodEnd: end,
|
||||
totalBudgetCents: BigInt(Math.max(0, fallbackAvailableCents)),
|
||||
allocatedCents: 0n,
|
||||
fundedCents: 0n,
|
||||
availableCents: BigInt(Math.max(0, fallbackAvailableCents)),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function ensureBudgetSessionAvailableSynced(
|
||||
app: FastifyInstance,
|
||||
userId: string,
|
||||
availableCents: number
|
||||
) {
|
||||
const normalizedAvailableCents = BigInt(Math.max(0, Math.trunc(availableCents)));
|
||||
const session = await ensureBudgetSession(app, userId, Number(normalizedAvailableCents));
|
||||
if ((session.availableCents ?? 0n) === normalizedAvailableCents) return session;
|
||||
return app.prisma.budgetSession.update({
|
||||
where: { id: session.id },
|
||||
data: { availableCents: normalizedAvailableCents },
|
||||
});
|
||||
}
|
||||
|
||||
const variableCategoriesRoutes: FastifyPluginAsync<VariableCategoriesRoutesOptions> = async (
|
||||
app,
|
||||
opts
|
||||
@@ -132,15 +87,7 @@ const variableCategoriesRoutes: FastifyPluginAsync<VariableCategoriesRoutesOptio
|
||||
return reply.code(400).send({ message: "Invalid payload" });
|
||||
}
|
||||
const userId = req.userId;
|
||||
const userTimezone =
|
||||
(
|
||||
await app.prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { timezone: true },
|
||||
})
|
||||
)?.timezone ?? "America/New_York";
|
||||
const normalizedName = parsed.data.name.trim().toLowerCase();
|
||||
void userTimezone;
|
||||
|
||||
return await app.prisma.$transaction(async (tx) => {
|
||||
try {
|
||||
@@ -169,18 +116,10 @@ const variableCategoriesRoutes: FastifyPluginAsync<VariableCategoriesRoutesOptio
|
||||
}
|
||||
const id = String((req.params as any).id);
|
||||
const userId = req.userId;
|
||||
const userTimezone =
|
||||
(
|
||||
await app.prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { timezone: true },
|
||||
})
|
||||
)?.timezone ?? "America/New_York";
|
||||
const updateData = {
|
||||
...patch.data,
|
||||
...(patch.data.name !== undefined ? { name: patch.data.name.trim().toLowerCase() } : {}),
|
||||
};
|
||||
void userTimezone;
|
||||
|
||||
return await app.prisma.$transaction(async (tx) => {
|
||||
const exists = await tx.variableCategory.findFirst({
|
||||
@@ -271,7 +210,7 @@ const variableCategoriesRoutes: FastifyPluginAsync<VariableCategoriesRoutesOptio
|
||||
select: { id: true, name: true, percent: true, isSavings: true, balanceCents: true },
|
||||
});
|
||||
const totalBalance = cats.reduce((s, c) => s + Number(c.balanceCents ?? 0n), 0);
|
||||
await ensureBudgetSessionAvailableSynced(app, userId, totalBalance);
|
||||
await ensureBudgetSessionAvailableSynced(app.prisma, userId, totalBalance);
|
||||
|
||||
return reply.send({
|
||||
ok: true,
|
||||
@@ -296,7 +235,7 @@ const variableCategoriesRoutes: FastifyPluginAsync<VariableCategoriesRoutesOptio
|
||||
|
||||
const totalBalance = cats.reduce((s, c) => s + Number(c.balanceCents ?? 0n), 0);
|
||||
const availableCents = totalBalance;
|
||||
await ensureBudgetSessionAvailableSynced(app, userId, availableCents);
|
||||
await ensureBudgetSessionAvailableSynced(app.prisma, userId, availableCents);
|
||||
|
||||
const targetMap = new Map<string, number>();
|
||||
for (const t of parsed.data.targets) {
|
||||
|
||||
Reference in New Issue
Block a user