import {z} from 'zod' export type CancellationItem = {item_id: string, item_type: 'item' | 'modifier_option'}; export type UpdateOrderRequest = {state: 'accepted' | 'ready' | 'cancelled', cancellation_reason: 'ITEM_PERMANENTLY_NOT_AVAILABLE' | 'ITEM_TEMPORARILY_UNAVAILABLE' | 'KITCHEN_TOO_BUSY_TO_PREPARE_ORDER' | 'OUT_OF_KITCHEN_OPERATIONAL_HOURS' | 'OUTLET_CLOSED' | 'PARTNER_POS_OUTAGE' | 'PARTNER_ORDER_TIMEOUT' | 'OTHER', unavailable_items?: Array | undefined}; export type UpdateApiOrdersOrderIdArgs = {Authorization: string, 'User-Agent': string, 'Brand-Id'?: string | undefined, 'Branch-Id': string, order_id: number, body: UpdateOrderRequest}; export type UpdateApiOrdersOrderIdOptions = {baseUrl?: string | undefined, CareemIDPClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const branch = z.object({id: z.string(), name: z.string(), brand_id: z.string(), state: z.enum(["UNMAPPED", "MAPPED"]), created_at: z.string(), updated_at: z.string()}); export const orderPrice = z.object({original_total_price: z.number().multipleOf(0.01).optional(), careem_discount_amount: z.number().multipleOf(0.01).optional(), merchant_discount_amount: z.number().multipleOf(0.01).optional(), merchant_promo_amount: z.number().multipleOf(0.01).optional(), careem_promo_amount: z.number().multipleOf(0.01).optional(), tax_percentage: z.number().multipleOf(0.01).optional(), total_taxable_price: z.number().multipleOf(0.01).optional(), delivery_fee: z.number().multipleOf(0.01).optional(), free_delivery_discount_value: z.number().multipleOf(0.01).optional(), service_fee: z.number().multipleOf(0.01).optional(), promo_code: z.string().optional()}); export const address = z.object({name: z.string(), location: z.object({lat: z.string(), lng: z.string()}), number: z.string(), building: z.string(), street: z.string(), area: z.string(), city: z.string(), note: z.string()}); export const customer = z.object({name: z.string(), phone_number: z.string(), address: address.optional(), payment_type: z.enum(["cash", "card", "wallet", "apple_pay"]).optional()}); export const captain = z.object({name: z.string(), phone_number: z.string(), eta: z.string()}); export const itemNestedOption = z.object({id: z.string(), quantity: z.number().int(), discount: z.number().multipleOf(0.01).optional(), careem_discount_amount: z.number().multipleOf(0.01).optional(), merchant_discount_amount: z.number().multipleOf(0.01).optional(), total_price: z.number().multipleOf(0.01)}); export const itemNestedGroup = z.object({id: z.string(), options: z.array(itemNestedOption)}); export const itemOption = z.object({id: z.string(), quantity: z.number().int(), discount: z.number().multipleOf(0.01).optional(), careem_discount_amount: z.number().multipleOf(0.01).optional(), merchant_discount_amount: z.number().multipleOf(0.01).optional(), total_price: z.number().multipleOf(0.01), groups: z.array(itemNestedGroup).optional()}); export const itemGroup = z.object({id: z.string(), options: z.array(itemOption).optional()}); export const orderItem = z.object({id: z.string(), quantity: z.number().int(), groups: z.array(itemGroup).optional(), unit_price: z.number().multipleOf(0.01).optional(), item_price: z.number().multipleOf(0.01), total_price: z.number().multipleOf(0.01), discount: z.number().multipleOf(0.01), careem_discount_amount: z.number().multipleOf(0.01), merchant_discount_amount: z.number().multipleOf(0.01), notes: z.string().optional(), tags: z.array(z.string())}); export const cancelledOrder = z.object({id: z.string(), status: z.enum(["pending", "accepted", "cancelled", "delivered"]), branch: branch, notes: z.string().optional(), price: orderPrice, customer: customer.optional(), captain: captain.optional(), items: z.array(orderItem), cancellation_reason: z.string(), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Update order state * * PUT /orders/{order_id} */ export const updateApiOrdersOrderId = async (args: UpdateApiOrdersOrderIdArgs, options?: UpdateApiOrdersOrderIdOptions) => { const {baseUrl = "https://apigateway.careemdash.com/pos/api/v1", fetch: fetchFn = fetch, CareemIDPClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) if (args['Brand-Id'] != null) headers.set("Brand-Id", String(args['Brand-Id'])) if (args['Branch-Id'] != null) headers.set("Branch-Id", String(args['Branch-Id'])) if (CareemIDPClientCredentials !== undefined) headers.set('authorization', `Bearer ${CareemIDPClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/${encodeURIComponent(String(args.order_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(cancelledOrder, await res.json()) };