Consume the provided resource-envelope API snapshot. Preserve financial snapshots during refresh, observe synchronization without importing, and fence late responses across account and connection changes.
73 lines
1.8 KiB
TypeScript
73 lines
1.8 KiB
TypeScript
import { apiQueryClient, type paths, useResourceQuery } from "@/shared/api";
|
|
|
|
type OperationsEndpoint =
|
|
paths["/api/v1/brokerage-accounts/{accountId}/operations"]["get"];
|
|
|
|
type BrokerageOperation = NonNullable<
|
|
OperationsEndpoint["responses"][200]["content"]["application/json"]["data"]
|
|
>["items"][number];
|
|
type BrokerageOperationsQuery = Omit<
|
|
NonNullable<OperationsEndpoint["parameters"]["query"]>,
|
|
"sync"
|
|
>;
|
|
type OperationCategory = BrokerageOperation["category"];
|
|
|
|
const operationCategories = [
|
|
"trade",
|
|
"income",
|
|
"tax",
|
|
"fee",
|
|
"transfer",
|
|
"other",
|
|
] as const satisfies readonly OperationCategory[];
|
|
|
|
function brokerageOperationsQueryParams(query: BrokerageOperationsQuery) {
|
|
return {
|
|
...(query.instrumentUid ? { instrumentUid: query.instrumentUid } : {}),
|
|
...(query.from ? { from: query.from } : {}),
|
|
...(query.to ? { to: query.to } : {}),
|
|
...(query.categories?.length
|
|
? { categories: [...query.categories].sort() }
|
|
: {}),
|
|
...(query.cursor ? { cursor: query.cursor } : {}),
|
|
...(query.limit ? { limit: query.limit } : {}),
|
|
};
|
|
}
|
|
|
|
function brokerageOperationsQueryOptions(
|
|
accountId: string | undefined,
|
|
query: BrokerageOperationsQuery = {},
|
|
) {
|
|
return apiQueryClient.queryOptions(
|
|
"get",
|
|
"/api/v1/brokerage-accounts/{accountId}/operations",
|
|
{
|
|
params: {
|
|
path: { accountId: accountId ?? "" },
|
|
query: brokerageOperationsQueryParams(query),
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
function useBrokerageOperations(
|
|
accountId: string | undefined,
|
|
query: BrokerageOperationsQuery = {},
|
|
) {
|
|
return useResourceQuery({
|
|
meta: { accessGroup: "brokerage" },
|
|
...brokerageOperationsQueryOptions(accountId, query),
|
|
enabled: Boolean(accountId),
|
|
staleTime: 30_000,
|
|
});
|
|
}
|
|
|
|
export {
|
|
type BrokerageOperation,
|
|
brokerageOperationsQueryOptions,
|
|
brokerageOperationsQueryParams,
|
|
type OperationCategory,
|
|
operationCategories,
|
|
useBrokerageOperations,
|
|
};
|