import {z} from 'zod' export type GetApiHdDesksDeskIdTicketsNumberArgs = {desk_id: string, number: string, 'X-SV-Client': 'help-desk-portal', response?: string | undefined}; export type GetApiHdDesksDeskIdTicketsNumberOptions = {baseUrl?: string | undefined, HelpDeskCustomerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const helpDeskCustomerFeedback = z.object({eligible: z.boolean(), lookup_complete: z.boolean(), rating: z.enum(["positive", "negative", "null"]).nullable(), submitted_at: z.number().int().nullable(), updated_at: z.number().int().nullable(), version: z.number().int().gte(0), reply_event_id: z.string().nullable(), destination_url: z.string().nullable()}); export const helpDeskCustomerEvent = z.object({event_id: z.string(), seq: z.number().int().gte(1), type: z.enum(["ticket_created", "customer_message", "operator_reply", "status_changed", "reopened", "resolved_by_customer", "merged_into", "merged_from"]), created_at: z.number().int(), actor: z.object({kind: z.enum(["customer", "operator", "system", "rule"]), label: z.string().nullable(), avatar_handle: z.string().nullable()}), body: z.object({}).nullable(), attachments: z.array(z.object({attachment_id: z.string(), name: z.string(), size: z.number().int(), disposition: z.enum(["inline", "attachment"]), scan: z.enum(["pass", "not_scanned"])})), status: z.object({from: z.string().nullable().optional(), to: z.string().nullable().optional()}).optional(), merged: z.object({number: z.number().int().nullable().optional()}).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 getApiHdDesksDeskIdTicketsNumberResponse = z.object({data: z.object({ticket: z.object({number: z.number().int().gte(1), subject: z.string(), status: z.enum(["open", "waiting_on_customer", "on_hold", "closed"]), close_reason: z.enum(["resolved", "duplicate", "null"]).nullable(), awaiting_first_response: z.boolean(), created_at: z.number().int(), last_public_activity_at: z.number().int().nullable(), muted: z.boolean(), feedback: helpDeskCustomerFeedback, can_resolve: z.boolean()}), events: z.array(helpDeskCustomerEvent), next_cursor: z.string().nullable()}), 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 one of the customer's requests * * GET /hd/desks/{desk_id}/tickets/{number} */ export const getApiHdDesksDeskIdTicketsNumber = async (args: GetApiHdDesksDeskIdTicketsNumberArgs, options?: GetApiHdDesksDeskIdTicketsNumberOptions) => { const {baseUrl = "https://api.sparkvault.com/v1", fetch: fetchFn = fetch, HelpDeskCustomerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-SV-Client'] != null) headers.set("X-SV-Client", String(args['X-SV-Client'])) if (args.response != null) query.append("response", String(args.response)) if (HelpDeskCustomerToken !== undefined) headers.set('authorization', `Bearer ${HelpDeskCustomerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/hd/desks/${encodeURIComponent(String(args.desk_id))}/tickets/${encodeURIComponent(String(args.number))}${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(getApiHdDesksDeskIdTicketsNumberResponse, await res.json()) };