|
1
|
import { ResourceEntry, ResourceTypeHandler, StepResourceData } from "../types" |
|
2
|
|
|
3
|
export class StepHandler implements ResourceTypeHandler<StepResourceData> { |
|
4
|
readonly type = "step" |
|
5
|
|
|
6
|
constructor(private inverseRegistry: Map<string, string[]>) {} |
|
7
|
|
|
8
|
validate(data: StepResourceData): void { |
|
9
|
if (!data.id) { |
|
10
|
throw new Error( |
|
11
|
`Step registration requires id. Received: ${JSON.stringify(data)}` |
|
12
|
) |
|
13
|
} |
|
14
|
|
|
15
|
if (!data.sourcePath && !data.workflowId) { |
|
16
|
throw new Error( |
|
17
|
`Step registration requires either sourcePath or workflowId. Received: ${JSON.stringify( |
|
18
|
data |
|
19
|
)}` |
|
20
|
) |
|
21
|
} |
|
22
|
} |
|
23
|
|
|
24
|
resolveSourcePath(data: StepResourceData): string { |
|
25
|
if (data.sourcePath) { |
|
26
|
return data.sourcePath |
|
27
|
} |
|
28
|
|
|
29
|
// Look up workflow's source path |
|
30
|
const workflowKey = `workflow:${data.workflowId}` |
|
31
|
const workflowSourcePaths = this.inverseRegistry.get(workflowKey) |
|
32
|
|
|
33
|
if (!workflowSourcePaths || workflowSourcePaths.length === 0) { |
|
34
|
throw new Error( |
|
35
|
`step workflow not found: ${data.workflowId} for step ${data.id}` |
|
36
|
) |
|
37
|
} |
|
38
|
|
|
39
|
return workflowSourcePaths[0] |
|
40
|
} |
|
41
|
|
|
42
|
createEntry(data: StepResourceData): ResourceEntry { |
|
43
|
return { |
|
44
|
id: data.id, |
|
45
|
workflowId: data.workflowId, |
|
46
|
} |
|
47
|
} |
|
48
|
|
|
49
|
getInverseKey(data: StepResourceData): string { |
|
50
|
return `${this.type}:${data.id}` |
|
51
|
} |
|
52
|
} |