feat(expenses): add edit amount and due date for pending expenses
This commit is contained in:
@@ -12,6 +12,7 @@ import { listExpensesHandler } from "./handlers/listExpenses";
|
||||
import { listPeriodicExpensesHandler } from "./handlers/listPeriodicExpenses";
|
||||
import { payExpenseHandler } from "./handlers/payExpense";
|
||||
import { softDeletePeriodicExpenseHandler } from "./handlers/softDeletePeriodicExpense";
|
||||
import { updateExpenseHandler } from "./handlers/updateExpense";
|
||||
import { updatePeriodicExpenseHandler } from "./handlers/updatePeriodicExpense";
|
||||
|
||||
const app = new Hono();
|
||||
@@ -34,5 +35,6 @@ app.get("/expenses", listExpensesHandler);
|
||||
app.post("/expenses", createNonPeriodicExpenseHandler);
|
||||
app.post("/expenses/import", importExpensesHandler);
|
||||
app.put("/expenses/:id/pay", payExpenseHandler);
|
||||
app.put("/expenses/:id", updateExpenseHandler);
|
||||
|
||||
export default app;
|
||||
|
||||
@@ -23,6 +23,11 @@ export const payExpenseSchema = z.object({
|
||||
paymentDate: z.string().datetime().optional(),
|
||||
});
|
||||
|
||||
export const updateExpenseSchema = z.object({
|
||||
amount: z.number().positive("El monto debe ser mayor a 0"),
|
||||
dueDate: z.string().datetime({ message: "La fecha debe ser una fecha ISO válida" }),
|
||||
});
|
||||
|
||||
function lastDayOfMonth(year: number, month: number): number {
|
||||
return new Date(year, month, 0).getDate();
|
||||
}
|
||||
@@ -187,6 +192,32 @@ export async function payExpense(
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function updateExpense(
|
||||
id: number,
|
||||
data: z.infer<typeof updateExpenseSchema>,
|
||||
) {
|
||||
const existing = await prisma.expense.findUniqueOrThrow({ where: { id } });
|
||||
if (existing.status !== PaymentStatus.PENDING) {
|
||||
throw new Error("Solo se pueden editar gastos pendientes");
|
||||
}
|
||||
|
||||
const dueDate = new Date(data.dueDate);
|
||||
const year = dueDate.getUTCFullYear();
|
||||
const month = dueDate.getUTCMonth() + 1;
|
||||
|
||||
const result = await prisma.expense.update({
|
||||
where: { id },
|
||||
data: {
|
||||
amount: data.amount,
|
||||
dueDate,
|
||||
year,
|
||||
month,
|
||||
},
|
||||
});
|
||||
cache.invalidateByPrefix("expenses:");
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getMonthlyPayedTotal(year: number, month: number) {
|
||||
const cacheKey = `expenses:monthly-total:${year}:${month}`;
|
||||
const cached = cache.get(cacheKey);
|
||||
|
||||
10
apps/backend/src/modules/expenses/handlers/updateExpense.ts
Normal file
10
apps/backend/src/modules/expenses/handlers/updateExpense.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { Context } from "hono";
|
||||
import { updateExpense, updateExpenseSchema } from "../expenses.service";
|
||||
|
||||
export async function updateExpenseHandler(c: Context) {
|
||||
const id = Number(c.req.param("id"));
|
||||
const body = await c.req.json();
|
||||
const parsed = updateExpenseSchema.parse(body);
|
||||
const result = await updateExpense(id, parsed);
|
||||
return c.json(result);
|
||||
}
|
||||
@@ -11,6 +11,7 @@ const mockPrisma = {
|
||||
expense: {
|
||||
findMany: mock(),
|
||||
findFirst: mock(),
|
||||
findUniqueOrThrow: mock(),
|
||||
create: mock(),
|
||||
update: mock(),
|
||||
count: mock(),
|
||||
@@ -36,6 +37,7 @@ const {
|
||||
createPeriodicExpenseSchema,
|
||||
createNonPeriodicExpenseSchema,
|
||||
payExpenseSchema,
|
||||
updateExpenseSchema,
|
||||
buildDueDate,
|
||||
getCurrentYearMonthUTC,
|
||||
listPeriodicExpenses,
|
||||
@@ -45,6 +47,7 @@ const {
|
||||
listExpenses,
|
||||
createNonPeriodicExpense,
|
||||
payExpense,
|
||||
updateExpense,
|
||||
generateExpensesForCurrentMonth,
|
||||
generateMonthlyExpense,
|
||||
} = await import("../src/modules/expenses/expenses.service");
|
||||
@@ -179,6 +182,35 @@ describe("schemas", () => {
|
||||
expect(() => payExpenseSchema.parse({ amountPayed: 0 })).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateExpenseSchema", () => {
|
||||
test("accepts valid input", () => {
|
||||
const result = updateExpenseSchema.parse({
|
||||
amount: 2500,
|
||||
dueDate: "2026-07-15T00:00:00.000Z",
|
||||
});
|
||||
expect(result.amount).toBe(2500);
|
||||
expect(result.dueDate).toBe("2026-07-15T00:00:00.000Z");
|
||||
});
|
||||
|
||||
test("rejects non-positive amount", () => {
|
||||
expect(() =>
|
||||
updateExpenseSchema.parse({
|
||||
amount: 0,
|
||||
dueDate: "2026-07-15T00:00:00.000Z",
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
test("rejects invalid dueDate", () => {
|
||||
expect(() =>
|
||||
updateExpenseSchema.parse({
|
||||
amount: 100,
|
||||
dueDate: "not-a-date",
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildDueDate", () => {
|
||||
@@ -430,6 +462,45 @@ describe("payExpense", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateExpense", () => {
|
||||
test("updates amount and dueDate for a PENDING expense", async () => {
|
||||
const existing = {
|
||||
id: 1,
|
||||
status: PaymentStatus.PENDING,
|
||||
amount: 1500,
|
||||
dueDate: new Date("2026-06-15T00:00:00.000Z"),
|
||||
year: 2026,
|
||||
month: 6,
|
||||
};
|
||||
mockPrisma.expense.findUniqueOrThrow.mockResolvedValueOnce(existing);
|
||||
mockPrisma.expense.update.mockImplementationOnce(
|
||||
async ({ where, data }) => ({ id: where.id, ...existing, ...data }),
|
||||
);
|
||||
|
||||
const result = await updateExpense(1, {
|
||||
amount: 2000,
|
||||
dueDate: "2026-07-20T00:00:00.000Z",
|
||||
});
|
||||
expect(result.amount).toBe(2000);
|
||||
expect(result.year).toBe(2026);
|
||||
expect(result.month).toBe(7);
|
||||
});
|
||||
|
||||
test("rejects update for PAYED expense", async () => {
|
||||
mockPrisma.expense.findUniqueOrThrow.mockResolvedValueOnce({
|
||||
id: 1,
|
||||
status: PaymentStatus.PAYED,
|
||||
});
|
||||
|
||||
expect(
|
||||
updateExpense(1, {
|
||||
amount: 100,
|
||||
dueDate: "2026-07-15T00:00:00.000Z",
|
||||
}),
|
||||
).rejects.toThrow("Solo se pueden editar gastos pendientes");
|
||||
});
|
||||
});
|
||||
|
||||
describe("generateExpensesForCurrentMonth", () => {
|
||||
test("skips existing expenses to avoid duplicates", async () => {
|
||||
const { month } = getCurrentYearMonthUTC();
|
||||
|
||||
Reference in New Issue
Block a user