import {z} from 'zod' export type GetApiV1MatchingCandidatesArgs = {contextId: string, transactionId: string, limit?: number | undefined}; export type GetApiV1MatchingCandidatesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const candidateTransactionView = z.object({amount: z.string(), baseAmount: z.string().optional(), currency: z.string(), date: z.string(), externalId: z.string().optional(), id: z.string(), sourceId: z.string()}); export const candidateMatchedKey = z.object({agreed: z.boolean(), field: z.string()}); export const candidateWhy = z.object({amountMatch: z.boolean(), currencyMatch: z.boolean(), dateMatch: z.boolean(), matchedKeys: z.array(candidateMatchedKey).nullable().optional(), referenceScore: z.number().gte(0).lte(1)}); export const candidateProposalResponse = z.object({amountDelta: z.string(), dateDeltaDays: z.number().int(), ruleId: z.string(), ruleType: z.string(), score: z.number().int().gte(0).lte(100), transaction: candidateTransactionView, why: candidateWhy}); export const candidateProposalsResponse = z.object({items: z.array(candidateProposalResponse).nullable(), targetTransactionId: z.string()}); 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 }; /** * List match candidates for a transaction * * GET /v1/matching/candidates */ export const getApiV1MatchingCandidates = async (args: GetApiV1MatchingCandidatesArgs, options?: GetApiV1MatchingCandidatesOptions) => { const {baseUrl = "https://identity.sandbox.lerian.net", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.contextId != null) query.append("contextId", String(args.contextId)) if (args.transactionId != null) query.append("transactionId", String(args.transactionId)) if (args.limit != null) query.append("limit", String(args.limit)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/matching/candidates${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(candidateProposalsResponse, await res.json()) };