phase 5: fixed expense logic simplified and compacted
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
||||
import request from "supertest";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { toZonedTime, fromZonedTime } from "date-fns-tz";
|
||||
import appFactory from "./appFactory";
|
||||
import { prisma, ensureUser, resetUser, U, closePrisma } from "./helpers";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
@@ -16,15 +18,75 @@ beforeEach(async () => {
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
if (app) {
|
||||
await app.close();
|
||||
}
|
||||
await closePrisma();
|
||||
});
|
||||
|
||||
function getUserMidnightFromDateOnly(timezone: string, date: Date): Date {
|
||||
const zoned = toZonedTime(date, timezone);
|
||||
zoned.setHours(0, 0, 0, 0);
|
||||
return fromZonedTime(zoned, timezone);
|
||||
}
|
||||
|
||||
function calculateNextDueDateLikeServer(
|
||||
currentDueDate: Date,
|
||||
frequency: string,
|
||||
timezone: string = "UTC"
|
||||
): Date {
|
||||
const base = getUserMidnightFromDateOnly(timezone, currentDueDate);
|
||||
const zoned = toZonedTime(base, timezone);
|
||||
|
||||
switch (frequency) {
|
||||
case "weekly":
|
||||
zoned.setUTCDate(zoned.getUTCDate() + 7);
|
||||
break;
|
||||
case "biweekly":
|
||||
zoned.setUTCDate(zoned.getUTCDate() + 14);
|
||||
break;
|
||||
case "monthly": {
|
||||
const targetDay = zoned.getUTCDate();
|
||||
const nextMonth = zoned.getUTCMonth() + 1;
|
||||
const nextYear = zoned.getUTCFullYear() + Math.floor(nextMonth / 12);
|
||||
const nextMonthIndex = nextMonth % 12;
|
||||
const lastDay = new Date(
|
||||
Date.UTC(nextYear, nextMonthIndex + 1, 0)
|
||||
).getUTCDate();
|
||||
zoned.setUTCFullYear(nextYear, nextMonthIndex, Math.min(targetDay, lastDay));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return base;
|
||||
}
|
||||
|
||||
zoned.setUTCHours(0, 0, 0, 0);
|
||||
return fromZonedTime(zoned, timezone);
|
||||
}
|
||||
|
||||
describe("Payment-Triggered Rollover", () => {
|
||||
async function getUserTimezoneOrDefault() {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: U },
|
||||
select: { timezone: true },
|
||||
});
|
||||
return user?.timezone ?? "America/New_York";
|
||||
}
|
||||
|
||||
function postTransactionsWithCsrf() {
|
||||
const csrf = randomUUID().replace(/-/g, "");
|
||||
return request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`);
|
||||
}
|
||||
|
||||
it("advances due date for weekly frequency on payment", async () => {
|
||||
// Create a fixed plan with weekly frequency
|
||||
const plan = await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Weekly Subscription",
|
||||
totalCents: 1000n,
|
||||
@@ -38,14 +100,13 @@ describe("Payment-Triggered Rollover", () => {
|
||||
});
|
||||
|
||||
// Make payment
|
||||
const txRes = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
const txRes = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
occurredAtISO: "2025-11-27T12:00:00Z",
|
||||
kind: "fixed_payment",
|
||||
amountCents: 1000,
|
||||
planId: plan.id,
|
||||
isReconciled: true,
|
||||
});
|
||||
|
||||
if (txRes.status !== 200) {
|
||||
@@ -58,12 +119,19 @@ describe("Payment-Triggered Rollover", () => {
|
||||
const updated = await prisma.fixedPlan.findUnique({ where: { id: plan.id } });
|
||||
expect(updated?.fundedCents).toBe(0n);
|
||||
expect(updated?.currentFundedCents).toBe(0n);
|
||||
expect(updated?.dueOn.toISOString()).toBe("2025-12-08T00:00:00.000Z");
|
||||
const userTimezone = await getUserTimezoneOrDefault();
|
||||
const expectedDue = calculateNextDueDateLikeServer(
|
||||
new Date("2025-12-01T00:00:00Z"),
|
||||
"weekly",
|
||||
userTimezone
|
||||
);
|
||||
expect(updated?.dueOn.toISOString()).toBe(expectedDue.toISOString());
|
||||
});
|
||||
|
||||
it("advances due date for biweekly frequency on payment", async () => {
|
||||
const plan = await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Biweekly Bill",
|
||||
totalCents: 5000n,
|
||||
@@ -76,26 +144,32 @@ describe("Payment-Triggered Rollover", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const txRes = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
const txRes = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
occurredAtISO: "2025-11-27T12:00:00Z",
|
||||
kind: "fixed_payment",
|
||||
amountCents: 5000,
|
||||
planId: plan.id,
|
||||
isReconciled: true,
|
||||
});
|
||||
|
||||
expect(txRes.status).toBe(200);
|
||||
|
||||
const updated = await prisma.fixedPlan.findUnique({ where: { id: plan.id } });
|
||||
expect(updated?.fundedCents).toBe(0n);
|
||||
expect(updated?.dueOn.toISOString()).toBe("2025-12-15T00:00:00.000Z");
|
||||
const userTimezone = await getUserTimezoneOrDefault();
|
||||
const expectedDue = calculateNextDueDateLikeServer(
|
||||
new Date("2025-12-01T00:00:00Z"),
|
||||
"biweekly",
|
||||
userTimezone
|
||||
);
|
||||
expect(updated?.dueOn.toISOString()).toBe(expectedDue.toISOString());
|
||||
});
|
||||
|
||||
it("advances due date for monthly frequency on payment", async () => {
|
||||
const plan = await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Monthly Rent",
|
||||
totalCents: 100000n,
|
||||
@@ -108,27 +182,33 @@ describe("Payment-Triggered Rollover", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const txRes = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
const txRes = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
occurredAtISO: "2025-11-27T12:00:00Z",
|
||||
kind: "fixed_payment",
|
||||
amountCents: 100000,
|
||||
planId: plan.id,
|
||||
isReconciled: true,
|
||||
});
|
||||
|
||||
expect(txRes.status).toBe(200);
|
||||
|
||||
const updated = await prisma.fixedPlan.findUnique({ where: { id: plan.id } });
|
||||
expect(updated?.fundedCents).toBe(0n);
|
||||
expect(updated?.dueOn.toISOString()).toBe("2026-01-01T00:00:00.000Z");
|
||||
const userTimezone = await getUserTimezoneOrDefault();
|
||||
const expectedDue = calculateNextDueDateLikeServer(
|
||||
new Date("2025-12-01T00:00:00Z"),
|
||||
"monthly",
|
||||
userTimezone
|
||||
);
|
||||
expect(updated?.dueOn.toISOString()).toBe(expectedDue.toISOString());
|
||||
});
|
||||
|
||||
it("does not advance due date for one-time frequency", async () => {
|
||||
it("deletes one-time plan after payment", async () => {
|
||||
const originalDueDate = new Date("2025-12-01T00:00:00Z");
|
||||
const plan = await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "One-time Expense",
|
||||
totalCents: 2000n,
|
||||
@@ -141,28 +221,26 @@ describe("Payment-Triggered Rollover", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const txRes = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
const txRes = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
occurredAtISO: "2025-11-27T12:00:00Z",
|
||||
kind: "fixed_payment",
|
||||
amountCents: 2000,
|
||||
planId: plan.id,
|
||||
isReconciled: true,
|
||||
});
|
||||
|
||||
expect(txRes.status).toBe(200);
|
||||
|
||||
const updated = await prisma.fixedPlan.findUnique({ where: { id: plan.id } });
|
||||
expect(updated?.fundedCents).toBe(0n);
|
||||
// Due date should remain unchanged for one-time expenses
|
||||
expect(updated?.dueOn.toISOString()).toBe(originalDueDate.toISOString());
|
||||
expect(updated).toBeNull();
|
||||
});
|
||||
|
||||
it("does not advance due date when no frequency is set", async () => {
|
||||
it("treats null frequency as one-time and deletes plan after payment", async () => {
|
||||
const originalDueDate = new Date("2025-12-01T00:00:00Z");
|
||||
const plan = await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Manual Bill",
|
||||
totalCents: 3000n,
|
||||
@@ -175,27 +253,25 @@ describe("Payment-Triggered Rollover", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const txRes = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
const txRes = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
occurredAtISO: "2025-11-27T12:00:00Z",
|
||||
kind: "fixed_payment",
|
||||
amountCents: 3000,
|
||||
planId: plan.id,
|
||||
isReconciled: true,
|
||||
});
|
||||
|
||||
expect(txRes.status).toBe(200);
|
||||
|
||||
const updated = await prisma.fixedPlan.findUnique({ where: { id: plan.id } });
|
||||
expect(updated?.fundedCents).toBe(0n);
|
||||
// Due date should remain unchanged when no frequency
|
||||
expect(updated?.dueOn.toISOString()).toBe(originalDueDate.toISOString());
|
||||
expect(updated).toBeNull();
|
||||
});
|
||||
|
||||
it("prevents payment when insufficient funded amount", async () => {
|
||||
const plan = await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Underfunded Bill",
|
||||
totalCents: 10000n,
|
||||
@@ -209,18 +285,17 @@ describe("Payment-Triggered Rollover", () => {
|
||||
});
|
||||
|
||||
// Try to pay more than funded amount
|
||||
const txRes = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
const txRes = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
occurredAtISO: "2025-11-27T12:00:00Z",
|
||||
kind: "fixed_payment",
|
||||
amountCents: 10000,
|
||||
planId: plan.id,
|
||||
isReconciled: true,
|
||||
});
|
||||
|
||||
expect(txRes.status).toBe(400);
|
||||
expect(txRes.body.code).toBe("OVERDRAFT_PLAN");
|
||||
expect(txRes.body.code).toBe("INSUFFICIENT_AVAILABLE_BUDGET");
|
||||
|
||||
// Plan should remain unchanged
|
||||
const unchanged = await prisma.fixedPlan.findUnique({ where: { id: plan.id } });
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import request from "supertest";
|
||||
import { describe, it, expect, beforeAll, beforeEach, afterAll } from "vitest";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import appFactory from "./appFactory";
|
||||
import { prisma, resetUser, ensureUser, U, cid, pid, closePrisma } from "./helpers";
|
||||
import { prisma, resetUser, ensureUser, U, closePrisma } from "./helpers";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
@@ -11,7 +12,9 @@ beforeAll(async () => {
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
if (app) {
|
||||
await app.close();
|
||||
}
|
||||
await closePrisma();
|
||||
});
|
||||
|
||||
@@ -23,16 +26,22 @@ describe("GET /transactions", () => {
|
||||
await resetUser(U);
|
||||
await ensureUser(U);
|
||||
|
||||
catId = cid("c");
|
||||
planId = pid("p");
|
||||
|
||||
await prisma.variableCategory.create({
|
||||
data: { id: catId, userId: U, name: "Groceries", percent: 100, priority: 1, isSavings: false, balanceCents: 5000n },
|
||||
});
|
||||
|
||||
await prisma.fixedPlan.create({
|
||||
const category = await prisma.variableCategory.create({
|
||||
data: {
|
||||
id: planId,
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Groceries",
|
||||
percent: 100,
|
||||
priority: 1,
|
||||
isSavings: false,
|
||||
balanceCents: 5000n,
|
||||
},
|
||||
});
|
||||
catId = category.id;
|
||||
|
||||
const plan = await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Rent",
|
||||
totalCents: 10000n,
|
||||
@@ -43,6 +52,7 @@ describe("GET /transactions", () => {
|
||||
fundingMode: "auto-on-deposit",
|
||||
},
|
||||
});
|
||||
planId = plan.id;
|
||||
|
||||
await prisma.transaction.createMany({
|
||||
data: [
|
||||
@@ -100,16 +110,31 @@ describe("POST /transactions", () => {
|
||||
let catId: string;
|
||||
let planId: string;
|
||||
|
||||
function postTransactionsWithCsrf() {
|
||||
const csrf = randomUUID().replace(/-/g, "");
|
||||
return request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`);
|
||||
}
|
||||
|
||||
function patchWithCsrf(path: string) {
|
||||
const csrf = randomUUID().replace(/-/g, "");
|
||||
return request(app.server)
|
||||
.patch(path)
|
||||
.set("x-user-id", U)
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`);
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetUser(U);
|
||||
await ensureUser(U);
|
||||
|
||||
catId = cid("cat");
|
||||
planId = pid("plan");
|
||||
|
||||
await prisma.variableCategory.create({
|
||||
const category = await prisma.variableCategory.create({
|
||||
data: {
|
||||
id: catId,
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Dining",
|
||||
percent: 100,
|
||||
@@ -118,10 +143,11 @@ describe("POST /transactions", () => {
|
||||
balanceCents: 5000n,
|
||||
},
|
||||
});
|
||||
catId = category.id;
|
||||
|
||||
await prisma.fixedPlan.create({
|
||||
const plan = await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: planId,
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Loan",
|
||||
totalCents: 10000n,
|
||||
@@ -132,12 +158,11 @@ describe("POST /transactions", () => {
|
||||
fundingMode: "auto-on-deposit",
|
||||
},
|
||||
});
|
||||
planId = plan.id;
|
||||
});
|
||||
|
||||
it("spends from a variable category and updates balance", async () => {
|
||||
const res = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
const res = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
kind: "variable_spend",
|
||||
amountCents: 2000,
|
||||
@@ -157,10 +182,8 @@ describe("POST /transactions", () => {
|
||||
expect(tx.isReconciled).toBe(true);
|
||||
});
|
||||
|
||||
it("prevents overdrawing fixed plans", async () => {
|
||||
const res = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
it("returns insufficient budget when fixed payment requires unavailable top-up", async () => {
|
||||
const res = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
kind: "fixed_payment",
|
||||
amountCents: 400000, // exceeds funded
|
||||
@@ -169,13 +192,11 @@ describe("POST /transactions", () => {
|
||||
});
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.code).toBe("OVERDRAFT_PLAN");
|
||||
expect(res.body.code).toBe("INSUFFICIENT_AVAILABLE_BUDGET");
|
||||
});
|
||||
|
||||
it("updates note/receipt and reconciliation via patch", async () => {
|
||||
const created = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
const created = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
kind: "variable_spend",
|
||||
amountCents: 1000,
|
||||
@@ -185,9 +206,7 @@ describe("POST /transactions", () => {
|
||||
expect(created.status).toBe(200);
|
||||
const txId = created.body.id;
|
||||
|
||||
const res = await request(app.server)
|
||||
.patch(`/transactions/${txId}`)
|
||||
.set("x-user-id", U)
|
||||
const res = await patchWithCsrf(`/transactions/${txId}`)
|
||||
.send({
|
||||
note: "Cleared",
|
||||
isReconciled: true,
|
||||
|
||||
Reference in New Issue
Block a user