import {z} from 'zod' export type ShopifyOrderStatus = {financial?: (string | null) | undefined, fulfillment?: (string | null) | undefined}; export type ShopifyOrderCustomer = {id?: (string | null) | undefined, first_name?: (string | null) | undefined, last_name?: (string | null) | undefined, display_name?: (string | null) | undefined, name?: (string | null) | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined}; export type ShopifyOrderAddress = {name?: (string | null) | undefined, company?: (string | null) | undefined, document_number?: (string | null) | undefined, document_type?: (string | null) | undefined, first_name?: (string | null) | undefined, last_name?: (string | null) | undefined, address1?: (string | null) | undefined, address2?: (string | null) | undefined, city?: (string | null) | undefined, province?: (string | null) | undefined, province_code?: (string | null) | undefined, country?: (string | null) | undefined, country_code?: (string | null) | undefined, zip?: (string | null) | undefined, phone?: (string | null) | undefined}; export type ShopifyOrderLineItem = {id: string, sku?: (string | null) | undefined, name: string, quantity: number, unit_price?: number | undefined, discounted_unit_price?: number | undefined, discount_total?: number | undefined, currency?: (string | null) | undefined}; export type ShopifyOrderShippingLine = {id: string, title?: (string | null) | undefined, original_price?: number | undefined, discounted_price?: number | undefined, discount_total?: number | undefined, currency?: (string | null) | undefined}; export type ShopifyOrderDiscounts = {total?: number | undefined}; export type ShopifyOrderTotals = {subtotal?: number | undefined, shipping?: number | undefined, tax?: number | undefined, total?: number | undefined, taxes_included?: boolean | undefined}; export type ShopifyFactproBilling = {invoice_type?: (string | null) | undefined, customer_document_type?: (string | null) | undefined, customer_document_number?: (string | null) | undefined, customer_full_name?: (string | null) | undefined, company_ruc?: (string | null) | undefined, company_address?: (string | null) | undefined, company_name?: (string | null) | undefined}; export type ShopifyOrderSyncRequest = {provider?: string | undefined, channel?: string | undefined, tax_in_shipping?: (string | null) | undefined, auto_invoice_mode?: (string | null) | undefined, external_order_id: string, external_order_number: string, legacy_resource_id?: (string | null) | undefined, created_at?: (string | null) | undefined, updated_at?: (string | null) | undefined, external_status?: ShopifyOrderStatus | undefined, customer?: ShopifyOrderCustomer | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined, billing_address?: (ShopifyOrderAddress | null) | undefined, shipping_address?: (ShopifyOrderAddress | null) | undefined, line_items?: Array | undefined, shipping_lines?: Array | undefined, discounts?: ShopifyOrderDiscounts | undefined, totals?: ShopifyOrderTotals | undefined, currency?: (string | null) | undefined, factpro_billing?: ShopifyFactproBilling | undefined, raw_payload?: {[key: string]: unknown} | undefined}; export type CreateApiApiV3OrdersExternalShopifyArgs = {body: ShopifyOrderSyncRequest}; export type CreateApiApiV3OrdersExternalShopifyOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const shopifyOrderSyncData = z.object({order_id: z.number().int(), number: z.string(), external_order_id: z.string(), external_order_number: z.string().nullable().optional(), provider: z.string().optional(), channel: z.string().optional(), status: z.string()}); export const shopifyOrderSyncResponse = z.object({success: z.boolean().optional(), message: z.string(), data: shopifyOrderSyncData}); 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 }; /** * Sync Shopify Order * * POST /api/v3/orders/external/shopify */ export const createApiApiV3OrdersExternalShopify = async (args: CreateApiApiV3OrdersExternalShopifyArgs, options: CreateApiApiV3OrdersExternalShopifyOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/orders/external/shopify${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(shopifyOrderSyncResponse, await res.json()) };