import {z} from 'zod' export type PatchApiAppYelpAutorespondersBusinessIdArgs = {businessId: string}; export type PatchApiAppYelpAutorespondersBusinessIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const appYelpAutoresponderSchema = z.object({id: z.number().int(), businessId: z.string(), businessName: z.string().nullable(), status: z.string(), connectionStatus: z.string(), sendEnabled: z.boolean(), tone: z.string(), firstMessageBody: z.string().nullable(), sequenceMessages: z.array(z.string()), sequenceSize: z.number().int(), stopwords: z.array(z.string()), notifyNewLead: z.boolean(), notifyDraftReady: z.boolean(), notificationMode: z.enum(["off", "every_interaction", "new_conversation", "phone_detected"]), notificationEmail: z.string().nullable(), offHoursEnabled: z.boolean(), timezone: z.string().nullable(), businessHours: z.object({}), calendarIntegrationEnabled: z.boolean(), googleCalendarConnected: z.boolean(), googleCalendarId: z.string().nullable(), googleCalendarName: z.string().nullable(), googleCalendarTimezone: z.string().nullable(), calendarAppointmentMinutes: z.number().int(), calendarMinNoticeMinutes: z.number().int(), calendarBufferMinutes: z.number().int(), googleCalendarTokenExpiresAt: z.string().nullable(), googleCalendarConnectedAt: z.string().nullable(), leadExportWebhookEnabled: z.boolean(), leadExportWebhookUrl: z.string().nullable(), leadExportWebhookSecret: z.string().nullable(), conversationsCount: z.number().int(), message: z.string().nullable(), activatedAt: z.string().nullable(), deactivatedAt: z.string().nullable(), lastCheckedAt: z.string().nullable(), telegramConnected: z.boolean().optional(), telegramChatId: z.string().nullable().optional(), telegramConnectedAt: z.string().nullable().optional(), telegramDeepLink: z.string().nullable().optional(), telegramChats: z.array(z.object({id: z.number().int().nullable().optional(), chatId: z.string().optional(), chatType: z.string().nullable().optional(), title: z.string().nullable().optional(), username: z.string().nullable().optional(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), connectedAt: z.string().nullable().optional(), lastSeenAt: z.string().nullable().optional()})).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 a local Yelp autoresponder configuration * * PATCH /app/yelp/autoresponders/{businessId} */ export const patchApiAppYelpAutorespondersBusinessId = async (args: PatchApiAppYelpAutorespondersBusinessIdArgs, options: PatchApiAppYelpAutorespondersBusinessIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/app/yelp/autoresponders/${encodeURIComponent(String(args.businessId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(appYelpAutoresponderSchema, await res.json()) };