import {z} from 'zod' export type ElementCreateRequest = {type: string, parentLocator?: unknown | undefined, elements: Array, coverageTerms?: {[key: string]: Record} | undefined, data?: {[key: string]: Record} | undefined, staticLocator?: unknown | undefined}; export type AddChangeInstructionCreateRequest = {action: 'add', elements: Array}; export type ContinuationChangeInstructionRequest = {action: 'continuation', accountLocator?: unknown | undefined, productName?: string | undefined, policyNumber?: string | undefined, continueWithTermNumber?: number | undefined, effectiveTime?: string | undefined, endTime?: string | undefined}; export type DeleteChangeInstructionCreateRequest = {action: 'delete', staticElementLocators: Array}; export type MigrateChangeInstructionCreateRequest = {action: 'migrate', configVersionLocator?: unknown | undefined}; export type ModifyChangeInstructionCreateRequest = {action: 'modify', staticLocator: unknown, setData: {[key: string]: Record}, removeData: {[key: string]: Record}, setCoverageTerms: {[key: string]: Record}, removeCoverageTerms: {[key: string]: Record}}; export type InstallmentPreferences = {cadence?: ('none' | 'fullPay' | 'weekly' | 'everyOtherWeek' | 'monthly' | 'quarterly' | 'semiannually' | 'annually' | 'thirtyDays' | 'everyNDays') | undefined, anchorMode?: ('generateDay' | 'termStartDay' | 'dueDay') | undefined, generateLeadDays?: number | undefined, dueLeadDays?: number | undefined, installmentWeights: Array, maxInstallmentsPerTerm?: number | undefined, installmentPlanName?: string | undefined, anchorType?: ('none' | 'dayOfMonth' | 'anchorTime' | 'dayOfWeek' | 'weekOfMonth') | undefined, dayOfMonth?: number | undefined, dayOfWeek?: ('monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sunday') | undefined, weekOfMonth?: ('none' | 'first' | 'second' | 'third' | 'fourth' | 'fifth') | undefined, anchorTime?: string | undefined, autopayLeadDays?: number | undefined}; export type Preferences = {installmentPreferences?: InstallmentPreferences | undefined}; export type ParamsChangeInstructionCreateRequest = {action: 'params', effectiveTime: string, newPolicyEndTime?: string | undefined, preferences?: Preferences | undefined, billingModeChange?: boolean | undefined, triggerBillingChange?: boolean | undefined, inheritSettings?: boolean | undefined, expirationTime?: string | undefined}; export type ProducersChangeInstructionCreateRequest = {action: 'producers', setProducerCode?: string | undefined, clearProducerCode?: boolean | undefined, setProducerCodeOfRecord?: string | undefined, revertProducerCodeOfRecord?: boolean | undefined}; export type ProxyPayerChangeInstructionRequest = {action: 'proxyPayer', setProxyPayerLocator?: unknown | undefined, clearProxyPayerLocator?: boolean | undefined}; export type TransactionDataChangeInstructionCreateRequest = {action: 'transactionData', data: {[key: string]: Record}}; export type PatchApiPolicyTenantLocatorTransactionsLocatorChangeInstructionsInstructionLocatorArgs = {tenantLocator: string, locator: unknown, instructionLocator: unknown, body: AddChangeInstructionCreateRequest | ContinuationChangeInstructionRequest | DeleteChangeInstructionCreateRequest | MigrateChangeInstructionCreateRequest | ModifyChangeInstructionCreateRequest | ParamsChangeInstructionCreateRequest | ProducersChangeInstructionCreateRequest | ProxyPayerChangeInstructionRequest | TransactionDataChangeInstructionCreateRequest}; export type PatchApiPolicyTenantLocatorTransactionsLocatorChangeInstructionsInstructionLocatorOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const validationItemResponse = z.object({elementType: z.string(), locator: z.string(), errors: z.array(z.string())}); export const validationResult = z.object({validationItems: z.array(validationItemResponse).optional(), success: z.boolean()}); export const elementCreateRequest: z.ZodType = z.object({type: z.string(), parentLocator: z.string().optional(), elements: z.array(z.lazy(() => elementCreateRequest)), coverageTerms: z.record(z.string(), z.object({})).optional(), data: z.record(z.string(), z.object({})).optional(), staticLocator: z.string().optional()}); export const addChangeInstructionResponse = z.object({action: z.literal("add"), elements: z.array(elementCreateRequest), transactionConflictResolution: z.literal("elidedInstruction"), locator: z.string()}); export const deleteChangeInstructionResponse = z.object({action: z.literal("delete"), staticElementLocators: z.array(z.string()), locator: z.string()}); export const modifyChangeInstructionResponse = z.object({action: z.literal("modify"), staticLocator: z.string(), setData: z.record(z.string(), z.object({})), removeData: z.record(z.string(), z.object({})), setCoverageTerms: z.record(z.string(), z.object({})), removeCoverageTerms: z.record(z.string(), z.object({})), transactionConflictResolution: z.literal("elidedInstruction"), locator: z.string()}); export const installmentPreferences = z.object({cadence: z.enum(["none", "fullPay", "weekly", "everyOtherWeek", "monthly", "quarterly", "semiannually", "annually", "thirtyDays", "everyNDays"]).optional(), anchorMode: z.enum(["generateDay", "termStartDay", "dueDay"]).optional(), generateLeadDays: z.number().int().optional(), dueLeadDays: z.number().int().optional(), installmentWeights: z.array(z.number()), maxInstallmentsPerTerm: z.number().int().optional(), installmentPlanName: z.string().optional(), anchorType: z.enum(["none", "dayOfMonth", "anchorTime", "dayOfWeek", "weekOfMonth"]).optional(), dayOfMonth: z.number().int().optional(), dayOfWeek: z.enum(["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]).optional(), weekOfMonth: z.enum(["none", "first", "second", "third", "fourth", "fifth"]).optional(), anchorTime: z.string().optional(), autopayLeadDays: z.number().optional()}); export const preferences = z.object({installmentPreferences: installmentPreferences.optional()}); export const paramsChangeInstructionResponse = z.object({action: z.literal("params"), effectiveTime: z.string(), newPolicyEndTime: z.string().optional(), preferences: preferences.optional(), billingModeChange: z.boolean().optional(), triggerBillingChange: z.boolean().optional(), inheritSettings: z.boolean().optional(), expirationTime: z.string().optional(), locator: z.string()}); export const producersChangeInstructionResponse = z.object({action: z.literal("producers"), setProducerCode: z.string().max(128).optional(), clearProducerCode: z.boolean().optional(), setProducerCodeOfRecord: z.string().max(128).optional(), revertProducerCodeOfRecord: z.boolean().optional(), locator: z.string()}); export const migrateChangeInstructionResponse = z.object({action: z.literal("migrate"), configVersionLocator: z.string().optional(), locator: z.string()}); export const transactionDataChangeInstructionResponse = z.object({action: z.literal("transactionData"), data: z.record(z.string(), z.object({})), locator: z.string()}); export const policyTransactionResponse: z.ZodType = z.object({locator: z.string(), transactionCategory: z.enum(["issuance", "change", "renewal", "cancellation", "continuation", "flatCancellation", "reinstatement", "reversal", "aggregate"]), transactionState: z.enum(["draft", "initialized", "validated", "earlyUnderwritten", "priced", "underwritten", "accepted", "issued", "underwrittenBlocked", "declined", "rejected", "refused", "discarded", "invalidated", "reversed"]), underwritingStatus: z.string().optional(), policyLocator: z.string(), baseTransactionLocator: z.string().optional(), aggregateTransactionLocator: z.string().optional(), createdAt: z.string(), createdBy: z.string(), validationResult: validationResult.optional(), changeInstructions: z.array(z.union([addChangeInstructionResponse, deleteChangeInstructionResponse, modifyChangeInstructionResponse, paramsChangeInstructionResponse, producersChangeInstructionResponse, migrateChangeInstructionResponse, transactionDataChangeInstructionResponse])).optional(), effectiveTime: z.string(), aggregatedTransactions: z.array(z.lazy(() => policyTransactionResponse)).optional(), termLocator: z.string(), preferences: preferences.optional(), transactionType: z.string(), issuedTime: z.string().optional(), acceptedTime: z.string().optional(), reapplicationOfLocator: z.string().optional(), maskingLevel: z.enum(["none", "level1", "level2"]).optional(), anonymizedAt: z.string().optional(), staticLocator: z.string(), expirationTime: z.string().optional(), data: z.record(z.string(), z.object({}))}); 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 }; /** * PATCH /policy/{tenantLocator}/transactions/{locator}/changeInstructions/{instructionLocator} */ export const patchApiPolicyTenantLocatorTransactionsLocatorChangeInstructionsInstructionLocator = async (args: PatchApiPolicyTenantLocatorTransactionsLocatorChangeInstructionsInstructionLocatorArgs, options: PatchApiPolicyTenantLocatorTransactionsLocatorChangeInstructionsInstructionLocatorOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/policy/${encodeURIComponent(String(args.tenantLocator))}/transactions/${encodeURIComponent(String(args.locator))}/changeInstructions/${encodeURIComponent(String(args.instructionLocator))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(policyTransactionResponse, await res.json()) };