@crombolo/schema 0.45.0 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,200 @@
1
+ /**
2
+ * Changes to an approval workflow (story 251): its rules and what it applies to,
3
+ * and its stages. Pure, each returning the changed workflow or why not; whether
4
+ * the graph still holds is the parser's to say when the change is checked.
5
+ */
6
+ import {
7
+ type SchemaWorkflow,
8
+ SEND_BACK_TO_EDITOR,
9
+ type StageRequirement,
10
+ type WorkflowComments,
11
+ type WorkflowOnChange,
12
+ type WorkflowOnEdit,
13
+ type WorkflowSelfApproval,
14
+ type WorkflowStage,
15
+ } from '@crombolo/core'
16
+
17
+ export type WorkflowEditResult =
18
+ | { ok: true; workflow: SchemaWorkflow }
19
+ | { ok: false; reason: string }
20
+
21
+ const refuse = (reason: string): WorkflowEditResult => ({ ok: false, reason })
22
+
23
+ const added = (list: readonly string[], more: readonly string[] = []) => [
24
+ ...list,
25
+ ...more.filter((item) => !list.includes(item)),
26
+ ]
27
+ const without = (list: readonly string[], less: readonly string[] = []) =>
28
+ list.filter((item) => !less.includes(item))
29
+
30
+ export interface WorkflowChanges {
31
+ name?: string
32
+ addContentTypes?: readonly string[]
33
+ removeContentTypes?: readonly string[]
34
+ addBranches?: readonly string[]
35
+ removeBranches?: readonly string[]
36
+ publishersMaySkip?: boolean
37
+ onEdit?: WorkflowOnEdit
38
+ selfApproval?: WorkflowSelfApproval
39
+ comments?: WorkflowComments
40
+ unpublish?: boolean
41
+ onChange?: WorkflowOnChange
42
+ scheduling?: boolean
43
+ }
44
+
45
+ /** A workflow's name, rules and what it applies to. */
46
+ export function updateWorkflow(
47
+ workflow: SchemaWorkflow,
48
+ changes: WorkflowChanges,
49
+ ): WorkflowEditResult {
50
+ for (const type of changes.removeContentTypes ?? [])
51
+ if (!workflow.appliesTo.contentTypes.includes(type))
52
+ return refuse(`"${workflow.alias}" does not apply to the content type "${type}"`)
53
+ for (const branch of changes.removeBranches ?? [])
54
+ if (!workflow.appliesTo.branches.includes(branch))
55
+ return refuse(`"${workflow.alias}" does not apply to the branch "${branch}"`)
56
+ return {
57
+ ok: true,
58
+ workflow: {
59
+ ...workflow,
60
+ ...(changes.name !== undefined ? { name: changes.name } : {}),
61
+ appliesTo: {
62
+ contentTypes: without(
63
+ added(workflow.appliesTo.contentTypes, changes.addContentTypes),
64
+ changes.removeContentTypes,
65
+ ),
66
+ branches: without(
67
+ added(workflow.appliesTo.branches, changes.addBranches),
68
+ changes.removeBranches,
69
+ ),
70
+ },
71
+ ...(changes.publishersMaySkip !== undefined
72
+ ? { publishersMaySkip: changes.publishersMaySkip }
73
+ : {}),
74
+ ...(changes.onEdit !== undefined ? { onEdit: changes.onEdit } : {}),
75
+ ...(changes.selfApproval !== undefined ? { selfApproval: changes.selfApproval } : {}),
76
+ ...(changes.comments !== undefined ? { comments: changes.comments } : {}),
77
+ ...(changes.unpublish !== undefined ? { unpublish: changes.unpublish } : {}),
78
+ ...(changes.onChange !== undefined ? { onChange: changes.onChange } : {}),
79
+ ...(changes.scheduling !== undefined ? { scheduling: changes.scheduling || undefined } : {}),
80
+ },
81
+ }
82
+ }
83
+
84
+ export interface NewStage {
85
+ alias: string
86
+ name?: string
87
+ approvers: readonly string[]
88
+ required?: StageRequirement
89
+ /** The stages it follows; the last ones (those nothing follows) when absent. */
90
+ after?: readonly string[]
91
+ /** The stages that follow it; none, so it is a last stage, when absent. */
92
+ next?: readonly string[]
93
+ sendBack?: string
94
+ }
95
+
96
+ const stageNamed = (workflow: SchemaWorkflow, alias: string) =>
97
+ workflow.stages.find((stage) => stage.alias.toLowerCase() === alias.toLowerCase())
98
+
99
+ /** A stage added: after the stages named, or after the last ones, so a line grows at its end. */
100
+ export function addStage(workflow: SchemaWorkflow, stage: NewStage): WorkflowEditResult {
101
+ if (stageNamed(workflow, stage.alias))
102
+ return refuse(`"${workflow.alias}" already has a stage "${stage.alias}"`)
103
+ if (stage.approvers.length === 0) return refuse('a stage needs at least one approver group')
104
+ for (const alias of [...(stage.after ?? []), ...(stage.next ?? [])])
105
+ if (!stageNamed(workflow, alias)) return refuse(`"${workflow.alias}" has no stage "${alias}"`)
106
+ const after =
107
+ stage.after ?? workflow.stages.filter((s) => s.next.length === 0).map((s) => s.alias)
108
+ const next: WorkflowStage = {
109
+ alias: stage.alias,
110
+ ...(stage.name ? { name: stage.name } : {}),
111
+ approvers: [...stage.approvers],
112
+ required: stage.required ?? 'one',
113
+ next: [...(stage.next ?? [])],
114
+ sendBack: stage.sendBack ?? SEND_BACK_TO_EDITOR,
115
+ }
116
+ return {
117
+ ok: true,
118
+ workflow: {
119
+ ...workflow,
120
+ stages: [
121
+ ...workflow.stages.map((s) =>
122
+ after.includes(s.alias)
123
+ ? { ...s, next: added(without(s.next, stage.next), [stage.alias]) }
124
+ : s,
125
+ ),
126
+ next,
127
+ ],
128
+ },
129
+ }
130
+ }
131
+
132
+ export interface StageChanges {
133
+ name?: string
134
+ /** Its approver groups, all of them. */
135
+ approvers?: readonly string[]
136
+ addApprovers?: readonly string[]
137
+ removeApprovers?: readonly string[]
138
+ required?: StageRequirement
139
+ /** What follows it, all of it; empty makes it a last stage. */
140
+ next?: readonly string[]
141
+ sendBack?: string
142
+ }
143
+
144
+ /** A stage's name, approvers, what it needs, what follows it and where it sends back to. */
145
+ export function updateStage(
146
+ workflow: SchemaWorkflow,
147
+ alias: string,
148
+ changes: StageChanges,
149
+ ): WorkflowEditResult {
150
+ const stage = stageNamed(workflow, alias)
151
+ if (!stage) return refuse(`"${workflow.alias}" has no stage "${alias}"`)
152
+ for (const other of changes.next ?? [])
153
+ if (!stageNamed(workflow, other)) return refuse(`"${workflow.alias}" has no stage "${other}"`)
154
+ for (const group of changes.removeApprovers ?? [])
155
+ if (!stage.approvers.includes(group))
156
+ return refuse(`"${stage.alias}" is not approved by "${group}"`)
157
+ const approvers = without(
158
+ added(changes.approvers ?? stage.approvers, changes.addApprovers),
159
+ changes.removeApprovers,
160
+ )
161
+ if (approvers.length === 0) return refuse('a stage needs at least one approver group')
162
+ const next: WorkflowStage = {
163
+ ...stage,
164
+ ...(changes.name !== undefined ? { name: changes.name || undefined } : {}),
165
+ approvers,
166
+ ...(changes.required !== undefined ? { required: changes.required } : {}),
167
+ ...(changes.next !== undefined ? { next: [...changes.next] } : {}),
168
+ ...(changes.sendBack !== undefined ? { sendBack: changes.sendBack } : {}),
169
+ }
170
+ return {
171
+ ok: true,
172
+ workflow: { ...workflow, stages: workflow.stages.map((s) => (s === stage ? next : s)) },
173
+ }
174
+ }
175
+
176
+ /** A stage taken out, the stages before it passing on to what came after it. */
177
+ export function removeStage(workflow: SchemaWorkflow, alias: string): WorkflowEditResult {
178
+ const stage = stageNamed(workflow, alias)
179
+ if (!stage) return refuse(`"${workflow.alias}" has no stage "${alias}"`)
180
+ if (workflow.stages.length === 1)
181
+ return refuse('a workflow keeps at least one stage: remove the workflow instead')
182
+ const sendingBack = workflow.stages.filter((s) => s.sendBack === stage.alias).map((s) => s.alias)
183
+ if (sendingBack.length > 0)
184
+ return refuse(
185
+ `${sendingBack.map((s) => `"${s}"`).join(', ')} send${sendingBack.length === 1 ? 's' : ''} back to "${stage.alias}"; change where ${sendingBack.length === 1 ? 'it sends' : 'they send'} back first`,
186
+ )
187
+ return {
188
+ ok: true,
189
+ workflow: {
190
+ ...workflow,
191
+ stages: workflow.stages
192
+ .filter((s) => s !== stage)
193
+ .map((s) =>
194
+ s.next.includes(stage.alias)
195
+ ? { ...s, next: added(without(s.next, [stage.alias]), stage.next) }
196
+ : s,
197
+ ),
198
+ },
199
+ }
200
+ }