import {z} from 'zod' export type PostingLegHumaInputBody = {account: string, component?: string | undefined, optional?: boolean | undefined, role?: ('principal' | 'interest' | 'iof' | 'tax' | 'cash' | 'unapplied_cash' | 'counterparty') | undefined, side: 'debit' | 'credit'}; export type PostingRuleHumaInputBody = {eventType: 'disbursement' | 'repayment' | 'prepayment' | 'accrual' | 'accrual_tax' | 'collection_unapplied' | 'collection_reapply' | 'collection_refund', legs: Array, metadata?: {[key: string]: string} | undefined}; export type ConfigureAccountingProfileHumaInputBody = {accountingMode: 'accrual' | 'cash', loanProductVersionId: string, midazLedgerId?: string | undefined, midazOrganizationId?: string | undefined, postingRules: Array}; export type CreateApiApiV1LoanProductsIdAccountingProfilesArgs = {id: string, body: ConfigureAccountingProfileHumaInputBody}; export type CreateApiApiV1LoanProductsIdAccountingProfilesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const accountingPostingLegBody = z.object({account: z.string(), component: z.string().optional(), optional: z.boolean(), role: z.enum(["principal", "interest", "iof", "tax", "cash", "unapplied_cash", "counterparty"]).optional(), side: z.enum(["debit", "credit"])}); export const accountingPostingRuleHumaBody = z.object({eventType: z.string(), legs: z.array(accountingPostingLegBody), metadata: z.record(z.string(), z.string())}); export const accountingProfileHumaBody = z.object({accountingMode: z.string(), createdAt: z.string(), id: z.string(), loanProductVersionId: z.string(), postingRules: z.array(accountingPostingRuleHumaBody).nullable()}); 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 loan product accounting profile * * POST /api/v1/loan-products/{id}/accounting-profiles */ export const createApiApiV1LoanProductsIdAccountingProfiles = async (args: CreateApiApiV1LoanProductsIdAccountingProfilesArgs, options?: CreateApiApiV1LoanProductsIdAccountingProfilesOptions) => { const {baseUrl = "https://identity.sandbox.lerian.net", 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(/\/+$/, '')}/api/v1/loan-products/${encodeURIComponent(String(args.id))}/accounting-profiles${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(accountingProfileHumaBody, await res.json()) };