import {z} from 'zod' export type UpdateApiV1TicketsLogsIdArgs = {id: number, body: unknown}; export type UpdateApiV1TicketsLogsIdOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const ticketStatus = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const dealTicketLogMetadata = z.object({event: z.string().nullable().optional(), previousValue: z.string().max(200).nullable().optional(), value: z.string().max(200).nullable().optional(), extra: z.string().max(100).nullable().optional()}); export const ticketLog = z.object({id: z.number().int().optional(), ticketId: z.number().int(), status: ticketStatus, pipelineId: z.number().int().optional(), stageId: z.number().int().optional(), notes: z.string().nullable().optional(), isPrivate: z.number().int().optional(), callLogId: z.number().int().nullable().optional(), chatId: z.number().int().nullable().optional(), emailId: z.number().int().nullable().optional(), smsLogId: z.number().int().nullable().optional(), contactId: z.number().int().nullable().optional(), automationId: z.number().int().nullable().optional(), pinDate: z.string().nullable().optional(), metadata: dealTicketLogMetadata.optional(), createdDate: z.string().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 }; /** * Update ticket log * * PUT /v1/tickets/logs/{id} * * Also accepts auth: API Key */ export const updateApiV1TicketsLogsId = async (args: UpdateApiV1TicketsLogsIdArgs, options: UpdateApiV1TicketsLogsIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tickets/logs/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(ticketLog, await res.json()) };