import {z} from 'zod' export type RepaymentRequestHumaBody = {amount: string, effectiveDate?: string | undefined, transactionDate?: string | undefined}; export type CreateApiApiV1LoanAccountsIdRepaymentsArgs = {id: string, 'X-Request-ID'?: string | undefined, 'X-Idempotency'?: string | undefined, body: RepaymentRequestHumaBody}; export type CreateApiApiV1LoanAccountsIdRepaymentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const repaymentAllocationHumaBody = z.object({dueDate: z.string(), feesAmount: z.string(), fullyPaid: z.boolean(), installmentNumber: z.number().int(), interestAmount: z.string(), penaltiesAmount: z.string(), principalAmount: z.string(), totalAmount: z.string()}); export const repaymentRecordHumaBody = z.object({allocations: z.array(repaymentAllocationHumaBody).nullable(), appliedAmount: z.string(), backdated: z.boolean(), effectiveDate: z.string(), loanAccountId: z.string(), overpaymentAmount: z.string(), requestId: z.string().optional(), transactionId: 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 }; /** * Record repayment * * POST /api/v1/loan-accounts/{id}/repayments */ export const createApiApiV1LoanAccountsIdRepayments = async (args: CreateApiApiV1LoanAccountsIdRepaymentsArgs, options?: CreateApiApiV1LoanAccountsIdRepaymentsOptions) => { 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") if (args['X-Request-ID'] != null) headers.set("X-Request-ID", String(args['X-Request-ID'])) if (args['X-Idempotency'] != null) headers.set("X-Idempotency", String(args['X-Idempotency'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/loan-accounts/${encodeURIComponent(String(args.id))}/repayments${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(repaymentRecordHumaBody, await res.json()) };