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.
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import type { QueryClient } from "@tanstack/react-query";
|
|
import {
|
|
apiQueryClient,
|
|
invalidateApiRequests,
|
|
type paths,
|
|
} from "@/shared/api";
|
|
|
|
type TbankConnectionStatus =
|
|
paths["/api/v1/tbank-connection"]["get"]["responses"][200]["content"]["application/json"];
|
|
|
|
const brokerageAccountsPath = "/api/v1/brokerage-accounts";
|
|
|
|
const tbankConnectionQueryOptions = () =>
|
|
apiQueryClient.queryOptions("get", "/api/v1/tbank-connection");
|
|
|
|
const tbankConnectionQueryKey = tbankConnectionQueryOptions().queryKey;
|
|
|
|
function commitTbankConnectionBoundary(
|
|
queryClient: QueryClient,
|
|
status: TbankConnectionStatus,
|
|
) {
|
|
invalidateApiRequests(
|
|
(path) =>
|
|
path.startsWith("/api/v1/brokerage-accounts") ||
|
|
path.startsWith("/api/v1/stocks") ||
|
|
path.startsWith("/api/v1/bonds") ||
|
|
path.startsWith("/api/v1/tbank-sync"),
|
|
);
|
|
queryClient.setQueryData(tbankConnectionQueryKey, status);
|
|
void queryClient.resetQueries({
|
|
predicate: ({ queryKey }) => {
|
|
const [method, path] = queryKey;
|
|
return (
|
|
method === "get" &&
|
|
typeof path === "string" &&
|
|
(path.startsWith("/api/v1/tbank-sync") ||
|
|
path === "/api/v1/bonds" ||
|
|
path.startsWith("/api/v1/bonds/") ||
|
|
path === "/api/v1/stocks" ||
|
|
path.startsWith("/api/v1/stocks/") ||
|
|
path === brokerageAccountsPath ||
|
|
path.startsWith(`${brokerageAccountsPath}/`))
|
|
);
|
|
},
|
|
});
|
|
}
|
|
|
|
export {
|
|
commitTbankConnectionBoundary,
|
|
type TbankConnectionStatus,
|
|
tbankConnectionQueryOptions,
|
|
};
|