import {z} from 'zod' export type ExternalCode = {external_code_type_id: string, value: string}; export type ProductReservationItem = {reservable_product_id: string, amount: number, unit_price?: number | undefined, vat_rate?: number | undefined}; export type UTM = {utm_source?: string | undefined, utm_medium?: string | undefined, utm_campaign?: string | undefined}; export type ProductReservationProgress = {reservation_progress_id?: number | undefined, reservation_progress_text?: string | undefined, reservation_progress_time?: string | undefined}; export type ProductReservationComment = {comment_text?: string | undefined, comment_time?: string | undefined}; export type ProductReservation = {product_reservation_id?: string | undefined, customer_id: string, store_id: string, reservation_codes?: Array | undefined, external_reservation_codes?: Array | undefined, benefit_codes?: Array | undefined, product_reservation_source_id: string, pickup_date?: string | undefined, customers_note?: string | undefined, total_price?: number | undefined, currency_id?: string | undefined, product_reservation_items?: Array | undefined, external_purchase_id?: string | undefined, utm?: UTM | undefined, actual_state?: (0 | 1 | 2 | 4 | 5 | 6 | 7 | 8) | undefined, reservation_progress_history?: Array | undefined, comments?: Array | undefined, last_change?: string | undefined, state?: (0 | 1 | 2) | undefined}; export type CreateApiProductReservationsArgs = {'Accept-Language'?: string | undefined, body: {product_reservation: ProductReservation}}; export type CreateApiProductReservationsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiProductReservationsResponse = z.object({data: z.object({product_reservation_id: z.string().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 }; /** * Create a reservation * * POST /product-reservations * * Also accepts auth: basicAuth */ export const createApiProductReservations = async (args: CreateApiProductReservationsArgs, options?: CreateApiProductReservationsOptions) => { const {baseUrl = "https://sandbox.crmcarecloud.com/webservice/rest-api/enterprise-interface/v1.0", 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 (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/product-reservations${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(createApiProductReservationsResponse, await res.json()) };