feat(expenses): add edit amount and due date for pending expenses

This commit is contained in:
Jose Selesan
2026-06-04 11:00:36 -03:00
parent 5b6ccf0fa6
commit 55098a7b95
10 changed files with 366 additions and 11 deletions

View File

@@ -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);