feat: improve bank card expenses summary

This commit is contained in:
2026-09-12 20:54:57 +03:00
parent 9a65687d38
commit d3ac8c242d
7 changed files with 227 additions and 81 deletions
+17
View File
@@ -5,6 +5,8 @@ import {
useAuthenticatedQuery,
} from "@/shared/api";
import { moneyCents } from "../model/form-validation";
type BankCard =
paths["/api/v1/bank-cards/{id}"]["get"]["responses"][200]["content"]["application/json"];
type BankOperation =
@@ -119,11 +121,25 @@ function useBankExpenses(
});
}
function sortBankSummary(data: BankSummary): BankSummary {
return {
...data,
categories: data.categories.toSorted((a, b) => {
const left = moneyCents(a.net.value);
const right = moneyCents(b.net.value);
if (left === null || right === null)
throw new Error("Invalid bank summary amount");
return left > right ? -1 : left < right ? 1 : 0;
}),
};
}
function useBankSummary(month: string, cardId?: string) {
return useAuthenticatedQuery({
...apiQueryClient.queryOptions("get", "/api/v1/bank-cards/summary", {
params: { query: { month, cardId } },
}),
select: sortBankSummary,
enabled: Boolean(month),
});
}
@@ -147,6 +163,7 @@ export {
type CreateBankOperation,
type CreateExpenseCategory,
type ExpenseCategory,
sortBankSummary,
type UpdateBankCard,
type UpdateBankOperation,
type UpdateExpenseCategory,