import {z} from 'zod' export type CreateApiReconciliationRecordsReconcileArgs = {body: {id: Array, tagSlugs?: Array | undefined, reasons?: (Array<'FEE_ADJUSTMENT' | 'FX_ADJUSTMENT' | 'RECONCILES_WITH_OTHER_TRANSACTIONS' | 'WITHIN_TOLERANCE' | 'SEPARATE_REFUND_OR_VOUCHER_ISSUED' | 'DUPLICATE_RECORD' | 'DATA_ERROR' | 'OTHER'>) | undefined, comments?: (string | null) | undefined}}; export type CreateApiReconciliationRecordsReconcileOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiReconciliationRecordsReconcileResponse = z.object({success: z.number().int(), total: z.number().int(), failed: z.number().int(), result: z.array(z.object({id: z.string(), currentResolution: z.enum(["RECONCILED", "UNRECONCILED"]).optional(), currentStatus: z.enum(["OPEN", "FOREIGN", "DISCREPANCY", "MATCHED", "MANUALLY_RECONCILED", "MANUALLY_MATCHED", "MISSING_FROM_PSP", "MISSING_FROM_ACQUIRER", "MISSING_FROM_MERCHANT"]).optional(), error: z.string().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 }; /** * Bulk manually reconcile records * * POST /reconciliation/records/reconcile */ export const createApiReconciliationRecordsReconcile = async (args: CreateApiReconciliationRecordsReconcileArgs, options?: CreateApiReconciliationRecordsReconcileOptions) => { const {baseUrl = "https://api.staging.payrails.io", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/reconciliation/records/reconcile${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(createApiReconciliationRecordsReconcileResponse, await res.json()) };