feat: migrate from react-router-dom to @tanstack/react-router (code-first)
- Create code-first route tree in src/app/routing/routeTree.tsx - Replace ProtectedRoute with beforeLoad auth guards - Add useSearchParamsCompat for URLSearchParams access - Update App.tsx, layouts, and all page/widget imports - Add frontend tooling: biome, prettier, env config - Update all tests for TanStack Router compatibility - Remove react-router-dom dependency, @tanstack/router-plugin - Consolidate biome config at root level
This commit is contained in:
parent
6d3601a8f4
commit
cfadb2adbe
1
.prettierignore
Normal file
1
.prettierignore
Normal file
@ -0,0 +1 @@
|
||||
apps/frontend/
|
||||
@ -5,21 +5,14 @@ module.exports = {
|
||||
parser: '@typescript-eslint/parser',
|
||||
parserOptions: {
|
||||
sourceType: 'module',
|
||||
ecmaFeatures: { jsx: true },
|
||||
},
|
||||
plugins: ['@typescript-eslint/eslint-plugin', 'react', 'react-hooks', 'import', '@conarti/feature-sliced'],
|
||||
extends: [
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
'plugin:react/recommended',
|
||||
'plugin:react-hooks/recommended',
|
||||
],
|
||||
plugins: ['@conarti/feature-sliced', 'import'],
|
||||
root: true,
|
||||
env: {
|
||||
browser: true,
|
||||
es2020: true,
|
||||
},
|
||||
settings: {
|
||||
react: { version: 'detect' },
|
||||
'import/resolver': {
|
||||
typescript: {
|
||||
alwaysTryTypes: true,
|
||||
@ -29,63 +22,31 @@ module.exports = {
|
||||
},
|
||||
ignorePatterns: ['.eslintrc.cjs', 'vite.config.ts', 'vitest.config.ts', 'dist/'],
|
||||
rules: {
|
||||
'no-restricted-imports': ['warn', {
|
||||
paths: [{
|
||||
name: '@mui/material',
|
||||
importNames: [
|
||||
// DS-covered: import from @moex-vibe/design-system
|
||||
'Typography', 'Button', 'TextField', 'Select', 'Checkbox',
|
||||
'Paper', 'Chip', 'Badge', 'Alert', 'Dialog', 'Skeleton',
|
||||
'CircularProgress', 'Link', 'IconButton',
|
||||
'Table', 'TableBody', 'TableCell', 'TableContainer',
|
||||
'TableHead', 'TableRow', 'TableSortLabel',
|
||||
'TablePagination', 'Pagination',
|
||||
],
|
||||
message: 'Import from @moex-vibe/design-system instead, or use Box/Stack/Grid for layout.',
|
||||
}],
|
||||
}],
|
||||
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
|
||||
'@typescript-eslint/no-explicit-any': 'off',
|
||||
'react/react-in-jsx-scope': 'off',
|
||||
|
||||
// FSD layer boundaries (from @conarti/eslint-plugin-feature-sliced)
|
||||
// layers-slices: catches cross-layer violations (e.g., shared→entities)
|
||||
'@conarti/feature-sliced/layers-slices': ['error', {
|
||||
// allow test files and test utilities to import from any layer for mocking
|
||||
ignoreInFilesPatterns: ['**/*.test.ts', '**/*.test.tsx', '**/*.spec.ts', '**/*.spec.tsx', '**/test/**'],
|
||||
}],
|
||||
// absolute-relative: false positives with @/ alias convention — disabled
|
||||
'@conarti/feature-sliced/absolute-relative': 'off',
|
||||
// public-api: too strict for app/ and test internals — disabled
|
||||
'@conarti/feature-sliced/public-api': 'off',
|
||||
|
||||
// FSD layer boundaries (from import/no-restricted-paths)
|
||||
// NOTE: `from` = what's being imported, `target` = the file doing the import
|
||||
'import/no-restricted-paths': [
|
||||
'error',
|
||||
{
|
||||
zones: [
|
||||
// shared/ cannot import from entities/, features/, widgets/, pages/, app/
|
||||
{ from: `${src}/entities`, target: `${src}/shared` },
|
||||
{ from: `${src}/features`, target: `${src}/shared` },
|
||||
{ from: `${src}/widgets`, target: `${src}/shared` },
|
||||
{ from: `${src}/pages`, target: `${src}/shared` },
|
||||
{ from: `${src}/app`, target: `${src}/shared` },
|
||||
// entities/ cannot import from features/, widgets/, pages/, app/
|
||||
{ from: `${src}/features`, target: `${src}/entities` },
|
||||
{ from: `${src}/widgets`, target: `${src}/entities` },
|
||||
{ from: `${src}/pages`, target: `${src}/entities` },
|
||||
{ from: `${src}/app`, target: `${src}/entities` },
|
||||
// features/ cannot import from widgets/, pages/, app/
|
||||
{ from: `${src}/widgets`, target: `${src}/features` },
|
||||
{ from: `${src}/pages`, target: `${src}/features` },
|
||||
{ from: `${src}/app`, target: `${src}/features` },
|
||||
// widgets/ cannot import from pages/, app/
|
||||
{ from: `${src}/pages`, target: `${src}/widgets` },
|
||||
{ from: `${src}/app`, target: `${src}/widgets` },
|
||||
// pages/ cannot import from app/
|
||||
{ from: `${src}/app`, target: `${src}/pages` },
|
||||
|
||||
],
|
||||
},
|
||||
],
|
||||
|
||||
@ -5,10 +5,14 @@
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc -b && vite build",
|
||||
"build": "vite build",
|
||||
"typecheck": "tsc -b",
|
||||
"preview": "vite preview",
|
||||
"codegen": "openapi-typescript http://localhost:3000/api/docs-json -o src/api/types.ts",
|
||||
"lint": "eslint \"src/**/*.{ts,tsx}\"",
|
||||
"lint": "biome check src/",
|
||||
"lint:fix": "biome check --write src/",
|
||||
"format": "biome format --write src/",
|
||||
"format:check": "biome format src/",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest"
|
||||
},
|
||||
@ -16,11 +20,12 @@
|
||||
"@emotion/react": "^11.14.0",
|
||||
"@emotion/styled": "^11.14.1",
|
||||
"@fontsource/inter": "^5.2.8",
|
||||
"@moex-vibe/design-system": "*",
|
||||
"@hookform/resolvers": "^3.10.0",
|
||||
"@moex-vibe/design-system": "*",
|
||||
"@mui/icons-material": "^6.5.0",
|
||||
"@mui/material": "^6.5.0",
|
||||
"@tanstack/react-query": "^5.20.0",
|
||||
"@tanstack/react-router": "^1.170.16",
|
||||
"@tanstack/react-table": "^8.21.3",
|
||||
"clsx": "^2.1.1",
|
||||
"dayjs": "^1.11.21",
|
||||
@ -31,27 +36,25 @@
|
||||
"react-dom": "^18.3.0",
|
||||
"react-hook-form": "^7.80.0",
|
||||
"react-is": "^18.3.1",
|
||||
"react-router-dom": "^6.20.0",
|
||||
"zod": "^4.4.3",
|
||||
"zustand": "^5.0.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@biomejs/biome": "^2.5.0",
|
||||
"@conarti/eslint-plugin-feature-sliced": "^1.0.5",
|
||||
"@tanstack/router-devtools": "^1.167.0",
|
||||
|
||||
"@testing-library/jest-dom": "^6.9.1",
|
||||
"@testing-library/react": "^16.3.2",
|
||||
"@testing-library/user-event": "^14.6.1",
|
||||
"@types/node": "^25.9.3",
|
||||
"@types/react": "^18.3.0",
|
||||
"@types/react-dom": "^18.3.0",
|
||||
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
||||
"@typescript-eslint/parser": "^7.0.0",
|
||||
"@vitejs/plugin-react": "^4.2.0",
|
||||
"eslint": "^8.0.0",
|
||||
"eslint-import-resolver-alias": "^1.1.2",
|
||||
"eslint-import-resolver-typescript": "^4.4.5",
|
||||
"eslint-plugin-import": "^2.32.0",
|
||||
"eslint-plugin-react": "^7.34.0",
|
||||
"eslint-plugin-react-hooks": "^4.6.0",
|
||||
"jsdom": "^29.1.1",
|
||||
"msw": "^2.14.6",
|
||||
"openapi-typescript": "^7.0.0",
|
||||
|
||||
349
apps/frontend/public/mockServiceWorker.js
Normal file
349
apps/frontend/public/mockServiceWorker.js
Normal file
@ -0,0 +1,349 @@
|
||||
/* eslint-disable */
|
||||
/* tslint:disable */
|
||||
|
||||
/**
|
||||
* Mock Service Worker.
|
||||
* @see https://github.com/mswjs/msw
|
||||
* - Please do NOT modify this file.
|
||||
*/
|
||||
|
||||
const PACKAGE_VERSION = '2.14.6'
|
||||
const INTEGRITY_CHECKSUM = '4db4a41e972cec1b64cc569c66952d82'
|
||||
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
|
||||
const activeClientIds = new Set()
|
||||
|
||||
addEventListener('install', function () {
|
||||
self.skipWaiting()
|
||||
})
|
||||
|
||||
addEventListener('activate', function (event) {
|
||||
event.waitUntil(self.clients.claim())
|
||||
})
|
||||
|
||||
addEventListener('message', async function (event) {
|
||||
const clientId = Reflect.get(event.source || {}, 'id')
|
||||
|
||||
if (!clientId || !self.clients) {
|
||||
return
|
||||
}
|
||||
|
||||
const client = await self.clients.get(clientId)
|
||||
|
||||
if (!client) {
|
||||
return
|
||||
}
|
||||
|
||||
const allClients = await self.clients.matchAll({
|
||||
type: 'window',
|
||||
})
|
||||
|
||||
switch (event.data) {
|
||||
case 'KEEPALIVE_REQUEST': {
|
||||
sendToClient(client, {
|
||||
type: 'KEEPALIVE_RESPONSE',
|
||||
})
|
||||
break
|
||||
}
|
||||
|
||||
case 'INTEGRITY_CHECK_REQUEST': {
|
||||
sendToClient(client, {
|
||||
type: 'INTEGRITY_CHECK_RESPONSE',
|
||||
payload: {
|
||||
packageVersion: PACKAGE_VERSION,
|
||||
checksum: INTEGRITY_CHECKSUM,
|
||||
},
|
||||
})
|
||||
break
|
||||
}
|
||||
|
||||
case 'MOCK_ACTIVATE': {
|
||||
activeClientIds.add(clientId)
|
||||
|
||||
sendToClient(client, {
|
||||
type: 'MOCKING_ENABLED',
|
||||
payload: {
|
||||
client: {
|
||||
id: client.id,
|
||||
frameType: client.frameType,
|
||||
},
|
||||
},
|
||||
})
|
||||
break
|
||||
}
|
||||
|
||||
case 'CLIENT_CLOSED': {
|
||||
activeClientIds.delete(clientId)
|
||||
|
||||
const remainingClients = allClients.filter((client) => {
|
||||
return client.id !== clientId
|
||||
})
|
||||
|
||||
// Unregister itself when there are no more clients
|
||||
if (remainingClients.length === 0) {
|
||||
self.registration.unregister()
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
addEventListener('fetch', function (event) {
|
||||
const requestInterceptedAt = Date.now()
|
||||
|
||||
// Bypass navigation requests.
|
||||
if (event.request.mode === 'navigate') {
|
||||
return
|
||||
}
|
||||
|
||||
// Opening the DevTools triggers the "only-if-cached" request
|
||||
// that cannot be handled by the worker. Bypass such requests.
|
||||
if (
|
||||
event.request.cache === 'only-if-cached' &&
|
||||
event.request.mode !== 'same-origin'
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
// Bypass all requests when there are no active clients.
|
||||
// Prevents the self-unregistered worked from handling requests
|
||||
// after it's been terminated (still remains active until the next reload).
|
||||
if (activeClientIds.size === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const requestId = crypto.randomUUID()
|
||||
event.respondWith(handleRequest(event, requestId, requestInterceptedAt))
|
||||
})
|
||||
|
||||
/**
|
||||
* @param {FetchEvent} event
|
||||
* @param {string} requestId
|
||||
* @param {number} requestInterceptedAt
|
||||
*/
|
||||
async function handleRequest(event, requestId, requestInterceptedAt) {
|
||||
const client = await resolveMainClient(event)
|
||||
const requestCloneForEvents = event.request.clone()
|
||||
const response = await getResponse(
|
||||
event,
|
||||
client,
|
||||
requestId,
|
||||
requestInterceptedAt,
|
||||
)
|
||||
|
||||
// Send back the response clone for the "response:*" life-cycle events.
|
||||
// Ensure MSW is active and ready to handle the message, otherwise
|
||||
// this message will pend indefinitely.
|
||||
if (client && activeClientIds.has(client.id)) {
|
||||
const serializedRequest = await serializeRequest(requestCloneForEvents)
|
||||
|
||||
// Clone the response so both the client and the library could consume it.
|
||||
const responseClone = response.clone()
|
||||
|
||||
sendToClient(
|
||||
client,
|
||||
{
|
||||
type: 'RESPONSE',
|
||||
payload: {
|
||||
isMockedResponse: IS_MOCKED_RESPONSE in response,
|
||||
request: {
|
||||
id: requestId,
|
||||
...serializedRequest,
|
||||
},
|
||||
response: {
|
||||
type: responseClone.type,
|
||||
status: responseClone.status,
|
||||
statusText: responseClone.statusText,
|
||||
headers: Object.fromEntries(responseClone.headers.entries()),
|
||||
body: responseClone.body,
|
||||
},
|
||||
},
|
||||
},
|
||||
responseClone.body ? [serializedRequest.body, responseClone.body] : [],
|
||||
)
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the main client for the given event.
|
||||
* Client that issues a request doesn't necessarily equal the client
|
||||
* that registered the worker. It's with the latter the worker should
|
||||
* communicate with during the response resolving phase.
|
||||
* @param {FetchEvent} event
|
||||
* @returns {Promise<Client | undefined>}
|
||||
*/
|
||||
async function resolveMainClient(event) {
|
||||
const client = await self.clients.get(event.clientId)
|
||||
|
||||
if (activeClientIds.has(event.clientId)) {
|
||||
return client
|
||||
}
|
||||
|
||||
if (client?.frameType === 'top-level') {
|
||||
return client
|
||||
}
|
||||
|
||||
const allClients = await self.clients.matchAll({
|
||||
type: 'window',
|
||||
})
|
||||
|
||||
return allClients
|
||||
.filter((client) => {
|
||||
// Get only those clients that are currently visible.
|
||||
return client.visibilityState === 'visible'
|
||||
})
|
||||
.find((client) => {
|
||||
// Find the client ID that's recorded in the
|
||||
// set of clients that have registered the worker.
|
||||
return activeClientIds.has(client.id)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {FetchEvent} event
|
||||
* @param {Client | undefined} client
|
||||
* @param {string} requestId
|
||||
* @param {number} requestInterceptedAt
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
async function getResponse(event, client, requestId, requestInterceptedAt) {
|
||||
// Clone the request because it might've been already used
|
||||
// (i.e. its body has been read and sent to the client).
|
||||
const requestClone = event.request.clone()
|
||||
|
||||
function passthrough() {
|
||||
// Cast the request headers to a new Headers instance
|
||||
// so the headers can be manipulated with.
|
||||
const headers = new Headers(requestClone.headers)
|
||||
|
||||
// Remove the "accept" header value that marked this request as passthrough.
|
||||
// This prevents request alteration and also keeps it compliant with the
|
||||
// user-defined CORS policies.
|
||||
const acceptHeader = headers.get('accept')
|
||||
if (acceptHeader) {
|
||||
const values = acceptHeader.split(',').map((value) => value.trim())
|
||||
const filteredValues = values.filter(
|
||||
(value) => value !== 'msw/passthrough',
|
||||
)
|
||||
|
||||
if (filteredValues.length > 0) {
|
||||
headers.set('accept', filteredValues.join(', '))
|
||||
} else {
|
||||
headers.delete('accept')
|
||||
}
|
||||
}
|
||||
|
||||
return fetch(requestClone, { headers })
|
||||
}
|
||||
|
||||
// Bypass mocking when the client is not active.
|
||||
if (!client) {
|
||||
return passthrough()
|
||||
}
|
||||
|
||||
// Bypass initial page load requests (i.e. static assets).
|
||||
// The absence of the immediate/parent client in the map of the active clients
|
||||
// means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
|
||||
// and is not ready to handle requests.
|
||||
if (!activeClientIds.has(client.id)) {
|
||||
return passthrough()
|
||||
}
|
||||
|
||||
// Notify the client that a request has been intercepted.
|
||||
const serializedRequest = await serializeRequest(event.request)
|
||||
const clientMessage = await sendToClient(
|
||||
client,
|
||||
{
|
||||
type: 'REQUEST',
|
||||
payload: {
|
||||
id: requestId,
|
||||
interceptedAt: requestInterceptedAt,
|
||||
...serializedRequest,
|
||||
},
|
||||
},
|
||||
[serializedRequest.body],
|
||||
)
|
||||
|
||||
switch (clientMessage.type) {
|
||||
case 'MOCK_RESPONSE': {
|
||||
return respondWithMock(clientMessage.data)
|
||||
}
|
||||
|
||||
case 'PASSTHROUGH': {
|
||||
return passthrough()
|
||||
}
|
||||
}
|
||||
|
||||
return passthrough()
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Client} client
|
||||
* @param {any} message
|
||||
* @param {Array<Transferable>} transferrables
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
function sendToClient(client, message, transferrables = []) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const channel = new MessageChannel()
|
||||
|
||||
channel.port1.onmessage = (event) => {
|
||||
if (event.data && event.data.error) {
|
||||
return reject(event.data.error)
|
||||
}
|
||||
|
||||
resolve(event.data)
|
||||
}
|
||||
|
||||
client.postMessage(message, [
|
||||
channel.port2,
|
||||
...transferrables.filter(Boolean),
|
||||
])
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Response} response
|
||||
* @returns {Response}
|
||||
*/
|
||||
function respondWithMock(response) {
|
||||
// Setting response status code to 0 is a no-op.
|
||||
// However, when responding with a "Response.error()", the produced Response
|
||||
// instance will have status code set to 0. Since it's not possible to create
|
||||
// a Response instance with status code 0, handle that use-case separately.
|
||||
if (response.status === 0) {
|
||||
return Response.error()
|
||||
}
|
||||
|
||||
const mockedResponse = new Response(response.body, response)
|
||||
|
||||
Reflect.defineProperty(mockedResponse, IS_MOCKED_RESPONSE, {
|
||||
value: true,
|
||||
enumerable: true,
|
||||
})
|
||||
|
||||
return mockedResponse
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Request} request
|
||||
*/
|
||||
async function serializeRequest(request) {
|
||||
return {
|
||||
url: request.url,
|
||||
mode: request.mode,
|
||||
method: request.method,
|
||||
headers: Object.fromEntries(request.headers.entries()),
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
destination: request.destination,
|
||||
integrity: request.integrity,
|
||||
redirect: request.redirect,
|
||||
referrer: request.referrer,
|
||||
referrerPolicy: request.referrerPolicy,
|
||||
body: await request.arrayBuffer(),
|
||||
keepalive: request.keepalive,
|
||||
}
|
||||
}
|
||||
@ -1,10 +1,6 @@
|
||||
import { BrowserRouter } from 'react-router-dom';
|
||||
import { AppRoutes } from './routing/AppRoutes';
|
||||
import { RouterProvider } from '@tanstack/react-router'
|
||||
import { router } from './routing/router'
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<BrowserRouter future={{ v7_startTransition: true, v7_relativeSplatPath: true }}>
|
||||
<AppRoutes />
|
||||
</BrowserRouter>
|
||||
);
|
||||
return <RouterProvider router={router} />
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
export { default as App } from './App';
|
||||
export { default as App } from './App'
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
import { Outlet, Link, useNavigate } from 'react-router-dom';
|
||||
import { SearchBar } from '@/widgets/search-bar';
|
||||
import { useSession } from '@/entities/session';
|
||||
import { Link, Outlet, useNavigate } from '@tanstack/react-router'
|
||||
import { useSession } from '@/entities/session'
|
||||
import { SearchBar } from '@/widgets/search-bar'
|
||||
|
||||
export function AppLayout() {
|
||||
const { isAuthenticated, user, logout } = useSession();
|
||||
const navigate = useNavigate();
|
||||
const { isAuthenticated, user, logout } = useSession()
|
||||
const navigate = useNavigate()
|
||||
|
||||
async function handleLogout() {
|
||||
await logout();
|
||||
navigate('/');
|
||||
await logout()
|
||||
navigate('/')
|
||||
}
|
||||
|
||||
return (
|
||||
@ -138,5 +138,5 @@ export function AppLayout() {
|
||||
<Outlet />
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
export { AppLayout } from './AppLayout';
|
||||
export { AppLayout } from './AppLayout'
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import { type ReactNode } from 'react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import '@fontsource/inter/400.css';
|
||||
import '@fontsource/inter/500.css';
|
||||
import '@fontsource/inter/600.css';
|
||||
import '@fontsource/inter/700.css';
|
||||
import { MoexVibeThemeProvider } from '@moex-vibe/design-system/theme';
|
||||
import { SessionProvider } from './SessionProvider';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import type { ReactNode } from 'react'
|
||||
import '@fontsource/inter/400.css'
|
||||
import '@fontsource/inter/500.css'
|
||||
import '@fontsource/inter/600.css'
|
||||
import '@fontsource/inter/700.css'
|
||||
import { MoexVibeThemeProvider } from '@moex-vibe/design-system/theme'
|
||||
import { SessionProvider } from './SessionProvider'
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
@ -15,7 +15,7 @@ const queryClient = new QueryClient({
|
||||
refetchOnWindowFocus: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
export function AppProviders({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
@ -24,5 +24,5 @@ export function AppProviders({ children }: { children: ReactNode }) {
|
||||
<SessionProvider>{children}</SessionProvider>
|
||||
</QueryClientProvider>
|
||||
</MoexVibeThemeProvider>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,27 +1,27 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { useContext } from 'react';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { server } from '@/shared/lib/test/server';
|
||||
import { SessionContext } from '@/entities/session';
|
||||
import { SessionProvider } from './SessionProvider';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { render, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
import { useContext } from 'react'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { SessionContext } from '@/entities/session'
|
||||
import { server } from '@/shared/lib/test/server'
|
||||
import { SessionProvider } from './SessionProvider'
|
||||
|
||||
const API = '/api/v1';
|
||||
const API = '/api/v1'
|
||||
|
||||
function renderWithProviders(ui: React.ReactElement) {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
return render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<SessionProvider>{ui}</SessionProvider>
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
function TestConsumer() {
|
||||
const ctx = useContext(SessionContext);
|
||||
if (!ctx) return <div>no context</div>;
|
||||
const ctx = useContext(SessionContext)
|
||||
if (!ctx) return <div>no context</div>
|
||||
return (
|
||||
<div>
|
||||
<span data-testid="session">{ctx.isAuthenticated ? 'authenticated' : 'anonymous'}</span>
|
||||
@ -31,44 +31,44 @@ function TestConsumer() {
|
||||
<button onClick={() => ctx.logout()}>logout</button>
|
||||
<button onClick={() => ctx.updateProfile({ name: 'New' })}>updateProfile</button>
|
||||
</div>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
describe('SessionProvider', () => {
|
||||
it('starts unauthenticated when refresh fails', async () => {
|
||||
server.use(http.post(`${API}/auth/refresh`, () => new HttpResponse(null, { status: 401 })));
|
||||
renderWithProviders(<TestConsumer />);
|
||||
server.use(http.post(`${API}/auth/refresh`, () => new HttpResponse(null, { status: 401 })))
|
||||
renderWithProviders(<TestConsumer />)
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('session')).toHaveTextContent('anonymous');
|
||||
});
|
||||
});
|
||||
expect(screen.getByTestId('session')).toHaveTextContent('anonymous')
|
||||
})
|
||||
})
|
||||
|
||||
it('restores session on mount when refresh succeeds', async () => {
|
||||
renderWithProviders(<TestConsumer />);
|
||||
renderWithProviders(<TestConsumer />)
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('session')).toHaveTextContent('authenticated');
|
||||
expect(screen.getByTestId('email')).toHaveTextContent('user@test.com');
|
||||
});
|
||||
});
|
||||
expect(screen.getByTestId('session')).toHaveTextContent('authenticated')
|
||||
expect(screen.getByTestId('email')).toHaveTextContent('user@test.com')
|
||||
})
|
||||
})
|
||||
|
||||
it('updates state after login', async () => {
|
||||
server.use(http.post(`${API}/auth/refresh`, () => new HttpResponse(null, { status: 401 })));
|
||||
const user = userEvent.setup();
|
||||
renderWithProviders(<TestConsumer />);
|
||||
await waitFor(() => expect(screen.getByTestId('session')).toHaveTextContent('anonymous'));
|
||||
await user.click(screen.getByRole('button', { name: 'login' }));
|
||||
server.use(http.post(`${API}/auth/refresh`, () => new HttpResponse(null, { status: 401 })))
|
||||
const user = userEvent.setup()
|
||||
renderWithProviders(<TestConsumer />)
|
||||
await waitFor(() => expect(screen.getByTestId('session')).toHaveTextContent('anonymous'))
|
||||
await user.click(screen.getByRole('button', { name: 'login' }))
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('session')).toHaveTextContent('authenticated');
|
||||
});
|
||||
});
|
||||
expect(screen.getByTestId('session')).toHaveTextContent('authenticated')
|
||||
})
|
||||
})
|
||||
|
||||
it('updates state after logout', async () => {
|
||||
const user = userEvent.setup();
|
||||
renderWithProviders(<TestConsumer />);
|
||||
await waitFor(() => expect(screen.getByTestId('session')).toHaveTextContent('authenticated'));
|
||||
await user.click(screen.getByRole('button', { name: 'logout' }));
|
||||
const user = userEvent.setup()
|
||||
renderWithProviders(<TestConsumer />)
|
||||
await waitFor(() => expect(screen.getByTestId('session')).toHaveTextContent('authenticated'))
|
||||
await user.click(screen.getByRole('button', { name: 'logout' }))
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('session')).toHaveTextContent('anonymous');
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(screen.getByTestId('session')).toHaveTextContent('anonymous')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,97 +1,97 @@
|
||||
import { useState, useEffect, useCallback, type ReactNode } from 'react';
|
||||
import * as sessionApi from '@/entities/session';
|
||||
import { SessionContext, type SessionContextValue } from '@/entities/session';
|
||||
import { configureAuth } from '@/shared/api/client';
|
||||
import { type ReactNode, useCallback, useEffect, useState } from 'react'
|
||||
import * as sessionApi from '@/entities/session'
|
||||
import { SessionContext, type SessionContextValue } from '@/entities/session'
|
||||
import {
|
||||
setOnUnauthorized,
|
||||
getAccessToken,
|
||||
handleUnauthorized,
|
||||
} from '@/entities/session/api/tokenManager';
|
||||
import type { UserResponse } from '@/shared/api/responses';
|
||||
setOnUnauthorized,
|
||||
} from '@/entities/session/api/tokenManager'
|
||||
import { configureKyAuth } from '@/shared/api/kyClient'
|
||||
import type { UserResponse } from '@/shared/api/responses'
|
||||
|
||||
export function SessionProvider({ children }: { children: ReactNode }) {
|
||||
const [user, setUser] = useState<UserResponse | null>(null);
|
||||
const [accessToken, setAccessTokenState] = useState<string | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [initialized, setInitialized] = useState(false);
|
||||
const [user, setUser] = useState<UserResponse | null>(null)
|
||||
const [accessToken, setAccessTokenState] = useState<string | null>(null)
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
const [initialized, setInitialized] = useState(false)
|
||||
|
||||
const updateSession = useCallback((authData: { user: UserResponse; accessToken: string }) => {
|
||||
setUser(authData.user);
|
||||
setAccessTokenState(authData.accessToken);
|
||||
}, []);
|
||||
setUser(authData.user)
|
||||
setAccessTokenState(authData.accessToken)
|
||||
}, [])
|
||||
|
||||
const clearSession = useCallback(() => {
|
||||
setUser(null);
|
||||
setAccessTokenState(null);
|
||||
}, []);
|
||||
setUser(null)
|
||||
setAccessTokenState(null)
|
||||
}, [])
|
||||
|
||||
const login = useCallback(
|
||||
async (email: string, password: string) => {
|
||||
const result = await sessionApi.login(email, password);
|
||||
updateSession(result);
|
||||
const result = await sessionApi.login(email, password)
|
||||
updateSession(result)
|
||||
},
|
||||
[updateSession],
|
||||
);
|
||||
)
|
||||
|
||||
const register = useCallback(
|
||||
async (email: string, password: string, name?: string) => {
|
||||
const result = await sessionApi.register(email, password, name);
|
||||
updateSession(result);
|
||||
const result = await sessionApi.register(email, password, name)
|
||||
updateSession(result)
|
||||
},
|
||||
[updateSession],
|
||||
);
|
||||
)
|
||||
|
||||
const logout = useCallback(async () => {
|
||||
try {
|
||||
await sessionApi.logout();
|
||||
await sessionApi.logout()
|
||||
} catch {
|
||||
// ignore network errors on logout
|
||||
}
|
||||
clearSession();
|
||||
}, [clearSession]);
|
||||
clearSession()
|
||||
}, [clearSession])
|
||||
|
||||
const updateProfileFn = useCallback(async (data: { name?: string }) => {
|
||||
const result = await sessionApi.updateProfile(data);
|
||||
setUser(result);
|
||||
}, []);
|
||||
const result = await sessionApi.updateProfile(data)
|
||||
setUser(result)
|
||||
}, [])
|
||||
|
||||
// Try to restore session on mount
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
let mounted = true
|
||||
|
||||
async function init() {
|
||||
try {
|
||||
const result = await sessionApi.refresh();
|
||||
const result = await sessionApi.refresh()
|
||||
if (mounted) {
|
||||
updateSession(result);
|
||||
updateSession(result)
|
||||
}
|
||||
} catch {
|
||||
// No valid session
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setIsLoading(false);
|
||||
setInitialized(true);
|
||||
setIsLoading(false)
|
||||
setInitialized(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init();
|
||||
init()
|
||||
|
||||
return () => {
|
||||
mounted = false;
|
||||
};
|
||||
}, [updateSession]);
|
||||
mounted = false
|
||||
}
|
||||
}, [updateSession])
|
||||
|
||||
// Wire up auth config and auto-logout on unauthorized
|
||||
useEffect(() => {
|
||||
configureAuth({
|
||||
configureKyAuth({
|
||||
getAccessToken,
|
||||
handleUnauthorized,
|
||||
});
|
||||
})
|
||||
setOnUnauthorized(() => {
|
||||
clearSession();
|
||||
});
|
||||
}, [clearSession]);
|
||||
clearSession()
|
||||
})
|
||||
}, [clearSession])
|
||||
|
||||
if (!initialized && isLoading) {
|
||||
return (
|
||||
@ -106,7 +106,7 @@ export function SessionProvider({ children }: { children: ReactNode }) {
|
||||
>
|
||||
Загрузка...
|
||||
</div>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
const value: SessionContextValue = {
|
||||
@ -118,7 +118,7 @@ export function SessionProvider({ children }: { children: ReactNode }) {
|
||||
register,
|
||||
logout,
|
||||
updateProfile: updateProfileFn,
|
||||
};
|
||||
}
|
||||
|
||||
return <SessionContext.Provider value={value}>{children}</SessionContext.Provider>;
|
||||
return <SessionContext.Provider value={value}>{children}</SessionContext.Provider>
|
||||
}
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
export { SessionProvider } from './SessionProvider';
|
||||
export { AppProviders } from './AppProviders';
|
||||
export { AppProviders } from './AppProviders'
|
||||
export { SessionProvider } from './SessionProvider'
|
||||
|
||||
@ -1,78 +0,0 @@
|
||||
import { Routes, Route } from 'react-router-dom';
|
||||
import { AppLayout } from '../layouts/AppLayout';
|
||||
import { ProtectedRoute } from './ProtectedRoute';
|
||||
import { HomePage } from '@/pages/home';
|
||||
import { StockPage } from '@/pages/stock';
|
||||
import { BondPage } from '@/pages/bond';
|
||||
import { LoginPage } from '@/pages/login';
|
||||
import { RegisterPage } from '@/pages/register';
|
||||
import { ProfilePage } from '@/pages/profile';
|
||||
import { PortfoliosListPage, PortfolioDetailPage } from '@/pages/portfolios';
|
||||
import { ScreenerPage } from '@/pages/screener';
|
||||
import { BrokerAccountsPage } from '@/pages/broker-accounts';
|
||||
import { BrokerAccountLayout } from '@/widgets/broker-account-layout';
|
||||
import { BrokerAccountOverviewPage } from '@/pages/broker-account';
|
||||
import { BrokerEventsPage } from '@/pages/broker-events';
|
||||
import { BrokerPositionsPage } from '@/pages/broker-positions';
|
||||
import { BrokerOperationsPage } from '@/pages/broker-operations';
|
||||
|
||||
export function AppRoutes() {
|
||||
return (
|
||||
<Routes>
|
||||
<Route element={<AppLayout />}>
|
||||
<Route path="/" element={<HomePage />} />
|
||||
<Route path="/stocks/:secid" element={<StockPage />} />
|
||||
<Route path="/bonds/:secid" element={<BondPage />} />
|
||||
<Route path="/screener" element={<ScreenerPage />} />
|
||||
<Route path="/login" element={<LoginPage />} />
|
||||
<Route path="/register" element={<RegisterPage />} />
|
||||
<Route
|
||||
path="/profile"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<ProfilePage />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/portfolios"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<PortfoliosListPage />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/portfolios/:id"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<PortfolioDetailPage />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/broker"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<BrokerAccountsPage />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/broker/:accountId"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<BrokerAccountLayout />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
>
|
||||
<Route index element={<BrokerAccountOverviewPage />} />
|
||||
<Route path="shares" element={<BrokerPositionsPage type="share" title="Акции" />} />
|
||||
<Route path="bonds" element={<BrokerPositionsPage type="bond" title="Облигации" />} />
|
||||
<Route path="operations" element={<BrokerOperationsPage />} />
|
||||
<Route path="events" element={<BrokerEventsPage />} />
|
||||
</Route>
|
||||
</Route>
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
import { Navigate, useLocation } from 'react-router-dom';
|
||||
import { useSession } from '@/entities/session';
|
||||
import type { ReactNode } from 'react';
|
||||
|
||||
export function ProtectedRoute({ children }: { children: ReactNode }) {
|
||||
const { isAuthenticated, isLoading } = useSession();
|
||||
const location = useLocation();
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
justifyContent: 'center',
|
||||
padding: 40,
|
||||
color: 'var(--color-text-secondary)',
|
||||
}}
|
||||
>
|
||||
Загрузка...
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Navigate to={`/login?redirect=${encodeURIComponent(location.pathname)}`} replace />;
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
@ -1,2 +1 @@
|
||||
export { AppRoutes } from './AppRoutes';
|
||||
export { ProtectedRoute } from './ProtectedRoute';
|
||||
export { router } from './routeTree'
|
||||
|
||||
169
apps/frontend/src/app/routing/routeTree.tsx
Normal file
169
apps/frontend/src/app/routing/routeTree.tsx
Normal file
@ -0,0 +1,169 @@
|
||||
import {
|
||||
createRootRoute,
|
||||
createRoute,
|
||||
createRouter,
|
||||
Outlet,
|
||||
redirect,
|
||||
} from '@tanstack/react-router'
|
||||
import { useSessionStore } from '@/entities/session'
|
||||
import { BondPage } from '@/pages/bond'
|
||||
import { BrokerAccountOverviewPage } from '@/pages/broker-account'
|
||||
import { BrokerAccountsPage } from '@/pages/broker-accounts'
|
||||
import { BrokerEventsPage } from '@/pages/broker-events'
|
||||
import { BrokerOperationsPage } from '@/pages/broker-operations'
|
||||
import { BrokerPositionsPage } from '@/pages/broker-positions'
|
||||
import { HomePage } from '@/pages/home'
|
||||
import { LoginPage } from '@/pages/login'
|
||||
import { PortfolioDetailPage, PortfoliosListPage } from '@/pages/portfolios'
|
||||
import { ProfilePage } from '@/pages/profile'
|
||||
import { RegisterPage } from '@/pages/register'
|
||||
import { ScreenerPage } from '@/pages/screener'
|
||||
import { StockPage } from '@/pages/stock'
|
||||
import { BrokerAccountLayout } from '@/widgets/broker-account-layout'
|
||||
import { AppLayout } from '../layouts/AppLayout'
|
||||
|
||||
function requireAuth() {
|
||||
if (!useSessionStore.getState().isAuthenticated) {
|
||||
throw redirect({ to: '/login' })
|
||||
}
|
||||
}
|
||||
|
||||
const rootRoute = createRootRoute({
|
||||
component: () => <AppLayout />,
|
||||
})
|
||||
|
||||
const indexRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/',
|
||||
component: HomePage,
|
||||
})
|
||||
|
||||
const stockRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/stocks/$secid',
|
||||
component: StockPage,
|
||||
})
|
||||
|
||||
const bondRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/bonds/$secid',
|
||||
component: BondPage,
|
||||
})
|
||||
|
||||
const screenerRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/screener',
|
||||
component: ScreenerPage,
|
||||
})
|
||||
|
||||
const loginRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/login',
|
||||
component: LoginPage,
|
||||
})
|
||||
|
||||
const registerRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/register',
|
||||
component: RegisterPage,
|
||||
})
|
||||
|
||||
const profileRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/profile',
|
||||
beforeLoad: requireAuth,
|
||||
component: ProfilePage,
|
||||
})
|
||||
|
||||
const portfoliosRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/portfolios',
|
||||
beforeLoad: requireAuth,
|
||||
component: PortfoliosListPage,
|
||||
})
|
||||
|
||||
const portfolioDetailRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/portfolios/$id',
|
||||
beforeLoad: requireAuth,
|
||||
component: PortfolioDetailPage,
|
||||
})
|
||||
|
||||
const brokerRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/broker',
|
||||
beforeLoad: requireAuth,
|
||||
component: BrokerAccountsPage,
|
||||
})
|
||||
|
||||
const brokerAccountRoot = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/broker/$accountId',
|
||||
beforeLoad: requireAuth,
|
||||
component: () => (
|
||||
<BrokerAccountLayout>
|
||||
<Outlet />
|
||||
</BrokerAccountLayout>
|
||||
),
|
||||
})
|
||||
|
||||
const brokerAccountIndexRoute = createRoute({
|
||||
getParentRoute: () => brokerAccountRoot,
|
||||
path: '/',
|
||||
component: BrokerAccountOverviewPage,
|
||||
})
|
||||
|
||||
const brokerSharesRoute = createRoute({
|
||||
getParentRoute: () => brokerAccountRoot,
|
||||
path: '/shares',
|
||||
component: () => <BrokerPositionsPage type="share" title="Акции" />,
|
||||
})
|
||||
|
||||
const brokerBondsRoute = createRoute({
|
||||
getParentRoute: () => brokerAccountRoot,
|
||||
path: '/bonds',
|
||||
component: () => <BrokerPositionsPage type="bond" title="Облигации" />,
|
||||
})
|
||||
|
||||
const brokerOperationsRoute = createRoute({
|
||||
getParentRoute: () => brokerAccountRoot,
|
||||
path: '/operations',
|
||||
component: BrokerOperationsPage,
|
||||
})
|
||||
|
||||
const brokerEventsRoute = createRoute({
|
||||
getParentRoute: () => brokerAccountRoot,
|
||||
path: '/events',
|
||||
component: BrokerEventsPage,
|
||||
})
|
||||
|
||||
const routeTree = rootRoute.addChildren([
|
||||
indexRoute,
|
||||
stockRoute,
|
||||
bondRoute,
|
||||
screenerRoute,
|
||||
loginRoute,
|
||||
registerRoute,
|
||||
profileRoute,
|
||||
portfoliosRoute,
|
||||
portfolioDetailRoute,
|
||||
brokerRoute,
|
||||
brokerAccountRoot.addChildren([
|
||||
brokerAccountIndexRoute,
|
||||
brokerSharesRoute,
|
||||
brokerBondsRoute,
|
||||
brokerOperationsRoute,
|
||||
brokerEventsRoute,
|
||||
]),
|
||||
])
|
||||
|
||||
export const router = createRouter({
|
||||
routeTree,
|
||||
defaultPreload: 'intent',
|
||||
})
|
||||
|
||||
declare module '@tanstack/react-router' {
|
||||
interface Register {
|
||||
router: typeof router
|
||||
}
|
||||
}
|
||||
1
apps/frontend/src/app/routing/router.ts
Normal file
1
apps/frontend/src/app/routing/router.ts
Normal file
@ -0,0 +1 @@
|
||||
export { router } from './routeTree.tsx'
|
||||
@ -1,22 +1,20 @@
|
||||
import { request } from '@/shared/api/client';
|
||||
import { request } from '@/shared/api/kyClient'
|
||||
import type {
|
||||
ApiResponseMeta,
|
||||
BondResponse,
|
||||
BondMarketData,
|
||||
BondHistoryItem,
|
||||
BondMarketData,
|
||||
BondResponse,
|
||||
CandleItem,
|
||||
} from '@/shared/api/responses';
|
||||
} from '@/shared/api/responses'
|
||||
|
||||
export function getBond(secid: string): Promise<{ data: BondResponse; meta: ApiResponseMeta }> {
|
||||
return request<BondResponse>(`/api/v1/securities/bonds/${encodeURIComponent(secid)}`);
|
||||
return request<BondResponse>(`/api/v1/securities/bonds/${encodeURIComponent(secid)}`)
|
||||
}
|
||||
|
||||
export function getBondMarketData(
|
||||
secid: string,
|
||||
): Promise<{ data: BondMarketData; meta: ApiResponseMeta }> {
|
||||
return request<BondMarketData>(
|
||||
`/api/v1/securities/bonds/${encodeURIComponent(secid)}/marketdata`,
|
||||
);
|
||||
return request<BondMarketData>(`/api/v1/securities/bonds/${encodeURIComponent(secid)}/marketdata`)
|
||||
}
|
||||
|
||||
export function getBondHistory(
|
||||
@ -27,7 +25,7 @@ export function getBondHistory(
|
||||
return request<BondHistoryItem[]>(
|
||||
`/api/v1/securities/bonds/${encodeURIComponent(secid)}/history`,
|
||||
{ from, till },
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
export function getBondCandles(
|
||||
@ -40,5 +38,5 @@ export function getBondCandles(
|
||||
interval,
|
||||
from,
|
||||
till,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
export { useBond } from './model/useBond';
|
||||
export { useBondCandles } from './model/useBondCandles';
|
||||
export { getBond, getBondMarketData, getBondHistory, getBondCandles } from './api/bondApi';
|
||||
export { getBond, getBondCandles, getBondHistory, getBondMarketData } from './api/bondApi'
|
||||
export { useBond } from './model/useBond'
|
||||
export { useBondCandles } from './model/useBondCandles'
|
||||
|
||||
@ -1,26 +1,26 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { useBond } from './useBond';
|
||||
import { type ReactNode } from 'react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import type { ReactNode } from 'react'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { useBond } from './useBond'
|
||||
|
||||
function createWrapper() {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
return function Wrapper({ children }: { children: ReactNode }) {
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
|
||||
};
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
}
|
||||
}
|
||||
|
||||
describe('useBond', () => {
|
||||
it('returns bond data', async () => {
|
||||
const { result } = renderHook(() => useBond('SU26238RMFS5'), { wrapper: createWrapper() });
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
expect(result.current.data?.shortName).toBe('ОФЗ 26238');
|
||||
expect(result.current.data?.marketData.price).toBe(98.5);
|
||||
});
|
||||
const { result } = renderHook(() => useBond('SU26238RMFS5'), { wrapper: createWrapper() })
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(result.current.data?.shortName).toBe('ОФЗ 26238')
|
||||
expect(result.current.data?.marketData.price).toBe(98.5)
|
||||
})
|
||||
|
||||
it('returns error on 404', async () => {
|
||||
const { result } = renderHook(() => useBond('NOTFOUND'), { wrapper: createWrapper() });
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
});
|
||||
});
|
||||
const { result } = renderHook(() => useBond('NOTFOUND'), { wrapper: createWrapper() })
|
||||
await waitFor(() => expect(result.current.isError).toBe(true))
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { getBond } from '../api/bondApi';
|
||||
import type { BondResponse } from '@/shared/api/responses';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { BondResponse } from '@/shared/api/responses'
|
||||
import { getBond } from '../api/bondApi'
|
||||
|
||||
export function useBond(secid: string) {
|
||||
return useQuery<BondResponse>({
|
||||
queryKey: ['bond', secid],
|
||||
queryFn: async () => {
|
||||
const res = await getBond(secid);
|
||||
return res.data;
|
||||
const res = await getBond(secid)
|
||||
return res.data
|
||||
},
|
||||
staleTime: 900_000,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,18 +1,18 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { server } from '@/shared/lib/test/server';
|
||||
import { useBondCandles } from './useBondCandles';
|
||||
import { type ReactNode } from 'react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
import type { ReactNode } from 'react'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { server } from '@/shared/lib/test/server'
|
||||
import { useBondCandles } from './useBondCandles'
|
||||
|
||||
const API = '/api/v1';
|
||||
const API = '/api/v1'
|
||||
|
||||
function createWrapper() {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
return function Wrapper({ children }: { children: ReactNode }) {
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
|
||||
};
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
}
|
||||
}
|
||||
|
||||
describe('useBondCandles', () => {
|
||||
@ -20,24 +20,24 @@ describe('useBondCandles', () => {
|
||||
const { result } = renderHook(
|
||||
() => useBondCandles('SU26238RMFS5', '24h', '2024-01-01', '2024-01-31'),
|
||||
{ wrapper: createWrapper() },
|
||||
);
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
expect(result.current.data).toHaveLength(2);
|
||||
});
|
||||
)
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(result.current.data).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('returns empty array when no candles', async () => {
|
||||
server.use(
|
||||
http.get(`${API}/securities/bonds/:secid/candles`, () => {
|
||||
return HttpResponse.json({
|
||||
data: { data: [], meta: { fromCache: false, cachedAt: null } },
|
||||
});
|
||||
})
|
||||
}),
|
||||
);
|
||||
)
|
||||
const { result } = renderHook(
|
||||
() => useBondCandles('SU26238RMFS5', '24h', '2024-01-01', '2024-01-31'),
|
||||
{ wrapper: createWrapper() },
|
||||
);
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
)
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(result.current.data).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { getBondCandles } from '../api/bondApi';
|
||||
import type { CandleItem } from '@/shared/api/responses';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { CandleItem } from '@/shared/api/responses'
|
||||
import { getBondCandles } from '../api/bondApi'
|
||||
|
||||
export function useBondCandles(secid: string, interval: '1h' | '24h', from: string, till: string) {
|
||||
return useQuery<CandleItem[]>({
|
||||
queryKey: ['bondCandles', secid, interval, from, till],
|
||||
queryFn: async () => {
|
||||
const res = await getBondCandles(secid, interval, from, till);
|
||||
return res.data;
|
||||
const res = await getBondCandles(secid, interval, from, till)
|
||||
return res.data
|
||||
},
|
||||
staleTime: 3600_000,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,28 +1,28 @@
|
||||
import { request } from '@/shared/api/client';
|
||||
import type { ApiResponseMeta, BrokerAccount, BrokerPortfolio } from '@/shared/api/responses';
|
||||
import { request } from '@/shared/api/kyClient'
|
||||
import type { ApiResponseMeta, BrokerAccount, BrokerPortfolio } from '@/shared/api/responses'
|
||||
|
||||
export type BrokerOperationQuery = {
|
||||
from?: string;
|
||||
to?: string;
|
||||
cursor?: string;
|
||||
limit?: number;
|
||||
instrumentId?: string;
|
||||
operationTypes?: string;
|
||||
state?: string;
|
||||
};
|
||||
from?: string
|
||||
to?: string
|
||||
cursor?: string
|
||||
limit?: number
|
||||
instrumentId?: string
|
||||
operationTypes?: string
|
||||
state?: string
|
||||
}
|
||||
|
||||
export function getBrokerAccounts(): Promise<{
|
||||
data: BrokerAccount[];
|
||||
meta: ApiResponseMeta;
|
||||
data: BrokerAccount[]
|
||||
meta: ApiResponseMeta
|
||||
}> {
|
||||
return request<BrokerAccount[]>('/api/v1/broker/accounts');
|
||||
return request<BrokerAccount[]>('/api/v1/broker/accounts')
|
||||
}
|
||||
|
||||
export function getBrokerPortfolio(accountId: string): Promise<{
|
||||
data: BrokerPortfolio;
|
||||
meta: ApiResponseMeta;
|
||||
data: BrokerPortfolio
|
||||
meta: ApiResponseMeta
|
||||
}> {
|
||||
return request<BrokerPortfolio>(
|
||||
`/api/v1/broker/accounts/${encodeURIComponent(accountId)}/portfolio`,
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
export { useBrokerAccounts } from './model/useBrokerAccounts';
|
||||
export { useBrokerAccountPortfolios } from './model/useBrokerAccountPortfolios';
|
||||
export { useBrokerPortfolio } from './model/useBrokerPortfolio';
|
||||
export {
|
||||
type BrokerOperationQuery,
|
||||
getBrokerAccounts,
|
||||
getBrokerPortfolio,
|
||||
} from './api/brokerAccountApi'
|
||||
export {
|
||||
aggregateBrokerAccounts,
|
||||
type BrokerAccountsAggregate,
|
||||
} from './model/brokerAccountsOverview';
|
||||
export {
|
||||
getBrokerAccounts,
|
||||
getBrokerPortfolio,
|
||||
type BrokerOperationQuery,
|
||||
} from './api/brokerAccountApi';
|
||||
} from './model/brokerAccountsOverview'
|
||||
export { useBrokerAccountPortfolios } from './model/useBrokerAccountPortfolios'
|
||||
export { useBrokerAccounts } from './model/useBrokerAccounts'
|
||||
export { useBrokerPortfolio } from './model/useBrokerPortfolio'
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { BrokerPortfolio } from '@/shared/api/responses';
|
||||
import { aggregateBrokerAccounts } from '../model/brokerAccountsOverview';
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { BrokerPortfolio } from '@/shared/api/responses'
|
||||
import { aggregateBrokerAccounts } from '../model/brokerAccountsOverview'
|
||||
|
||||
function portfolio(
|
||||
id: string,
|
||||
@ -38,7 +38,7 @@ function portfolio(
|
||||
cash: [{ currency, units: '0', nano: 0, value: cash }],
|
||||
blockedCash: [],
|
||||
asOf: '2026-06-19T10:00:00.000Z',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
describe('aggregateBrokerAccounts', () => {
|
||||
@ -46,7 +46,7 @@ describe('aggregateBrokerAccounts', () => {
|
||||
const result = aggregateBrokerAccounts([
|
||||
portfolio('a', 'RUB', 1_100, 100, 200),
|
||||
portfolio('b', 'RUB', 2_200, 200, 300),
|
||||
]);
|
||||
])
|
||||
|
||||
expect(result.portfolios).toEqual([
|
||||
expect.objectContaining({
|
||||
@ -56,44 +56,44 @@ describe('aggregateBrokerAccounts', () => {
|
||||
dailyPercent: 10,
|
||||
allocation: { shares: 1_650, bonds: 990, etf: 0, cash: 660, other: 0 },
|
||||
}),
|
||||
]);
|
||||
expect(result.cash).toEqual([{ currency: 'RUB', value: 500 }]);
|
||||
});
|
||||
])
|
||||
expect(result.cash).toEqual([{ currency: 'RUB', value: 500 }])
|
||||
})
|
||||
|
||||
it('keeps different currencies separate', () => {
|
||||
const result = aggregateBrokerAccounts([
|
||||
portfolio('rub', 'RUB', 1_100, 100, 200),
|
||||
portfolio('usd', 'USD', 550, 50, 25),
|
||||
]);
|
||||
])
|
||||
|
||||
expect(result.portfolios.map(({ currency, total }) => ({ currency, total }))).toEqual([
|
||||
{ currency: 'RUB', total: 1_100 },
|
||||
{ currency: 'USD', total: 550 },
|
||||
]);
|
||||
});
|
||||
])
|
||||
})
|
||||
|
||||
it('does not expose a daily percent when one account lacks daily data', () => {
|
||||
const result = aggregateBrokerAccounts([
|
||||
portfolio('a', 'RUB', 1_100, 100, 200),
|
||||
portfolio('b', 'RUB', 2_000, null, 300),
|
||||
]);
|
||||
])
|
||||
|
||||
expect(result.portfolios[0]).toMatchObject({ daily: null, dailyPercent: null });
|
||||
});
|
||||
expect(result.portfolios[0]).toMatchObject({ daily: null, dailyPercent: null })
|
||||
})
|
||||
|
||||
it('does not expose a daily percent when start of day is non-positive', () => {
|
||||
const result = aggregateBrokerAccounts([portfolio('a', 'RUB', 100, 100, 20)]);
|
||||
const result = aggregateBrokerAccounts([portfolio('a', 'RUB', 100, 100, 20)])
|
||||
|
||||
expect(result.portfolios[0]).toMatchObject({ daily: 100, dailyPercent: null });
|
||||
});
|
||||
expect(result.portfolios[0]).toMatchObject({ daily: 100, dailyPercent: null })
|
||||
})
|
||||
|
||||
it('clamps negative residual other allocation to zero', () => {
|
||||
const overAllocated = portfolio('a', 'RUB', 1_000, 50, 100);
|
||||
overAllocated.totals.shares!.value = 700;
|
||||
overAllocated.totals.bonds!.value = 400;
|
||||
overAllocated.totals.currencies!.value = 100;
|
||||
const overAllocated = portfolio('a', 'RUB', 1_000, 50, 100)
|
||||
overAllocated.totals.shares!.value = 700
|
||||
overAllocated.totals.bonds!.value = 400
|
||||
overAllocated.totals.currencies!.value = 100
|
||||
|
||||
const result = aggregateBrokerAccounts([overAllocated]);
|
||||
const result = aggregateBrokerAccounts([overAllocated])
|
||||
|
||||
expect(result.portfolios[0].allocation).toEqual({
|
||||
shares: 700,
|
||||
@ -101,27 +101,27 @@ describe('aggregateBrokerAccounts', () => {
|
||||
etf: 0,
|
||||
cash: 100,
|
||||
other: 0,
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
it('returns empty summaries for empty or unsupported portfolios', () => {
|
||||
const missingTotal = portfolio('a', 'RUB', 1_000, 50, 100);
|
||||
missingTotal.totals.portfolio = null;
|
||||
const missingTotal = portfolio('a', 'RUB', 1_000, 50, 100)
|
||||
missingTotal.totals.portfolio = null
|
||||
|
||||
expect(aggregateBrokerAccounts([])).toEqual({ portfolios: [], cash: [] });
|
||||
expect(aggregateBrokerAccounts([])).toEqual({ portfolios: [], cash: [] })
|
||||
expect(aggregateBrokerAccounts([missingTotal])).toEqual({
|
||||
portfolios: [],
|
||||
cash: [{ currency: 'RUB', value: 100 }],
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
it('groups cash separately by currency', () => {
|
||||
const mixedCash = portfolio('a', 'RUB', 1_000, 50, 100);
|
||||
mixedCash.cash.push({ currency: 'USD', units: '0', nano: 0, value: 25 });
|
||||
const mixedCash = portfolio('a', 'RUB', 1_000, 50, 100)
|
||||
mixedCash.cash.push({ currency: 'USD', units: '0', nano: 0, value: 25 })
|
||||
|
||||
expect(aggregateBrokerAccounts([mixedCash]).cash).toEqual([
|
||||
{ currency: 'RUB', value: 100 },
|
||||
{ currency: 'USD', value: 25 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,74 +1,74 @@
|
||||
import type { BrokerMoney, BrokerPortfolio } from '@/shared/api/responses';
|
||||
import type { BrokerMoney, BrokerPortfolio } from '@/shared/api/responses'
|
||||
|
||||
export interface BrokerCurrencyAllocationSummary {
|
||||
shares: number;
|
||||
bonds: number;
|
||||
etf: number;
|
||||
cash: number;
|
||||
other: number;
|
||||
shares: number
|
||||
bonds: number
|
||||
etf: number
|
||||
cash: number
|
||||
other: number
|
||||
}
|
||||
|
||||
export interface BrokerCurrencyPortfolioSummary {
|
||||
currency: string;
|
||||
total: number;
|
||||
daily: number | null;
|
||||
dailyPercent: number | null;
|
||||
allocation: BrokerCurrencyAllocationSummary;
|
||||
currency: string
|
||||
total: number
|
||||
daily: number | null
|
||||
dailyPercent: number | null
|
||||
allocation: BrokerCurrencyAllocationSummary
|
||||
}
|
||||
|
||||
export interface BrokerCurrencyCashSummary {
|
||||
currency: string;
|
||||
value: number;
|
||||
currency: string
|
||||
value: number
|
||||
}
|
||||
|
||||
export interface BrokerAccountsAggregate {
|
||||
portfolios: BrokerCurrencyPortfolioSummary[];
|
||||
cash: BrokerCurrencyCashSummary[];
|
||||
portfolios: BrokerCurrencyPortfolioSummary[]
|
||||
cash: BrokerCurrencyCashSummary[]
|
||||
}
|
||||
|
||||
interface MutableCurrencySummary {
|
||||
currency: string;
|
||||
total: number;
|
||||
daily: number | null;
|
||||
dailyComparable: boolean;
|
||||
allocation: BrokerCurrencyAllocationSummary;
|
||||
currency: string
|
||||
total: number
|
||||
daily: number | null
|
||||
dailyComparable: boolean
|
||||
allocation: BrokerCurrencyAllocationSummary
|
||||
}
|
||||
|
||||
function moneyValue(money: BrokerMoney | null | undefined): number {
|
||||
return money?.value ?? 0;
|
||||
return money?.value ?? 0
|
||||
}
|
||||
|
||||
export function brokerAccountTypeLabel(type: 'brokerage' | 'iis'): string {
|
||||
return type === 'iis' ? 'ИИС' : 'Брокерский счёт';
|
||||
return type === 'iis' ? 'ИИС' : 'Брокерский счёт'
|
||||
}
|
||||
|
||||
export function aggregateBrokerAccounts(portfolios: BrokerPortfolio[]): BrokerAccountsAggregate {
|
||||
const portfolioSummaries = new Map<string, MutableCurrencySummary>();
|
||||
const cashSummaries = new Map<string, BrokerCurrencyCashSummary>();
|
||||
const portfolioSummaries = new Map<string, MutableCurrencySummary>()
|
||||
const cashSummaries = new Map<string, BrokerCurrencyCashSummary>()
|
||||
|
||||
for (const portfolio of portfolios) {
|
||||
for (const cash of portfolio.cash) {
|
||||
if (!cash.currency) {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
|
||||
const existingCash = cashSummaries.get(cash.currency);
|
||||
const existingCash = cashSummaries.get(cash.currency)
|
||||
|
||||
if (existingCash) {
|
||||
existingCash.value += cash.value;
|
||||
existingCash.value += cash.value
|
||||
} else {
|
||||
cashSummaries.set(cash.currency, { currency: cash.currency, value: cash.value });
|
||||
cashSummaries.set(cash.currency, { currency: cash.currency, value: cash.value })
|
||||
}
|
||||
}
|
||||
|
||||
const totalMoney = portfolio.totals.portfolio;
|
||||
const currency = totalMoney?.currency;
|
||||
const totalMoney = portfolio.totals.portfolio
|
||||
const currency = totalMoney?.currency
|
||||
|
||||
if (!totalMoney || !currency) {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
|
||||
const existingSummary = portfolioSummaries.get(currency);
|
||||
const existingSummary = portfolioSummaries.get(currency)
|
||||
const summary =
|
||||
existingSummary ??
|
||||
({
|
||||
@ -77,45 +77,43 @@ export function aggregateBrokerAccounts(portfolios: BrokerPortfolio[]): BrokerAc
|
||||
daily: 0,
|
||||
dailyComparable: true,
|
||||
allocation: { shares: 0, bonds: 0, etf: 0, cash: 0, other: 0 },
|
||||
} satisfies MutableCurrencySummary);
|
||||
} satisfies MutableCurrencySummary)
|
||||
|
||||
const total = totalMoney.value;
|
||||
const shares = moneyValue(portfolio.totals.shares);
|
||||
const bonds = moneyValue(portfolio.totals.bonds);
|
||||
const etf = moneyValue(portfolio.totals.etf);
|
||||
const cash = moneyValue(portfolio.totals.currencies);
|
||||
const other = Math.max(0, total - shares - bonds - etf - cash);
|
||||
const total = totalMoney.value
|
||||
const shares = moneyValue(portfolio.totals.shares)
|
||||
const bonds = moneyValue(portfolio.totals.bonds)
|
||||
const etf = moneyValue(portfolio.totals.etf)
|
||||
const cash = moneyValue(portfolio.totals.currencies)
|
||||
const other = Math.max(0, total - shares - bonds - etf - cash)
|
||||
|
||||
summary.total += total;
|
||||
summary.allocation.shares += shares;
|
||||
summary.allocation.bonds += bonds;
|
||||
summary.allocation.etf += etf;
|
||||
summary.allocation.cash += cash;
|
||||
summary.allocation.other += other;
|
||||
summary.total += total
|
||||
summary.allocation.shares += shares
|
||||
summary.allocation.bonds += bonds
|
||||
summary.allocation.etf += etf
|
||||
summary.allocation.cash += cash
|
||||
summary.allocation.other += other
|
||||
|
||||
const dailyMoney = portfolio.yields.daily;
|
||||
const comparableDaily = dailyMoney && dailyMoney.currency === currency;
|
||||
const dailyMoney = portfolio.yields.daily
|
||||
const comparableDaily = dailyMoney && dailyMoney.currency === currency
|
||||
|
||||
if (!comparableDaily) {
|
||||
summary.daily = null;
|
||||
summary.dailyComparable = false;
|
||||
summary.daily = null
|
||||
summary.dailyComparable = false
|
||||
} else if (summary.dailyComparable) {
|
||||
summary.daily = (summary.daily ?? 0) + dailyMoney.value;
|
||||
summary.daily = (summary.daily ?? 0) + dailyMoney.value
|
||||
}
|
||||
|
||||
if (!existingSummary) {
|
||||
portfolioSummaries.set(currency, summary);
|
||||
portfolioSummaries.set(currency, summary)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
portfolios: Array.from(portfolioSummaries.values()).map((summary) => {
|
||||
const daily = summary.dailyComparable ? summary.daily : null;
|
||||
const startOfDay = daily === null ? null : summary.total - daily;
|
||||
const daily = summary.dailyComparable ? summary.daily : null
|
||||
const startOfDay = daily === null ? null : summary.total - daily
|
||||
const dailyPercent =
|
||||
daily === null || startOfDay === null || startOfDay <= 0
|
||||
? null
|
||||
: (daily / startOfDay) * 100;
|
||||
daily === null || startOfDay === null || startOfDay <= 0 ? null : (daily / startOfDay) * 100
|
||||
|
||||
return {
|
||||
currency: summary.currency,
|
||||
@ -123,8 +121,8 @@ export function aggregateBrokerAccounts(portfolios: BrokerPortfolio[]): BrokerAc
|
||||
daily,
|
||||
dailyPercent,
|
||||
allocation: summary.allocation,
|
||||
};
|
||||
}
|
||||
}),
|
||||
cash: Array.from(cashSummaries.values()),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { useQueries } from '@tanstack/react-query';
|
||||
import type { BrokerAccount, BrokerPortfolio } from '@/shared/api/responses';
|
||||
import { getBrokerPortfolio } from '../api/brokerAccountApi';
|
||||
import { useQueries } from '@tanstack/react-query'
|
||||
import type { BrokerAccount, BrokerPortfolio } from '@/shared/api/responses'
|
||||
import { getBrokerPortfolio } from '../api/brokerAccountApi'
|
||||
|
||||
export function useBrokerAccountPortfolios(accounts: BrokerAccount[]) {
|
||||
const queries = useQueries({
|
||||
@ -11,7 +11,7 @@ export function useBrokerAccountPortfolios(accounts: BrokerAccount[]) {
|
||||
retry: 2,
|
||||
refetchOnWindowFocus: false,
|
||||
})),
|
||||
});
|
||||
})
|
||||
|
||||
return accounts.map((account, index) => ({ account, query: queries[index] }));
|
||||
return accounts.map((account, index) => ({ account, query: queries[index] }))
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import type { BrokerAccount } from '@/shared/api/responses';
|
||||
import { getBrokerAccounts } from '../api/brokerAccountApi';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { BrokerAccount } from '@/shared/api/responses'
|
||||
import { getBrokerAccounts } from '../api/brokerAccountApi'
|
||||
|
||||
export function useBrokerAccounts() {
|
||||
return useQuery<BrokerAccount[]>({
|
||||
@ -9,5 +9,5 @@ export function useBrokerAccounts() {
|
||||
staleTime: 3_600_000,
|
||||
retry: 2,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import type { BrokerPortfolio } from '@/shared/api/responses';
|
||||
import { getBrokerPortfolio } from '../api/brokerAccountApi';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { BrokerPortfolio } from '@/shared/api/responses'
|
||||
import { getBrokerPortfolio } from '../api/brokerAccountApi'
|
||||
|
||||
export function useBrokerPortfolio(accountId: string | undefined) {
|
||||
return useQuery<BrokerPortfolio>({
|
||||
@ -10,5 +10,5 @@ export function useBrokerPortfolio(accountId: string | undefined) {
|
||||
staleTime: 60_000,
|
||||
retry: 2,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import { request } from '@/shared/api/client';
|
||||
import type { ApiResponseMeta, BrokerEventsData } from '@/shared/api/responses';
|
||||
import { request } from '@/shared/api/kyClient'
|
||||
import type { ApiResponseMeta, BrokerEventsData } from '@/shared/api/responses'
|
||||
|
||||
export type BrokerEventsQuery = {
|
||||
from: string;
|
||||
to: string;
|
||||
types?: string;
|
||||
};
|
||||
from: string
|
||||
to: string
|
||||
types?: string
|
||||
}
|
||||
|
||||
export function getBrokerEvents(
|
||||
accountId: string,
|
||||
@ -18,5 +18,5 @@ export function getBrokerEvents(
|
||||
to: query.to,
|
||||
types: query.types,
|
||||
},
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
export { getBrokerEvents, type BrokerEventsQuery } from './api/brokerEventApi';
|
||||
export { useBrokerEvents } from './model/useBrokerEvents';
|
||||
export { type BrokerEventsQuery, getBrokerEvents } from './api/brokerEventApi'
|
||||
export { useBrokerEvents } from './model/useBrokerEvents'
|
||||
|
||||
@ -1,20 +1,20 @@
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { type ReactNode } from 'react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { getBrokerEvents } from '../api/brokerEventApi';
|
||||
import { useBrokerEvents } from './useBrokerEvents';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import type { ReactNode } from 'react'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { getBrokerEvents } from '../api/brokerEventApi'
|
||||
import { useBrokerEvents } from './useBrokerEvents'
|
||||
|
||||
vi.mock('../api/brokerEventApi', () => ({
|
||||
getBrokerEvents: vi.fn(),
|
||||
}));
|
||||
}))
|
||||
|
||||
function createWrapper(queryClient?: QueryClient) {
|
||||
const client = queryClient ?? new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const client = queryClient ?? new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
|
||||
return function Wrapper({ children }: { children: ReactNode }) {
|
||||
return <QueryClientProvider client={client}>{children}</QueryClientProvider>;
|
||||
};
|
||||
return <QueryClientProvider client={client}>{children}</QueryClientProvider>
|
||||
}
|
||||
}
|
||||
|
||||
const mockEventsData = {
|
||||
@ -53,55 +53,55 @@ const mockEventsData = {
|
||||
currency: 'RUB' as const,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const query = { from: '2026-06-22', to: '2026-06-29' };
|
||||
const query = { from: '2026-06-22', to: '2026-06-29' }
|
||||
|
||||
describe('useBrokerEvents', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('returns events data from API', async () => {
|
||||
vi.mocked(getBrokerEvents).mockResolvedValue({
|
||||
data: mockEventsData,
|
||||
meta: { fromCache: false, cachedAt: null },
|
||||
});
|
||||
})
|
||||
|
||||
const { result } = renderHook(() => useBrokerEvents('acc-1', query), {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
})
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
expect(result.current.data?.summary.eventCount).toBe(3);
|
||||
expect(getBrokerEvents).toHaveBeenCalledWith('acc-1', query);
|
||||
});
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(result.current.data?.summary.eventCount).toBe(3)
|
||||
expect(getBrokerEvents).toHaveBeenCalledWith('acc-1', query)
|
||||
})
|
||||
|
||||
it('reuses cache when query key matches', async () => {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
|
||||
queryClient.setQueryData(
|
||||
['broker', 'events', 'acc-1', '2026-06-22', '2026-06-29', 'dividend,coupon'],
|
||||
mockEventsData,
|
||||
);
|
||||
)
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useBrokerEvents('acc-1', { ...query, types: 'dividend,coupon' }),
|
||||
{
|
||||
wrapper: createWrapper(queryClient),
|
||||
},
|
||||
);
|
||||
)
|
||||
|
||||
await waitFor(() => expect(result.current.data).toBe(mockEventsData));
|
||||
expect(getBrokerEvents).not.toHaveBeenCalled();
|
||||
});
|
||||
await waitFor(() => expect(result.current.data).toBe(mockEventsData))
|
||||
expect(getBrokerEvents).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('is not enabled when accountId is undefined', async () => {
|
||||
const { result } = renderHook(() => useBrokerEvents(undefined, query), {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
})
|
||||
|
||||
expect(result.current.isPending).toBe(true);
|
||||
expect(getBrokerEvents).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
expect(result.current.isPending).toBe(true)
|
||||
expect(getBrokerEvents).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import type { BrokerEventsData } from '@/shared/api/responses';
|
||||
import { getBrokerEvents, type BrokerEventsQuery } from '../api/brokerEventApi';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { BrokerEventsData } from '@/shared/api/responses'
|
||||
import { type BrokerEventsQuery, getBrokerEvents } from '../api/brokerEventApi'
|
||||
|
||||
export function useBrokerEvents(accountId: string | undefined, query: BrokerEventsQuery) {
|
||||
const { from, to, types } = query;
|
||||
const { from, to, types } = query
|
||||
return useQuery<BrokerEventsData>({
|
||||
queryKey: ['broker', 'events', accountId, from, to, types],
|
||||
enabled: Boolean(accountId),
|
||||
@ -11,5 +11,5 @@ export function useBrokerEvents(accountId: string | undefined, query: BrokerEven
|
||||
staleTime: 300_000,
|
||||
retry: 2,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
import { request } from '@/shared/api/client';
|
||||
import type { ApiResponseMeta, BrokerOperationsPage } from '@/shared/api/responses';
|
||||
import { request } from '@/shared/api/kyClient'
|
||||
import type { ApiResponseMeta, BrokerOperationsPage } from '@/shared/api/responses'
|
||||
|
||||
export type BrokerOperationQuery = {
|
||||
from?: string;
|
||||
to?: string;
|
||||
cursor?: string;
|
||||
limit?: number;
|
||||
instrumentId?: string;
|
||||
operationTypes?: string;
|
||||
state?: string;
|
||||
};
|
||||
from?: string
|
||||
to?: string
|
||||
cursor?: string
|
||||
limit?: number
|
||||
instrumentId?: string
|
||||
operationTypes?: string
|
||||
state?: string
|
||||
}
|
||||
|
||||
export function getBrokerOperations(
|
||||
accountId: string,
|
||||
@ -26,5 +26,5 @@ export function getBrokerOperations(
|
||||
operationTypes: query.operationTypes,
|
||||
state: query.state,
|
||||
},
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
export { getBrokerOperations, type BrokerOperationQuery } from './api/brokerOperationApi';
|
||||
export { type BrokerOperationQuery, getBrokerOperations } from './api/brokerOperationApi'
|
||||
export {
|
||||
BROKER_OPERATION_TYPE_OPTIONS,
|
||||
type BrokerOperationImpact,
|
||||
getBrokerOperationImpact,
|
||||
getBrokerOperationTypeLabel,
|
||||
isBrokerOperationType,
|
||||
type BrokerOperationImpact,
|
||||
} from './model/operationFilters';
|
||||
export { useBrokerOperations } from './model/useBrokerOperations';
|
||||
} from './model/operationFilters'
|
||||
export { useBrokerOperations } from './model/useBrokerOperations'
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { BROKER_OPERATION_TYPE_OPTIONS, isBrokerOperationType } from '../model/operationFilters';
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { BROKER_OPERATION_TYPE_OPTIONS, isBrokerOperationType } from '../model/operationFilters'
|
||||
|
||||
describe('operationFilters', () => {
|
||||
it('accepts only declared broker operation types', () => {
|
||||
expect(isBrokerOperationType('OPERATION_TYPE_BUY')).toBe(true);
|
||||
expect(isBrokerOperationType('unexpected')).toBe(false);
|
||||
});
|
||||
expect(isBrokerOperationType('OPERATION_TYPE_BUY')).toBe(true)
|
||||
expect(isBrokerOperationType('unexpected')).toBe(false)
|
||||
})
|
||||
|
||||
it('keeps operation type option values unique and labels sorted for the filter', () => {
|
||||
const values = BROKER_OPERATION_TYPE_OPTIONS.map(({ value }) => value);
|
||||
const labels = BROKER_OPERATION_TYPE_OPTIONS.map(({ label }) => label);
|
||||
const values = BROKER_OPERATION_TYPE_OPTIONS.map(({ value }) => value)
|
||||
const labels = BROKER_OPERATION_TYPE_OPTIONS.map(({ label }) => label)
|
||||
|
||||
expect(new Set(values).size).toBe(values.length);
|
||||
expect(labels).toEqual([...labels].sort((left, right) => left.localeCompare(right, 'ru')));
|
||||
});
|
||||
});
|
||||
expect(new Set(values).size).toBe(values.length)
|
||||
expect(labels).toEqual([...labels].sort((left, right) => left.localeCompare(right, 'ru')))
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import type { BrokerOperation } from '@/shared/api/responses';
|
||||
import type { BrokerOperation } from '@/shared/api/responses'
|
||||
|
||||
export type BrokerOperationImpact = 'adds' | 'reduces' | 'neutral' | 'unknown';
|
||||
export type BrokerOperationImpact = 'adds' | 'reduces' | 'neutral' | 'unknown'
|
||||
|
||||
const TRADE_TYPES = new Set([
|
||||
'OPERATION_TYPE_BUY',
|
||||
@ -11,14 +11,14 @@ const TRADE_TYPES = new Set([
|
||||
'OPERATION_TYPE_SELL_MARGIN',
|
||||
'OPERATION_TYPE_DELIVERY_BUY',
|
||||
'OPERATION_TYPE_DELIVERY_SELL',
|
||||
]);
|
||||
])
|
||||
|
||||
const BOND_REPAYMENT_TYPES = new Set([
|
||||
'OPERATION_TYPE_BOND_REPAYMENT',
|
||||
'OPERATION_TYPE_BOND_REPAYMENT_FULL',
|
||||
]);
|
||||
])
|
||||
|
||||
const INCOME_TYPES = new Set(['OPERATION_TYPE_COUPON', 'OPERATION_TYPE_DIVIDEND']);
|
||||
const INCOME_TYPES = new Set(['OPERATION_TYPE_COUPON', 'OPERATION_TYPE_DIVIDEND'])
|
||||
|
||||
const TAX_TYPES = new Set([
|
||||
'OPERATION_TYPE_TAX',
|
||||
@ -26,35 +26,35 @@ const TAX_TYPES = new Set([
|
||||
'OPERATION_TYPE_DIVIDEND_TAX',
|
||||
'OPERATION_TYPE_TAX_CORRECTION',
|
||||
'OPERATION_TYPE_TAX_CORRECTION_COUPON',
|
||||
]);
|
||||
])
|
||||
|
||||
const FEE_TYPES = new Set([
|
||||
'OPERATION_TYPE_BROKER_FEE',
|
||||
'OPERATION_TYPE_SERVICE_FEE',
|
||||
'OPERATION_TYPE_MARGIN_FEE',
|
||||
'OPERATION_TYPE_SUCCESS_FEE',
|
||||
]);
|
||||
])
|
||||
|
||||
const TRANSFER_INPUT_TYPES = new Set([
|
||||
'OPERATION_TYPE_INPUT',
|
||||
'OPERATION_TYPE_INPUT_SWIFT',
|
||||
'OPERATION_TYPE_INPUT_ACQUIRING',
|
||||
'OPERATION_TYPE_INP_MULTI',
|
||||
]);
|
||||
])
|
||||
|
||||
const TRANSFER_OUTPUT_TYPES = new Set([
|
||||
'OPERATION_TYPE_OUTPUT',
|
||||
'OPERATION_TYPE_OUTPUT_SWIFT',
|
||||
'OPERATION_TYPE_OUTPUT_ACQUIRING',
|
||||
'OPERATION_TYPE_OUT_MULTI',
|
||||
]);
|
||||
])
|
||||
|
||||
const SECURITY_TRANSFER_TYPES = new Set([
|
||||
'OPERATION_TYPE_INPUT_SECURITIES',
|
||||
'OPERATION_TYPE_OUTPUT_SECURITIES',
|
||||
'OPERATION_TYPE_TRANS_IIS_BS',
|
||||
'OPERATION_TYPE_TRANS_BS_BS',
|
||||
]);
|
||||
])
|
||||
|
||||
const OPERATION_TYPE_LABELS: Record<string, string> = {
|
||||
OPERATION_TYPE_BUY: 'Покупка',
|
||||
@ -82,7 +82,7 @@ const OPERATION_TYPE_LABELS: Record<string, string> = {
|
||||
OPERATION_TYPE_OUTPUT: 'Вывод средств',
|
||||
OPERATION_TYPE_INPUT_SECURITIES: 'Зачисление бумаг',
|
||||
OPERATION_TYPE_OUTPUT_SECURITIES: 'Списание бумаг',
|
||||
};
|
||||
}
|
||||
|
||||
export const BROKER_OPERATION_TYPE_OPTIONS: ReadonlyArray<
|
||||
Readonly<{ value: string; label: string }>
|
||||
@ -90,25 +90,25 @@ export const BROKER_OPERATION_TYPE_OPTIONS: ReadonlyArray<
|
||||
Object.entries(OPERATION_TYPE_LABELS)
|
||||
.map(([value, label]) => Object.freeze({ value, label }))
|
||||
.sort((left, right) => left.label.localeCompare(right.label, 'ru')),
|
||||
);
|
||||
)
|
||||
|
||||
const BROKER_OPERATION_TYPES = new Set(BROKER_OPERATION_TYPE_OPTIONS.map(({ value }) => value));
|
||||
const BROKER_OPERATION_TYPES = new Set(BROKER_OPERATION_TYPE_OPTIONS.map(({ value }) => value))
|
||||
|
||||
export function isBrokerOperationType(value: string | null): value is string {
|
||||
return value !== null && BROKER_OPERATION_TYPES.has(value);
|
||||
return value !== null && BROKER_OPERATION_TYPES.has(value)
|
||||
}
|
||||
|
||||
export function getBrokerOperationTypeLabel(
|
||||
operation: Pick<BrokerOperation, 'type' | 'description'>,
|
||||
): string {
|
||||
const knownLabel = OPERATION_TYPE_LABELS[operation.type];
|
||||
if (knownLabel) return knownLabel;
|
||||
if (operation.description) return operation.description;
|
||||
const knownLabel = OPERATION_TYPE_LABELS[operation.type]
|
||||
if (knownLabel) return knownLabel
|
||||
if (operation.description) return operation.description
|
||||
|
||||
return operation.type
|
||||
.replace(/^OPERATION_TYPE_/, '')
|
||||
.replace(/_/g, ' ')
|
||||
.toLowerCase();
|
||||
.toLowerCase()
|
||||
}
|
||||
|
||||
export function getBrokerOperationImpact(
|
||||
@ -119,15 +119,15 @@ export function getBrokerOperationImpact(
|
||||
BOND_REPAYMENT_TYPES.has(operation.type) ||
|
||||
SECURITY_TRANSFER_TYPES.has(operation.type)
|
||||
) {
|
||||
return 'neutral';
|
||||
return 'neutral'
|
||||
}
|
||||
|
||||
if (INCOME_TYPES.has(operation.type)) return 'adds';
|
||||
if (TAX_TYPES.has(operation.type) || FEE_TYPES.has(operation.type)) return 'reduces';
|
||||
if (TRANSFER_INPUT_TYPES.has(operation.type)) return 'adds';
|
||||
if (TRANSFER_OUTPUT_TYPES.has(operation.type)) return 'reduces';
|
||||
if (operation.category === 'tax' || operation.category === 'fee') return 'reduces';
|
||||
if (operation.category === 'income' && (operation.payment?.value ?? 0) > 0) return 'adds';
|
||||
if (INCOME_TYPES.has(operation.type)) return 'adds'
|
||||
if (TAX_TYPES.has(operation.type) || FEE_TYPES.has(operation.type)) return 'reduces'
|
||||
if (TRANSFER_INPUT_TYPES.has(operation.type)) return 'adds'
|
||||
if (TRANSFER_OUTPUT_TYPES.has(operation.type)) return 'reduces'
|
||||
if (operation.category === 'tax' || operation.category === 'fee') return 'reduces'
|
||||
if (operation.category === 'income' && (operation.payment?.value ?? 0) > 0) return 'adds'
|
||||
|
||||
return 'unknown';
|
||||
return 'unknown'
|
||||
}
|
||||
|
||||
@ -1,26 +1,26 @@
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { type ReactNode } from 'react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { getBrokerOperations } from '../api/brokerOperationApi';
|
||||
import { useBrokerOperations } from '../model/useBrokerOperations';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import type { ReactNode } from 'react'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { getBrokerOperations } from '../api/brokerOperationApi'
|
||||
import { useBrokerOperations } from '../model/useBrokerOperations'
|
||||
|
||||
vi.mock('../api/brokerOperationApi', () => ({
|
||||
getBrokerOperations: vi.fn(),
|
||||
}));
|
||||
}))
|
||||
|
||||
function createWrapper(queryClient?: QueryClient) {
|
||||
const client = queryClient ?? new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const client = queryClient ?? new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
|
||||
return function Wrapper({ children }: { children: ReactNode }) {
|
||||
return <QueryClientProvider client={client}>{children}</QueryClientProvider>;
|
||||
};
|
||||
return <QueryClientProvider client={client}>{children}</QueryClientProvider>
|
||||
}
|
||||
}
|
||||
|
||||
describe('useBrokerOperations', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('returns operations page data from API', async () => {
|
||||
vi.mocked(getBrokerOperations).mockResolvedValue({
|
||||
@ -32,34 +32,34 @@ describe('useBrokerOperations', () => {
|
||||
asOf: '2026-06-19T00:00:00.000Z',
|
||||
},
|
||||
meta: { fromCache: false, cachedAt: null },
|
||||
});
|
||||
})
|
||||
|
||||
const { result } = renderHook(() => useBrokerOperations('acc-1', { limit: 5 }), {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
})
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
expect(result.current.data?.accountId).toBe('acc-1');
|
||||
expect(getBrokerOperations).toHaveBeenCalledWith('acc-1', { limit: 5 });
|
||||
});
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(result.current.data?.accountId).toBe('acc-1')
|
||||
expect(getBrokerOperations).toHaveBeenCalledWith('acc-1', { limit: 5 })
|
||||
})
|
||||
|
||||
it('reuses the broker operations cache key across the account overview and full history pages', async () => {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
const cachedPage = {
|
||||
accountId: 'acc-1',
|
||||
items: [],
|
||||
nextCursor: null,
|
||||
hasNext: false,
|
||||
asOf: '2026-06-19T00:00:00.000Z',
|
||||
};
|
||||
}
|
||||
|
||||
queryClient.setQueryData(['broker', 'operations', 'acc-1', { limit: 5 }], cachedPage);
|
||||
queryClient.setQueryData(['broker', 'operations', 'acc-1', { limit: 5 }], cachedPage)
|
||||
|
||||
const { result } = renderHook(() => useBrokerOperations('acc-1', { limit: 5 }), {
|
||||
wrapper: createWrapper(queryClient),
|
||||
});
|
||||
})
|
||||
|
||||
await waitFor(() => expect(result.current.data).toBe(cachedPage));
|
||||
expect(getBrokerOperations).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
await waitFor(() => expect(result.current.data).toBe(cachedPage))
|
||||
expect(getBrokerOperations).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { keepPreviousData, useQuery } from '@tanstack/react-query';
|
||||
import type { BrokerOperationsPage } from '@/shared/api/responses';
|
||||
import { getBrokerOperations, type BrokerOperationQuery } from '../api/brokerOperationApi';
|
||||
import { keepPreviousData, useQuery } from '@tanstack/react-query'
|
||||
import type { BrokerOperationsPage } from '@/shared/api/responses'
|
||||
import { type BrokerOperationQuery, getBrokerOperations } from '../api/brokerOperationApi'
|
||||
|
||||
export function useBrokerOperations(
|
||||
accountId: string | undefined,
|
||||
@ -14,5 +14,5 @@ export function useBrokerOperations(
|
||||
retry: 2,
|
||||
placeholderData: keepPreviousData,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { request } from '@/shared/api/client';
|
||||
import type { ApiResponseMeta, BrokerPositionsPage } from '@/shared/api/responses';
|
||||
import { request } from '@/shared/api/kyClient'
|
||||
import type { ApiResponseMeta, BrokerPositionsPage } from '@/shared/api/responses'
|
||||
|
||||
export function getBrokerPositions(
|
||||
accountId: string,
|
||||
@ -12,5 +12,5 @@ export function getBrokerPositions(
|
||||
limit: query.limit ? String(query.limit) : undefined,
|
||||
type: query.type,
|
||||
},
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
export { getBrokerPositions } from './api/brokerPositionApi';
|
||||
export { getBrokerPositions } from './api/brokerPositionApi'
|
||||
export {
|
||||
buildBrokerAllocation,
|
||||
type BrokerAllocationItem,
|
||||
type BrokerAllocationKey,
|
||||
} from './model/brokerAllocation';
|
||||
buildBrokerAllocation,
|
||||
} from './model/brokerAllocation'
|
||||
export {
|
||||
type BrokerPositionGroup,
|
||||
getBrokerInstrumentPath,
|
||||
getBrokerPositionGroup,
|
||||
type BrokerPositionGroup,
|
||||
} from './model/brokerDisplay';
|
||||
export { useBrokerPositions } from './model/useBrokerPositions';
|
||||
} from './model/brokerDisplay'
|
||||
export { useBrokerPositions } from './model/useBrokerPositions'
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { BrokerMoney, BrokerPortfolio } from '@/shared/api/responses';
|
||||
import { buildBrokerAllocation } from './brokerAllocation';
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { BrokerMoney, BrokerPortfolio } from '@/shared/api/responses'
|
||||
import { buildBrokerAllocation } from './brokerAllocation'
|
||||
|
||||
function money(value: number): BrokerMoney {
|
||||
return {
|
||||
@ -8,16 +8,16 @@ function money(value: number): BrokerMoney {
|
||||
units: String(Math.trunc(value)),
|
||||
nano: 0,
|
||||
value,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function portfolio(
|
||||
values: Partial<Record<'shares' | 'bonds' | 'etf' | 'currencies' | 'portfolio', number | null>>,
|
||||
): BrokerPortfolio {
|
||||
const total = (key: keyof typeof values): BrokerMoney | null => {
|
||||
const value = values[key];
|
||||
return value == null ? null : money(value);
|
||||
};
|
||||
const value = values[key]
|
||||
return value == null ? null : money(value)
|
||||
}
|
||||
|
||||
return {
|
||||
account: {
|
||||
@ -53,7 +53,7 @@ function portfolio(
|
||||
cash: [],
|
||||
blockedCash: [],
|
||||
asOf: '2025-01-01T00:00:00.000Z',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
describe('buildBrokerAllocation', () => {
|
||||
@ -72,73 +72,71 @@ describe('buildBrokerAllocation', () => {
|
||||
{ key: 'other', label: 'Прочие', value: 50, percent: 5, color: '#aeb6c5' },
|
||||
],
|
||||
negative: [],
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
it('omits zero-value sectors', () => {
|
||||
const result = buildBrokerAllocation(
|
||||
portfolio({ shares: 600, bonds: 0, etf: null, currencies: 400, portfolio: 1000 }),
|
||||
);
|
||||
)
|
||||
|
||||
expect(result.sectors.map(({ key }) => key)).toEqual(['shares', 'cash']);
|
||||
expect(result.negative).toEqual([]);
|
||||
});
|
||||
expect(result.sectors.map(({ key }) => key)).toEqual(['shares', 'cash'])
|
||||
expect(result.negative).toEqual([])
|
||||
})
|
||||
|
||||
it('reports a negative residual outside the sectors', () => {
|
||||
const result = buildBrokerAllocation(
|
||||
portfolio({ shares: 700, bonds: 300, etf: 100, currencies: 50, portfolio: 1000 }),
|
||||
);
|
||||
)
|
||||
|
||||
expect(result.sectors.map(({ key }) => key)).toEqual(['shares', 'bonds', 'etf', 'cash']);
|
||||
expect(result.sectors.map(({ key }) => key)).toEqual(['shares', 'bonds', 'etf', 'cash'])
|
||||
expect(result.negative).toEqual([
|
||||
{ key: 'other', label: 'Прочие', value: -150, color: '#aeb6c5' },
|
||||
]);
|
||||
});
|
||||
])
|
||||
})
|
||||
|
||||
it('ignores a tiny negative residual caused by decimal arithmetic', () => {
|
||||
const result = buildBrokerAllocation(portfolio({ shares: 0.1, bonds: 0.2, portfolio: 0.3 }));
|
||||
const result = buildBrokerAllocation(portfolio({ shares: 0.1, bonds: 0.2, portfolio: 0.3 }))
|
||||
|
||||
expect(result.sectors.map(({ key }) => key)).toEqual(['shares', 'bonds']);
|
||||
expect(result.negative).toEqual([]);
|
||||
});
|
||||
expect(result.sectors.map(({ key }) => key)).toEqual(['shares', 'bonds'])
|
||||
expect(result.negative).toEqual([])
|
||||
})
|
||||
|
||||
it('ignores a tiny positive residual caused by decimal arithmetic', () => {
|
||||
const result = buildBrokerAllocation(
|
||||
portfolio({ shares: 0.3, portfolio: 0.30000000000000004 }),
|
||||
);
|
||||
const result = buildBrokerAllocation(portfolio({ shares: 0.3, portfolio: 0.30000000000000004 }))
|
||||
|
||||
expect(result.sectors.map(({ key }) => key)).toEqual(['shares']);
|
||||
expect(result.negative).toEqual([]);
|
||||
});
|
||||
expect(result.sectors.map(({ key }) => key)).toEqual(['shares'])
|
||||
expect(result.negative).toEqual([])
|
||||
})
|
||||
|
||||
it('returns no allocation for missing or nonpositive portfolio totals', () => {
|
||||
expect(buildBrokerAllocation(portfolio({ shares: 100, portfolio: null }))).toEqual({
|
||||
total: 0,
|
||||
sectors: [],
|
||||
negative: [],
|
||||
});
|
||||
})
|
||||
expect(buildBrokerAllocation(portfolio({ shares: 100, portfolio: 0 }))).toEqual({
|
||||
total: 0,
|
||||
sectors: [],
|
||||
negative: [],
|
||||
});
|
||||
})
|
||||
expect(buildBrokerAllocation(portfolio({ shares: 100, portfolio: -10 }))).toEqual({
|
||||
total: -10,
|
||||
sectors: [],
|
||||
negative: [],
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
it('preserves named negative components when the portfolio total is nonpositive', () => {
|
||||
expect(buildBrokerAllocation(portfolio({ shares: 100, bonds: -20, portfolio: 0 }))).toEqual({
|
||||
total: 0,
|
||||
sectors: [],
|
||||
negative: [{ key: 'bonds', label: 'Облигации', value: -20, color: '#e5a33c' }],
|
||||
});
|
||||
})
|
||||
expect(buildBrokerAllocation(portfolio({ currencies: -30, etf: 5, portfolio: -10 }))).toEqual({
|
||||
total: -10,
|
||||
sectors: [],
|
||||
negative: [{ key: 'cash', label: 'Деньги', value: -30, color: '#7b63cf' }],
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
import type { BrokerPortfolio } from '@/shared/api/responses';
|
||||
import type { BrokerPortfolio } from '@/shared/api/responses'
|
||||
|
||||
export type BrokerAllocationKey = 'shares' | 'bonds' | 'etf' | 'cash' | 'other';
|
||||
export type BrokerAllocationKey = 'shares' | 'bonds' | 'etf' | 'cash' | 'other'
|
||||
|
||||
export interface BrokerAllocationItem {
|
||||
key: BrokerAllocationKey;
|
||||
label: string;
|
||||
value: number;
|
||||
percent: number;
|
||||
color: string;
|
||||
key: BrokerAllocationKey
|
||||
label: string
|
||||
value: number
|
||||
percent: number
|
||||
color: string
|
||||
}
|
||||
|
||||
type BrokerNegativeAllocationItem = Omit<BrokerAllocationItem, 'percent'>;
|
||||
type BrokerNegativeAllocationItem = Omit<BrokerAllocationItem, 'percent'>
|
||||
|
||||
const ALLOCATION_CONFIG: Array<Pick<BrokerAllocationItem, 'key' | 'label' | 'color'>> = [
|
||||
{ key: 'shares', label: 'Акции', color: '#4969f5' },
|
||||
@ -18,40 +18,40 @@ const ALLOCATION_CONFIG: Array<Pick<BrokerAllocationItem, 'key' | 'label' | 'col
|
||||
{ key: 'etf', label: 'ETF/фонды', color: '#62b889' },
|
||||
{ key: 'cash', label: 'Деньги', color: '#7b63cf' },
|
||||
{ key: 'other', label: 'Прочие', color: '#aeb6c5' },
|
||||
];
|
||||
]
|
||||
|
||||
export function buildBrokerAllocation(portfolio: BrokerPortfolio): {
|
||||
total: number;
|
||||
sectors: BrokerAllocationItem[];
|
||||
negative: BrokerNegativeAllocationItem[];
|
||||
total: number
|
||||
sectors: BrokerAllocationItem[]
|
||||
negative: BrokerNegativeAllocationItem[]
|
||||
} {
|
||||
const total = portfolio.totals.portfolio?.value ?? 0;
|
||||
const shares = portfolio.totals.shares?.value ?? 0;
|
||||
const bonds = portfolio.totals.bonds?.value ?? 0;
|
||||
const etf = portfolio.totals.etf?.value ?? 0;
|
||||
const cash = portfolio.totals.currencies?.value ?? 0;
|
||||
const total = portfolio.totals.portfolio?.value ?? 0
|
||||
const shares = portfolio.totals.shares?.value ?? 0
|
||||
const bonds = portfolio.totals.bonds?.value ?? 0
|
||||
const etf = portfolio.totals.etf?.value ?? 0
|
||||
const cash = portfolio.totals.currencies?.value ?? 0
|
||||
const namedValues: Record<Exclude<BrokerAllocationKey, 'other'>, number> = {
|
||||
shares,
|
||||
bonds,
|
||||
etf,
|
||||
cash,
|
||||
};
|
||||
}
|
||||
|
||||
if (total <= 0) {
|
||||
const negative = ALLOCATION_CONFIG.filter(
|
||||
(
|
||||
item,
|
||||
): item is (typeof ALLOCATION_CONFIG)[number] & {
|
||||
key: Exclude<BrokerAllocationKey, 'other'>;
|
||||
key: Exclude<BrokerAllocationKey, 'other'>
|
||||
} => item.key !== 'other',
|
||||
)
|
||||
.filter((item) => namedValues[item.key] < 0)
|
||||
.map((item) => ({ ...item, value: namedValues[item.key] }));
|
||||
return { total, sectors: [], negative };
|
||||
.map((item) => ({ ...item, value: namedValues[item.key] }))
|
||||
return { total, sectors: [], negative }
|
||||
}
|
||||
|
||||
const mappedTotal = shares + bonds + etf + cash;
|
||||
const residual = total - mappedTotal;
|
||||
const mappedTotal = shares + bonds + etf + cash
|
||||
const residual = total - mappedTotal
|
||||
const residualTolerance =
|
||||
Number.EPSILON *
|
||||
Math.max(
|
||||
@ -59,27 +59,27 @@ export function buildBrokerAllocation(portfolio: BrokerPortfolio): {
|
||||
Math.abs(total),
|
||||
Math.abs(shares) + Math.abs(bonds) + Math.abs(etf) + Math.abs(cash),
|
||||
) *
|
||||
8;
|
||||
8
|
||||
const values: Record<BrokerAllocationKey, number> = {
|
||||
shares,
|
||||
bonds,
|
||||
etf,
|
||||
cash,
|
||||
other: Math.abs(residual) <= residualTolerance ? 0 : residual,
|
||||
};
|
||||
}
|
||||
|
||||
const sectors: BrokerAllocationItem[] = [];
|
||||
const negative: BrokerNegativeAllocationItem[] = [];
|
||||
const sectors: BrokerAllocationItem[] = []
|
||||
const negative: BrokerNegativeAllocationItem[] = []
|
||||
|
||||
for (const item of ALLOCATION_CONFIG) {
|
||||
const value = values[item.key];
|
||||
const value = values[item.key]
|
||||
|
||||
if (value > 0) {
|
||||
sectors.push({ ...item, value, percent: (value / total) * 100 });
|
||||
sectors.push({ ...item, value, percent: (value / total) * 100 })
|
||||
} else if (value < 0) {
|
||||
negative.push({ ...item, value });
|
||||
negative.push({ ...item, value })
|
||||
}
|
||||
}
|
||||
|
||||
return { total, sectors, negative };
|
||||
return { total, sectors, negative }
|
||||
}
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { BrokerOperation, BrokerPosition } from '@/shared/api/responses';
|
||||
import { getBrokerInstrumentPath, getBrokerPositionGroup } from './brokerDisplay';
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
BROKER_OPERATION_TYPE_OPTIONS,
|
||||
getBrokerOperationImpact,
|
||||
getBrokerOperationTypeLabel,
|
||||
isBrokerOperationType,
|
||||
} from '@/entities/broker-operation';
|
||||
} from '@/entities/broker-operation'
|
||||
import type { BrokerOperation, BrokerPosition } from '@/shared/api/responses'
|
||||
import { getBrokerInstrumentPath, getBrokerPositionGroup } from './brokerDisplay'
|
||||
|
||||
function position(input: Partial<BrokerPosition>): BrokerPosition {
|
||||
return {
|
||||
@ -25,7 +25,7 @@ function position(input: Partial<BrokerPosition>): BrokerPosition {
|
||||
expectedYieldPercent: null,
|
||||
dailyYield: null,
|
||||
...input,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function operation(input: Partial<BrokerOperation>): BrokerOperation {
|
||||
@ -53,70 +53,70 @@ function operation(input: Partial<BrokerOperation>): BrokerOperation {
|
||||
quantity: null,
|
||||
quantityDone: null,
|
||||
...input,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
describe('broker display helpers', () => {
|
||||
it('groups positions by instrument type', () => {
|
||||
expect(getBrokerPositionGroup(position({ instrumentType: 'share' }))).toBe('shares');
|
||||
expect(getBrokerPositionGroup(position({ instrumentType: 'bond' }))).toBe('bonds');
|
||||
expect(getBrokerPositionGroup(position({ instrumentType: 'etf' }))).toBe('other');
|
||||
expect(getBrokerPositionGroup(position({ instrumentType: null }))).toBe('other');
|
||||
});
|
||||
expect(getBrokerPositionGroup(position({ instrumentType: 'share' }))).toBe('shares')
|
||||
expect(getBrokerPositionGroup(position({ instrumentType: 'bond' }))).toBe('bonds')
|
||||
expect(getBrokerPositionGroup(position({ instrumentType: 'etf' }))).toBe('other')
|
||||
expect(getBrokerPositionGroup(position({ instrumentType: null }))).toBe('other')
|
||||
})
|
||||
|
||||
it('builds stock and bond routes from instrument metadata', () => {
|
||||
expect(
|
||||
getBrokerInstrumentPath({ ticker: 'sber', instrumentType: 'share', classCode: 'TQBR' }),
|
||||
).toBe('/stocks/SBER');
|
||||
).toBe('/stocks/SBER')
|
||||
expect(
|
||||
getBrokerInstrumentPath({
|
||||
ticker: 'SU26238RMFS5',
|
||||
instrumentType: 'bond',
|
||||
classCode: 'TQOB',
|
||||
}),
|
||||
).toBe('/bonds/SU26238RMFS5');
|
||||
).toBe('/bonds/SU26238RMFS5')
|
||||
expect(
|
||||
getBrokerInstrumentPath({ ticker: null, instrumentType: 'share', classCode: 'TQBR' }),
|
||||
).toBeNull();
|
||||
).toBeNull()
|
||||
expect(
|
||||
getBrokerInstrumentPath({ ticker: 'TMOS', instrumentType: 'etf', classCode: 'TQTF' }),
|
||||
).toBeNull();
|
||||
});
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('uses class code fallback when instrument type is missing', () => {
|
||||
expect(
|
||||
getBrokerInstrumentPath({ ticker: 'SBER', instrumentType: null, classCode: 'TQBR' }),
|
||||
).toBe('/stocks/SBER');
|
||||
).toBe('/stocks/SBER')
|
||||
expect(
|
||||
getBrokerInstrumentPath({ ticker: 'RU000A0JX0J2', instrumentType: null, classCode: 'TQOB' }),
|
||||
).toBe('/bonds/RU000A0JX0J2');
|
||||
});
|
||||
).toBe('/bonds/RU000A0JX0J2')
|
||||
})
|
||||
|
||||
it('does not let class code override a known unsupported or conflicting instrument type', () => {
|
||||
expect(
|
||||
getBrokerInstrumentPath({ ticker: 'TMOS', instrumentType: 'etf', classCode: 'TQBR' }),
|
||||
).toBeNull();
|
||||
).toBeNull()
|
||||
expect(
|
||||
getBrokerInstrumentPath({
|
||||
ticker: 'SU26238RMFS5',
|
||||
instrumentType: 'bond',
|
||||
classCode: 'TQBR',
|
||||
}),
|
||||
).toBe('/bonds/SU26238RMFS5');
|
||||
});
|
||||
).toBe('/bonds/SU26238RMFS5')
|
||||
})
|
||||
|
||||
it('maps operation enum values to Russian labels', () => {
|
||||
expect(getBrokerOperationTypeLabel(operation({ type: 'OPERATION_TYPE_COUPON' }))).toBe(
|
||||
'Выплата купона',
|
||||
);
|
||||
expect(getBrokerOperationTypeLabel(operation({ type: 'OPERATION_TYPE_TAX' }))).toBe('Налог');
|
||||
expect(getBrokerOperationTypeLabel(operation({ type: 'OPERATION_TYPE_BUY' }))).toBe('Покупка');
|
||||
)
|
||||
expect(getBrokerOperationTypeLabel(operation({ type: 'OPERATION_TYPE_TAX' }))).toBe('Налог')
|
||||
expect(getBrokerOperationTypeLabel(operation({ type: 'OPERATION_TYPE_BUY' }))).toBe('Покупка')
|
||||
expect(
|
||||
getBrokerOperationTypeLabel(
|
||||
operation({ type: 'OPERATION_TYPE_UNKNOWN_VALUE', description: 'Custom' }),
|
||||
),
|
||||
).toBe('Custom');
|
||||
});
|
||||
).toBe('Custom')
|
||||
})
|
||||
|
||||
it('exposes independently selectable known operation types', () => {
|
||||
expect(BROKER_OPERATION_TYPE_OPTIONS).toEqual(
|
||||
@ -126,28 +126,28 @@ describe('broker display helpers', () => {
|
||||
{ value: 'OPERATION_TYPE_BOND_TAX', label: 'Налог по облигациям' },
|
||||
{ value: 'OPERATION_TYPE_DIVIDEND_TAX', label: 'Налог на дивиденды' },
|
||||
]),
|
||||
);
|
||||
});
|
||||
)
|
||||
})
|
||||
|
||||
it('keeps operation type option values unique and labels in Russian order', () => {
|
||||
const values = BROKER_OPERATION_TYPE_OPTIONS.map(({ value }) => value);
|
||||
const labels = BROKER_OPERATION_TYPE_OPTIONS.map(({ label }) => label);
|
||||
const values = BROKER_OPERATION_TYPE_OPTIONS.map(({ value }) => value)
|
||||
const labels = BROKER_OPERATION_TYPE_OPTIONS.map(({ label }) => label)
|
||||
|
||||
expect(new Set(values).size).toBe(values.length);
|
||||
expect(labels).toEqual([...labels].sort((left, right) => left.localeCompare(right, 'ru')));
|
||||
});
|
||||
expect(new Set(values).size).toBe(values.length)
|
||||
expect(labels).toEqual([...labels].sort((left, right) => left.localeCompare(right, 'ru')))
|
||||
})
|
||||
|
||||
it('keeps operation type options immutable at runtime', () => {
|
||||
expect(Object.isFrozen(BROKER_OPERATION_TYPE_OPTIONS)).toBe(true);
|
||||
expect(BROKER_OPERATION_TYPE_OPTIONS.every((option) => Object.isFrozen(option))).toBe(true);
|
||||
});
|
||||
expect(Object.isFrozen(BROKER_OPERATION_TYPE_OPTIONS)).toBe(true)
|
||||
expect(BROKER_OPERATION_TYPE_OPTIONS.every((option) => Object.isFrozen(option))).toBe(true)
|
||||
})
|
||||
|
||||
it('validates only exact known operation type values', () => {
|
||||
expect(isBrokerOperationType('OPERATION_TYPE_COUPON')).toBe(true);
|
||||
expect(isBrokerOperationType('operation_type_coupon')).toBe(false);
|
||||
expect(isBrokerOperationType('OPERATION_TYPE_UNKNOWN')).toBe(false);
|
||||
expect(isBrokerOperationType(null)).toBe(false);
|
||||
});
|
||||
expect(isBrokerOperationType('OPERATION_TYPE_COUPON')).toBe(true)
|
||||
expect(isBrokerOperationType('operation_type_coupon')).toBe(false)
|
||||
expect(isBrokerOperationType('OPERATION_TYPE_UNKNOWN')).toBe(false)
|
||||
expect(isBrokerOperationType(null)).toBe(false)
|
||||
})
|
||||
|
||||
it('classifies operations by portfolio impact', () => {
|
||||
expect(
|
||||
@ -158,7 +158,7 @@ describe('broker display helpers', () => {
|
||||
payment: { currency: 'RUB', units: '120', nano: 0, value: 120 },
|
||||
}),
|
||||
),
|
||||
).toBe('adds');
|
||||
).toBe('adds')
|
||||
expect(
|
||||
getBrokerOperationImpact(
|
||||
operation({
|
||||
@ -167,7 +167,7 @@ describe('broker display helpers', () => {
|
||||
payment: { currency: 'RUB', units: '-13', nano: 0, value: -13 },
|
||||
}),
|
||||
),
|
||||
).toBe('reduces');
|
||||
).toBe('reduces')
|
||||
expect(
|
||||
getBrokerOperationImpact(
|
||||
operation({
|
||||
@ -176,11 +176,11 @@ describe('broker display helpers', () => {
|
||||
payment: { currency: 'RUB', units: '1000', nano: 0, value: 1000 },
|
||||
}),
|
||||
),
|
||||
).toBe('neutral');
|
||||
).toBe('neutral')
|
||||
expect(getBrokerOperationImpact(operation({ type: 'OPERATION_TYPE_UNSPECIFIED' }))).toBe(
|
||||
'unknown',
|
||||
);
|
||||
});
|
||||
)
|
||||
})
|
||||
|
||||
it('keeps unknown operation types unclear even when they have non-zero payments', () => {
|
||||
expect(
|
||||
@ -191,7 +191,7 @@ describe('broker display helpers', () => {
|
||||
payment: { currency: 'RUB', units: '100', nano: 0, value: 100 },
|
||||
}),
|
||||
),
|
||||
).toBe('unknown');
|
||||
).toBe('unknown')
|
||||
expect(
|
||||
getBrokerOperationImpact(
|
||||
operation({
|
||||
@ -200,8 +200,8 @@ describe('broker display helpers', () => {
|
||||
payment: { currency: 'RUB', units: '-100', nano: 0, value: -100 },
|
||||
}),
|
||||
),
|
||||
).toBe('unknown');
|
||||
});
|
||||
).toBe('unknown')
|
||||
})
|
||||
|
||||
it('classifies known income operation types as additions even with weak metadata', () => {
|
||||
expect(
|
||||
@ -212,7 +212,7 @@ describe('broker display helpers', () => {
|
||||
payment: null,
|
||||
}),
|
||||
),
|
||||
).toBe('adds');
|
||||
).toBe('adds')
|
||||
expect(
|
||||
getBrokerOperationImpact(
|
||||
operation({
|
||||
@ -221,6 +221,6 @@ describe('broker display helpers', () => {
|
||||
payment: null,
|
||||
}),
|
||||
),
|
||||
).toBe('adds');
|
||||
});
|
||||
});
|
||||
).toBe('adds')
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,46 +1,46 @@
|
||||
import type { BrokerPosition } from '@/shared/api/responses';
|
||||
import type { BrokerPosition } from '@/shared/api/responses'
|
||||
|
||||
export type BrokerPositionGroup = 'shares' | 'bonds' | 'other';
|
||||
export type BrokerPositionGroup = 'shares' | 'bonds' | 'other'
|
||||
|
||||
type BrokerInstrumentLinkInput = {
|
||||
ticker: string | null;
|
||||
instrumentType: string | null;
|
||||
classCode: string | null;
|
||||
};
|
||||
ticker: string | null
|
||||
instrumentType: string | null
|
||||
classCode: string | null
|
||||
}
|
||||
|
||||
const STOCK_CLASS_CODES = new Set(['TQBR']);
|
||||
const BOND_CLASS_CODES = new Set(['TQOB', 'TQCB', 'TQIR']);
|
||||
const STOCK_CLASS_CODES = new Set(['TQBR'])
|
||||
const BOND_CLASS_CODES = new Set(['TQOB', 'TQCB', 'TQIR'])
|
||||
|
||||
export function getBrokerPositionGroup(
|
||||
position: Pick<BrokerPosition, 'instrumentType'>,
|
||||
): BrokerPositionGroup {
|
||||
const instrumentType = position.instrumentType?.toLowerCase();
|
||||
const instrumentType = position.instrumentType?.toLowerCase()
|
||||
|
||||
if (instrumentType === 'share') return 'shares';
|
||||
if (instrumentType === 'bond') return 'bonds';
|
||||
if (instrumentType === 'share') return 'shares'
|
||||
if (instrumentType === 'bond') return 'bonds'
|
||||
|
||||
return 'other';
|
||||
return 'other'
|
||||
}
|
||||
|
||||
export function getBrokerInstrumentPath(input: BrokerInstrumentLinkInput): string | null {
|
||||
const ticker = input.ticker?.trim().toUpperCase();
|
||||
if (!ticker) return null;
|
||||
const ticker = input.ticker?.trim().toUpperCase()
|
||||
if (!ticker) return null
|
||||
|
||||
const instrumentType = input.instrumentType?.toLowerCase();
|
||||
const classCode = input.classCode?.toUpperCase() ?? null;
|
||||
const instrumentType = input.instrumentType?.toLowerCase()
|
||||
const classCode = input.classCode?.toUpperCase() ?? null
|
||||
|
||||
if (instrumentType === 'share') {
|
||||
return `/stocks/${encodeURIComponent(ticker)}`;
|
||||
return `/stocks/${encodeURIComponent(ticker)}`
|
||||
}
|
||||
|
||||
if (instrumentType === 'bond') {
|
||||
return `/bonds/${encodeURIComponent(ticker)}`;
|
||||
return `/bonds/${encodeURIComponent(ticker)}`
|
||||
}
|
||||
|
||||
if (instrumentType) return null;
|
||||
if (instrumentType) return null
|
||||
|
||||
if (classCode && STOCK_CLASS_CODES.has(classCode)) return `/stocks/${encodeURIComponent(ticker)}`;
|
||||
if (classCode && BOND_CLASS_CODES.has(classCode)) return `/bonds/${encodeURIComponent(ticker)}`;
|
||||
if (classCode && STOCK_CLASS_CODES.has(classCode)) return `/stocks/${encodeURIComponent(ticker)}`
|
||||
if (classCode && BOND_CLASS_CODES.has(classCode)) return `/bonds/${encodeURIComponent(ticker)}`
|
||||
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { keepPreviousData, useQuery } from '@tanstack/react-query';
|
||||
import type { BrokerPositionsPage } from '@/shared/api/responses';
|
||||
import { getBrokerPositions } from '../api/brokerPositionApi';
|
||||
import { keepPreviousData, useQuery } from '@tanstack/react-query'
|
||||
import type { BrokerPositionsPage } from '@/shared/api/responses'
|
||||
import { getBrokerPositions } from '../api/brokerPositionApi'
|
||||
|
||||
export function useBrokerPositions(
|
||||
accountId: string | undefined,
|
||||
@ -14,5 +14,5 @@ export function useBrokerPositions(
|
||||
retry: 2,
|
||||
placeholderData: keepPreviousData,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,33 +1,33 @@
|
||||
import { request } from '@/shared/api/client';
|
||||
import { request } from '@/shared/api/kyClient'
|
||||
import type {
|
||||
AnalyticsResponse,
|
||||
Portfolio,
|
||||
PortfolioDetail,
|
||||
Position,
|
||||
} from '@/shared/api/responses';
|
||||
} from '@/shared/api/responses'
|
||||
|
||||
export function getPortfolios(): Promise<{
|
||||
data: Portfolio[];
|
||||
meta: { cachedAt: string | null; fromCache: boolean };
|
||||
data: Portfolio[]
|
||||
meta: { cachedAt: string | null; fromCache: boolean }
|
||||
}> {
|
||||
return request<Portfolio[]>('/api/v1/portfolios');
|
||||
return request<Portfolio[]>('/api/v1/portfolios')
|
||||
}
|
||||
|
||||
export function getPortfolio(
|
||||
id: number,
|
||||
): Promise<{ data: PortfolioDetail; meta: { cachedAt: string | null; fromCache: boolean } }> {
|
||||
return request<PortfolioDetail>(`/api/v1/portfolios/${id}`);
|
||||
return request<PortfolioDetail>(`/api/v1/portfolios/${id}`)
|
||||
}
|
||||
|
||||
export function createPortfolio(data: {
|
||||
name: string;
|
||||
description?: string;
|
||||
currency?: string;
|
||||
name: string
|
||||
description?: string
|
||||
currency?: string
|
||||
}): Promise<{ data: Portfolio; meta: { cachedAt: string | null; fromCache: boolean } }> {
|
||||
return request<Portfolio>('/api/v1/portfolios', undefined, {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function updatePortfolio(
|
||||
@ -37,7 +37,7 @@ export function updatePortfolio(
|
||||
return request<Portfolio>(`/api/v1/portfolios/${id}`, undefined, {
|
||||
method: 'PATCH',
|
||||
body: data,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function deletePortfolio(
|
||||
@ -45,41 +45,41 @@ export function deletePortfolio(
|
||||
): Promise<{ data: null; meta: { cachedAt: string | null; fromCache: boolean } }> {
|
||||
return request<null>(`/api/v1/portfolios/${id}`, undefined, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function addPosition(
|
||||
portfolioId: number,
|
||||
data: {
|
||||
secid: string;
|
||||
quantity: number;
|
||||
buyPrice?: number;
|
||||
buyDate?: string;
|
||||
notes?: string;
|
||||
tags?: string[];
|
||||
secid: string
|
||||
quantity: number
|
||||
buyPrice?: number
|
||||
buyDate?: string
|
||||
notes?: string
|
||||
tags?: string[]
|
||||
},
|
||||
): Promise<{ data: Position; meta: { cachedAt: string | null; fromCache: boolean } }> {
|
||||
return request<Position>(`/api/v1/portfolios/${portfolioId}/positions`, undefined, {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function updatePosition(
|
||||
portfolioId: number,
|
||||
positionId: number,
|
||||
data: {
|
||||
quantity?: number;
|
||||
buyPrice?: number;
|
||||
buyDate?: string;
|
||||
notes?: string;
|
||||
tags?: string[];
|
||||
quantity?: number
|
||||
buyPrice?: number
|
||||
buyDate?: string
|
||||
notes?: string
|
||||
tags?: string[]
|
||||
},
|
||||
): Promise<{ data: Position; meta: { cachedAt: string | null; fromCache: boolean } }> {
|
||||
return request<Position>(`/api/v1/portfolios/${portfolioId}/positions/${positionId}`, undefined, {
|
||||
method: 'PATCH',
|
||||
body: data,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function removePosition(
|
||||
@ -88,11 +88,11 @@ export function removePosition(
|
||||
): Promise<{ data: null; meta: { cachedAt: string | null; fromCache: boolean } }> {
|
||||
return request<null>(`/api/v1/portfolios/${portfolioId}/positions/${positionId}`, undefined, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function getPortfolioAnalytics(
|
||||
portfolioId: number,
|
||||
): Promise<{ data: AnalyticsResponse; meta: { cachedAt: string | null; fromCache: boolean } }> {
|
||||
return request<AnalyticsResponse>(`/api/v1/portfolios/${portfolioId}/analytics`);
|
||||
return request<AnalyticsResponse>(`/api/v1/portfolios/${portfolioId}/analytics`)
|
||||
}
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
export { usePortfolio } from './model/usePortfolio';
|
||||
export { usePortfolios } from './model/usePortfolios';
|
||||
export { usePortfolioAnalytics } from './model/usePortfolioAnalytics';
|
||||
export { usePortfolioMutations } from './model/usePortfolioMutations';
|
||||
export { usePositionMutations } from './model/usePositionMutations';
|
||||
export {
|
||||
getPortfolios,
|
||||
getPortfolio,
|
||||
createPortfolio,
|
||||
updatePortfolio,
|
||||
deletePortfolio,
|
||||
addPosition,
|
||||
updatePosition,
|
||||
removePosition,
|
||||
createPortfolio,
|
||||
deletePortfolio,
|
||||
getPortfolio,
|
||||
getPortfolioAnalytics,
|
||||
} from './api/portfolioApi';
|
||||
getPortfolios,
|
||||
removePosition,
|
||||
updatePortfolio,
|
||||
updatePosition,
|
||||
} from './api/portfolioApi'
|
||||
export { usePortfolio } from './model/usePortfolio'
|
||||
export { usePortfolioAnalytics } from './model/usePortfolioAnalytics'
|
||||
export { usePortfolioMutations } from './model/usePortfolioMutations'
|
||||
export { usePortfolios } from './model/usePortfolios'
|
||||
export { usePositionMutations } from './model/usePositionMutations'
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { getPortfolio } from '../api/portfolioApi';
|
||||
import type { PortfolioDetail } from '@/shared/api/responses';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { PortfolioDetail } from '@/shared/api/responses'
|
||||
import { getPortfolio } from '../api/portfolioApi'
|
||||
|
||||
export function usePortfolio(id: number) {
|
||||
return useQuery<PortfolioDetail>({
|
||||
queryKey: ['portfolio', id],
|
||||
queryFn: async () => {
|
||||
const res = await getPortfolio(id);
|
||||
return res.data;
|
||||
const res = await getPortfolio(id)
|
||||
return res.data
|
||||
},
|
||||
staleTime: 900_000,
|
||||
retry: 2,
|
||||
refetchOnWindowFocus: false,
|
||||
enabled: !!id,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { getPortfolioAnalytics } from '../api/portfolioApi';
|
||||
import type { AnalyticsResponse } from '@/shared/api/responses';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { AnalyticsResponse } from '@/shared/api/responses'
|
||||
import { getPortfolioAnalytics } from '../api/portfolioApi'
|
||||
|
||||
export function usePortfolioAnalytics(portfolioId: number) {
|
||||
return useQuery<AnalyticsResponse>({
|
||||
queryKey: ['portfolio', portfolioId, 'analytics'],
|
||||
queryFn: async () => {
|
||||
const res = await getPortfolioAnalytics(portfolioId);
|
||||
return res.data;
|
||||
const res = await getPortfolioAnalytics(portfolioId)
|
||||
return res.data
|
||||
},
|
||||
staleTime: 900_000,
|
||||
retry: 2,
|
||||
refetchOnWindowFocus: false,
|
||||
enabled: !!portfolioId,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,45 +1,45 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { createPortfolio, updatePortfolio, deletePortfolio } from '../api/portfolioApi';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useNavigate } from '@tanstack/react-router'
|
||||
import { createPortfolio, deletePortfolio, updatePortfolio } from '../api/portfolioApi'
|
||||
|
||||
export function usePortfolioMutations() {
|
||||
const queryClient = useQueryClient();
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient()
|
||||
const navigate = useNavigate()
|
||||
|
||||
const create = useMutation({
|
||||
mutationFn: (data: { name: string; description?: string; currency?: string }) =>
|
||||
createPortfolio(data),
|
||||
onSuccess: (res) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolios'] });
|
||||
navigate(`/portfolios/${res.data.id}`);
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolios'] })
|
||||
navigate({ to: `/portfolios/${res.data.id}` })
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const update = useMutation({
|
||||
mutationFn: ({
|
||||
id,
|
||||
data,
|
||||
}: {
|
||||
id: number;
|
||||
id: number
|
||||
data: {
|
||||
name?: string;
|
||||
description?: string;
|
||||
currency?: string;
|
||||
};
|
||||
name?: string
|
||||
description?: string
|
||||
currency?: string
|
||||
}
|
||||
}) => updatePortfolio(id, data),
|
||||
onSuccess: (_, { id }) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolios'] });
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolio', id] });
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolios'] })
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolio', id] })
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const remove = useMutation({
|
||||
mutationFn: (id: number) => deletePortfolio(id),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolios'] });
|
||||
navigate('/portfolios');
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolios'] })
|
||||
navigate({ to: '/portfolios' })
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
return { create, update, remove };
|
||||
return { create, update, remove }
|
||||
}
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { getPortfolios } from '../api/portfolioApi';
|
||||
import type { Portfolio } from '@/shared/api/responses';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { Portfolio } from '@/shared/api/responses'
|
||||
import { getPortfolios } from '../api/portfolioApi'
|
||||
|
||||
export function usePortfolios() {
|
||||
return useQuery<Portfolio[]>({
|
||||
queryKey: ['portfolios'],
|
||||
queryFn: async () => {
|
||||
const res = await getPortfolios();
|
||||
return res.data;
|
||||
const res = await getPortfolios()
|
||||
return res.data
|
||||
},
|
||||
staleTime: 900_000,
|
||||
retry: 2,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,46 +1,46 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { addPosition, updatePosition, removePosition } from '../api/portfolioApi';
|
||||
import type { PortfolioDetail } from '@/shared/api/responses';
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import type { PortfolioDetail } from '@/shared/api/responses'
|
||||
import { addPosition, removePosition, updatePosition } from '../api/portfolioApi'
|
||||
|
||||
export function usePositionMutations(portfolioId: number) {
|
||||
const queryClient = useQueryClient();
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const add = useMutation({
|
||||
mutationFn: (data: {
|
||||
secid: string;
|
||||
quantity: number;
|
||||
buyPrice?: number;
|
||||
buyDate?: string;
|
||||
notes?: string;
|
||||
tags?: string[];
|
||||
secid: string
|
||||
quantity: number
|
||||
buyPrice?: number
|
||||
buyDate?: string
|
||||
notes?: string
|
||||
tags?: string[]
|
||||
}) => addPosition(portfolioId, data),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolio', portfolioId] });
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolio', portfolioId] })
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const update = useMutation({
|
||||
mutationFn: ({
|
||||
positionId,
|
||||
data,
|
||||
}: {
|
||||
positionId: number;
|
||||
positionId: number
|
||||
data: {
|
||||
quantity?: number;
|
||||
buyPrice?: number;
|
||||
buyDate?: string;
|
||||
notes?: string;
|
||||
tags?: string[];
|
||||
};
|
||||
quantity?: number
|
||||
buyPrice?: number
|
||||
buyDate?: string
|
||||
notes?: string
|
||||
tags?: string[]
|
||||
}
|
||||
}) => updatePosition(portfolioId, positionId, data),
|
||||
onMutate: async ({ positionId, data }) => {
|
||||
await queryClient.cancelQueries({ queryKey: ['portfolio', portfolioId] });
|
||||
await queryClient.cancelQueries({ queryKey: ['portfolio', portfolioId] })
|
||||
const previous = queryClient.getQueryData<{ data: PortfolioDetail }>([
|
||||
'portfolio',
|
||||
portfolioId,
|
||||
]);
|
||||
])
|
||||
queryClient.setQueryData(['portfolio', portfolioId], (old: any) => {
|
||||
if (!old) return old;
|
||||
if (!old) return old
|
||||
return {
|
||||
...old,
|
||||
positions: old.positions.map((p: any) =>
|
||||
@ -53,26 +53,26 @@ export function usePositionMutations(portfolioId: number) {
|
||||
}
|
||||
: p,
|
||||
),
|
||||
};
|
||||
});
|
||||
return { previous };
|
||||
}
|
||||
})
|
||||
return { previous }
|
||||
},
|
||||
onError: (_err, _vars, context) => {
|
||||
if (context?.previous) {
|
||||
queryClient.setQueryData(['portfolio', portfolioId], context.previous);
|
||||
queryClient.setQueryData(['portfolio', portfolioId], context.previous)
|
||||
}
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolio', portfolioId] });
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolio', portfolioId] })
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const remove = useMutation({
|
||||
mutationFn: (positionId: number) => removePosition(portfolioId, positionId),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolio', portfolioId] });
|
||||
queryClient.invalidateQueries({ queryKey: ['portfolio', portfolioId] })
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
return { add, update, remove };
|
||||
return { add, update, remove }
|
||||
}
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import { request } from '@/shared/api/client';
|
||||
import type { SearchResultItem } from '@/shared/api/responses';
|
||||
import { request } from '@/shared/api/kyClient'
|
||||
import type { SearchResultItem } from '@/shared/api/responses'
|
||||
|
||||
export function searchSecurities(q: string, type: 'all' | 'share' | 'bond' = 'all', limit = 20) {
|
||||
return request<SearchResultItem[]>('/api/v1/securities/search', {
|
||||
q,
|
||||
type,
|
||||
limit: String(limit),
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
export { useSearch } from './model/useSearch';
|
||||
export { searchSecurities } from './api/searchApi';
|
||||
export { searchSecurities } from './api/searchApi'
|
||||
export { useSearch } from './model/useSearch'
|
||||
|
||||
@ -1,46 +1,46 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { type ReactNode } from 'react';
|
||||
import { server } from '@/shared/lib/test/server';
|
||||
import { useSearch } from '@/entities/search';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
import type { ReactNode } from 'react'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { useSearch } from '@/entities/search'
|
||||
import { server } from '@/shared/lib/test/server'
|
||||
|
||||
const API = '/api/v1';
|
||||
const API = '/api/v1'
|
||||
|
||||
function createWrapper() {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
|
||||
return function Wrapper({ children }: { children: ReactNode }) {
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
|
||||
};
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
}
|
||||
}
|
||||
|
||||
describe('useSearch', () => {
|
||||
it('does not fetch when query is empty', () => {
|
||||
const { result } = renderHook(() => useSearch(''), { wrapper: createWrapper() });
|
||||
const { result } = renderHook(() => useSearch(''), { wrapper: createWrapper() })
|
||||
|
||||
expect(result.current.isFetching).toBe(false);
|
||||
expect(result.current.data).toBeUndefined();
|
||||
});
|
||||
expect(result.current.isFetching).toBe(false)
|
||||
expect(result.current.data).toBeUndefined()
|
||||
})
|
||||
|
||||
it('does not fetch when query is too short', () => {
|
||||
const { result } = renderHook(() => useSearch('a'), { wrapper: createWrapper() });
|
||||
const { result } = renderHook(() => useSearch('a'), { wrapper: createWrapper() })
|
||||
|
||||
expect(result.current.data).toBeUndefined();
|
||||
});
|
||||
expect(result.current.data).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns search results for valid query', async () => {
|
||||
const { result } = renderHook(() => useSearch('sber'), { wrapper: createWrapper() });
|
||||
const { result } = renderHook(() => useSearch('sber'), { wrapper: createWrapper() })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
expect(result.current.isSuccess).toBe(true)
|
||||
})
|
||||
|
||||
expect(result.current.data).toBeDefined();
|
||||
expect(result.current.data?.length).toBeGreaterThan(0);
|
||||
expect(result.current.data?.[0].secid).toBe('SBER');
|
||||
});
|
||||
expect(result.current.data).toBeDefined()
|
||||
expect(result.current.data?.length).toBeGreaterThan(0)
|
||||
expect(result.current.data?.[0].secid).toBe('SBER')
|
||||
})
|
||||
|
||||
it('returns empty array when no results', async () => {
|
||||
server.use(
|
||||
@ -49,24 +49,24 @@ describe('useSearch', () => {
|
||||
data: { data: [], meta: { fromCache: false, cachedAt: null } },
|
||||
}),
|
||||
),
|
||||
);
|
||||
)
|
||||
|
||||
const { result } = renderHook(() => useSearch('zzzzz'), { wrapper: createWrapper() });
|
||||
const { result } = renderHook(() => useSearch('zzzzz'), { wrapper: createWrapper() })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
expect(result.current.isSuccess).toBe(true)
|
||||
})
|
||||
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
expect(result.current.data).toEqual([])
|
||||
})
|
||||
|
||||
it('returns error state on network failure', async () => {
|
||||
server.use(http.get(`${API}/securities/search`, () => new HttpResponse(null, { status: 500 })));
|
||||
server.use(http.get(`${API}/securities/search`, () => new HttpResponse(null, { status: 500 })))
|
||||
|
||||
const { result } = renderHook(() => useSearch('error'), { wrapper: createWrapper() });
|
||||
const { result } = renderHook(() => useSearch('error'), { wrapper: createWrapper() })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isError).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(result.current.isError).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { searchSecurities } from '../api/searchApi';
|
||||
import type { SearchResultItem } from '@/shared/api/responses';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { SearchResultItem } from '@/shared/api/responses'
|
||||
import { searchSecurities } from '../api/searchApi'
|
||||
|
||||
export function useSearch(query: string) {
|
||||
return useQuery<SearchResultItem[]>({
|
||||
queryKey: ['securities', 'search', query],
|
||||
queryFn: async () => {
|
||||
const res = await searchSecurities(query);
|
||||
const res = await searchSecurities(query)
|
||||
|
||||
return res.data;
|
||||
return res.data
|
||||
},
|
||||
enabled: query.length >= 2,
|
||||
staleTime: 60_000,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,22 +1,22 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { server } from '@/shared/lib/test/server';
|
||||
import { setAccessToken, getAccessToken } from './tokenManager';
|
||||
import { login, register, refresh, logout, getMe, updateProfile } from './sessionApi';
|
||||
import { HttpResponse, http } from 'msw'
|
||||
import { beforeEach, describe, expect, it } from 'vitest'
|
||||
import { server } from '@/shared/lib/test/server'
|
||||
import { getMe, login, logout, refresh, register, updateProfile } from './sessionApi'
|
||||
import { getAccessToken, setAccessToken } from './tokenManager'
|
||||
|
||||
const API = '/api/v1';
|
||||
const API = '/api/v1'
|
||||
|
||||
beforeEach(() => {
|
||||
setAccessToken(null);
|
||||
});
|
||||
setAccessToken(null)
|
||||
})
|
||||
|
||||
describe('login', () => {
|
||||
it('returns auth data and sets access token', async () => {
|
||||
const result = await login('user@test.com', 'password');
|
||||
expect(result.user.email).toBe('user@test.com');
|
||||
expect(result.accessToken).toBe('mock-access-token');
|
||||
expect(getAccessToken()).toBe('mock-access-token');
|
||||
});
|
||||
const result = await login('user@test.com', 'password')
|
||||
expect(result.user.email).toBe('user@test.com')
|
||||
expect(result.accessToken).toBe('mock-access-token')
|
||||
expect(getAccessToken()).toBe('mock-access-token')
|
||||
})
|
||||
|
||||
it('throws on invalid credentials', async () => {
|
||||
server.use(
|
||||
@ -24,45 +24,45 @@ describe('login', () => {
|
||||
`${API}/auth/login`,
|
||||
() => new HttpResponse(null, { status: 401, statusText: 'Unauthorized' }),
|
||||
),
|
||||
);
|
||||
await expect(login('wrong@test.com', 'wrong')).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
)
|
||||
await expect(login('wrong@test.com', 'wrong')).rejects.toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
describe('register', () => {
|
||||
it('returns auth data and sets access token', async () => {
|
||||
const result = await register('new@test.com', 'password', 'New User');
|
||||
expect(result.user.email).toBe('user@test.com');
|
||||
expect(getAccessToken()).toBe('mock-access-token');
|
||||
});
|
||||
});
|
||||
const result = await register('new@test.com', 'password', 'New User')
|
||||
expect(result.user.email).toBe('user@test.com')
|
||||
expect(getAccessToken()).toBe('mock-access-token')
|
||||
})
|
||||
})
|
||||
|
||||
describe('refresh', () => {
|
||||
it('returns auth data and sets access token', async () => {
|
||||
const result = await refresh();
|
||||
expect(result.accessToken).toBe('mock-access-token');
|
||||
expect(getAccessToken()).toBe('mock-access-token');
|
||||
});
|
||||
});
|
||||
const result = await refresh()
|
||||
expect(result.accessToken).toBe('mock-access-token')
|
||||
expect(getAccessToken()).toBe('mock-access-token')
|
||||
})
|
||||
})
|
||||
|
||||
describe('logout', () => {
|
||||
it('clears access token', async () => {
|
||||
setAccessToken('test-token');
|
||||
await logout();
|
||||
expect(getAccessToken()).toBeNull();
|
||||
});
|
||||
});
|
||||
setAccessToken('test-token')
|
||||
await logout()
|
||||
expect(getAccessToken()).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('getMe', () => {
|
||||
it('returns current user', async () => {
|
||||
const result = await getMe();
|
||||
expect(result.email).toBe('user@test.com');
|
||||
});
|
||||
});
|
||||
const result = await getMe()
|
||||
expect(result.email).toBe('user@test.com')
|
||||
})
|
||||
})
|
||||
|
||||
describe('updateProfile', () => {
|
||||
it('updates and returns user', async () => {
|
||||
const result = await updateProfile({ name: 'Updated' });
|
||||
expect(result.name).toBe('Updated');
|
||||
});
|
||||
});
|
||||
const result = await updateProfile({ name: 'Updated' })
|
||||
expect(result.name).toBe('Updated')
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
import { request } from '@/shared/api/client';
|
||||
import { setAccessToken } from './tokenManager';
|
||||
import type { AuthResponse, UserResponse } from '@/shared/api/responses';
|
||||
import { request } from '@/shared/api/kyClient'
|
||||
import type { AuthResponse, UserResponse } from '@/shared/api/responses'
|
||||
import { setAccessToken } from './tokenManager'
|
||||
|
||||
export async function login(email: string, password: string) {
|
||||
const result = await request<AuthResponse>('/api/v1/auth/login', undefined, {
|
||||
method: 'POST',
|
||||
body: { email, password },
|
||||
skipAuth: true,
|
||||
});
|
||||
setAccessToken(result.data.accessToken);
|
||||
return result.data;
|
||||
})
|
||||
setAccessToken(result.data.accessToken)
|
||||
return result.data
|
||||
}
|
||||
|
||||
export async function register(email: string, password: string, name?: string) {
|
||||
@ -17,37 +17,37 @@ export async function register(email: string, password: string, name?: string) {
|
||||
method: 'POST',
|
||||
body: { email, password, name },
|
||||
skipAuth: true,
|
||||
});
|
||||
setAccessToken(result.data.accessToken);
|
||||
return result.data;
|
||||
})
|
||||
setAccessToken(result.data.accessToken)
|
||||
return result.data
|
||||
}
|
||||
|
||||
export async function refresh() {
|
||||
const result = await request<AuthResponse>('/api/v1/auth/refresh', undefined, {
|
||||
method: 'POST',
|
||||
skipAuth: true,
|
||||
});
|
||||
setAccessToken(result.data.accessToken);
|
||||
return result.data;
|
||||
})
|
||||
setAccessToken(result.data.accessToken)
|
||||
return result.data
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
const result = await request<{ message: string }>('/api/v1/auth/logout', undefined, {
|
||||
method: 'POST',
|
||||
});
|
||||
setAccessToken(null);
|
||||
return result.data;
|
||||
})
|
||||
setAccessToken(null)
|
||||
return result.data
|
||||
}
|
||||
|
||||
export async function getMe() {
|
||||
const result = await request<UserResponse>('/api/v1/auth/me');
|
||||
return result.data;
|
||||
const result = await request<UserResponse>('/api/v1/auth/me')
|
||||
return result.data
|
||||
}
|
||||
|
||||
export async function updateProfile(data: { name?: string }) {
|
||||
const result = await request<UserResponse>('/api/v1/auth/me', undefined, {
|
||||
method: 'PATCH',
|
||||
body: data,
|
||||
});
|
||||
return result.data;
|
||||
})
|
||||
return result.data
|
||||
}
|
||||
|
||||
@ -1,21 +1,21 @@
|
||||
import type { AuthResponse } from '@/shared/api/responses';
|
||||
import { normalizeEnvelope } from '@/shared/api/client';
|
||||
import { normalizeEnvelope } from '@/shared/api/kyClient'
|
||||
import type { AuthResponse } from '@/shared/api/responses'
|
||||
|
||||
let accessToken: string | null = null;
|
||||
let onUnauthorized: (() => void) | null = null;
|
||||
let isRefreshing = false;
|
||||
let refreshPromise: Promise<boolean> | null = null;
|
||||
let accessToken: string | null = null
|
||||
let onUnauthorized: (() => void) | null = null
|
||||
let isRefreshing = false
|
||||
let refreshPromise: Promise<boolean> | null = null
|
||||
|
||||
export function setAccessToken(token: string | null) {
|
||||
accessToken = token;
|
||||
accessToken = token
|
||||
}
|
||||
|
||||
export function getAccessToken(): string | null {
|
||||
return accessToken;
|
||||
return accessToken
|
||||
}
|
||||
|
||||
export function setOnUnauthorized(cb: () => void) {
|
||||
onUnauthorized = cb;
|
||||
onUnauthorized = cb
|
||||
}
|
||||
|
||||
async function refreshTokens(): Promise<boolean> {
|
||||
@ -23,31 +23,31 @@ async function refreshTokens(): Promise<boolean> {
|
||||
const res = await fetch('/api/v1/auth/refresh', {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
});
|
||||
if (!res.ok) return false;
|
||||
const json = await res.json();
|
||||
accessToken = normalizeEnvelope<AuthResponse>(json).data.accessToken;
|
||||
return true;
|
||||
})
|
||||
if (!res.ok) return false
|
||||
const json = await res.json()
|
||||
accessToken = normalizeEnvelope<AuthResponse>(json).data.accessToken
|
||||
return true
|
||||
} catch {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleUnauthorized(): Promise<boolean> {
|
||||
if (isRefreshing && refreshPromise) {
|
||||
return refreshPromise;
|
||||
return refreshPromise
|
||||
}
|
||||
|
||||
isRefreshing = true;
|
||||
isRefreshing = true
|
||||
refreshPromise = refreshTokens().then((success) => {
|
||||
isRefreshing = false;
|
||||
refreshPromise = null;
|
||||
isRefreshing = false
|
||||
refreshPromise = null
|
||||
if (!success) {
|
||||
accessToken = null;
|
||||
onUnauthorized?.();
|
||||
accessToken = null
|
||||
onUnauthorized?.()
|
||||
}
|
||||
return success;
|
||||
});
|
||||
return success
|
||||
})
|
||||
|
||||
return refreshPromise;
|
||||
return refreshPromise
|
||||
}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
export { login, register, refresh, logout, getMe, updateProfile } from './api/sessionApi';
|
||||
export { SessionContext, type SessionContextValue } from './model/sessionContext';
|
||||
export { useSession } from './model/useSession';
|
||||
export { getMe, login, logout, refresh, register, updateProfile } from './api/sessionApi'
|
||||
export { SessionContext, type SessionContextValue } from './model/sessionContext'
|
||||
export { useSession } from './model/useSession'
|
||||
export { useSessionStore } from './model/useSessionStore'
|
||||
|
||||
@ -1 +1 @@
|
||||
export { SessionContext, type SessionContextValue } from '@/shared/lib/session-context';
|
||||
export { SessionContext, type SessionContextValue } from '@/shared/lib/session-context'
|
||||
|
||||
@ -1,20 +1,20 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { SessionContext } from './sessionContext';
|
||||
import { useSession } from './useSession';
|
||||
import type { ReactNode } from 'react';
|
||||
import { renderHook } from '@testing-library/react'
|
||||
import type { ReactNode } from 'react'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { SessionContext } from './sessionContext'
|
||||
import { useSession } from './useSession'
|
||||
|
||||
type SessionState = {
|
||||
user: { id: number; email: string; name: string; role: string } | null;
|
||||
accessToken: string | null;
|
||||
isAuthenticated: boolean;
|
||||
isLoading: boolean;
|
||||
login: () => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
register: () => Promise<void>;
|
||||
updateProfile: () => Promise<void>;
|
||||
refreshSession: () => Promise<void>;
|
||||
};
|
||||
user: { id: number; email: string; name: string; role: string } | null
|
||||
accessToken: string | null
|
||||
isAuthenticated: boolean
|
||||
isLoading: boolean
|
||||
login: () => Promise<void>
|
||||
logout: () => Promise<void>
|
||||
register: () => Promise<void>
|
||||
updateProfile: () => Promise<void>
|
||||
refreshSession: () => Promise<void>
|
||||
}
|
||||
|
||||
const mockSession: SessionState = {
|
||||
user: { id: 1, email: 'user@test.com', name: 'Test User', role: 'user' },
|
||||
@ -26,34 +26,34 @@ const mockSession: SessionState = {
|
||||
register: vi.fn().mockResolvedValue(undefined),
|
||||
updateProfile: vi.fn().mockResolvedValue(undefined),
|
||||
refreshSession: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
}
|
||||
|
||||
function createWrapper(session: SessionState = mockSession) {
|
||||
return function Wrapper({ children }: { children: ReactNode }) {
|
||||
return <SessionContext.Provider value={session}>{children}</SessionContext.Provider>;
|
||||
};
|
||||
return <SessionContext.Provider value={session}>{children}</SessionContext.Provider>
|
||||
}
|
||||
}
|
||||
|
||||
describe('useSession', () => {
|
||||
it('returns session context with user', () => {
|
||||
const { result } = renderHook(() => useSession(), { wrapper: createWrapper() });
|
||||
expect(result.current.isAuthenticated).toBe(true);
|
||||
expect(result.current.user?.email).toBe('user@test.com');
|
||||
expect(result.current.accessToken).toBe('mock-access-token');
|
||||
});
|
||||
const { result } = renderHook(() => useSession(), { wrapper: createWrapper() })
|
||||
expect(result.current.isAuthenticated).toBe(true)
|
||||
expect(result.current.user?.email).toBe('user@test.com')
|
||||
expect(result.current.accessToken).toBe('mock-access-token')
|
||||
})
|
||||
|
||||
it('provides login function', () => {
|
||||
const { result } = renderHook(() => useSession(), { wrapper: createWrapper() });
|
||||
expect(typeof result.current.login).toBe('function');
|
||||
});
|
||||
const { result } = renderHook(() => useSession(), { wrapper: createWrapper() })
|
||||
expect(typeof result.current.login).toBe('function')
|
||||
})
|
||||
|
||||
it('provides logout function', () => {
|
||||
const { result } = renderHook(() => useSession(), { wrapper: createWrapper() });
|
||||
expect(typeof result.current.logout).toBe('function');
|
||||
});
|
||||
const { result } = renderHook(() => useSession(), { wrapper: createWrapper() })
|
||||
expect(typeof result.current.logout).toBe('function')
|
||||
})
|
||||
|
||||
it('provides register function', () => {
|
||||
const { result } = renderHook(() => useSession(), { wrapper: createWrapper() });
|
||||
expect(typeof result.current.register).toBe('function');
|
||||
});
|
||||
});
|
||||
const { result } = renderHook(() => useSession(), { wrapper: createWrapper() })
|
||||
expect(typeof result.current.register).toBe('function')
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import { useContext } from 'react';
|
||||
import { SessionContext, type SessionContextValue } from './sessionContext';
|
||||
import { useContext } from 'react'
|
||||
import { SessionContext, type SessionContextValue } from './sessionContext'
|
||||
|
||||
export function useSession(): SessionContextValue {
|
||||
const ctx = useContext(SessionContext);
|
||||
const ctx = useContext(SessionContext)
|
||||
if (!ctx) {
|
||||
throw new Error('useSession must be used within a SessionProvider');
|
||||
throw new Error('useSession must be used within a SessionProvider')
|
||||
}
|
||||
return ctx;
|
||||
return ctx
|
||||
}
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
import { create } from 'zustand';
|
||||
import type { UserResponse } from '@/shared/api/responses';
|
||||
import { create } from 'zustand'
|
||||
import type { UserResponse } from '@/shared/api/responses'
|
||||
|
||||
interface SessionState {
|
||||
user: UserResponse | null;
|
||||
accessToken: string | null;
|
||||
isLoading: boolean;
|
||||
isAuthenticated: boolean;
|
||||
setSession: (authData: { user: UserResponse; accessToken: string }) => void;
|
||||
clearSession: () => void;
|
||||
setLoading: (isLoading: boolean) => void;
|
||||
user: UserResponse | null
|
||||
accessToken: string | null
|
||||
isLoading: boolean
|
||||
isAuthenticated: boolean
|
||||
setSession: (authData: { user: UserResponse; accessToken: string }) => void
|
||||
clearSession: () => void
|
||||
setLoading: (isLoading: boolean) => void
|
||||
}
|
||||
|
||||
export const useSessionStore = create<SessionState>((set) => ({
|
||||
@ -22,7 +22,7 @@ export const useSessionStore = create<SessionState>((set) => ({
|
||||
accessToken: authData.accessToken,
|
||||
isAuthenticated: true,
|
||||
isLoading: false,
|
||||
});
|
||||
})
|
||||
},
|
||||
clearSession: () => {
|
||||
set({
|
||||
@ -30,9 +30,9 @@ export const useSessionStore = create<SessionState>((set) => ({
|
||||
accessToken: null,
|
||||
isAuthenticated: false,
|
||||
isLoading: false,
|
||||
});
|
||||
})
|
||||
},
|
||||
setLoading: (isLoading) => {
|
||||
set({ isLoading });
|
||||
set({ isLoading })
|
||||
},
|
||||
}));
|
||||
}))
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
import { request } from '@/shared/api/client';
|
||||
import { request } from '@/shared/api/kyClient'
|
||||
import type {
|
||||
ApiResponseMeta,
|
||||
ShareResponse,
|
||||
StockMarketData,
|
||||
CandleItem,
|
||||
DividendItem,
|
||||
ShareHistoryItem,
|
||||
CandleItem,
|
||||
} from '@/shared/api/responses';
|
||||
ShareResponse,
|
||||
StockMarketData,
|
||||
} from '@/shared/api/responses'
|
||||
|
||||
export function getShare(secid: string): Promise<{ data: ShareResponse; meta: ApiResponseMeta }> {
|
||||
return request<ShareResponse>(`/api/v1/securities/shares/${encodeURIComponent(secid)}`);
|
||||
return request<ShareResponse>(`/api/v1/securities/shares/${encodeURIComponent(secid)}`)
|
||||
}
|
||||
|
||||
export function getShareMarketData(
|
||||
@ -17,15 +17,13 @@ export function getShareMarketData(
|
||||
): Promise<{ data: StockMarketData; meta: ApiResponseMeta }> {
|
||||
return request<StockMarketData>(
|
||||
`/api/v1/securities/shares/${encodeURIComponent(secid)}/marketdata`,
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
export function getShareDividends(
|
||||
secid: string,
|
||||
): Promise<{ data: DividendItem[]; meta: ApiResponseMeta }> {
|
||||
return request<DividendItem[]>(
|
||||
`/api/v1/securities/shares/${encodeURIComponent(secid)}/dividends`,
|
||||
);
|
||||
return request<DividendItem[]>(`/api/v1/securities/shares/${encodeURIComponent(secid)}/dividends`)
|
||||
}
|
||||
|
||||
export function getShareHistory(
|
||||
@ -36,7 +34,7 @@ export function getShareHistory(
|
||||
return request<ShareHistoryItem[]>(
|
||||
`/api/v1/securities/shares/${encodeURIComponent(secid)}/history`,
|
||||
{ from, till },
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
export function getShareCandles(
|
||||
@ -49,5 +47,5 @@ export function getShareCandles(
|
||||
interval,
|
||||
from,
|
||||
till,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
export { useStock } from './model/useStock';
|
||||
export { useStockCandles } from './model/useStockCandles';
|
||||
export { useStockDividends } from './model/useStockDividends';
|
||||
export { useStock } from './model/useStock'
|
||||
export { useStockCandles } from './model/useStockCandles'
|
||||
export { useStockDividends } from './model/useStockDividends'
|
||||
|
||||
@ -1,32 +1,32 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { useStock } from './useStock';
|
||||
import { type ReactNode } from 'react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import type { ReactNode } from 'react'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { useStock } from './useStock'
|
||||
|
||||
function createWrapper() {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
return function Wrapper({ children }: { children: ReactNode }) {
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
|
||||
};
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
}
|
||||
}
|
||||
|
||||
describe('useStock', () => {
|
||||
it('returns share data', async () => {
|
||||
const { result } = renderHook(() => useStock('SBER'), { wrapper: createWrapper() });
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
expect(result.current.data?.secid).toBe('SBER');
|
||||
expect(result.current.data?.shortName).toBe('Сбер');
|
||||
expect(result.current.data?.marketData.price).toBe(289.5);
|
||||
});
|
||||
const { result } = renderHook(() => useStock('SBER'), { wrapper: createWrapper() })
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(result.current.data?.secid).toBe('SBER')
|
||||
expect(result.current.data?.shortName).toBe('Сбер')
|
||||
expect(result.current.data?.marketData.price).toBe(289.5)
|
||||
})
|
||||
|
||||
it('returns error for not found', async () => {
|
||||
const { result } = renderHook(() => useStock('NOTFOUND'), { wrapper: createWrapper() });
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
});
|
||||
const { result } = renderHook(() => useStock('NOTFOUND'), { wrapper: createWrapper() })
|
||||
await waitFor(() => expect(result.current.isError).toBe(true))
|
||||
})
|
||||
|
||||
it('starts in loading state', () => {
|
||||
const { result } = renderHook(() => useStock('SBER'), { wrapper: createWrapper() });
|
||||
expect(result.current.isLoading).toBe(true);
|
||||
});
|
||||
});
|
||||
const { result } = renderHook(() => useStock('SBER'), { wrapper: createWrapper() })
|
||||
expect(result.current.isLoading).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { getShare } from '../api/stockApi';
|
||||
import type { ShareResponse } from '@/shared/api/responses';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { ShareResponse } from '@/shared/api/responses'
|
||||
import { getShare } from '../api/stockApi'
|
||||
|
||||
export function useStock(secid: string) {
|
||||
return useQuery<ShareResponse>({
|
||||
queryKey: ['stock', secid],
|
||||
queryFn: async () => {
|
||||
const res = await getShare(secid);
|
||||
return res.data;
|
||||
const res = await getShare(secid)
|
||||
return res.data
|
||||
},
|
||||
staleTime: 900_000,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,18 +1,18 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { server } from '@/shared/lib/test/server';
|
||||
import { useStockCandles } from './useStockCandles';
|
||||
import { type ReactNode } from 'react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
import type { ReactNode } from 'react'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { server } from '@/shared/lib/test/server'
|
||||
import { useStockCandles } from './useStockCandles'
|
||||
|
||||
const API = '/api/v1';
|
||||
const API = '/api/v1'
|
||||
|
||||
function createWrapper() {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
return function Wrapper({ children }: { children: ReactNode }) {
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
|
||||
};
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
}
|
||||
}
|
||||
|
||||
describe('useStockCandles', () => {
|
||||
@ -20,25 +20,25 @@ describe('useStockCandles', () => {
|
||||
const { result } = renderHook(
|
||||
() => useStockCandles('SBER', '24h', '2024-01-01', '2024-01-31'),
|
||||
{ wrapper: createWrapper() },
|
||||
);
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
expect(result.current.data).toHaveLength(2);
|
||||
expect(result.current.data?.[0].open).toBe(280);
|
||||
});
|
||||
)
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(result.current.data).toHaveLength(2)
|
||||
expect(result.current.data?.[0].open).toBe(280)
|
||||
})
|
||||
|
||||
it('returns empty array when no candles', async () => {
|
||||
server.use(
|
||||
http.get(`${API}/securities/shares/:secid/candles`, () => {
|
||||
return HttpResponse.json({
|
||||
data: { data: [], meta: { fromCache: false, cachedAt: null } },
|
||||
});
|
||||
})
|
||||
}),
|
||||
);
|
||||
)
|
||||
const { result } = renderHook(
|
||||
() => useStockCandles('SBER', '24h', '2024-01-01', '2024-01-31'),
|
||||
{ wrapper: createWrapper() },
|
||||
);
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
)
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(result.current.data).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { getShareCandles } from '../api/stockApi';
|
||||
import type { CandleItem } from '@/shared/api/responses';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { CandleItem } from '@/shared/api/responses'
|
||||
import { getShareCandles } from '../api/stockApi'
|
||||
|
||||
export function useStockCandles(secid: string, interval: '1h' | '24h', from: string, till: string) {
|
||||
return useQuery<CandleItem[]>({
|
||||
queryKey: ['stockCandles', secid, interval, from, till],
|
||||
queryFn: async () => {
|
||||
const res = await getShareCandles(secid, interval, from, till);
|
||||
return res.data;
|
||||
const res = await getShareCandles(secid, interval, from, till)
|
||||
return res.data
|
||||
},
|
||||
staleTime: 3600_000,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,38 +1,38 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { server } from '@/shared/lib/test/server';
|
||||
import { useStockDividends } from './useStockDividends';
|
||||
import { type ReactNode } from 'react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
import type { ReactNode } from 'react'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { server } from '@/shared/lib/test/server'
|
||||
import { useStockDividends } from './useStockDividends'
|
||||
|
||||
const API = '/api/v1';
|
||||
const API = '/api/v1'
|
||||
|
||||
function createWrapper() {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
return function Wrapper({ children }: { children: ReactNode }) {
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
|
||||
};
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
}
|
||||
}
|
||||
|
||||
describe('useStockDividends', () => {
|
||||
it('returns dividend data', async () => {
|
||||
const { result } = renderHook(() => useStockDividends('SBER'), { wrapper: createWrapper() });
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
expect(result.current.data).toHaveLength(2);
|
||||
expect(result.current.data?.[0].value).toBe(35);
|
||||
});
|
||||
const { result } = renderHook(() => useStockDividends('SBER'), { wrapper: createWrapper() })
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(result.current.data).toHaveLength(2)
|
||||
expect(result.current.data?.[0].value).toBe(35)
|
||||
})
|
||||
|
||||
it('returns empty array when no dividends', async () => {
|
||||
server.use(
|
||||
http.get(`${API}/securities/shares/:secid/dividends`, () => {
|
||||
return HttpResponse.json({
|
||||
data: { data: [], meta: { fromCache: false, cachedAt: null } },
|
||||
});
|
||||
})
|
||||
}),
|
||||
);
|
||||
const { result } = renderHook(() => useStockDividends('SBER'), { wrapper: createWrapper() });
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
)
|
||||
const { result } = renderHook(() => useStockDividends('SBER'), { wrapper: createWrapper() })
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(result.current.data).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { getShareDividends } from '../api/stockApi';
|
||||
import type { DividendItem } from '@/shared/api/responses';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { DividendItem } from '@/shared/api/responses'
|
||||
import { getShareDividends } from '../api/stockApi'
|
||||
|
||||
export function useStockDividends(secid: string) {
|
||||
return useQuery<DividendItem[]>({
|
||||
queryKey: ['stockDividends', secid],
|
||||
queryFn: async () => {
|
||||
const res = await getShareDividends(secid);
|
||||
return res.data;
|
||||
const res = await getShareDividends(secid)
|
||||
return res.data
|
||||
},
|
||||
staleTime: 86400_000,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { usePositionMutations } from '@/entities/portfolio';
|
||||
import { usePositionMutations } from '@/entities/portfolio'
|
||||
|
||||
export function useAddPosition(portfolioId: number) {
|
||||
const { add } = usePositionMutations(portfolioId);
|
||||
return add;
|
||||
const { add } = usePositionMutations(portfolioId)
|
||||
return add
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
export { AddPositionForm } from './ui/AddPositionForm';
|
||||
export { AddPositionForm } from './ui/AddPositionForm'
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
import { useState } from 'react';
|
||||
import { useState } from 'react'
|
||||
|
||||
export function useAddPositionForm() {
|
||||
const [showAddForm, setShowAddForm] = useState(false);
|
||||
const [newSecid, setNewSecid] = useState('');
|
||||
const [newQty, setNewQty] = useState('1');
|
||||
const [newPrice, setNewPrice] = useState('');
|
||||
const [newDate, setNewDate] = useState(new Date().toISOString().split('T')[0]);
|
||||
const [showAddForm, setShowAddForm] = useState(false)
|
||||
const [newSecid, setNewSecid] = useState('')
|
||||
const [newQty, setNewQty] = useState('1')
|
||||
const [newPrice, setNewPrice] = useState('')
|
||||
const [newDate, setNewDate] = useState(new Date().toISOString().split('T')[0])
|
||||
|
||||
function reset() {
|
||||
setNewSecid('');
|
||||
setNewQty('1');
|
||||
setNewPrice('');
|
||||
setNewDate(new Date().toISOString().split('T')[0]);
|
||||
setNewSecid('')
|
||||
setNewQty('1')
|
||||
setNewPrice('')
|
||||
setNewDate(new Date().toISOString().split('T')[0])
|
||||
}
|
||||
|
||||
return {
|
||||
@ -26,5 +26,5 @@ export function useAddPositionForm() {
|
||||
newDate,
|
||||
setNewDate,
|
||||
reset,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
import { useAddPosition } from '../api/useAddPosition';
|
||||
import { useAddPositionForm } from '../model/useAddPositionForm';
|
||||
import { useAddPosition } from '../api/useAddPosition'
|
||||
import { useAddPositionForm } from '../model/useAddPositionForm'
|
||||
|
||||
const inputStyle: React.CSSProperties = {
|
||||
padding: '8px 12px',
|
||||
border: '1px solid #e0e0e0',
|
||||
borderRadius: 'var(--border-radius)',
|
||||
fontSize: 14,
|
||||
};
|
||||
}
|
||||
|
||||
export function AddPositionForm({ portfolioId }: { portfolioId: number }) {
|
||||
const addPosition = useAddPosition(portfolioId);
|
||||
const form = useAddPositionForm();
|
||||
const addPosition = useAddPosition(portfolioId)
|
||||
const form = useAddPositionForm()
|
||||
|
||||
function handleAddPosition() {
|
||||
if (!form.newSecid.trim() || !parseInt(form.newQty, 10)) return;
|
||||
if (!form.newSecid.trim() || !parseInt(form.newQty, 10)) return
|
||||
addPosition.mutate(
|
||||
{
|
||||
secid: form.newSecid.trim().toUpperCase(),
|
||||
@ -23,11 +23,11 @@ export function AddPositionForm({ portfolioId }: { portfolioId: number }) {
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
form.setShowAddForm(false);
|
||||
form.reset();
|
||||
form.setShowAddForm(false)
|
||||
form.reset()
|
||||
},
|
||||
},
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
@ -107,5 +107,5 @@ export function AddPositionForm({ portfolioId }: { portfolioId: number }) {
|
||||
Добавить
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,40 +1,40 @@
|
||||
import { request } from '@/shared/api/client';
|
||||
import type { ScreenerResult } from '@/shared/api/responses';
|
||||
import { request } from '@/shared/api/kyClient'
|
||||
import type { ScreenerResult } from '@/shared/api/responses'
|
||||
|
||||
export interface ScreenerQuery {
|
||||
type: 'share' | 'bond';
|
||||
priceMin?: number;
|
||||
priceMax?: number;
|
||||
volumeMin?: number;
|
||||
listLevel?: number;
|
||||
changePercentMin?: number;
|
||||
changePercentMax?: number;
|
||||
capitalizationMin?: number;
|
||||
yieldMin?: number;
|
||||
yieldMax?: number;
|
||||
durationMin?: number;
|
||||
durationMax?: number;
|
||||
couponMin?: number;
|
||||
couponMax?: number;
|
||||
couponPercentMin?: number;
|
||||
couponPercentMax?: number;
|
||||
maturityBefore?: string;
|
||||
maturityAfter?: string;
|
||||
bondType?: string;
|
||||
sortBy?: string;
|
||||
sortOrder?: 'asc' | 'desc';
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
type: 'share' | 'bond'
|
||||
priceMin?: number
|
||||
priceMax?: number
|
||||
volumeMin?: number
|
||||
listLevel?: number
|
||||
changePercentMin?: number
|
||||
changePercentMax?: number
|
||||
capitalizationMin?: number
|
||||
yieldMin?: number
|
||||
yieldMax?: number
|
||||
durationMin?: number
|
||||
durationMax?: number
|
||||
couponMin?: number
|
||||
couponMax?: number
|
||||
couponPercentMin?: number
|
||||
couponPercentMax?: number
|
||||
maturityBefore?: string
|
||||
maturityAfter?: string
|
||||
bondType?: string
|
||||
sortBy?: string
|
||||
sortOrder?: 'asc' | 'desc'
|
||||
page?: number
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export function getScreenerResults(
|
||||
params: ScreenerQuery,
|
||||
): Promise<{ data: ScreenerResult; meta: { cachedAt: string | null; fromCache: boolean } }> {
|
||||
const query: Record<string, string> = {};
|
||||
const query: Record<string, string> = {}
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null) {
|
||||
query[key] = String(value);
|
||||
query[key] = String(value)
|
||||
}
|
||||
});
|
||||
return request<ScreenerResult>('/api/v1/securities/screener', query);
|
||||
})
|
||||
return request<ScreenerResult>('/api/v1/securities/screener', query)
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
export { useScreener } from './model/useScreener';
|
||||
export { FilterPanel, FilterPanelShare, FilterPanelBond, ScreenerTable } from './ui';
|
||||
export type { ScreenerQuery } from './api/screenerApi';
|
||||
export type { ScreenerQuery } from './api/screenerApi'
|
||||
export { useScreener } from './model/useScreener'
|
||||
export { FilterPanel, FilterPanelBond, FilterPanelShare, ScreenerTable } from './ui'
|
||||
|
||||
@ -1 +1 @@
|
||||
export { useScreener } from './useScreener';
|
||||
export { useScreener } from './useScreener'
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import { useSearchParams } from 'react-router-dom';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { getScreenerResults } from '../api/screenerApi';
|
||||
import type { ScreenerQuery } from '../api/screenerApi';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { useSearchParamsCompat } from '@/shared/lib/router/useSearchParams'
|
||||
import type { ScreenerQuery } from '../api/screenerApi'
|
||||
import { getScreenerResults } from '../api/screenerApi'
|
||||
|
||||
export function useScreener() {
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const [searchParams, setSearchParams] = useSearchParamsCompat()
|
||||
|
||||
const params: ScreenerQuery = {
|
||||
type: (searchParams.get('type') as 'share' | 'bond') || 'share',
|
||||
@ -44,9 +44,9 @@ export function useScreener() {
|
||||
sortOrder: (searchParams.get('sortOrder') as 'asc' | 'desc') || undefined,
|
||||
page: searchParams.get('page') ? Number(searchParams.get('page')) : undefined,
|
||||
pageSize: searchParams.get('pageSize') ? Number(searchParams.get('pageSize')) : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
const queryKey = ['screener', params];
|
||||
const queryKey = ['screener', params]
|
||||
|
||||
const query = useQuery({
|
||||
queryKey,
|
||||
@ -55,62 +55,62 @@ export function useScreener() {
|
||||
retry: 2,
|
||||
refetchOnWindowFocus: false,
|
||||
placeholderData: (previousData) => previousData,
|
||||
});
|
||||
})
|
||||
|
||||
function setParam(key: string, value: string | undefined) {
|
||||
setSearchParams((prev) => {
|
||||
const next = new URLSearchParams(prev);
|
||||
const next = new URLSearchParams(prev)
|
||||
if (value === undefined || value === '') {
|
||||
next.delete(key);
|
||||
next.delete(key)
|
||||
} else {
|
||||
next.set(key, value);
|
||||
next.set(key, value)
|
||||
}
|
||||
next.set('page', '1');
|
||||
return next;
|
||||
});
|
||||
next.set('page', '1')
|
||||
return next
|
||||
})
|
||||
}
|
||||
|
||||
function setFilters(filters: Partial<ScreenerQuery>) {
|
||||
setSearchParams((prev) => {
|
||||
const next = new URLSearchParams(prev);
|
||||
const next = new URLSearchParams(prev)
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
next.delete(key);
|
||||
next.delete(key)
|
||||
} else {
|
||||
next.set(key, String(value));
|
||||
next.set(key, String(value))
|
||||
}
|
||||
});
|
||||
next.set('page', '1');
|
||||
return next;
|
||||
});
|
||||
})
|
||||
next.set('page', '1')
|
||||
return next
|
||||
})
|
||||
}
|
||||
|
||||
function setPage(page: number) {
|
||||
setSearchParams((prev) => {
|
||||
const next = new URLSearchParams(prev);
|
||||
next.set('page', String(page));
|
||||
return next;
|
||||
});
|
||||
const next = new URLSearchParams(prev)
|
||||
next.set('page', String(page))
|
||||
return next
|
||||
})
|
||||
}
|
||||
|
||||
function setSort(sortBy: string) {
|
||||
setSearchParams((prev) => {
|
||||
const next = new URLSearchParams(prev);
|
||||
const current = next.get('sortBy');
|
||||
const currentOrder = next.get('sortOrder') || 'asc';
|
||||
const next = new URLSearchParams(prev)
|
||||
const current = next.get('sortBy')
|
||||
const currentOrder = next.get('sortOrder') || 'asc'
|
||||
if (current === sortBy) {
|
||||
next.set('sortOrder', currentOrder === 'asc' ? 'desc' : 'asc');
|
||||
next.set('sortOrder', currentOrder === 'asc' ? 'desc' : 'asc')
|
||||
} else {
|
||||
next.set('sortBy', sortBy);
|
||||
next.set('sortOrder', 'asc');
|
||||
next.set('sortBy', sortBy)
|
||||
next.set('sortOrder', 'asc')
|
||||
}
|
||||
next.set('page', '1');
|
||||
return next;
|
||||
});
|
||||
next.set('page', '1')
|
||||
return next
|
||||
})
|
||||
}
|
||||
|
||||
function resetFilters() {
|
||||
setSearchParams(new URLSearchParams({ type: params.type }));
|
||||
setSearchParams(new URLSearchParams({ type: params.type }))
|
||||
}
|
||||
|
||||
return {
|
||||
@ -123,5 +123,5 @@ export function useScreener() {
|
||||
setSort,
|
||||
setParam,
|
||||
resetFilters,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,36 +1,36 @@
|
||||
import { useState } from 'react';
|
||||
import type { ScreenerQuery } from '../api/screenerApi';
|
||||
import { FilterPanelShare } from './FilterPanelShare';
|
||||
import { FilterPanelBond } from './FilterPanelBond';
|
||||
import { useState } from 'react'
|
||||
import type { ScreenerQuery } from '../api/screenerApi'
|
||||
import { FilterPanelBond } from './FilterPanelBond'
|
||||
import { FilterPanelShare } from './FilterPanelShare'
|
||||
|
||||
interface Props {
|
||||
params: ScreenerQuery;
|
||||
onApply: (filters: Partial<ScreenerQuery>) => void;
|
||||
onReset: () => void;
|
||||
params: ScreenerQuery
|
||||
onApply: (filters: Partial<ScreenerQuery>) => void
|
||||
onReset: () => void
|
||||
}
|
||||
|
||||
export function FilterPanel({ params, onApply, onReset }: Props) {
|
||||
const [type, setType] = useState<'share' | 'bond'>(params.type);
|
||||
const [local, setLocal] = useState<Record<string, string>>({});
|
||||
const [type, setType] = useState<'share' | 'bond'>(params.type)
|
||||
const [local, setLocal] = useState<Record<string, string>>({})
|
||||
|
||||
function handleApply() {
|
||||
const filters: Partial<ScreenerQuery> = { type };
|
||||
const filters: Partial<ScreenerQuery> = { type }
|
||||
Object.entries(local).forEach(([key, value]) => {
|
||||
if (value !== '') {
|
||||
const num = Number(value);
|
||||
filters[key as keyof ScreenerQuery] = isNaN(num) ? (value as any) : (num as any);
|
||||
const num = Number(value)
|
||||
filters[key as keyof ScreenerQuery] = Number.isNaN(num) ? (value as any) : (num as any)
|
||||
}
|
||||
});
|
||||
onApply(filters);
|
||||
})
|
||||
onApply(filters)
|
||||
}
|
||||
|
||||
function handleReset() {
|
||||
setLocal({});
|
||||
onReset();
|
||||
setLocal({})
|
||||
onReset()
|
||||
}
|
||||
|
||||
function updateField(key: string, value: string) {
|
||||
setLocal((prev) => ({ ...prev, [key]: value }));
|
||||
setLocal((prev) => ({ ...prev, [key]: value }))
|
||||
}
|
||||
|
||||
return (
|
||||
@ -104,5 +104,5 @@ export function FilterPanel({ params, onApply, onReset }: Props) {
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
interface Props {
|
||||
params: Record<string, any>;
|
||||
local: Record<string, string>;
|
||||
updateField: (key: string, value: string) => void;
|
||||
params: Record<string, any>
|
||||
local: Record<string, string>
|
||||
updateField: (key: string, value: string) => void
|
||||
}
|
||||
|
||||
export function FilterPanelBond({ local, updateField }: Props) {
|
||||
@ -16,7 +16,7 @@ export function FilterPanelBond({ local, updateField }: Props) {
|
||||
{ key: 'couponMax', label: 'Купон (₽) до' },
|
||||
{ key: 'couponPercentMin', label: 'Купон % от' },
|
||||
{ key: 'couponPercentMax', label: 'Купон % до' },
|
||||
];
|
||||
]
|
||||
|
||||
return (
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
|
||||
@ -49,5 +49,5 @@ export function FilterPanelBond({ local, updateField }: Props) {
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
interface Props {
|
||||
params: Record<string, any>;
|
||||
local: Record<string, string>;
|
||||
updateField: (key: string, value: string) => void;
|
||||
params: Record<string, any>
|
||||
local: Record<string, string>
|
||||
updateField: (key: string, value: string) => void
|
||||
}
|
||||
|
||||
export function FilterPanelShare({ local, updateField }: Props) {
|
||||
@ -12,7 +12,7 @@ export function FilterPanelShare({ local, updateField }: Props) {
|
||||
{ key: 'changePercentMax', label: 'Изм. % до' },
|
||||
{ key: 'volumeMin', label: 'Объём от' },
|
||||
{ key: 'capitalizationMin', label: 'Капитализация от' },
|
||||
];
|
||||
]
|
||||
|
||||
return (
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
|
||||
@ -45,5 +45,5 @@ export function FilterPanelShare({ local, updateField }: Props) {
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,31 +1,31 @@
|
||||
import { Link } from 'react-router-dom';
|
||||
import type { ScreenerResult } from '@/shared/api/responses';
|
||||
import { Link } from '@tanstack/react-router'
|
||||
import type { ScreenerResult } from '@/shared/api/responses'
|
||||
|
||||
interface Props {
|
||||
result: ScreenerResult;
|
||||
sortBy: string;
|
||||
sortOrder: 'asc' | 'desc';
|
||||
onSort: (field: string) => void;
|
||||
onPageChange: (page: number) => void;
|
||||
result: ScreenerResult
|
||||
sortBy: string
|
||||
sortOrder: 'asc' | 'desc'
|
||||
onSort: (field: string) => void
|
||||
onPageChange: (page: number) => void
|
||||
}
|
||||
|
||||
function formatNum(value: number | null | undefined, digits = 2): string {
|
||||
if (value == null) return '—';
|
||||
if (value == null) return '—'
|
||||
return value.toLocaleString('ru-RU', {
|
||||
minimumFractionDigits: digits,
|
||||
maximumFractionDigits: digits,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
function formatChange(value: number | null | undefined): { text: string; color: string } {
|
||||
if (value == null) return { text: '—', color: 'inherit' };
|
||||
const color = value > 0 ? '#43a047' : value < 0 ? '#e53935' : 'inherit';
|
||||
return { text: `${value > 0 ? '+' : ''}${value.toFixed(2)}%`, color };
|
||||
if (value == null) return { text: '—', color: 'inherit' }
|
||||
const color = value > 0 ? '#43a047' : value < 0 ? '#e53935' : 'inherit'
|
||||
return { text: `${value > 0 ? '+' : ''}${value.toFixed(2)}%`, color }
|
||||
}
|
||||
|
||||
export function ScreenerTable({ result, sortBy, sortOrder, onSort, onPageChange }: Props) {
|
||||
function SortHeader({ field, children }: { field: string; children: string }) {
|
||||
const isActive = sortBy === field;
|
||||
const isActive = sortBy === field
|
||||
return (
|
||||
<th
|
||||
onClick={() => onSort(field)}
|
||||
@ -42,10 +42,10 @@ export function ScreenerTable({ result, sortBy, sortOrder, onSort, onPageChange
|
||||
>
|
||||
{children} {isActive ? (sortOrder === 'asc' ? '▲' : '▼') : ''}
|
||||
</th>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
const isShare = result.items[0]?.type === 'share';
|
||||
const isShare = result.items[0]?.type === 'share'
|
||||
|
||||
return (
|
||||
<div style={{ flex: 1 }}>
|
||||
@ -95,8 +95,8 @@ export function ScreenerTable({ result, sortBy, sortOrder, onSort, onPageChange
|
||||
</thead>
|
||||
<tbody>
|
||||
{result.items.map((item) => {
|
||||
const change = formatChange(item.changePercent);
|
||||
const link = isShare ? `/stocks/${item.secid}` : `/bonds/${item.secid}`;
|
||||
const change = formatChange(item.changePercent)
|
||||
const link = isShare ? `/stocks/${item.secid}` : `/bonds/${item.secid}`
|
||||
return (
|
||||
<tr key={item.secid} style={{ borderBottom: '1px solid #f0f0f0' }}>
|
||||
<td style={{ padding: '8px 12px', fontWeight: 600, fontFamily: 'monospace' }}>
|
||||
@ -139,7 +139,7 @@ export function ScreenerTable({ result, sortBy, sortOrder, onSort, onPageChange
|
||||
</>
|
||||
)}
|
||||
</tr>
|
||||
);
|
||||
)
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
@ -167,5 +167,5 @@ export function ScreenerTable({ result, sortBy, sortOrder, onSort, onPageChange
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export { FilterPanel } from './FilterPanel';
|
||||
export { FilterPanelShare } from './FilterPanelShare';
|
||||
export { FilterPanelBond } from './FilterPanelBond';
|
||||
export { ScreenerTable } from './ScreenerTable';
|
||||
export { FilterPanel } from './FilterPanel'
|
||||
export { FilterPanelBond } from './FilterPanelBond'
|
||||
export { FilterPanelShare } from './FilterPanelShare'
|
||||
export { ScreenerTable } from './ScreenerTable'
|
||||
|
||||
@ -1,13 +1,23 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom/client';
|
||||
import { AppProviders } from './app/providers/AppProviders';
|
||||
import App from './app/App';
|
||||
import './styles.css';
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom/client'
|
||||
import App from './app/App'
|
||||
import { AppProviders } from './app/providers/AppProviders'
|
||||
import './styles.css'
|
||||
import { env } from './shared/config/env'
|
||||
|
||||
ReactDOM.createRoot(document.getElementById('root')!).render(
|
||||
<React.StrictMode>
|
||||
<AppProviders>
|
||||
<App />
|
||||
</AppProviders>
|
||||
</React.StrictMode>,
|
||||
);
|
||||
async function startApp() {
|
||||
if (env.VITE_API_MOCK) {
|
||||
const { worker } = await import('./shared/lib/test/browser')
|
||||
await worker.start({ onUnhandledRequest: 'bypass' })
|
||||
}
|
||||
|
||||
ReactDOM.createRoot(document.getElementById('root')!).render(
|
||||
<React.StrictMode>
|
||||
<AppProviders>
|
||||
<App />
|
||||
</AppProviders>
|
||||
</React.StrictMode>,
|
||||
)
|
||||
}
|
||||
|
||||
startApp()
|
||||
|
||||
@ -1 +1 @@
|
||||
export { BondPage } from './ui/BondPage';
|
||||
export { BondPage } from './ui/BondPage'
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
import { useParams } from 'react-router-dom';
|
||||
import { useBond, useBondCandles } from '@/entities/bond';
|
||||
import { BondDetails } from '@/widgets/bond-details';
|
||||
import { PriceChart } from '@/widgets/price-chart';
|
||||
import { useParams } from '@tanstack/react-router'
|
||||
import { useBond, useBondCandles } from '@/entities/bond'
|
||||
import { BondDetails } from '@/widgets/bond-details'
|
||||
import { PriceChart } from '@/widgets/price-chart'
|
||||
|
||||
export function BondPage() {
|
||||
const { secid } = useParams<{ secid: string }>();
|
||||
const { data: bond, isLoading, error } = useBond(secid!);
|
||||
const till = new Date().toISOString().split('T')[0];
|
||||
const from = new Date(Date.now() - 365 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
|
||||
const { data: candles } = useBondCandles(secid!, '24h', from, till);
|
||||
const { secid } = useParams<{ secid: string }>()
|
||||
const { data: bond, isLoading, error } = useBond(secid!)
|
||||
const till = new Date().toISOString().split('T')[0]
|
||||
const from = new Date(Date.now() - 365 * 24 * 60 * 60 * 1000).toISOString().split('T')[0]
|
||||
const { data: candles } = useBondCandles(secid!, '24h', from, till)
|
||||
|
||||
if (isLoading) return <div>Загрузка...</div>;
|
||||
if (error || !bond) return <div>Инструмент не найден</div>;
|
||||
if (isLoading) return <div>Загрузка...</div>
|
||||
if (error || !bond) return <div>Инструмент не найден</div>
|
||||
|
||||
return (
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
|
||||
@ -29,5 +29,5 @@ export function BondPage() {
|
||||
<PriceChart data={candles ?? []} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
export { BrokerAccountOverviewPage } from './ui/BrokerAccountOverviewPage';
|
||||
export { BrokerAccountOverviewPage } from './ui/BrokerAccountOverviewPage'
|
||||
|
||||
@ -1,24 +1,24 @@
|
||||
import { Link } from 'react-router-dom';
|
||||
import { Box } from '@mui/material';
|
||||
import { Text } from '@moex-vibe/design-system';
|
||||
import { useBrokerOperations } from '@/entities/broker-operation';
|
||||
import { useBrokerAccountContext } from '@/widgets/broker-account-layout';
|
||||
import { BrokerAllocationChart } from '@/widgets/broker-allocation-chart';
|
||||
import { BrokerEventsOverview } from '@/widgets/broker-events-overview';
|
||||
import { BrokerOperationsTable } from '@/widgets/broker-operations-table';
|
||||
import { BrokerSummary, BrokerAssetCards, BrokerOverviewSkeleton } from '@/widgets/broker-overview';
|
||||
import { Text } from '@moex-vibe/design-system'
|
||||
import { Box } from '@mui/material'
|
||||
import { Link } from '@tanstack/react-router'
|
||||
import { useBrokerOperations } from '@/entities/broker-operation'
|
||||
import { useBrokerAccountContext } from '@/widgets/broker-account-layout'
|
||||
import { BrokerAllocationChart } from '@/widgets/broker-allocation-chart'
|
||||
import { BrokerEventsOverview } from '@/widgets/broker-events-overview'
|
||||
import { BrokerOperationsTable } from '@/widgets/broker-operations-table'
|
||||
import { BrokerAssetCards, BrokerOverviewSkeleton, BrokerSummary } from '@/widgets/broker-overview'
|
||||
|
||||
export function BrokerAccountOverviewPage() {
|
||||
const { accountId, portfolio } = useBrokerAccountContext();
|
||||
const operations = useBrokerOperations(accountId, { limit: 5 });
|
||||
const { accountId, portfolio } = useBrokerAccountContext()
|
||||
const operations = useBrokerOperations(accountId, { limit: 5 })
|
||||
|
||||
if (portfolio.isLoading) return <BrokerOverviewSkeleton />;
|
||||
if (portfolio.isLoading) return <BrokerOverviewSkeleton />
|
||||
if (portfolio.error || !portfolio.data) {
|
||||
return (
|
||||
<Text component="p" role="alert" tone="negative">
|
||||
Не удалось загрузить сводку счёта
|
||||
</Text>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
@ -44,5 +44,5 @@ export function BrokerAccountOverviewPage() {
|
||||
/>
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
export { BrokerAccountsPage } from './ui/BrokerAccountsPage';
|
||||
export { BrokerAccountsPage } from './ui/BrokerAccountsPage'
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
import { Box } from '@mui/material';
|
||||
import { EmptyState, Heading, Text } from '@moex-vibe/design-system';
|
||||
import { EmptyState, Heading, Text } from '@moex-vibe/design-system'
|
||||
import { Box } from '@mui/material'
|
||||
import {
|
||||
aggregateBrokerAccounts,
|
||||
useBrokerAccounts,
|
||||
useBrokerAccountPortfolios,
|
||||
} from '@/entities/broker-account';
|
||||
import { BrokerAccountCard } from '@/widgets/broker-account-card';
|
||||
import { BrokerAccountsSummary } from '@/widgets/broker-accounts-summary';
|
||||
useBrokerAccounts,
|
||||
} from '@/entities/broker-account'
|
||||
import { BrokerAccountCard } from '@/widgets/broker-account-card'
|
||||
import { BrokerAccountsSummary } from '@/widgets/broker-accounts-summary'
|
||||
|
||||
function BrokerAccountsPageSkeleton() {
|
||||
return (
|
||||
@ -42,20 +42,20 @@ function BrokerAccountsPageSkeleton() {
|
||||
))}
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
export function BrokerAccountsPage() {
|
||||
const { data: accounts, isLoading, error } = useBrokerAccounts();
|
||||
const safeAccounts = accounts ?? [];
|
||||
const accountQueries = useBrokerAccountPortfolios(safeAccounts);
|
||||
const { data: accounts, isLoading, error } = useBrokerAccounts()
|
||||
const safeAccounts = accounts ?? []
|
||||
const accountQueries = useBrokerAccountPortfolios(safeAccounts)
|
||||
|
||||
if (isLoading) {
|
||||
return <BrokerAccountsPageSkeleton />;
|
||||
return <BrokerAccountsPageSkeleton />
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return <Text tone="negative">Не удалось загрузить счета</Text>;
|
||||
return <Text tone="negative">Не удалось загрузить счета</Text>
|
||||
}
|
||||
|
||||
if (safeAccounts.length === 0) {
|
||||
@ -72,17 +72,17 @@ export function BrokerAccountsPage() {
|
||||
description="После подключения T-Bank здесь появятся брокерские счета и ИИС со сводкой по капиталу."
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
const successfulPortfolios = accountQueries
|
||||
.map(({ query }) => query.data)
|
||||
.filter((portfolio): portfolio is NonNullable<typeof portfolio> => Boolean(portfolio));
|
||||
.filter((portfolio): portfolio is NonNullable<typeof portfolio> => Boolean(portfolio))
|
||||
const loadingCount = accountQueries.filter(
|
||||
({ query }) => (query.isLoading || query.isPending || query.isFetching) && !query.data,
|
||||
).length;
|
||||
const availableCount = successfulPortfolios.length;
|
||||
const aggregate = aggregateBrokerAccounts(successfulPortfolios);
|
||||
).length
|
||||
const availableCount = successfulPortfolios.length
|
||||
const aggregate = aggregateBrokerAccounts(successfulPortfolios)
|
||||
|
||||
return (
|
||||
<Box sx={{ display: 'grid', gap: 3 }}>
|
||||
@ -112,11 +112,11 @@ export function BrokerAccountsPage() {
|
||||
isLoading={(query.isLoading || query.isPending || query.isFetching) && !query.data}
|
||||
error={(query.error as Error | null) ?? null}
|
||||
onRetry={() => {
|
||||
void query.refetch();
|
||||
void query.refetch()
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
export { BrokerEventsPage } from './ui/BrokerEventsPage';
|
||||
export { BrokerEventsPage } from './ui/BrokerEventsPage'
|
||||
|
||||
@ -1,23 +1,34 @@
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import React, { type ReactNode } from 'react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { BrokerEventsPage } from './BrokerEventsPage';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import type { ReactNode } from 'react'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { BrokerEventsPage } from './BrokerEventsPage'
|
||||
|
||||
const mockSetSearchParams = vi.fn()
|
||||
const mockSearchParams = new URLSearchParams()
|
||||
|
||||
vi.mock('@/entities/broker-event', () => ({
|
||||
useBrokerEvents: vi.fn(),
|
||||
}));
|
||||
}))
|
||||
|
||||
vi.mock('@/widgets/broker-account-layout', () => ({
|
||||
useBrokerAccountContext: () => ({ accountId: 'acc-1', portfolio: null }),
|
||||
}));
|
||||
}))
|
||||
|
||||
const mockSetSearchParams = vi.fn();
|
||||
let currentSearchParams = new URLSearchParams();
|
||||
vi.mock('react-router-dom', () => ({
|
||||
useSearchParams: () => [currentSearchParams, mockSetSearchParams],
|
||||
}));
|
||||
vi.mock('@/shared/lib/router/useSearchParams', () => ({
|
||||
useSearchParamsCompat: () => [mockSearchParams, mockSetSearchParams],
|
||||
}))
|
||||
|
||||
vi.mock('@tanstack/react-router', async () => {
|
||||
const actual = await vi.importActual('@tanstack/react-router')
|
||||
return {
|
||||
...actual,
|
||||
useNavigate: () => vi.fn(),
|
||||
Link: actual.Link,
|
||||
Outlet: actual.Outlet,
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('@moex-vibe/design-system', () => ({
|
||||
Button: ({ children, onClick, disabled }: any) => (
|
||||
@ -42,16 +53,16 @@ vi.mock('@moex-vibe/design-system', () => ({
|
||||
type={props.type || 'text'}
|
||||
/>
|
||||
),
|
||||
}));
|
||||
}))
|
||||
|
||||
import { useBrokerEvents } from '@/entities/broker-event';
|
||||
import { useBrokerEvents } from '@/entities/broker-event'
|
||||
|
||||
function createWrapper() {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
|
||||
return function Wrapper({ children }: { children: ReactNode }) {
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
|
||||
};
|
||||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
}
|
||||
}
|
||||
|
||||
const mockData = {
|
||||
@ -124,13 +135,15 @@ const mockData = {
|
||||
estimateMode: null,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
describe('BrokerEventsPage', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
currentSearchParams = new URLSearchParams();
|
||||
});
|
||||
vi.clearAllMocks()
|
||||
mockSearchParams.delete('from')
|
||||
mockSearchParams.delete('to')
|
||||
mockSearchParams.delete('types')
|
||||
})
|
||||
|
||||
it('renders loading state', () => {
|
||||
vi.mocked(useBrokerEvents).mockReturnValue({
|
||||
@ -158,11 +171,11 @@ describe('BrokerEventsPage', () => {
|
||||
promise: new Promise<never>(() => {}),
|
||||
status: 'pending',
|
||||
fetchStatus: 'fetching',
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>);
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>)
|
||||
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() });
|
||||
expect(screen.getByText('Загрузка событий…')).toBeInTheDocument();
|
||||
});
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() })
|
||||
expect(screen.getByText('Загрузка событий…')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renders error state', () => {
|
||||
vi.mocked(useBrokerEvents).mockReturnValue({
|
||||
@ -190,11 +203,11 @@ describe('BrokerEventsPage', () => {
|
||||
promise: new Promise<never>(() => {}),
|
||||
status: 'error',
|
||||
fetchStatus: 'idle',
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>);
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>)
|
||||
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() });
|
||||
expect(screen.getByText('Не удалось загрузить календарь событий')).toBeInTheDocument();
|
||||
});
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() })
|
||||
expect(screen.getByText('Не удалось загрузить календарь событий')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renders empty state', () => {
|
||||
vi.mocked(useBrokerEvents).mockReturnValue({
|
||||
@ -252,11 +265,11 @@ describe('BrokerEventsPage', () => {
|
||||
}),
|
||||
status: 'success',
|
||||
fetchStatus: 'idle',
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>);
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>)
|
||||
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() });
|
||||
expect(screen.getByText('В выбранном диапазоне событий нет')).toBeInTheDocument();
|
||||
});
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() })
|
||||
expect(screen.getByText('В выбранном диапазоне событий нет')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renders date range inputs', () => {
|
||||
vi.mocked(useBrokerEvents).mockReturnValue({
|
||||
@ -284,12 +297,12 @@ describe('BrokerEventsPage', () => {
|
||||
promise: Promise.resolve(mockData),
|
||||
status: 'success',
|
||||
fetchStatus: 'idle',
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>);
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>)
|
||||
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() });
|
||||
expect(screen.getByLabelText('С')).toBeInTheDocument();
|
||||
expect(screen.getByLabelText('По')).toBeInTheDocument();
|
||||
});
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() })
|
||||
expect(screen.getByLabelText('С')).toBeInTheDocument()
|
||||
expect(screen.getByLabelText('По')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renders events heading, summary and table', () => {
|
||||
vi.mocked(useBrokerEvents).mockReturnValue({
|
||||
@ -317,33 +330,31 @@ describe('BrokerEventsPage', () => {
|
||||
promise: Promise.resolve(mockData),
|
||||
status: 'success',
|
||||
fetchStatus: 'idle',
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>);
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>)
|
||||
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() });
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() })
|
||||
|
||||
expect(screen.getByText('События')).toBeInTheDocument();
|
||||
expect(screen.getByText('Событий')).toBeInTheDocument();
|
||||
expect(screen.getByText('3')).toBeInTheDocument();
|
||||
expect(screen.getByText('Ближайшее')).toBeInTheDocument();
|
||||
expect(screen.getByText('Прогноз выплат')).toBeInTheDocument();
|
||||
expect(screen.getByText('Дивиденд')).toBeInTheDocument();
|
||||
expect(screen.getByText('Купон')).toBeInTheDocument();
|
||||
expect(screen.getByText('Погашение')).toBeInTheDocument();
|
||||
expect(screen.getByText('SBER')).toBeInTheDocument();
|
||||
expect(screen.getByText('SU26238RMFS5')).toBeInTheDocument();
|
||||
expect(screen.getByText('VTBR')).toBeInTheDocument();
|
||||
expect(screen.getByText('Прогноз выплат')).toBeInTheDocument();
|
||||
expect(screen.getAllByText('Поступило').length).toBeGreaterThan(0);
|
||||
expect(screen.getByText('Факт')).toBeInTheDocument();
|
||||
expect(screen.getAllByText('Прогноз').length).toBeGreaterThan(0);
|
||||
});
|
||||
expect(screen.getByText('События')).toBeInTheDocument()
|
||||
expect(screen.getByText('Событий')).toBeInTheDocument()
|
||||
expect(screen.getByText('3')).toBeInTheDocument()
|
||||
expect(screen.getByText('Ближайшее')).toBeInTheDocument()
|
||||
expect(screen.getByText('Прогноз выплат')).toBeInTheDocument()
|
||||
expect(screen.getByText('Дивиденд')).toBeInTheDocument()
|
||||
expect(screen.getByText('Купон')).toBeInTheDocument()
|
||||
expect(screen.getByText('Погашение')).toBeInTheDocument()
|
||||
expect(screen.getByText('SBER')).toBeInTheDocument()
|
||||
expect(screen.getByText('SU26238RMFS5')).toBeInTheDocument()
|
||||
expect(screen.getByText('VTBR')).toBeInTheDocument()
|
||||
expect(screen.getByText('Прогноз выплат')).toBeInTheDocument()
|
||||
expect(screen.getAllByText('Поступило').length).toBeGreaterThan(0)
|
||||
expect(screen.getByText('Факт')).toBeInTheDocument()
|
||||
expect(screen.getAllByText('Прогноз').length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('keeps date and type changes as draft until applying filters', async () => {
|
||||
currentSearchParams = new URLSearchParams({
|
||||
from: '2026-06-15',
|
||||
to: '2026-06-29',
|
||||
types: 'dividend,coupon',
|
||||
});
|
||||
mockSearchParams.set('from', '2026-06-15')
|
||||
mockSearchParams.set('to', '2026-06-29')
|
||||
mockSearchParams.set('types', 'dividend,coupon')
|
||||
vi.mocked(useBrokerEvents).mockReturnValue({
|
||||
data: mockData,
|
||||
isLoading: false,
|
||||
@ -369,21 +380,21 @@ describe('BrokerEventsPage', () => {
|
||||
promise: Promise.resolve(mockData),
|
||||
status: 'success',
|
||||
fetchStatus: 'idle',
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>);
|
||||
} as unknown as ReturnType<typeof useBrokerEvents>)
|
||||
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() });
|
||||
render(<BrokerEventsPage />, { wrapper: createWrapper() })
|
||||
|
||||
await userEvent.clear(screen.getByLabelText('С'));
|
||||
await userEvent.type(screen.getByLabelText('С'), '2026-06-10');
|
||||
await userEvent.click(screen.getByLabelText('Купоны'));
|
||||
await userEvent.clear(screen.getByLabelText('С'))
|
||||
await userEvent.type(screen.getByLabelText('С'), '2026-06-10')
|
||||
await userEvent.click(screen.getByLabelText('Купоны'))
|
||||
|
||||
expect(mockSetSearchParams).not.toHaveBeenCalled();
|
||||
expect(mockSetSearchParams).not.toHaveBeenCalled()
|
||||
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Показать' }));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Показать' }))
|
||||
|
||||
const applied = mockSetSearchParams.mock.calls[0][0] as URLSearchParams;
|
||||
expect(applied.get('from')).toBe('2026-06-10');
|
||||
expect(applied.get('to')).toBe('2026-06-29');
|
||||
expect(applied.get('types')).toBe('dividend');
|
||||
});
|
||||
});
|
||||
const applied = mockSetSearchParams.mock.calls[0][0] as URLSearchParams
|
||||
expect(applied.get('from')).toBe('2026-06-10')
|
||||
expect(applied.get('to')).toBe('2026-06-29')
|
||||
expect(applied.get('types')).toBe('dividend')
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,122 +1,122 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useSearchParams } from 'react-router-dom';
|
||||
import { Box } from '@mui/material';
|
||||
import { Button, Checkbox, Chip, Heading, Text, TextField } from '@moex-vibe/design-system';
|
||||
import dayjs from 'dayjs';
|
||||
import { useBrokerEvents } from '@/entities/broker-event';
|
||||
import { useBrokerAccountContext } from '@/widgets/broker-account-layout';
|
||||
import { formatBrokerCurrencyValue, formatBrokerDate } from '@/shared/lib/formatters';
|
||||
import { Button, Checkbox, Chip, Heading, Text, TextField } from '@moex-vibe/design-system'
|
||||
import { Box } from '@mui/material'
|
||||
import dayjs from 'dayjs'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useBrokerEvents } from '@/entities/broker-event'
|
||||
import { formatBrokerCurrencyValue, formatBrokerDate } from '@/shared/lib/formatters'
|
||||
import { useSearchParamsCompat } from '@/shared/lib/router/useSearchParams'
|
||||
import { useBrokerAccountContext } from '@/widgets/broker-account-layout'
|
||||
|
||||
const EVENT_TYPES = ['dividend', 'coupon', 'maturity', 'offer'] as const;
|
||||
const EVENT_TYPES = ['dividend', 'coupon', 'maturity', 'offer'] as const
|
||||
|
||||
type EventType = (typeof EVENT_TYPES)[number];
|
||||
type EventType = (typeof EVENT_TYPES)[number]
|
||||
|
||||
type Filters = {
|
||||
from: string;
|
||||
to: string;
|
||||
types: EventType[];
|
||||
};
|
||||
from: string
|
||||
to: string
|
||||
types: EventType[]
|
||||
}
|
||||
|
||||
const EVENT_TYPE_OPTIONS: { value: EventType; label: string }[] = [
|
||||
{ value: 'dividend', label: 'Дивиденды' },
|
||||
{ value: 'coupon', label: 'Купоны' },
|
||||
{ value: 'maturity', label: 'Погашения' },
|
||||
{ value: 'offer', label: 'Оферты' },
|
||||
];
|
||||
]
|
||||
|
||||
function eventTypeLabel(type: string): string {
|
||||
switch (type) {
|
||||
case 'dividend':
|
||||
return 'Дивиденд';
|
||||
return 'Дивиденд'
|
||||
case 'coupon':
|
||||
return 'Купон';
|
||||
return 'Купон'
|
||||
case 'maturity':
|
||||
return 'Погашение';
|
||||
return 'Погашение'
|
||||
case 'offer':
|
||||
return 'Оферта';
|
||||
return 'Оферта'
|
||||
default:
|
||||
return type;
|
||||
return type
|
||||
}
|
||||
}
|
||||
|
||||
function defaultPeriod(): { from: string; to: string } {
|
||||
const now = dayjs();
|
||||
const now = dayjs()
|
||||
return {
|
||||
from: now.subtract(7, 'day').format('YYYY-MM-DD'),
|
||||
to: now.add(7, 'day').format('YYYY-MM-DD'),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function parseTypes(value: string | null): EventType[] {
|
||||
if (!value) return [...EVENT_TYPES];
|
||||
if (!value) return [...EVENT_TYPES]
|
||||
|
||||
const parsed = value
|
||||
.split(',')
|
||||
.map((type) => type.trim())
|
||||
.filter((type): type is EventType => EVENT_TYPES.includes(type as EventType));
|
||||
.filter((type): type is EventType => EVENT_TYPES.includes(type as EventType))
|
||||
|
||||
return parsed.length > 0 ? parsed : [...EVENT_TYPES];
|
||||
return parsed.length > 0 ? parsed : [...EVENT_TYPES]
|
||||
}
|
||||
|
||||
function filtersFromSearchParams(searchParams: URLSearchParams): Filters {
|
||||
const def = defaultPeriod();
|
||||
const from = searchParams.get('from');
|
||||
const to = searchParams.get('to');
|
||||
const def = defaultPeriod()
|
||||
const from = searchParams.get('from')
|
||||
const to = searchParams.get('to')
|
||||
|
||||
return {
|
||||
from: from && dayjs(from).isValid() ? from : def.from,
|
||||
to: to && dayjs(to).isValid() ? to : def.to,
|
||||
types: parseTypes(searchParams.get('types')),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function filtersToSearchParams(filters: Filters): URLSearchParams {
|
||||
const next = new URLSearchParams();
|
||||
next.set('from', filters.from);
|
||||
next.set('to', filters.to);
|
||||
next.set('types', filters.types.join(','));
|
||||
return next;
|
||||
const next = new URLSearchParams()
|
||||
next.set('from', filters.from)
|
||||
next.set('to', filters.to)
|
||||
next.set('types', filters.types.join(','))
|
||||
return next
|
||||
}
|
||||
|
||||
function sourceLabel(source: string): string {
|
||||
return source === 'actual' ? 'Факт' : 'Прогноз';
|
||||
return source === 'actual' ? 'Факт' : 'Прогноз'
|
||||
}
|
||||
|
||||
export function BrokerEventsPage() {
|
||||
const { accountId } = useBrokerAccountContext();
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const { accountId } = useBrokerAccountContext()
|
||||
const [searchParams, setSearchParams] = useSearchParamsCompat()
|
||||
const [appliedFilters, setAppliedFilters] = useState<Filters>(() =>
|
||||
filtersFromSearchParams(searchParams),
|
||||
);
|
||||
)
|
||||
const [draftFilters, setDraftFilters] = useState<Filters>(() =>
|
||||
filtersFromSearchParams(searchParams),
|
||||
);
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
const next = filtersFromSearchParams(searchParams);
|
||||
setAppliedFilters(next);
|
||||
setDraftFilters(next);
|
||||
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
const next = filtersFromSearchParams(searchParams)
|
||||
setAppliedFilters(next)
|
||||
setDraftFilters(next)
|
||||
}, [searchParams])
|
||||
|
||||
const from = draftFilters.from;
|
||||
const to = draftFilters.to;
|
||||
const from = draftFilters.from
|
||||
const to = draftFilters.to
|
||||
|
||||
const validFrom = dayjs(from);
|
||||
const validTo = dayjs(to);
|
||||
const validFrom = dayjs(from)
|
||||
const validTo = dayjs(to)
|
||||
const dateError =
|
||||
from && to && validFrom.isValid() && validTo.isValid() && validTo.isBefore(validFrom)
|
||||
? '"По" не может быть раньше "С"'
|
||||
: '';
|
||||
const typeError = draftFilters.types.length === 0 ? 'Выберите хотя бы один тип события' : '';
|
||||
const filterError = dateError || typeError;
|
||||
: ''
|
||||
const typeError = draftFilters.types.length === 0 ? 'Выберите хотя бы один тип события' : ''
|
||||
const filterError = dateError || typeError
|
||||
|
||||
const events = useBrokerEvents(filterError ? undefined : accountId, {
|
||||
from: appliedFilters.from,
|
||||
to: appliedFilters.to,
|
||||
types: appliedFilters.types.join(','),
|
||||
});
|
||||
})
|
||||
|
||||
const ev = events.data;
|
||||
const ev = events.data
|
||||
|
||||
function toggleType(type: EventType, checked: boolean) {
|
||||
setDraftFilters((current) => ({
|
||||
@ -124,13 +124,13 @@ export function BrokerEventsPage() {
|
||||
types: checked
|
||||
? [...new Set([...current.types, type])]
|
||||
: current.types.filter((t) => t !== type),
|
||||
}));
|
||||
}))
|
||||
}
|
||||
|
||||
function applyFilters() {
|
||||
if (filterError) return;
|
||||
setAppliedFilters(draftFilters);
|
||||
setSearchParams(filtersToSearchParams(draftFilters), { replace: true });
|
||||
if (filterError) return
|
||||
setAppliedFilters(draftFilters)
|
||||
setSearchParams(filtersToSearchParams(draftFilters), { replace: true })
|
||||
}
|
||||
|
||||
return (
|
||||
@ -147,7 +147,7 @@ export function BrokerEventsPage() {
|
||||
type="date"
|
||||
value={from}
|
||||
onChange={(e) => {
|
||||
setDraftFilters((current) => ({ ...current, from: e.target.value }));
|
||||
setDraftFilters((current) => ({ ...current, from: e.target.value }))
|
||||
}}
|
||||
InputLabelProps={{ shrink: true }}
|
||||
/>
|
||||
@ -156,7 +156,7 @@ export function BrokerEventsPage() {
|
||||
type="date"
|
||||
value={to}
|
||||
onChange={(e) => {
|
||||
setDraftFilters((current) => ({ ...current, to: e.target.value }));
|
||||
setDraftFilters((current) => ({ ...current, to: e.target.value }))
|
||||
}}
|
||||
InputLabelProps={{ shrink: true }}
|
||||
error={!!dateError}
|
||||
@ -406,5 +406,5 @@ export function BrokerEventsPage() {
|
||||
</Box>
|
||||
) : null}
|
||||
</Box>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
export { BrokerOperationsPage } from './ui/BrokerOperationsPage';
|
||||
export { BrokerOperationsPage } from './ui/BrokerOperationsPage'
|
||||
|
||||
@ -1,36 +1,36 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useSearchParams } from 'react-router-dom';
|
||||
import { Box } from '@mui/material';
|
||||
import { Heading, Text } from '@moex-vibe/design-system';
|
||||
import { Heading, Text } from '@moex-vibe/design-system'
|
||||
import { Box } from '@mui/material'
|
||||
import { useEffect } from 'react'
|
||||
import {
|
||||
BROKER_OPERATION_TYPE_OPTIONS,
|
||||
isBrokerOperationType,
|
||||
useBrokerOperations,
|
||||
} from '@/entities/broker-operation';
|
||||
import { useBrokerAccountContext } from '@/widgets/broker-account-layout';
|
||||
import { BrokerOperationsTable } from '@/widgets/broker-operations-table';
|
||||
import { useCursorPagination } from '@/shared/lib/useCursorPagination';
|
||||
} from '@/entities/broker-operation'
|
||||
import { useSearchParamsCompat } from '@/shared/lib/router/useSearchParams'
|
||||
import { useCursorPagination } from '@/shared/lib/useCursorPagination'
|
||||
import { useBrokerAccountContext } from '@/widgets/broker-account-layout'
|
||||
import { BrokerOperationsTable } from '@/widgets/broker-operations-table'
|
||||
|
||||
export function BrokerOperationsPage() {
|
||||
const { accountId } = useBrokerAccountContext();
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const urlType = searchParams.get('type');
|
||||
const selectedType = isBrokerOperationType(urlType) ? urlType : '';
|
||||
const pagination = useCursorPagination();
|
||||
const { accountId } = useBrokerAccountContext()
|
||||
const [searchParams, setSearchParams] = useSearchParamsCompat()
|
||||
const urlType = searchParams.get('type')
|
||||
const selectedType = isBrokerOperationType(urlType) ? urlType : ''
|
||||
const pagination = useCursorPagination()
|
||||
|
||||
const operations = useBrokerOperations(accountId, {
|
||||
limit: 10,
|
||||
cursor: pagination.cursor,
|
||||
operationTypes: selectedType || undefined,
|
||||
});
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
pagination.reset();
|
||||
}, [selectedType]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
pagination.reset()
|
||||
}, [pagination.reset])
|
||||
|
||||
function handleTypeChange(event: React.ChangeEvent<HTMLSelectElement>) {
|
||||
const nextType = event.target.value;
|
||||
setSearchParams(nextType ? { type: nextType } : {}, { replace: true });
|
||||
const nextType = event.target.value
|
||||
setSearchParams(nextType ? { type: nextType } : {}, { replace: true })
|
||||
}
|
||||
|
||||
const history = operations.error ? (
|
||||
@ -54,7 +54,7 @@ export function BrokerOperationsPage() {
|
||||
onNext: () => pagination.handleNext(operations.data?.nextCursor),
|
||||
}}
|
||||
/>
|
||||
);
|
||||
)
|
||||
|
||||
return (
|
||||
<Box component="section" aria-labelledby="broker-operations-heading">
|
||||
@ -78,5 +78,5 @@ export function BrokerOperationsPage() {
|
||||
</Box>
|
||||
{history}
|
||||
</Box>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
export { BrokerPositionsPage } from './ui/BrokerPositionsPage';
|
||||
export { BrokerPositionsPage } from './ui/BrokerPositionsPage'
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user