import {z} from 'zod' export type GetApiApiV1BrLoanAccountsIdPayoffStatementsQuoteIdArgs = {id: string, quote_id: string}; export type GetApiApiV1BrLoanAccountsIdPayoffStatementsQuoteIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const payoffBoletoInstructions = z.object({barcode: z.string().optional(), digitableLine: z.string().optional(), documentUrl: z.string().optional()}); export const payoffPIXInstructions = z.object({copyPasteCode: z.string().optional(), qrCodeUrl: z.string().optional()}); export const payoffInstrumentResponse = z.object({boleto: payoffBoletoInstructions.optional(), id: z.string(), issuedAt: z.string().optional(), pix: payoffPIXInstructions.optional(), requestedAt: z.string(), status: z.enum(["requested", "issued", "paid", "expired", "cancelled", "failed"]), type: z.enum(["pix", "boleto"])}); export const payoffStatementResponse = z.object({chargeDiscount: z.string(), currency: z.string(), expiresAt: z.string(), grossAmount: z.string(), instrument: payoffInstrumentResponse, interestDiscount: z.string(), iofReconciliation: z.string(), loanAccountId: z.string(), netAmount: z.string(), principalOutstanding: z.string(), quoteId: z.string(), slaDueAt: z.string(), slaStatus: z.enum(["pending", "met", "breached"]), statementAvailableAt: 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 }; /** * Get payoff statement * * GET /api/v1/br/loan-accounts/{id}/payoff-statements/{quote_id} */ export const getApiApiV1BrLoanAccountsIdPayoffStatementsQuoteId = async (args: GetApiApiV1BrLoanAccountsIdPayoffStatementsQuoteIdArgs, options?: GetApiApiV1BrLoanAccountsIdPayoffStatementsQuoteIdOptions) => { const {baseUrl = "https://identity.sandbox.lerian.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/br/loan-accounts/${encodeURIComponent(String(args.id))}/payoff-statements/${encodeURIComponent(String(args.quote_id))}${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(payoffStatementResponse, await res.json()) };