import {z} from 'zod' export type NotificationCooldownDTO = {enabled?: boolean | undefined, minutes?: number | undefined}; export type NotificationDigestDTO = {enabled?: boolean | undefined, errorsImmediate?: boolean | undefined, windowSeconds?: number | undefined}; export type LogTypeDTO = {id: number, name?: string | undefined}; export type NotificationQuietHoursDTO = {activeDays?: Array | undefined, after?: string | undefined, allowErrors?: boolean | undefined, enabled?: boolean | undefined, from?: string | undefined, timeZone?: string | undefined, to?: string | undefined}; export type NotificationTargetDTO = {featureId?: string | undefined, recipient?: string | undefined}; export type FeatureNotificationRuleDTO = {accountIds?: Array | undefined, categories?: (Array<'TRADING' | 'COPY_SKIPPED' | 'EXECUTION_ERROR' | 'ACCOUNT_CONNECTION' | 'RISK' | 'NEWS' | 'SIGNALS' | 'SYSTEM' | 'OTHER'>) | undefined, cooldown?: NotificationCooldownDTO | undefined, digest?: NotificationDigestDTO | undefined, enabled?: boolean | undefined, events?: (Array<'POSITION_OPENED' | 'POSITION_CLOSED' | 'POSITION_MODIFIED' | 'HISTORY_UPDATED' | 'TRADE_APPROVAL_REQUIRED' | 'TRADE_APPROVED' | 'UNMANAGED_POSITION_DETECTED' | 'COPY_SKIPPED' | 'ORDER_REJECTED' | 'INSUFFICIENT_MARGIN' | 'TRADE_FAILED_DISCONNECTED' | 'COPY_FAILED' | 'DUPLICATE_POSITION_DETECTED' | 'ACCOUNT_STARTED' | 'ACCOUNT_STOPPED' | 'ACCOUNT_CONNECTED' | 'ACCOUNT_DISCONNECTED' | 'ACCOUNT_LOGIN_FAILED' | 'ACCOUNT_TOKEN_EXPIRING' | 'ACCOUNT_RESTRICTED' | 'ACCOUNT_FALLBACK_MODE' | 'RISK_LIMIT_REACHED' | 'PROFIT_TARGET_REACHED' | 'LOSS_PROTECTED_EXIT' | 'GUARDRAIL_TRIGGERED' | 'COPIER_STOPPED_BY_RISK' | 'LIMIT_RESET' | 'BALANCE_ADJUSTMENT_DETECTED' | 'NEWS_EVENT_UPCOMING' | 'NEWS_EVENT_RELEASED' | 'NEWS_FILTER_ACTION_SCHEDULED' | 'NEWS_FILTER_ACTION_IMMINENT' | 'NEWS_FILTER_DAILY_AGENDA' | 'NEWS_FILTER_SKIPPED_COPY' | 'NEWS_FILTER_CLOSED_POSITION' | 'NEWS_FILTER_REOPENED' | 'NEWS_FILTER_REOPEN_SKIPPED' | 'NEWS_FILTER_HOLIDAY_MODE' | 'MARKET_NEWS_HIGH_IMPACT' | 'SIGNAL_RECEIVED' | 'SIGNAL_EXECUTED' | 'SIGNAL_REJECTED' | 'SIGNAL_AI_LIMIT_REACHED' | 'SIGNAL_SUBSCRIPTION_ENDED' | 'CONFIGURATION_ERROR' | 'CONFIGURATION_CHANGED' | 'MAINTENANCE_WINDOW' | 'PROXY_ISSUE' | 'EXPERT_ADVISOR_STARTED' | 'EXPERT_ADVISOR_STOPPED' | 'EXPERT_ADVISOR_WARNING' | 'LOG_RATE_LIMITED' | 'NOTIFICATION_CHANNEL_DISABLED' | 'GENERAL'>) | undefined, excludeAccountIds?: Array | undefined, includeProjectEvents?: boolean | undefined, kind?: string | undefined, migratedFromFeatureId?: string | undefined, minLogLevel: LogTypeDTO, name: string, quietHours?: NotificationQuietHoursDTO | undefined, sourceFeatureIds?: Array | undefined, sourceFeatureTypeIds?: Array | undefined, targets?: Array | undefined, useAlias?: boolean | undefined}; export type NotificationRulePreviewRequestDTO = {from?: string | undefined, rule?: FeatureNotificationRuleDTO | undefined, to?: string | undefined}; export type CreateApiRestApiV1ProjectsProjectIdNotificationRulesPreviewArgs = {projectId: string, body: NotificationRulePreviewRequestDTO}; export type CreateApiRestApiV1ProjectsProjectIdNotificationRulesPreviewOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationRulePreviewChannelDTO = z.object({channelType: z.enum(["TELEGRAM", "WEBHOOK", "SLACK", "MS_TEAMS", "DISCORD"]).optional(), estimatedRequestCount: z.number().int().optional(), featureId: z.string().optional(), matchedCount: z.number().int().optional(), recipient: z.string().optional()}); export const notificationRulePreviewDTO = z.object({assumptions: z.array(z.string()).optional(), complete: z.boolean().optional(), estimatedRequestCount: z.number().int().optional(), evaluatedCount: z.number().int().optional(), from: z.string().optional(), matchedCount: z.number().int().optional(), perChannel: z.array(notificationRulePreviewChannelDTO).optional(), to: z.string().optional(), truncated: z.boolean().optional(), unclassifiedCount: z.number().int().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 }; /** * Estimate how often a rule would have fired * * POST /rest/api/v1/projects/{projectId}/notificationRules/preview */ export const createApiRestApiV1ProjectsProjectIdNotificationRulesPreview = async (args: CreateApiRestApiV1ProjectsProjectIdNotificationRulesPreviewArgs, options?: CreateApiRestApiV1ProjectsProjectIdNotificationRulesPreviewOptions) => { 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/projects/${encodeURIComponent(String(args.projectId))}/notificationRules/preview${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(notificationRulePreviewDTO, await res.json()) };