import {z} from 'zod' export type OrderStatusEnum = 'DRAFT' | 'SUBMITTED' | 'INPROGRESS' | 'COMPLETED' | 'CANCELLED' | 'ONHOLD'; export type OrderType = 'POIOrder' | 'MerchantOrder' | 'POIMaintenanceOrder' | 'MerchantMaintenanceOrder' | 'POIAccessoryOrder' | 'AdditionalServicesOrder' | 'HardwareOrder'; export type FilterDateType = 'createdDate' | 'modifiedDate'; export type MerchantName = string; export type MerchantCompanyEntityUid = string; export type EntityUid = string; export type GetApiOrdersCreatorsArgs = {orderReference?: string | undefined, status?: OrderStatusEnum | undefined, orderType?: OrderType | undefined, filterDateType?: FilterDateType | undefined, userUids?: Array | undefined, startTime?: string | undefined, endTime?: string | undefined, merchantCompany?: MerchantName | undefined, merchantCompanyEntityUid?: MerchantCompanyEntityUid | undefined, merchantCompanyEntityUids?: Array | undefined, customerEntityUids?: Array | undefined, customerEntityUid?: EntityUid | undefined}; export type GetApiOrdersCreatorsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createdUserId = z.string(); export const userName = z.string().min(1); export const creator = z.object({userUid: createdUserId, name: userName}); export const creatorsResponse = z.object({creators: z.array(creator)}); 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 }; /** * Get the list of the creators for the orders * * GET /orders/creators * * Also accepts auth: BearerAuth */ export const getApiOrdersCreators = async (args: GetApiOrdersCreatorsArgs, options?: GetApiOrdersCreatorsOptions) => { const {baseUrl = "https://emea.gsc.verifone.cloud/oidc/checkout-service", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.orderReference != null) query.append("orderReference", String(args.orderReference)) if (args.status != null) query.append("status", String(args.status)) if (args.orderType != null) query.append("orderType", String(args.orderType)) if (args.filterDateType != null) query.append("filterDateType", String(args.filterDateType)) if (args.userUids != null) query.append("userUids", args.userUids.join(',')) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.merchantCompany != null) query.append("merchantCompany", String(args.merchantCompany)) if (args.merchantCompanyEntityUid != null) query.append("merchantCompanyEntityUid", String(args.merchantCompanyEntityUid)) if (args.merchantCompanyEntityUids != null) query.append("merchantCompanyEntityUids", args.merchantCompanyEntityUids.join(',')) for (const item of args.customerEntityUids ?? []) query.append("customerEntityUids", String(item)) if (args.customerEntityUid != null) query.append("customerEntityUid", String(args.customerEntityUid)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/creators${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(creatorsResponse, await res.json()) };