import {z} from 'zod' export type GetApiOrdersOrderIdArgs = {Authorization: string, 'User-Agent': string, 'Brand-Id'?: string | undefined, 'Branch-Id': string, order_id: number}; export type GetApiOrdersOrderIdOptions = {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 order = z.object({id: z.number().int(), status: z.enum(["pending", "accepted", "cancelled", "delivered"]), merchant_pay_type: z.string().optional(), delivery_type: z.enum(["careem", "merchant"]).optional(), branch: branch, notes: z.string().optional(), price: orderPrice, customer: customer.optional(), captain: captain.optional(), cash_in: z.number().multipleOf(0.01).optional(), items: z.array(orderItem), cancellation_reason: z.string().optional(), created_at: z.string(), is_scheduled: z.boolean().optional(), prepare_time: z.string().optional(), pickup_time: z.string().optional(), updated_at: z.string(), metadata: z.object({order_instructions: z.object({merchant_notes: z.string().optional(), merchant_instructions: z.array(z.object({label: z.string().optional(), icon_url: z.string().optional(), name_localized: z.object({en: z.string().optional(), ar: z.string().optional()}).optional()})).optional()}).optional(), tags: z.array(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 }; /** * Fetch order's details by ID * * GET /orders/{order_id} */ export const getApiOrdersOrderId = async (args: GetApiOrdersOrderIdArgs, options?: GetApiOrdersOrderIdOptions) => { const {baseUrl = "https://apigateway.careemdash.com/pos/api/v1", fetch: fetchFn = fetch, CareemIDPClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(order, await res.json()) };