import {z} from 'zod' export type MandateSealedPaymentPlanLegDto = {legIndex: number, sourceAmountMinor: string, sourceCurrency: string, sourceAccountId: string, sourceRail: string, destinationAmountMinor: string, destinationCurrency: string, payeeAccountId: string, destinationRail: string}; export type MandateSealedPaymentPlanDto = {v: number, legs: Array}; export type MandateDraftProposalDto = {customerId: string, amountMinor: string, currency: string, destinationAccountId: string, destinationRail: string, sourceAccountId?: (string | null) | undefined, sourceRail?: (string | null) | undefined, drafterPrincipalId?: (string | null) | undefined}; export type EvaluateMandateDto = {sealedPlan?: MandateSealedPaymentPlanDto | undefined, proposal?: MandateDraftProposalDto | undefined, asOf?: string | undefined, priorSpendDayMinor?: string | undefined, priorSpendMonthMinor?: string | undefined}; export type CreateApiV1MandatesMandateIdEvaluateArgs = {'X-API-Key'?: string | undefined, mandateId: string, body: EvaluateMandateDto}; export type CreateApiV1MandatesMandateIdEvaluateOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const mandateDenialDto = z.object({code: z.enum(["mandate_not_found", "mandate_not_active", "mandate_outside_validity_window", "mandate_limit_exceeded", "mandate_payee_not_allowed", "mandate_rail_not_allowed", "mandate_drafter_not_allowed", "mandate_currency_mismatch", "mandate_customer_mismatch", "mandate_empty_subject", "mandate_prior_spend_required"]), window: z.enum(["PER_TRANSACTION", "PER_DAY", "PER_MONTH"]).optional(), limit: z.string().optional(), used: z.string().optional(), message: z.string().optional()}); export const mandateEvaluationResponseDto = z.object({allowed: z.boolean(), mandateId: z.string(), denial: mandateDenialDto.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 }; /** * Evaluate a mandate against a sealed plan or draft proposal * * POST /v1/mandates/{mandateId}/evaluate */ export const createApiV1MandatesMandateIdEvaluate = async (args: CreateApiV1MandatesMandateIdEvaluateArgs, options?: CreateApiV1MandatesMandateIdEvaluateOptions) => { const {baseUrl = "https://sandbox.api.infinite.dev", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-Key'] != null) headers.set("X-API-Key", String(args['X-API-Key'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/mandates/${encodeURIComponent(String(args.mandateId))}/evaluate${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(mandateEvaluationResponseDto, await res.json()) };