161 lines
4.6 KiB
TypeScript
161 lines
4.6 KiB
TypeScript
import {
|
|
apiClient,
|
|
apiQueryClient,
|
|
type paths,
|
|
useAuthenticatedQuery,
|
|
} from "@/shared/api";
|
|
|
|
type BankCard =
|
|
paths["/api/v1/bank-cards/{id}"]["get"]["responses"][200]["content"]["application/json"];
|
|
type BankOperation =
|
|
paths["/api/v1/bank-cards/operations/{id}"]["get"]["responses"][200]["content"]["application/json"];
|
|
type BankOperations =
|
|
paths["/api/v1/bank-cards/{id}/operations"]["get"]["responses"][200]["content"]["application/json"];
|
|
type BankSummary =
|
|
paths["/api/v1/bank-cards/summary"]["get"]["responses"][200]["content"]["application/json"];
|
|
type ExpenseCategory =
|
|
paths["/api/v1/bank-cards/categories"]["get"]["responses"][200]["content"]["application/json"][number];
|
|
type CreateBankCard =
|
|
paths["/api/v1/bank-cards"]["post"]["requestBody"]["content"]["application/json"];
|
|
type UpdateBankCard =
|
|
paths["/api/v1/bank-cards/{id}"]["patch"]["requestBody"]["content"]["application/json"];
|
|
type CreateBankOperation =
|
|
paths["/api/v1/bank-cards/operations"]["post"]["requestBody"]["content"]["application/json"];
|
|
type UpdateBankOperation =
|
|
paths["/api/v1/bank-cards/operations/{id}"]["patch"]["requestBody"]["content"]["application/json"];
|
|
type CreateExpenseCategory =
|
|
paths["/api/v1/bank-cards/categories"]["post"]["requestBody"]["content"]["application/json"];
|
|
type UpdateExpenseCategory =
|
|
paths["/api/v1/bank-cards/categories/{id}"]["patch"]["requestBody"]["content"]["application/json"];
|
|
|
|
function useBankCards(includeArchived = false) {
|
|
return useAuthenticatedQuery(
|
|
apiQueryClient.queryOptions("get", "/api/v1/bank-cards", {
|
|
params: {
|
|
query: { includeArchived: includeArchived ? "true" : "false" },
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
|
|
function useBankCard(id: string | undefined) {
|
|
return useAuthenticatedQuery({
|
|
...apiQueryClient.queryOptions("get", "/api/v1/bank-cards/{id}", {
|
|
params: { path: { id: id ?? "" } },
|
|
}),
|
|
enabled: Boolean(id),
|
|
});
|
|
}
|
|
|
|
function useBankCategories() {
|
|
return useAuthenticatedQuery(
|
|
apiQueryClient.queryOptions("get", "/api/v1/bank-cards/categories"),
|
|
);
|
|
}
|
|
|
|
function useBankOperations(
|
|
id: string | undefined,
|
|
query: { month: string; page: number; limit?: number | "all" },
|
|
) {
|
|
const all = query.limit === "all";
|
|
const params = {
|
|
path: { id: id ?? "" },
|
|
query: {
|
|
...query,
|
|
page: all ? 1 : query.page,
|
|
limit: query.limit === "all" ? 100 : query.limit,
|
|
},
|
|
};
|
|
const options = apiQueryClient.queryOptions(
|
|
"get",
|
|
"/api/v1/bank-cards/{id}/operations",
|
|
{ params },
|
|
);
|
|
return useAuthenticatedQuery({
|
|
queryKey: all ? ([...options.queryKey, "all"] as const) : options.queryKey,
|
|
placeholderData: (
|
|
previousData: BankOperations | undefined,
|
|
previousQuery,
|
|
) => {
|
|
const previous = previousQuery?.queryKey[2]?.params;
|
|
return !all &&
|
|
previousQuery?.queryKey.length === 3 &&
|
|
previous?.path?.id === id &&
|
|
previous?.query?.month === query.month &&
|
|
previous?.query?.limit === query.limit
|
|
? previousData
|
|
: undefined;
|
|
},
|
|
queryFn: async ({ signal }) => {
|
|
const items: BankOperation[] = [];
|
|
for (let page = params.query.page; ; page++) {
|
|
const { data, error } = await apiClient.GET(
|
|
"/api/v1/bank-cards/{id}/operations",
|
|
{
|
|
params: { ...params, query: { ...params.query, page } },
|
|
signal,
|
|
},
|
|
);
|
|
if (error) throw error;
|
|
if (!data) throw new Error("Missing bank operations response");
|
|
if (!all) return data;
|
|
items.push(...data.items);
|
|
if (!data.hasNext) return { ...data, items, page: 1, hasNext: false };
|
|
}
|
|
},
|
|
enabled: Boolean(id && query.month),
|
|
});
|
|
}
|
|
|
|
function useBankExpenses(
|
|
id: string | undefined,
|
|
query: { page: number; limit?: number },
|
|
) {
|
|
return useAuthenticatedQuery({
|
|
...apiQueryClient.queryOptions("get", "/api/v1/bank-cards/{id}/expenses", {
|
|
params: { path: { id: id ?? "" }, query },
|
|
}),
|
|
enabled: Boolean(id),
|
|
});
|
|
}
|
|
|
|
function useBankSummary(month: string, cardId?: string) {
|
|
return useAuthenticatedQuery({
|
|
...apiQueryClient.queryOptions("get", "/api/v1/bank-cards/summary", {
|
|
params: { query: { month, cardId } },
|
|
}),
|
|
enabled: Boolean(month),
|
|
});
|
|
}
|
|
|
|
function useBankOperation(id: string | undefined) {
|
|
return useAuthenticatedQuery({
|
|
...apiQueryClient.queryOptions(
|
|
"get",
|
|
"/api/v1/bank-cards/operations/{id}",
|
|
{ params: { path: { id: id ?? "" } } },
|
|
),
|
|
enabled: Boolean(id),
|
|
});
|
|
}
|
|
|
|
export {
|
|
type BankCard,
|
|
type BankOperation,
|
|
type BankSummary,
|
|
type CreateBankCard,
|
|
type CreateBankOperation,
|
|
type CreateExpenseCategory,
|
|
type ExpenseCategory,
|
|
type UpdateBankCard,
|
|
type UpdateBankOperation,
|
|
type UpdateExpenseCategory,
|
|
useBankCard,
|
|
useBankCards,
|
|
useBankCategories,
|
|
useBankExpenses,
|
|
useBankOperation,
|
|
useBankOperations,
|
|
useBankSummary,
|
|
};
|