import {z} from 'zod' export type TicketStatus = 1 | 2 | 3 | 4; export type DealTicketLogMetadata = {event?: (string | null) | undefined, previousValue?: (string | null) | undefined, value?: (string | null) | undefined, extra?: (string | null) | undefined}; export type TicketLog = {id?: number | undefined, ticketId: number, status: TicketStatus, pipelineId?: number | undefined, stageId?: number | undefined, notes?: (string | null) | undefined, isPrivate?: number | undefined, callLogId?: (number | null) | undefined, chatId?: (number | null) | undefined, emailId?: (number | null) | undefined, smsLogId?: (number | null) | undefined, contactId?: (number | null) | undefined, automationId?: (number | null) | undefined, pinDate?: (string | null) | undefined, metadata?: DealTicketLogMetadata | undefined, createdDate?: string | undefined}; export type CreateApiV1TicketsLogsArgs = {body: TicketLog}; export type CreateApiV1TicketsLogsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const userReactionResponseModel = z.object({reactionId: z.number().int().optional(), reaction: z.string().nullable().optional(), userId: z.number().int().optional()}); 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 ticketLogResponseModel = z.object({id: z.number().int().optional(), ticketId: z.number().int().optional(), pipelineId: z.number().int().optional(), stageId: z.number().int().optional(), createdUserId: z.number().int().optional(), createdContactId: z.number().int().optional(), createdAutomationId: z.number().int().optional(), createdDate: z.string().optional(), notes: z.string().nullable().optional(), reactions: z.array(userReactionResponseModel).nullable().optional(), isPrivate: z.number().int().optional(), status: ticketStatus.optional(), callLogId: z.number().int().optional(), chatRoomId: z.number().int().optional(), chatId: z.number().int().optional(), emailId: z.number().int().optional(), emailFileNames: z.string().nullable().optional(), emailFileLinks: z.string().nullable().optional(), emailFileSizes: z.string().nullable().optional(), smsLogId: z.number().int().optional(), pinDate: z.string().nullable().optional(), metadata: dealTicketLogMetadata.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 }; /** * Adds a log to the ticket * * POST /v1/tickets/logs * * Also accepts auth: API Key */ export const createApiV1TicketsLogs = async (args: CreateApiV1TicketsLogsArgs, options: CreateApiV1TicketsLogsOptions) => { 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${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(ticketLogResponseModel, await res.json()) };