import {z} from 'zod' export type ClaimAcceptDetails = {option?: ('ACVA' | 'MODI') | undefined, value_date?: string | undefined}; export type CreateApiClaimsIdAcceptArgs = {id: string, body: ClaimAcceptDetails}; export type CreateApiClaimsIdAcceptOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const claimCreditorReferenceType = z.object({code: z.enum(["dispatch_advice", "foreign_exchange_deal_reference", "purchase_order", "related_payment_instruction", "remittance_advice_message", "structured_communication_reference", "other"]).optional(), issuer: z.string().optional()}); export const claimCreditorReferenceInformation = z.object({type: claimCreditorReferenceType.optional(), reference: z.string().optional()}); export const claimReferredDocumentType = z.object({code: z.enum(["commercial_invoice", "credit_note_related_to_financial_adjustment", "contract", "credit_note", "debit_note", "dispatch_advice", "debit_note_related_to_financial_adjustment", "hire_invoice", "invoice_signed", "metered_service_invoice", "proforma_invoice", "purchase_order", "quotation", "self_billed_invoice", "seller_presentment", "time_sheet", "usage_report", "account_receivable_open_item", "bill_of_lading", "commercial_contract", "statement_of_account", "trade_services_utility_transaction", "voucher", "other"]).optional(), issuer: z.string().optional()}); export const claimReferredDocumentInformation = z.object({type: claimReferredDocumentType.optional(), number: z.string().optional(), related_date: z.string().optional()}); export const claimStructuredReference = z.array(z.object({creditor_reference_information: claimCreditorReferenceInformation.optional(), referred_document_information: z.array(claimReferredDocumentInformation).optional(), invoicer: z.object({name: z.string().optional()}).optional(), invoicee: z.object({name: z.string().optional()}).optional(), additional_remittance_info: z.array(z.string()).optional()})); export const commonAddress = z.object({line_1: z.string().optional(), line_2: z.string().optional(), building_number: z.string().optional(), street_name: z.string().optional(), postal_code: z.string().optional(), region_state: z.string().optional(), city: z.string().optional(), country: z.string().optional(), department: z.string().nullable().optional(), sub_department: z.string().nullable().optional(), building_name: z.string().nullable().optional(), floor: z.string().nullable().optional(), postal_box: z.string().nullable().optional(), room: z.string().nullable().optional(), city_location_name: z.string().nullable().optional(), district_name: z.string().nullable().optional()}); export const claimRelatedAccountInfo = z.object({account_number: z.string(), bank_code: z.string(), holder_name: z.string(), holder_address: commonAddress}); export const claimRelatedPaymentBankData = z.object({message_id: z.string(), end_to_end_id: z.string(), transaction_id: z.string()}); export const claimRelatedPayment = z.object({amount: z.number().int(), currency: z.literal("EUR"), direction: z.string(), value_date: z.string(), reference: z.string(), structured_reference: claimStructuredReference.optional(), originating_account: claimRelatedAccountInfo, receiving_account: claimRelatedAccountInfo, bank_data: claimRelatedPaymentBankData.optional()}); export const claimDetails = z.object({id: z.string(), object: z.literal("claim"), type: z.enum(["sepa_non_receipt", "sepa_value_date_correction"]), status: z.enum(["created", "processing", "sent", "received", "accepted", "rejected"]), status_details: z.string(), value_date: z.string(), connected_account_id: z.string(), related_payment_type: z.enum(["payment_order", "incoming_payment"]), related_payment_id: z.string(), related_payment: claimRelatedPayment, description: z.string().max(140), metadata: z.object({}), investigation_fee: z.object({amount: z.number().int().optional(), currency: z.string().optional(), account_number: z.string().optional(), bank_code: z.string().optional()}).optional(), compensation_fee: z.object({amount: z.number().int().optional(), currency: z.string().optional(), account_number: z.string().optional(), bank_code: z.string().optional()}).optional(), bank_data: z.object({reference: z.string(), assignment_id: z.string(), case_id: z.string(), file_id: z.string(), file_reference: z.string().optional()}), created_at: 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 }; /** * Accept claim * * POST /claims/{id}/accept */ export const createApiClaimsIdAccept = async (args: CreateApiClaimsIdAcceptArgs, options?: CreateApiClaimsIdAcceptOptions) => { const {baseUrl = "https://sandbox.numeral.io/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/claims/${encodeURIComponent(String(args.id))}/accept${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(claimDetails, await res.json()) };