This commit is contained in:
2026-06-10 15:27:12 +03:00
parent b3752585ca
commit 493ba4ccef
17 changed files with 898 additions and 4756 deletions

View File

@@ -0,0 +1,81 @@
import { getCsrfToken } from '@shared/api/csrf'
import { refreshAccessToken } from '@shared/api/tokenStore'
const B2B_API_URL = 'https://app.b2b.elcsa.ru/api'
async function doB2bRequest<T>(
path: string,
options: RequestInit,
allowRetry: boolean,
): Promise<T> {
const csrf = await getCsrfToken()
const res = await fetch(`${B2B_API_URL}${path}`, {
...options,
credentials: 'include',
headers: {
'X-CSRF-Token': csrf,
...options.headers,
},
})
if (res.status === 401 && allowRetry) {
try {
await refreshAccessToken()
return doB2bRequest<T>(path, options, false)
} catch {
throw new Error('Unauthorized')
}
}
const data = await res.json()
if (!res.ok) throw data
return data as T
}
export interface CreatePurchaseRequestBody {
usdt_amount: number | string
comment?: string | null
target_wallet_chain?: string | null
target_wallet_address?: string | null
}
export interface B2bPurchaseRequest {
id: string
organization_id: string
status: string
usdt_amount: string
rub_amount: string | null
exchange_rate: string | null
service_fee_percent: string | null
comment: string | null
admin_comment: string | null
target_wallet_chain: string | null
target_wallet_address: string | null
tx_hash: string | null
created_at: string | null
updated_at: string | null
completed_at: string | null
}
export interface B2bPurchaseRequestListResponse {
items: B2bPurchaseRequest[]
total: number
}
export function createPurchaseRequest(
body: CreatePurchaseRequestBody,
): Promise<B2bPurchaseRequest> {
return doB2bRequest('/purchase-requests', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
}, true)
}
export function getMyPurchaseRequests(
limit = 50,
offset = 0,
): Promise<B2bPurchaseRequestListResponse> {
return doB2bRequest(`/purchase-requests?limit=${limit}&offset=${offset}`, {}, true)
}

View File

@@ -0,0 +1,13 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { createPurchaseRequest } from '../api/b2bApi'
export function useCreatePurchaseRequest() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: createPurchaseRequest,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['b2b', 'purchase-requests'] })
},
})
}

View File

@@ -0,0 +1,10 @@
import { useQuery } from '@tanstack/react-query'
import { getMyPurchaseRequests } from '../api/b2bApi'
export function useMyPurchaseRequests() {
return useQuery({
queryKey: ['b2b', 'purchase-requests'],
queryFn: () => getMyPurchaseRequests(),
staleTime: 30_000,
})
}

View File

@@ -0,0 +1,7 @@
export { useCreatePurchaseRequest } from './hooks/useCreatePurchaseRequest'
export { useMyPurchaseRequests } from './hooks/useMyPurchaseRequests'
export type {
CreatePurchaseRequestBody,
B2bPurchaseRequest,
B2bPurchaseRequestListResponse,
} from './api/b2bApi'