import {z} from 'zod' export type GetApiBusinessV2ExpenseReviewProcessesProcessIdArgs = {processId: string}; export type GetApiBusinessV2ExpenseReviewProcessesProcessIdOptions = {baseUrl?: string | undefined, standardAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const expenseReviewProcessStatus = z.enum(["ACTIVE", "INACTIVE"]); export const expenseReviewProcessConditionType = z.enum(["COMPANY_CARD", "COMPANY_WALLET", "EXPENSE_CATEGORY", "EXPENSE_TYPE", "MAIN_WALLET", "ONLINE_ADS_CARD", "OUT_OF_PLATFORM_COMPANY", "OUT_OF_PLATFORM_PERSONAL", "PURCHASE", "SUBSCRIPTION_CARD", "TAG", "USER", "USER_CARD"]); export const expenseReviewProcessCondition = z.object({type: expenseReviewProcessConditionType, value: z.array(z.string())}); export const expenseReviewProcessReviewerType = z.enum(["LINE_MANAGER", "ROLE", "USER"]); export const expenseReviewProcessReviewerInfo = z.object({id: z.array(z.string())}); export const expenseReviewProcessStepStatus = z.enum(["APPROVED", "PARTIALLY_APPROVED", "DECLINED"]); export const expenseReviewProcessComparator = z.enum(["HIGHER_EQUAL", "LOWER_EQUAL"]); export const expenseReviewProcessAmountCondition = z.object({amount: z.number(), operator: expenseReviewProcessComparator}); export const expenseReviewProcessAutomationRule = z.object({expense_status: expenseReviewProcessStepStatus, required_info: z.boolean(), amount_condition: expenseReviewProcessAmountCondition.optional()}); export const expenseReviewProcessStep = z.object({step_number: z.number().int().gte(1), reviewer_type: expenseReviewProcessReviewerType, reviewer_info: expenseReviewProcessReviewerInfo.optional(), automation_rule: expenseReviewProcessAutomationRule.optional()}); export const expenseReviewProcess = z.object({id: z.string().optional(), name: z.string(), description: z.string(), steps_count: z.number().int(), priority: z.number().int().optional(), status: expenseReviewProcessStatus, conditions: z.array(expenseReviewProcessCondition), steps: z.array(expenseReviewProcessStep)}); 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 Process * * GET /business/v2/expense-review/processes/{processId} */ export const getApiBusinessV2ExpenseReviewProcessesProcessId = async (args: GetApiBusinessV2ExpenseReviewProcessesProcessIdArgs, options?: GetApiBusinessV2ExpenseReviewProcessesProcessIdOptions) => { 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 (standardAuth !== undefined) headers.set('authorization', `Bearer ${standardAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/business/v2/expense-review/processes/${encodeURIComponent(String(args.processId))}${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(expenseReviewProcess, await res.json()) };