import {z} from 'zod' export type ExpenseStatus = 'APPROVED' | 'PARTIALLY_APPROVED' | 'DENIED' | 'TO_REVIEW'; export type ExpensePaymentMethod = 'USER' | 'COMPANY' | 'PURCHASE' | 'SUBSCRIPTION' | 'ONLINE_ADS' | 'OUT_OF_PLATFORM_COMPANY' | 'OUT_OF_PLATFORM_PERSONAL'; export type Direction = 'ASC' | 'DESC'; export type GetApiBusinessV2ExpenseReviewUserIdArgs = {userId: string, status?: Array | undefined, fromDate?: string | undefined, toDate?: string | undefined, paymentMethod?: Array | undefined, p?: number | undefined, s?: number | undefined, d?: Direction | undefined, props?: Array | undefined}; export type GetApiBusinessV2ExpenseReviewUserIdOptions = {baseUrl?: string | undefined, standardAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const expenseItem = z.object({user_id: z.string().optional(), user_name: z.string().optional(), user_surname: z.string().optional(), job_title: z.string().optional(), to_review_transaction: z.number().int().optional(), reviewed_transaction: z.number().int().optional(), exported_transaction: z.number().int().optional(), total_transaction: z.number().int().optional(), from_date: z.string().optional(), to_date: z.string().optional(), transaction_to_review: z.array(z.string()).optional(), transaction_reviewed: z.array(z.string()).optional(), transaction_exported: z.array(z.string()).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 }; /** * Get Expense Review * * GET /business/v2/expense-review/{userId} */ export const getApiBusinessV2ExpenseReviewUserId = async (args: GetApiBusinessV2ExpenseReviewUserIdArgs, options?: GetApiBusinessV2ExpenseReviewUserIdOptions) => { const {baseUrl = "https://api.soldo.com", fetch: fetchFn = fetch, standardAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.status ?? []) query.append("status", String(item)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) for (const item of args.paymentMethod ?? []) query.append("paymentMethod", String(item)) 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-review/${encodeURIComponent(String(args.userId))}${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(expenseItem, await res.json()) };