import {z} from 'zod' export type Channel = 'FACEBOOK' | 'WEB' | 'WHATSAPP' | 'INSTAGRAM'; export type Participant = {id?: string | undefined, name?: string | undefined, type?: ('robot' | 'visitor' | 'user' | 'externalOperator' | 'external_operator') | undefined, addedAt?: string | undefined, firstMessageAt?: string | undefined, lastMessageAt?: string | undefined}; export type ConversationTag = {tagName?: string | undefined, source?: ('human' | 'chatbot' | 'ai') | undefined, additionDate?: string | undefined, creatorUserId?: string | undefined}; export type Conversation = {id?: string | undefined, channelId?: string | undefined, companyId?: string | undefined, contactId?: string | undefined, channel?: Channel | undefined, status?: ('ACTIVE' | 'CLOSED' | 'AWAY' | 'INACTIVE' | 'FINALIZED') | undefined, lastMessage?: string | undefined, lastMessageAt?: string | undefined, visitorName?: string | undefined, visitorEmail?: string | undefined, visitorPhone?: string | undefined, participants?: Array | undefined, assignedTo?: string | undefined, preAssigned?: string | undefined, persistentLastOperator?: string | undefined, firstInterventionAt?: (string | null) | undefined, utmSource?: string | undefined, utmMedium?: string | undefined, utmCampaign?: string | undefined, gaClientId?: string | undefined, phaseId?: string | undefined, amount?: number | undefined, type?: string | undefined, operatorTags?: Array | undefined, tags?: Array | undefined, closed?: boolean | undefined, createdAt?: string | undefined}; export type UpdateApiV1ConversationsConversationIdArgs = {conversationId: string, body: Conversation}; export type UpdateApiV1ConversationsConversationIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const channel = z.enum(["FACEBOOK", "WEB", "WHATSAPP", "INSTAGRAM"]); export const participant = z.object({id: z.string().optional(), name: z.string().optional(), type: z.enum(["robot", "visitor", "user", "externalOperator", "external_operator"]).optional(), addedAt: z.string().optional(), firstMessageAt: z.string().optional(), lastMessageAt: z.string().optional()}); export const conversationTag = z.object({tagName: z.string().optional(), source: z.enum(["human", "chatbot", "ai"]).optional(), additionDate: z.string().optional(), creatorUserId: z.string().optional()}); export const conversation = z.object({id: z.string().optional(), channelId: z.string().optional(), companyId: z.string().optional(), contactId: z.string().optional(), channel: channel.optional(), status: z.enum(["ACTIVE", "CLOSED", "AWAY", "INACTIVE", "FINALIZED"]).optional(), lastMessage: z.string().optional(), lastMessageAt: z.string().optional(), visitorName: z.string().optional(), visitorEmail: z.string().optional(), visitorPhone: z.string().optional(), participants: z.array(participant).optional(), assignedTo: z.string().optional(), preAssigned: z.string().optional(), persistentLastOperator: z.string().optional(), firstInterventionAt: z.string().nullable().optional(), utmSource: z.string().optional(), utmMedium: z.string().optional(), utmCampaign: z.string().optional(), gaClientId: z.string().optional(), phaseId: z.string().optional(), amount: z.number().optional(), type: z.string().optional(), operatorTags: z.array(conversationTag).optional(), tags: z.array(z.string()).optional(), closed: z.boolean().optional(), createdAt: 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 }; /** * Actualizar conversación * * PUT /v1/conversations/{conversationId} */ export const updateApiV1ConversationsConversationId = async (args: UpdateApiV1ConversationsConversationIdArgs, options?: UpdateApiV1ConversationsConversationIdOptions) => { const {baseUrl = "https://connect.cliengo.com", 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(/\/+$/, '')}/v1/conversations/${encodeURIComponent(String(args.conversationId))}${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(conversation, await res.json()) };