import {z} from 'zod' export type GetApiMonitoringInboxArgs = {limit?: number | undefined, cursor?: string | undefined, status?: ('all' | 'unread' | 'acknowledged') | undefined, kind?: ('initial_match' | 'match_added' | 'match_removed' | 'evidence_changed' | 'policy_changed' | 'baseline_unavailable' | 'screening_failed' | 'screening_overdue' | 'screening_recovered' | 'review_required') | undefined, monitorId?: string | undefined}; export type GetApiMonitoringInboxOptions = {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 monitorEventPage = z.object({items: z.array(monitorEvent), nextCursor: z.string().nullable(), unreadCount: z.number().int().gte(0)}); export const getApiMonitoringInboxResponse = z.object({data: monitorEventPage}); 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 meaningful changes, failures and reviews that must be renewed * * GET /monitoring/inbox */ export const getApiMonitoringInbox = async (args: GetApiMonitoringInboxArgs, options: GetApiMonitoringInboxOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.status != null) query.append("status", String(args.status)) if (args.kind != null) query.append("kind", String(args.kind)) if (args.monitorId != null) query.append("monitorId", String(args.monitorId)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/monitoring/inbox${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(getApiMonitoringInboxResponse, await res.json()) };