import {z} from 'zod' export type ExpenseReportStatus = 'PROCESSED' | 'CANCELLED' | 'TO_BE_PROCESSED'; export type ExpenseReportType = 'EMPLOYEE' | 'COMPANY'; export type Direction = 'ASC' | 'DESC'; export type GetApiBusinessV2ExpenseReportsArgs = {number?: string | undefined, trxOwnerId?: string | undefined, trxDateFrom?: string | undefined, trxDateTo?: string | undefined, status?: Array | undefined, type?: Array | undefined, toReimburse?: boolean | undefined, exportedDate?: string | undefined, p?: number | undefined, s?: number | undefined, d?: Direction | undefined, props?: Array | undefined}; export type GetApiBusinessV2ExpenseReportsOptions = {baseUrl?: string | undefined, standardAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const expenseReportType = z.enum(["EMPLOYEE", "COMPANY"]); export const expenseReportStatusOutput = z.enum(["PROCESSED", "CANCELLED", "DELETED", "TO_BE_PROCESSED"]); export const expenseReport = z.object({reimburse_amount: z.number().optional(), deduct_amount: z.number().optional(), process_amount: z.number().optional(), file_url: z.string().optional(), id: z.string().optional(), protocol: z.string().optional(), number: z.string().optional(), type: expenseReportType.optional(), status: expenseReportStatusOutput.optional(), exported_date: z.string().optional(), exported_by: z.string().optional(), trx_owner_id: z.string().optional(), trx_date_from: z.string().optional(), trx_date_to: z.string().optional(), processed_date: z.string().optional(), processed_by: z.string().optional(), reimbursement_transaction_id: z.string().optional()}); export const expenseReports = z.object({total: z.number().int().optional(), pages: z.number().int().optional(), page_size: z.number().int().optional(), current_page: z.number().int().optional(), results_size: z.number().int().optional(), results: z.array(expenseReport).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 }; /** * Search Expense Reports * * GET /business/v2/expense-reports */ export const getApiBusinessV2ExpenseReports = async (args: GetApiBusinessV2ExpenseReportsArgs, options?: GetApiBusinessV2ExpenseReportsOptions) => { const {baseUrl = "https://api.soldo.com", fetch: fetchFn = fetch, standardAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.number != null) query.append("number", String(args.number)) if (args.trxOwnerId != null) query.append("trxOwnerId", String(args.trxOwnerId)) if (args.trxDateFrom != null) query.append("trxDateFrom", String(args.trxDateFrom)) if (args.trxDateTo != null) query.append("trxDateTo", String(args.trxDateTo)) for (const item of args.status ?? []) query.append("status", String(item)) for (const item of args.type ?? []) query.append("type", String(item)) if (args.toReimburse != null) query.append("toReimburse", String(args.toReimburse)) if (args.exportedDate != null) query.append("exportedDate", String(args.exportedDate)) if (args.p != null) query.append("p", String(args.p)) if (args.s != null) query.append("s", String(args.s)) if (args.d != null) query.append("d", String(args.d)) for (const item of args.props ?? []) query.append("props", String(item)) if (standardAuth !== undefined) headers.set('authorization', `Bearer ${standardAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/business/v2/expense-reports${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(expenseReports, await res.json()) };