import {z} from 'zod' export type ReportPerformanceParametersDTO = {id: Array, idType: 'ACCOUNT' | 'PROJECT' | 'FEATURE', start: string, stop: string}; export type CreateApiRestApiV1ReportsPerformanceArgs = {body: ReportPerformanceParametersDTO}; export type CreateApiRestApiV1ReportsPerformanceOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const positionDTO = z.object({brokerTimeClose: z.string().optional(), brokerTimeOpen: z.string().optional(), closePrice: z.number().optional(), closeTime: z.string().optional(), comment: z.string().optional(), commission: z.number().optional(), copierId: z.string().optional(), dealType: z.enum(["DealBuy", "DealSell", "Balance", "Credit", "Charge", "Correction", "Bonus", "Commission", "DailyCommission", "MonthlyCommission", "DailyAgentCommission", "MonthlyAgentCommission", "InterestRate", "CanceledBuy", "CanceledSell", "Dividend", "FrankedDividend", "Tax", "AgentCommission", "SoCompensation", "SoCreditCompensation"]).optional(), id: z.string().optional(), lossProtected: z.boolean().optional(), magicNumber: z.string().optional(), netProfit: z.number().optional(), openPrice: z.number().optional(), openTime: z.string().optional(), orderType: z.enum(["Buy", "Sell", "BuyLimit", "SellLimit", "BuyStop", "SellStop"]).optional(), placedType: z.enum(["Manually", "Mobile", "Web", "ByExpert", "OnSL", "OnTP", "OnStopOut", "OnRollover", "OnVmargin", "OnSplit", "ByDealer", "Gateway", "Signal", "Settlement", "Transfer", "Sync", "ExternalService", "Migration"]).optional(), profit: z.number().optional(), requestId: z.string().optional(), signalProviderId: z.string().optional(), sourceAccountId: z.string().optional(), sourcePositionId: z.string().optional(), state: z.string().optional(), stopLoss: z.number().optional(), swap: z.number().optional(), symbol: z.string().optional(), takeProfit: z.number().optional(), telegramConnectorId: z.string().optional(), volume: z.number().optional()}); export const accountPerformanceDTO = z.object({accountId: z.string().optional(), balance: z.number().optional(), currency: z.string().optional(), currentDrawdown: z.number().optional(), customerEmail: z.string().optional(), date: z.string().optional(), deleted: z.boolean().optional(), equity: z.number().optional(), history: z.array(positionDTO).optional(), netProfits: z.record(z.string(), z.record(z.string(), z.number())).optional()}); export const reportPerformanceDTO = z.object({accounts: z.array(accountPerformanceDTO).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a performance report * * POST /rest/api/v1/reports/performance */ export const createApiRestApiV1ReportsPerformance = async (args: CreateApiRestApiV1ReportsPerformanceArgs, options?: CreateApiRestApiV1ReportsPerformanceOptions) => { const {baseUrl = "https://api.metacopier.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/rest/api/v1/reports/performance${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(reportPerformanceDTO, await res.json()) };