import {z} from 'zod' export type ExpenseReviewProcessConditionType = '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 type ExpenseReviewProcessCondition = {type: ExpenseReviewProcessConditionType, value: Array}; export type ExpenseReviewProcessReviewerType = 'LINE_MANAGER' | 'ROLE' | 'USER'; export type ExpenseReviewProcessReviewerInfo = {id: Array}; export type ExpenseReviewProcessStepStatus = 'APPROVED' | 'PARTIALLY_APPROVED' | 'DECLINED'; export type ExpenseReviewProcessComparator = 'HIGHER_EQUAL' | 'LOWER_EQUAL'; export type ExpenseReviewProcessAmountCondition = {amount: number, operator: ExpenseReviewProcessComparator}; export type ExpenseReviewProcessAutomationRule = {expense_status: ExpenseReviewProcessStepStatus, required_info: boolean, amount_condition?: ExpenseReviewProcessAmountCondition | undefined}; export type ExpenseReviewProcessStep = {step_number: number, reviewer_type: ExpenseReviewProcessReviewerType, reviewer_info?: ExpenseReviewProcessReviewerInfo | undefined, automation_rule?: ExpenseReviewProcessAutomationRule | undefined}; export type UpdateExpenseReviewProcess = {name: string, description: string, conditions: Array, steps: Array}; export type UpdateApiBusinessV2ExpenseReviewProcessesProcessIdArgs = {processId: string, body: UpdateExpenseReviewProcess}; export type UpdateApiBusinessV2ExpenseReviewProcessesProcessIdOptions = {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 }; /** * Update Expense Review Process * * PUT /business/v2/expense-review/processes/{processId} */ export const updateApiBusinessV2ExpenseReviewProcessesProcessId = async (args: UpdateApiBusinessV2ExpenseReviewProcessesProcessIdArgs, options?: UpdateApiBusinessV2ExpenseReviewProcessesProcessIdOptions) => { const {baseUrl = "https://api.soldo.com", fetch: fetchFn = fetch, standardAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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: "PUT", 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(expenseReviewProcess, await res.json()) };