import {z} from 'zod' export type Id = number; export type RepairOrderNotificationPolicyCreateParameters = {payment_profile_id: Id, all_vehicles?: boolean | undefined, name?: (string | null) | undefined, notification_frequency_in_minutes?: (10 | 20 | 30 | 60 | 240 | 720 | 1440 | null) | undefined, group_ids?: Array | undefined, subscriptions?: (Array<{contact_id: Id, email?: boolean | undefined, text_message?: boolean | undefined, text_message_phone_type?: (string | null) | undefined}>) | undefined}; export type CreateApiRepairOrderNotificationPoliciesArgs = {body: RepairOrderNotificationPolicyCreateParameters}; export type CreateApiRepairOrderNotificationPoliciesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const repairOrderNotificationPolicy = z.object({id: z.number().int().optional(), name: z.string().nullable().optional(), payment_profile_id: z.number().int().optional(), all_vehicles: z.boolean().optional(), notification_frequency_in_minutes: z.number().int().nullable().optional(), groups: z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), ancestry: z.string().nullable().optional(), ancestry_names: z.string().nullable().optional()})).optional(), subscriptions: z.array(z.object({id: z.number().int().optional(), contact_id: z.number().int().nullable().optional(), user_id: z.number().int().optional(), email: z.boolean().optional(), text_message: z.boolean().optional(), text_message_phone_type: z.string().nullable().optional(), text_message_opted_out: z.boolean().optional(), user: z.object({id: z.number().int().optional(), email: z.string().nullable().optional(), full_name: z.string().nullable().optional()}).optional()})).optional()}).nullable(); 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 Repair Order Notification Policy * * POST /repair_order_notification_policies */ export const createApiRepairOrderNotificationPolicies = async (args: CreateApiRepairOrderNotificationPoliciesArgs, options?: CreateApiRepairOrderNotificationPoliciesOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/repair_order_notification_policies${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(repairOrderNotificationPolicy, await res.json()) };