import {z} from 'zod' export type GetApiProductsHelpDeskAnalyticsArgs = {from: string, to: string, team_id?: string | undefined, operator_id?: string | undefined}; export type GetApiProductsHelpDeskAnalyticsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const helpDeskAnalyticsFigures = z.object({created: z.number().int(), closed: z.number().int(), reopened: z.number().int(), feedback_positive: z.number().int().gte(0), feedback_negative: z.number().int().gte(0), feedback_total: z.number().int().gte(0), satisfaction_pct: z.number().gte(0).lte(100).nullable(), median_frt_s: z.number().int().nullable(), p90_frt_s: z.number().int().nullable(), mean_frt_s: z.number().int().nullable(), median_res_s: z.number().int().nullable(), p90_res_s: z.number().int().nullable(), mean_res_s: z.number().int().nullable(), backlog_end: z.number().int().nullable(), sample: z.object({frt: z.number().int(), res: z.number().int()})}); export const helpDeskOperatorFigures = z.object({assigned: z.number().int(), replies: z.number().int(), closed: z.number().int(), reopened: z.number().int(), median_frt_s: z.number().int().nullable(), mean_frt_s: z.number().int().nullable(), median_res_s: z.number().int().nullable(), mean_res_s: z.number().int().nullable(), first_responses: z.number().int(), resolutions: z.number().int(), feedback_positive: z.number().int().gte(0), feedback_negative: z.number().int().gte(0), feedback_total: z.number().int().gte(0), satisfaction_pct: z.number().gte(0).lte(100).nullable()}); export const helpDeskAnalyticsReport = z.object({kpis: helpDeskAnalyticsFigures, previous: helpDeskAnalyticsFigures, series: z.array(z.object({day: z.string(), created: z.number().int(), closed: z.number().int(), backlog: z.number().int().nullable(), feedback_positive: z.number().int().gte(0), feedback_negative: z.number().int().gte(0), feedback_total: z.number().int().gte(0), satisfaction_pct: z.number().gte(0).lte(100).nullable()})), frt_buckets: z.array(z.number().int()), res_buckets: z.array(z.number().int()), heatmap: z.array(z.array(z.number().int())), teams: z.array(z.object({team_id: z.string(), created: z.number().int(), closed: z.number().int(), feedback_positive: z.number().int().gte(0), feedback_negative: z.number().int().gte(0), feedback_total: z.number().int().gte(0), satisfaction_pct: z.number().gte(0).lte(100).nullable()})), operators: z.array(helpDeskOperatorFigures).optional(), partial: z.literal(true).optional()}); export const responseMeta = z.object({api_version: z.string().optional(), request_id: z.string().optional(), response_ms: z.number().int().optional(), timestamp: z.number().int().optional(), pools: z.object({}).optional(), billing: z.object({past_due: z.boolean().optional()}).optional(), quota: z.object({limit: z.number().int().optional(), used: z.number().int().optional(), remaining: z.number().int().optional(), resets_at: z.number().int().optional()}).optional()}); export const getApiProductsHelpDeskAnalyticsResponse = z.object({data: helpDeskAnalyticsReport, meta: responseMeta.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 }; /** * Read the help desk analytics * * GET /products/help-desk/analytics */ export const getApiProductsHelpDeskAnalytics = async (args: GetApiProductsHelpDeskAnalyticsArgs, options?: GetApiProductsHelpDeskAnalyticsOptions) => { const {baseUrl = "https://api.sparkvault.com/v1", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.team_id != null) query.append("team_id", String(args.team_id)) if (args.operator_id != null) query.append("operator_id", String(args.operator_id)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/products/help-desk/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(getApiProductsHelpDeskAnalyticsResponse, await res.json()) };