|
| 1 | +/** |
| 2 | + * Form commands |
| 3 | + */ |
| 4 | + |
| 5 | +import { getLogger } from '../logger.ts'; |
| 6 | +import { createClient } from '../client.ts'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Get form for a user task |
| 10 | + */ |
| 11 | +export async function getUserTaskForm(userTaskKey: string, options: { |
| 12 | + profile?: string; |
| 13 | +}): Promise<Record<string, unknown> | undefined> { |
| 14 | + const logger = getLogger(); |
| 15 | + const client = createClient(options.profile); |
| 16 | + |
| 17 | + try { |
| 18 | + const result = await client.getUserTaskForm( |
| 19 | + { userTaskKey: userTaskKey as any }, |
| 20 | + { consistency: { waitUpToMs: 0 } } |
| 21 | + ); |
| 22 | + // API returns null when user task exists but has no form |
| 23 | + if (result === null || result === undefined) { |
| 24 | + logger.info('User task found but has no associated form'); |
| 25 | + return undefined; |
| 26 | + } |
| 27 | + logger.json(result); |
| 28 | + return result as Record<string, unknown>; |
| 29 | + } catch (error: any) { |
| 30 | + // Handle 204 No Content (user task exists but has no form) |
| 31 | + if (error.statusCode === 204 || error.status === 204) { |
| 32 | + logger.info('User task found but has no associated form'); |
| 33 | + return undefined; |
| 34 | + } |
| 35 | + logger.error(`Failed to get form for user task ${userTaskKey}`, error as Error); |
| 36 | + process.exit(1); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Get start form for a process definition |
| 42 | + */ |
| 43 | +export async function getStartForm(processDefinitionKey: string, options: { |
| 44 | + profile?: string; |
| 45 | +}): Promise<Record<string, unknown> | undefined> { |
| 46 | + const logger = getLogger(); |
| 47 | + const client = createClient(options.profile); |
| 48 | + |
| 49 | + try { |
| 50 | + const result = await client.getStartProcessForm( |
| 51 | + { processDefinitionKey: processDefinitionKey as any }, |
| 52 | + { consistency: { waitUpToMs: 0 } } |
| 53 | + ); |
| 54 | + // API returns null when process definition exists but has no start form |
| 55 | + if (result === null || result === undefined) { |
| 56 | + logger.info('Process definition found but has no associated start form'); |
| 57 | + return undefined; |
| 58 | + } |
| 59 | + logger.json(result); |
| 60 | + return result as Record<string, unknown>; |
| 61 | + } catch (error: any) { |
| 62 | + // Handle 204 No Content (process definition exists but has no form) |
| 63 | + if (error.statusCode === 204 || error.status === 204) { |
| 64 | + logger.info('Process definition found but has no associated start form'); |
| 65 | + return undefined; |
| 66 | + } |
| 67 | + logger.error(`Failed to get start form for process definition ${processDefinitionKey}`, error as Error); |
| 68 | + process.exit(1); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Get form by trying both user task and process definition APIs |
| 74 | + */ |
| 75 | +export async function getForm(key: string, options: { |
| 76 | + profile?: string; |
| 77 | +}): Promise<{ type: string; key: string; form: Record<string, unknown> } | undefined> { |
| 78 | + const logger = getLogger(); |
| 79 | + const client = createClient(options.profile); |
| 80 | + |
| 81 | + const results: { type: string; key: string; form: any }[] = []; |
| 82 | + const errors: { type: string; error: any }[] = []; |
| 83 | + |
| 84 | + // Try user task form |
| 85 | + try { |
| 86 | + const result = await client.getUserTaskForm( |
| 87 | + { userTaskKey: key as any }, |
| 88 | + { consistency: { waitUpToMs: 0 } } |
| 89 | + ); |
| 90 | + if (result !== null && result !== undefined) { |
| 91 | + results.push({ type: 'user task', key, form: result }); |
| 92 | + } |
| 93 | + } catch (error: any) { |
| 94 | + // 204 means resource exists but no form - not an error |
| 95 | + if (error.statusCode !== 204 && error.status !== 204) { |
| 96 | + errors.push({ type: 'user task', error }); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Try process definition form |
| 101 | + try { |
| 102 | + const result = await client.getStartProcessForm( |
| 103 | + { processDefinitionKey: key as any }, |
| 104 | + { consistency: { waitUpToMs: 0 } } |
| 105 | + ); |
| 106 | + if (result !== null && result !== undefined) { |
| 107 | + results.push({ type: 'process definition', key, form: result }); |
| 108 | + } |
| 109 | + } catch (error: any) { |
| 110 | + // 204 means resource exists but no form - not an error |
| 111 | + if (error.statusCode !== 204 && error.status !== 204) { |
| 112 | + errors.push({ type: 'process definition', error }); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Report results |
| 117 | + if (results.length === 0) { |
| 118 | + if (errors.length === 0) { |
| 119 | + logger.info('No form found for user task or process definition'); |
| 120 | + return undefined; |
| 121 | + } else if (errors.length === 1) { |
| 122 | + logger.error(`Failed to get form: not found as ${errors[0].type}`, errors[0].error as Error); |
| 123 | + process.exit(1); |
| 124 | + } else { |
| 125 | + logger.error(`Failed to get form: not found as user task or process definition`); |
| 126 | + process.exit(1); |
| 127 | + } |
| 128 | + } else if (results.length === 1) { |
| 129 | + const keyType = results[0].type === 'user task' ? 'userTaskKey' : 'processDefinitionKey'; |
| 130 | + logger.info(`Form found for ${results[0].type} (${keyType}: ${key}):`); |
| 131 | + logger.json(results[0].form); |
| 132 | + return { type: results[0].type, key, form: results[0].form as Record<string, unknown> }; |
| 133 | + } else { |
| 134 | + logger.info(`Form found in both user task and process definition (key: ${key}):`); |
| 135 | + const combined = { |
| 136 | + userTaskKey: key, |
| 137 | + userTask: results.find(r => r.type === 'user task')?.form, |
| 138 | + processDefinitionKey: key, |
| 139 | + processDefinition: results.find(r => r.type === 'process definition')?.form, |
| 140 | + }; |
| 141 | + logger.json(combined); |
| 142 | + return { type: 'both', key, form: combined as Record<string, unknown> }; |
| 143 | + } |
| 144 | +} |
0 commit comments