import {z} from 'zod' export type HelpDeskTicketPatch = {status?: ('open' | 'waiting_on_customer' | 'on_hold' | 'closed') | undefined, close_reason?: ('resolved' | 'spam') | undefined, hold_until?: number | undefined, priority?: ('urgent' | 'high' | 'normal' | 'low') | undefined, team_id?: (string | null) | undefined, assignee_id?: (string | null) | undefined, tags?: Array | undefined, add_tags?: Array | undefined, remove_tags?: Array | undefined}; export type PatchApiProductsHelpDeskTicketsTicketIdArgs = {ticket_id: string, body: {patch: HelpDeskTicketPatch, version: number}}; export type PatchApiProductsHelpDeskTicketsTicketIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const helpDeskRequester = z.object({requester_id: z.string(), display_name: z.string().nullable(), email: z.string(), assurance: z.enum(["identity", "email_pin"])}).nullable(); export const helpDeskPresenceEntry = z.object({user_id: z.string(), state: z.enum(["viewing", "replying"])}); export const helpDeskOperatorFeedback = 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(), operator_id: z.string().nullable()}); export const helpDeskTicket = z.object({ticket_id: z.string(), number: z.number().int(), status: z.enum(["open", "waiting_on_customer", "on_hold", "closed"]), close_reason: z.enum(["resolved", "duplicate", "spam", "null"]).nullable(), hold_until: z.number().int().nullable(), priority: z.enum(["urgent", "high", "normal", "low"]), team_id: z.string().nullable(), assignee_id: z.string().nullable(), requester_id: z.string(), subject: z.string(), preview: z.string(), tags: z.array(z.string()), created_at: z.number().int(), last_activity_at: z.number().int().nullable(), last_customer_message_at: z.number().int().nullable(), first_operator_reply_at: z.number().int().nullable(), last_operator_reply_at: z.number().int().nullable(), merged_into: z.string().nullable(), merged_into_number: z.number().int().nullable(), muted: z.boolean(), awaiting_first_response: z.boolean(), version: z.number().int(), requester: helpDeskRequester, presence: z.array(helpDeskPresenceEntry).optional(), source: z.literal("portal"), routing_rule_id: z.string().nullable(), last_public_activity_at: z.number().int().nullable(), first_closed_at: z.number().int().nullable(), closed_at: z.number().int().nullable(), reopen_count: z.number().int(), merged_at: z.number().int().nullable(), legal_hold: z.object({}).nullable(), email_undeliverable: z.object({}).nullable(), held_count: z.number().int(), feedback: helpDeskOperatorFeedback}); export const helpDeskEvent = z.object({event_id: z.string(), seq: z.number().int(), type: z.enum(["ticket_created", "customer_message", "operator_reply", "status_changed", "reopened", "resolved_by_customer", "merged_into", "merged_from", "internal_note", "reply_review_submitted", "reply_review_rejected", "reply_review_approved", "reply_review_reassigned", "reply_review_discarded", "feedback_changed", "assigned", "team_changed", "priority_changed", "tags_changed", "routing_applied", "email_held", "email_released", "email_discarded", "email_rejected", "email_replayed", "email_undeliverable", "email_declined", "auto_response_sent", "attachment_deleted", "attachment_refused", "requester_blocked", "requester_unblocked", "legal_hold_set", "legal_hold_cleared", "storage_full"]), visibility: z.enum(["public", "internal"]), created_at: z.number().int(), actor: z.object({kind: z.enum(["customer", "operator", "system", "rule"]), id: z.string().optional(), label: z.string().nullable().optional(), avatar_handle: z.string().nullable().optional()}), body: z.object({}).nullable(), attachments: z.array(z.object({attachment_id: z.string(), name: z.string(), size: z.number().int(), content_type: z.string(), disposition: z.enum(["inline", "attachment"]), scan: z.enum(["pass", "not_scanned"])})), data: z.object({})}); export const helpDeskReplyReview = z.object({review_id: z.string(), ticket_id: z.string(), ticket_number: z.number().int(), operator_id: z.string(), manager_id: z.string(), status: z.enum(["pending", "rejected", "approved", "discarded"]), created_at: z.number().int(), submitted_at: z.number().int(), updated_at: z.number().int(), status_after: z.enum(["waiting_on_customer", "open"]), version: z.number().int().gte(1), body: z.object({}).nullable(), body_markup: z.string().nullable(), attachments: z.array(z.object({attachment_id: z.string().optional(), name: z.string().optional(), size: z.number().int().optional(), content_type: z.string().optional(), disposition: z.string().optional(), scan: z.string().optional()})), feedback: z.object({comment: z.string().optional(), reviewer_id: z.string().optional(), created_at: z.number().int().optional()}).nullable(), discard: z.object({reason: z.string().optional(), operator_id: z.string().optional(), created_at: z.number().int().optional()}).nullable()}); export const helpDeskTicketWrite = z.object({ticket: helpDeskTicket, events: z.array(helpDeskEvent), review: helpDeskReplyReview.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 patchApiProductsHelpDeskTicketsTicketIdResponse = z.object({data: helpDeskTicketWrite, 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 }; /** * Change a ticket * * PATCH /products/help-desk/tickets/{ticket_id} */ export const patchApiProductsHelpDeskTicketsTicketId = async (args: PatchApiProductsHelpDeskTicketsTicketIdArgs, options?: PatchApiProductsHelpDeskTicketsTicketIdOptions) => { const {baseUrl = "https://api.sparkvault.com/v1", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/products/help-desk/tickets/${encodeURIComponent(String(args.ticket_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiProductsHelpDeskTicketsTicketIdResponse, await res.json()) };