import {z} from 'zod' export type GetApiMonitoringInboxIdArgs = {id: string}; export type GetApiMonitoringInboxIdOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sourceProvenance = z.object({sourceId: z.string(), recordId: z.string(), field: z.string(), rawValue: z.string().optional()}); export const monitorRecordPreview = z.object({names: z.array(z.object({value: z.string(), kind: z.enum(["primary", "alias"]), quality: z.enum(["strong", "weak", "unknown"]).optional(), script: z.string().optional(), provenance: sourceProvenance.optional()})), identifiers: z.array(z.object({type: z.string(), value: z.string(), issuer: z.string().optional(), provenance: sourceProvenance.optional()})), birthDates: z.array(z.object({value: z.string(), precision: z.enum(["day", "month", "year", "range", "unknown"]), original: z.string()})), nationalities: z.array(z.string()), designations: z.array(z.object({category: z.enum(["financial_sanctions", "export_control", "debarment", "other"]), programs: z.array(z.string()), legalReferences: z.array(z.string()), wording: z.string().optional(), effectiveDate: z.string().optional()}))}); export const monitorEvent = z.object({id: z.string(), monitorId: z.string(), monitorName: z.string(), caseId: z.string().nullable(), screeningId: z.string().nullable(), previousScreeningId: z.string().nullable(), kind: z.enum(["initial_match", "match_added", "match_removed", "evidence_changed", "policy_changed", "baseline_unavailable", "screening_failed", "screening_overdue", "screening_recovered", "review_required"]), status: z.enum(["unread", "acknowledged"]), createdAt: z.string(), acknowledgedAt: z.string().nullable(), errorCode: z.string().nullable(), totalChanges: z.number().int().gte(0), evidenceExpired: z.boolean(), currentEvidenceAvailable: z.boolean(), previousEvidenceAvailable: z.boolean(), changes: z.array(z.object({sourceId: z.string(), recordId: z.string(), kind: z.enum(["added", "removed", "changed"]), fields: z.array(z.string()), before: monitorRecordPreview.optional(), after: monitorRecordPreview.optional()}))}); export const getApiMonitoringInboxIdResponse = z.object({data: monitorEvent}); 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 }; /** * Read bounded before-and-after previews from retained evidence * * GET /monitoring/inbox/{id} */ export const getApiMonitoringInboxId = async (args: GetApiMonitoringInboxIdArgs, options: GetApiMonitoringInboxIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/monitoring/inbox/${encodeURIComponent(String(args.id))}${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(getApiMonitoringInboxIdResponse, await res.json()) };