import {z} from 'zod' export type PostDealsDealIdTasks = {text: string, assigned_users?: Array | undefined, due_date?: string | undefined, finished_date?: string | undefined, finished_by?: number | undefined, type?: ('VISITA' | 'REUNIAO' | 'LIGACAO' | 'EMAIL' | 'PROPOSTA' | 'WHATSAPP') | undefined, metrics?: Array | undefined}; export type CreateApiDealsDealIdTasksArgs = {deal_id: number, body: PostDealsDealIdTasks}; export type CreateApiDealsDealIdTasksOptions = {baseUrl: string, token?: string | undefined, fetch?: typeof fetch | undefined}; export const leanUserEntity = z.object({accountId: z.number().int().optional(), id: z.number().int().optional(), name: z.string().optional()}); export const activityMetricEntity = z.object({id: z.number().int().optional(), name: z.string().optional(), value: z.number().optional()}); export const taskEntity = z.object({id: z.number().int().optional(), accountId: z.number().int().optional(), user: z.object({accountId: z.number().int().optional(), id: z.number().int().optional(), name: z.string().optional()}).optional(), text: z.string().optional(), organization: z.object({id: z.string().optional(), name: z.string().optional(), email: z.string().optional()}).optional(), person: z.object({id: z.number().int().optional(), name: z.string().optional(), email: z.string().optional()}).optional(), deal: z.object({id: z.string().optional(), title: z.string().optional(), currency: z.object({id: z.number().int().optional(), isoCode: z.string().optional(), name: z.string().optional(), symbol: z.string().optional()}).optional(), exchangeRate: z.number().optional(), exchangeRateSource: z.enum(["deal", "closed", "account"]).optional(), valueInBrl: z.number().optional()}).optional(), dueDate: z.unknown(), finishedAt: z.unknown(), finishedBy: z.object({accountId: z.number().int().optional(), id: z.number().int().optional(), name: z.string().optional()}).optional(), createdAt: z.unknown(), updatedAt: z.unknown(), assignedUsers: z.array(leanUserEntity).optional(), metrics: z.array(activityMetricEntity).optional(), type: z.string().optional(), _webUrl: z.string().optional(), audioTranscription: z.object({id: z.number().int().optional(), content: z.string().optional(), liked: z.boolean().optional(), feedback: z.string().optional()}).optional(), audioSummary: z.object({id: z.number().int().optional(), content: z.string().optional(), liked: z.boolean().optional(), feedback: z.string().optional(), audioTranscriptionId: z.number().int().optional()}).optional()}); export const createApiDealsDealIdTasksResponse = z.object({data: taskEntity.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 }; /** * Create a new task related to this deal. * * POST /deals/{deal_id}/tasks */ export const createApiDealsDealIdTasks = async (args: CreateApiDealsDealIdTasksArgs, options: CreateApiDealsDealIdTasksOptions) => { const {baseUrl, fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (token !== undefined) headers.set("Authorization", token) const url = `${baseUrl.replace(/\/+$/, '')}/deals/${encodeURIComponent(String(args.deal_id))}/tasks${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(createApiDealsDealIdTasksResponse, await res.json()) };