import {z} from 'zod' export type RejectLoanApplicationHumaRequest = {decisionAt: string, reason: string}; export type CreateApiApiV1LoanApplicationsIdRejectArgs = {id: string, body: RejectLoanApplicationHumaRequest}; export type CreateApiApiV1LoanApplicationsIdRejectOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const approvalDecisionHumaBody = z.object({approvedAmount: z.string(), decidedBy: z.string(), decisionAt: z.string(), id: z.string(), note: z.string().optional()}); export const disbursementEventHumaBody = z.object({disbursedAt: z.string(), grossRequestedAmount: z.string(), id: z.string(), loanAccountId: z.string(), netDeliveredAmount: z.string(), profileVersion: z.string()}); export const reasonedDecisionHumaBody = z.object({decidedBy: z.string(), decisionAt: z.string(), id: z.string(), reason: z.string()}); export const loanApplicationHumaBody = z.object({approvalDecision: approvalDecisionHumaBody.optional(), assignedOfficerId: z.string(), borrowerId: z.string(), disbursementEvent: disbursementEventHumaBody.optional(), expectedDisbursementDate: z.string(), id: z.string(), loanProductVersionId: z.string(), previewJurisdictionCode: z.string(), previewProfileVersion: z.string(), previewScheduleSnapshotId: z.string(), rejectionDecision: reasonedDecisionHumaBody.optional(), requestedInstallments: z.number().int(), requestedInterestRate: z.string(), requestedPrincipalAmount: z.string(), status: z.string(), withdrawalDecision: reasonedDecisionHumaBody.optional()}); 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 }; /** * Reject loan application * * POST /api/v1/loan-applications/{id}/reject */ export const createApiApiV1LoanApplicationsIdReject = async (args: CreateApiApiV1LoanApplicationsIdRejectArgs, options?: CreateApiApiV1LoanApplicationsIdRejectOptions) => { 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-applications/${encodeURIComponent(String(args.id))}/reject${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(loanApplicationHumaBody, await res.json()) };