import {z} from 'zod' export type BusinessId = string; export type GetApiMessagingSettingsArgs = {page?: number | undefined, per_page?: number | undefined, business__in?: Array | undefined}; export type GetApiMessagingSettingsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const messagingSettingWelcomeMessage = z.string().nullable(); export const messagingSettingOfflineMessage = z.string().nullable(); export const messagingSettingUsesBusinessHours = z.boolean().nullable(); export const timeWindow = z.string(); export const day = z.array(timeWindow); export const chatHours = z.object({monday: day, tuesday: day, wednesday: day, thursday: day, friday: day, saturday: day, sunday: day}).nullable(); export const messagingSettingIsLaunched = z.boolean().nullable(); export const messagingSetting = z.object({id: z.number().int().optional(), business_id: z.string().optional(), business_name: z.string().nullable().optional(), business_address: z.string().nullable().optional(), welcome_message: messagingSettingWelcomeMessage.optional(), offline_message: messagingSettingOfflineMessage.optional(), uses_business_hours: messagingSettingUsesBusinessHours.optional(), custom_chat_hours: chatHours.optional(), is_launched: messagingSettingIsLaunched.optional()}); export const page = z.number().int(); export const count = z.number().int(); export const maxPage = z.number().int(); export const getApiMessagingSettingsResponse = z.object({settings: z.array(messagingSetting).optional(), page: page.optional(), count: count.optional(), max_page: maxPage.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 }; /** * List messaging settings * * GET /messaging/settings */ export const getApiMessagingSettings = async (args: GetApiMessagingSettingsArgs, options?: GetApiMessagingSettingsOptions) => { const {baseUrl = "https://api.partoo.co/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) for (const item of args.business__in ?? []) query.append("business__in", String(item)) if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/messaging/settings${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiMessagingSettingsResponse, await res.json()) };