import {z} from 'zod' export type PaymentVoucher = {code?: string | undefined, name?: string | undefined, discount_value?: number | undefined, discount_percent?: number | undefined, bill_item_id?: string | undefined}; export type DiscountItem = {discount_code?: string | undefined, discount_value?: number | undefined, discount_percent?: number | undefined, description?: string | undefined, bill_item_id: string}; export type PaymentRecap = {credit_points?: number | undefined, amount_for_credit?: number | undefined, vouchers?: Array | undefined, recommended_discounts?: Array | undefined}; export type UTM = {utm_source?: string | undefined, utm_medium?: string | undefined, utm_campaign?: string | undefined}; export type PluId = {list_code: string, code: string}; export type PropertyRecord = {property_record_id?: string | undefined, property_id: string, property_name?: string | undefined, property_value?: (string | number | number | boolean | Array | Record) | undefined, last_change?: string | undefined}; export type BillItem = {plu_ids: Array, plu_name: string, category_plu_id?: string | undefined, vat_rate: number, quantity: number, paid_amount: number, price: number, bill_item_id: string, loyalty_off: boolean, purchase_item_type_id: string, customer_id?: string | undefined, purchase_item_property_records?: Array | undefined}; export type FinalBill = {fiscal?: boolean | undefined, purchase_type_id: string, canceled: boolean, payment_type: 'S' | 'A' | 'C' | 'D', payment_recap?: PaymentRecap | undefined, bill_id: string, bill_number?: string | undefined, payment_time: string, created_by?: string | undefined, currency_id: string, total_price: number, utm?: UTM | undefined, bill_items: Array, property_records?: Array | undefined}; export type CreateApiPurchasesActionsSendPurchaseArgs = {'Accept-Language'?: string | undefined, body: {store_id: string, cashdesk_number: number, card_number?: string | undefined, customer_id?: string | undefined, bill: FinalBill}}; export type CreateApiPurchasesActionsSendPurchaseOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPurchasesActionsSendPurchaseResponse = z.object({data: z.object({purchase_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 }; /** * Send purchase * * POST /purchases/actions/send-purchase * * Also accepts auth: basicAuth */ export const createApiPurchasesActionsSendPurchase = async (args: CreateApiPurchasesActionsSendPurchaseArgs, options?: CreateApiPurchasesActionsSendPurchaseOptions) => { 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(/\/+$/, '')}/purchases/actions/send-purchase${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(createApiPurchasesActionsSendPurchaseResponse, await res.json()) };