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,
@@ -0,0 +1,32 @@
import { expect, it } from "vitest";
import { type BankSummary, sortBankSummary } from "./queries";
it("orders displayed expenses descending, preserving cents, ties and source data", () => {
const values = [
"-10",
"9007199254740992.01",
"0",
"9007199254740992.02",
"-2",
"0",
];
const money = (value: string) => ({ currency: "RUB", value });
const data: BankSummary = {
month: "2026-09",
cardId: "a",
expenses: money("0"),
refunds: money("0"),
net: money("0"),
categories: values.map((value, index) => ({
categoryId: String(index),
categoryName: String(index),
expenses: money("100"),
refunds: money("0"),
net: money(value),
})),
};
expect(sortBankSummary(data).categories.map((row) => row.categoryId)).toEqual(
["3", "1", "2", "5", "4", "0"],
);
expect(data.categories.map((row) => row.net.value)).toEqual(values);
});