import {z} from 'zod' export type WorkflowReviewerType = 'LINE_MANAGER' | 'ROLE' | 'USER'; export type WorkflowReviewerInfo = {ids: Array}; export type WorkflowStepStatus = 'APPROVED' | 'PARTIALLY_APPROVED' | 'DECLINED'; export type WorkflowComparator = 'HIGHER_EQUAL' | 'LOWER_EQUAL'; export type WorkflowAmountCondition = {amount: number, operator: WorkflowComparator}; export type WorkflowAutomationRule = {expense_status: WorkflowStepStatus, required_info: boolean, amount_condition?: WorkflowAmountCondition | undefined}; export type WorkflowProcessStep = {step_number: number, reviewer_type: WorkflowReviewerType, reviewer_info?: WorkflowReviewerInfo | undefined, automation_rule?: WorkflowAutomationRule | undefined}; export type VisibilityOwnerType = 'USER'; export type PersistVisibility = {type: VisibilityOwnerType, value: Array}; export type WorkflowTargetType = 'FUNDS' | 'GOODS_AND_SERVICES' | 'TRIP'; export type UpdateWorkflow = {name?: string | undefined, description?: string | undefined, policy_url?: string | undefined, steps: Array, visibilities: Array, target_type: WorkflowTargetType}; export type UpdateApiBusinessV2WorkflowsWorkflowIdArgs = {workflowId: string, body: UpdateWorkflow}; export type UpdateApiBusinessV2WorkflowsWorkflowIdOptions = {baseUrl?: string | undefined, standardAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowStatus = z.enum(["ACTIVE", "DEACTIVATED", "DELETED"]); export const workflowTargetType = z.enum(["FUNDS", "GOODS_AND_SERVICES", "TRIP"]); export const workflowReviewerType = z.enum(["LINE_MANAGER", "ROLE", "USER"]); export const workflowReviewerInfo = z.object({ids: z.array(z.string())}); export const workflowStepStatus = z.enum(["APPROVED", "PARTIALLY_APPROVED", "DECLINED"]); export const workflowComparator = z.enum(["HIGHER_EQUAL", "LOWER_EQUAL"]); export const workflowAmountCondition = z.object({amount: z.number(), operator: workflowComparator}); export const workflowAutomationRule = z.object({expense_status: workflowStepStatus, required_info: z.boolean(), amount_condition: workflowAmountCondition.optional()}); export const workflowProcessStep = z.object({step_number: z.number().int().gte(1), reviewer_type: workflowReviewerType, reviewer_info: workflowReviewerInfo.optional(), automation_rule: workflowAutomationRule.optional()}); export const visibilityOwnerType = z.literal("USER"); export const persistVisibility = z.object({type: visibilityOwnerType, value: z.array(z.string())}); export const workflow = z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), policy_url: z.string().optional(), status: workflowStatus.optional(), target_type: workflowTargetType.optional(), priority: z.number().int().optional(), steps_count: z.number().int().optional(), steps: z.array(workflowProcessStep), visibilities: z.array(persistVisibility)}); 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 Workflow * * PUT /business/v2/workflows/{workflowId} */ export const updateApiBusinessV2WorkflowsWorkflowId = async (args: UpdateApiBusinessV2WorkflowsWorkflowIdArgs, options?: UpdateApiBusinessV2WorkflowsWorkflowIdOptions) => { 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/workflows/${encodeURIComponent(String(args.workflowId))}${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(workflow, await res.json()) };