feat(expenses): add monthly totals and pagination support for expenses
This commit is contained in:
@@ -84,16 +84,27 @@ export async function generateMonthlyExpense(id: number) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function listExpenses(status?: string) {
|
||||
export async function listExpenses(params: {
|
||||
status?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}) {
|
||||
const { status, page = 1, pageSize = 10 } = params;
|
||||
const where: Record<string, unknown> = {};
|
||||
if (status === "PENDING" || status === "PAYED") {
|
||||
where.status = status;
|
||||
}
|
||||
return prisma.expense.findMany({
|
||||
where,
|
||||
include: { periodicExpense: true },
|
||||
orderBy: { dueDate: "asc" },
|
||||
});
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.expense.findMany({
|
||||
where,
|
||||
include: { periodicExpense: true },
|
||||
orderBy: { dueDate: "desc" },
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
}),
|
||||
prisma.expense.count({ where }),
|
||||
]);
|
||||
return { data, total, page, pageSize };
|
||||
}
|
||||
|
||||
export async function createNonPeriodicExpense(data: z.infer<typeof createNonPeriodicExpenseSchema>) {
|
||||
@@ -126,6 +137,32 @@ export async function payExpense(id: number, data: z.infer<typeof payExpenseSche
|
||||
});
|
||||
}
|
||||
|
||||
export async function getMonthlyPayedTotal(year: number, month: number) {
|
||||
const expenses = await prisma.expense.findMany({
|
||||
where: { year, month, status: PaymentStatus.PAYED },
|
||||
select: { amountPayed: true },
|
||||
});
|
||||
const total = expenses.reduce((sum, e) => sum + Number(e.amountPayed ?? 0), 0);
|
||||
return { total };
|
||||
}
|
||||
|
||||
export async function getMonthlyTotals(year: number, month: number) {
|
||||
const [payed, pending] = await Promise.all([
|
||||
prisma.expense.findMany({
|
||||
where: { year, month, status: PaymentStatus.PAYED },
|
||||
select: { amountPayed: true },
|
||||
}),
|
||||
prisma.expense.findMany({
|
||||
where: { year, month, status: PaymentStatus.PENDING },
|
||||
select: { amount: true },
|
||||
}),
|
||||
]);
|
||||
return {
|
||||
payed: payed.reduce((sum, e) => sum + Number(e.amountPayed ?? 0), 0),
|
||||
pending: pending.reduce((sum, e) => sum + Number(e.amount), 0),
|
||||
};
|
||||
}
|
||||
|
||||
export async function generateExpensesForCurrentMonth() {
|
||||
const { year, month } = getCurrentYearMonthUTC();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user