import {z} from 'zod' export type RescheduleLoanAccountHumaRequest = {businessDate: string, effectiveDate: string, firstRescheduledDueDate: string, newTermMonths: number, reason: string, transactionId: string}; export type CreateApiApiV1LoanAccountsIdReschedulesArgs = {id: string, 'X-Idempotency': string, body: RescheduleLoanAccountHumaRequest}; export type CreateApiApiV1LoanAccountsIdReschedulesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduleVersionInstallmentHumaBody = z.object({dueDate: z.string(), feesAmount: z.string(), interestAmount: z.string(), number: z.number().int(), penaltiesAmount: z.string(), principalAmount: z.string(), principalRemaining: z.string(), totalAmount: z.string()}); export const scheduleVersionHumaBody = z.object({createdAt: z.string(), id: z.string(), installments: z.array(scheduleVersionInstallmentHumaBody).nullable(), loanAccountId: z.string(), predecessorVersionId: z.string().optional(), reason: z.string(), triggerTransactionId: z.string(), versionNumber: z.number().int()}); export const scheduleVersionCommandHumaBody = z.object({previousScheduleVersionId: z.string(), scheduleVersion: scheduleVersionHumaBody}); 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 }; /** * Reschedule loan account * * POST /api/v1/loan-accounts/{id}/reschedules */ export const createApiApiV1LoanAccountsIdReschedules = async (args: CreateApiApiV1LoanAccountsIdReschedulesArgs, options?: CreateApiApiV1LoanAccountsIdReschedulesOptions) => { 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-Idempotency'] != null) headers.set("X-Idempotency", String(args['X-Idempotency'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/loan-accounts/${encodeURIComponent(String(args.id))}/reschedules${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(scheduleVersionCommandHumaBody, await res.json()) };