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 } });
|
||||
|
||||
Reference in New Issue
Block a user