import {z} from 'zod' export type CreateInvestigationBody = {investigation_state?: string | undefined, investigation_type: string, entity_tokens: Array, journey_alerts: Array<{agent_token: string, journey_alert_token: string, outcome?: string | undefined, reason?: string | undefined, note?: string | undefined}>, investigation_assignee_token?: string | undefined, should_aggregate_alerts?: boolean | undefined, should_auto_assign?: boolean | undefined, investigation_status?: (string | null) | undefined, investigation_name?: string | undefined, note?: string | undefined, tags?: Array<{tag_name: string, is_outcome_reason: boolean}> | undefined} | {investigation_state?: string | undefined, investigation_type: string, entity_tokens: Array, journey_alerts?: (Array<{agent_token: string, journey_alert_token: string, outcome?: string | undefined, reason?: string | undefined, note?: string | undefined}>) | undefined, investigation_assignee_token?: string | undefined, should_aggregate_alerts?: boolean | undefined, should_auto_assign?: boolean | undefined, investigation_status?: (string | null) | undefined, investigation_name?: string | undefined, note: string, tags?: Array<{tag_name: string, is_outcome_reason: boolean}> | undefined}; export type CreateApiInvestigationsArgs = {body: CreateInvestigationBody}; export type CreateApiInvestigationsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const documentNotes = z.object({note: z.string().optional(), note_author_agent_email: z.string().optional(), created_at: z.number().optional(), updated_at: z.number().optional()}); export const document = z.object({document_token: z.string().optional(), type: z.enum(["contract", "license", "passport", "utility", "other", "internal", "identification_card", "residence_permit", "canada_provincial_id", "indigenous_card", "paystub", "bank_statement", "w2", "1099", "1120", "1065", "t1", "t4", "w9", "deposited_check", "voided_check", "social_security_card", "h1b", "passport_card", "credit_report", "iso_app", "brokerage_statement", "lease", "document", "ucc1", "incorporation_certificate", "good_standing_certificate"]).optional(), name: z.string().optional(), extension: z.string().optional(), uploaded: z.boolean().optional(), timestamp: z.number().optional(), approved: z.string().optional(), approval_agent_email: z.string().optional(), approval_timestamp: z.number().optional(), notes: z.array(documentNotes).optional()}); export const getInvestigationDetails = z.object({investigation_token: z.string().optional(), closed_at: z.string().optional(), created_at: z.string().optional(), name: z.string().nullable().optional(), type: z.string().optional(), state: z.string().optional(), source: z.string().optional(), outcome: z.string().optional(), updated_at: z.string().optional(), archived_at: z.string().optional(), _embedded: z.object({assigned_agent: z.object({first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), token: z.string().optional()}).optional(), entities: z.object({"P-KaKZxdSGnbxXfKXnXdyp": z.object({type: z.string().optional(), entity_token: z.string().optional(), entity_name: z.string().optional(), meta: z.string().nullable().optional(), external_ingestion_identifier: z.string().nullable().optional(), recent_action: z.string().nullable().optional(), external_entity_identifier: z.string().optional(), risk_score: z.number().nullable().optional()}).optional()}).optional(), events: z.array(z.object({event: z.string().optional(), agent: z.object({first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), token: z.string().optional()}).optional(), assigned_agent: z.object({first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), email: z.string().nullable().optional(), token: z.string().nullable().optional()}).optional(), note: z.string().nullable().optional(), meta: z.object({}).nullable().optional()})).optional(), evidences: z.object({journey_alerts: z.array(z.object({journey_application_token: z.string().optional(), journey_token: z.string().optional()})).optional(), tags: z.array(z.object({tag_color: z.string().optional(), tag_name: z.string().optional(), is_outcome_reason: z.boolean().optional(), added_by: z.object({email: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), token: z.string().optional()}).optional()})).optional()}).optional(), documents: z.array(document).optional(), notes: z.array(z.object({created_at: z.string().optional(), created_by: z.object({first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), token: z.string().optional()}).optional(), entity_name: z.string().optional(), entity_token: z.string().optional(), text: z.string().optional(), is_investigation_level_note: z.boolean().optional()})).optional(), reviews: z.array(z.object({new_investigation_state: z.string().optional(), note: z.string().nullable().optional(), outcome: z.string().optional(), previous_investigation_state: z.string().optional(), reason: z.string().optional(), reviewed_at: z.string().optional(), reviewed_by: z.object({first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), token: z.string().optional()}).optional()})).optional()}).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 }; /** * Create an Investigation * * POST /investigations * * Also accepts auth: basic */ export const createApiInvestigations = async (args: CreateApiInvestigationsArgs, options?: CreateApiInvestigationsOptions) => { const {baseUrl = "https://sandbox.alloy.co/v1", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/investigations${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(getInvestigationDetails, await res.json()) };