import {z} from 'zod' export type OrderItem = {order_item_id?: string | undefined, product_variant_id: string, customer_id?: string | undefined, amount: number, unit_price: number, vat_rate: number}; export type Address = {address1?: string | undefined, address2?: string | undefined, address3?: string | undefined, address4?: string | undefined, address5?: string | undefined, address6?: string | undefined, address7?: string | undefined, zip?: string | undefined, city?: string | undefined, country_code?: string | undefined}; export type CustomAgreements = {agreement_id: string, agreement_value: 0 | 1 | 2}; export type Agreement = {agreement_gtc?: (0 | 1 | 2) | undefined, agreement_profiling?: (0 | 1 | 2) | undefined, agreement_marketing_communication?: (0 | 1 | 2) | undefined, custom_agreements?: Array | undefined}; export type Consents = {consent_id: string, consent_value: 0 | 1 | 2, last_change?: string | undefined}; export type PersonalInformation = {salutation?: string | undefined, gender?: (1 | 2 | 3 | 4 | 5 | null) | undefined, first_name?: string | undefined, last_name?: string | undefined, pre_nominals?: string | undefined, post_nominals?: string | undefined, birthdate?: string | undefined, email?: string | undefined, phone?: string | undefined, language_id: string, store_id?: string | undefined, photo_url?: string | undefined, address?: Address | undefined, agreement?: Agreement | undefined, consents?: Array | undefined}; export type OrderInvoicing = {payment_id: string, paid_date?: string | undefined, personal_information?: PersonalInformation | undefined}; export type OrderShipping = {shipping_id: string, contact_firstname?: string | undefined, contact_lastname?: string | undefined, phone?: string | undefined, address?: Address | undefined}; export type Order = {order_id?: string | undefined, order_number?: string | undefined, customer_id: string, currency_id: string, total_price: number, discount_codes?: Array | undefined, order_items: Array, invoicing_data: OrderInvoicing, shipping_data?: OrderShipping | undefined, customer_note?: string | undefined, order_status?: string | undefined, shipping_date?: string | undefined, program_ranking?: number | undefined, payment_status?: string | undefined, invoice_document_url?: string | undefined, last_change?: string | undefined}; export type CreateApiOrdersArgs = {'Accept-Language'?: string | undefined, body: {order: Order}}; export type CreateApiOrdersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiOrdersResponse = z.object({data: z.object({order_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 an order * * POST /orders * * Also accepts auth: basicAuth */ export const createApiOrders = async (args: CreateApiOrdersArgs, options?: CreateApiOrdersOptions) => { 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(/\/+$/, '')}/orders${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(createApiOrdersResponse, await res.json()) };