import {z} from 'zod' export type GetApiApiV1DashboardAnalyticsArgs = {tracker?: Array | undefined, team_id?: (Array | null) | undefined, company_user_id?: (Array | null) | undefined, period?: ('today' | 'last_24h' | 'last_7d' | 'this_week' | 'last_30d' | 'this_month' | 'last_90d' | 'this_year' | 'custom' | null) | undefined, from?: (string | null) | undefined, to?: (string | null) | undefined, tz?: (string | null) | undefined, chartMetrics?: (Array<'clicks' | 'uniqueClicks' | 'installs' | 'leads' | 'firstSales' | 'repeatedSales' | 'totalEvents' | 'blockedEvents' | 'whiteClicks' | 'blackClicks'> | null) | undefined, summaryMetrics?: (Array<'totalClicks' | 'uniqueClicks' | 'totalInstalls' | 'totalLeads' | 'totalFirstSales' | 'totalRepeatedSales' | 'totalEvents' | 'blockedEvents' | 'uniqueUsers' | 'repeatBuyers' | 'whiteClicks' | 'blackClicks'> | null) | undefined}; export type GetApiApiV1DashboardAnalyticsOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dashboardAnalyticsSummaryDTO = z.object({totalClicks: z.number().int().optional(), uniqueClicks: z.number().int().optional(), totalInstalls: z.number().int().optional(), totalLeads: z.number().int().optional(), totalFirstSales: z.number().int().optional(), totalRepeatedSales: z.number().int().optional(), totalEvents: z.number().int().optional(), blockedEvents: z.number().int().optional(), repeatBuyers: z.number().int().optional(), whiteClicks: z.number().int().optional(), blackClicks: z.number().int().optional(), uniqueUsers: z.number().int().nullable().optional()}); export const dashboardTrendPointDTO = z.object({timestamp: z.string(), clicks: z.number().int().nullable().optional(), uniqueClicks: z.number().int().nullable().optional(), installs: z.number().int().nullable().optional(), leads: z.number().int().nullable().optional(), firstSales: z.number().int().nullable().optional(), repeatedSales: z.number().int().nullable().optional(), totalEvents: z.number().int().nullable().optional(), blockedEvents: z.number().int().nullable().optional(), whiteClicks: z.number().int().nullable().optional(), blackClicks: z.number().int().nullable().optional()}); export const dashboardAnalyticsWindowDTO = z.object({startDate: z.string(), endDate: z.string(), summary: dashboardAnalyticsSummaryDTO, dataPoints: z.array(dashboardTrendPointDTO).optional()}); export const dashboardAnalyticsResponseDTO = z.object({period: z.enum(["today", "last_24h", "last_7d", "this_week", "last_30d", "this_month", "last_90d", "this_year", "custom"]), interval: z.enum(["hour", "day", "week"]), current: dashboardAnalyticsWindowDTO, previous: dashboardAnalyticsWindowDTO}); 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 }; /** * Analytics for current + previous period * * GET /api/v1/dashboard/analytics */ export const getApiApiV1DashboardAnalytics = async (args: GetApiApiV1DashboardAnalyticsArgs, options: GetApiApiV1DashboardAnalyticsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.tracker ?? []) query.append("tracker", String(item)) for (const item of args.team_id ?? []) query.append("team_id", String(item)) for (const item of args.company_user_id ?? []) query.append("company_user_id", String(item)) if (args.period != null) query.append("period", String(args.period)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.tz != null) query.append("tz", String(args.tz)) for (const item of args.chartMetrics ?? []) query.append("chartMetrics", String(item)) for (const item of args.summaryMetrics ?? []) query.append("summaryMetrics", String(item)) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/dashboard/analytics${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dashboardAnalyticsResponseDTO, await res.json()) };