@elementumai/edk 0.12.0 → 0.13.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.
Files changed (193) hide show
  1. package/README.md +2 -0
  2. package/dist-sdk/src/catalog/factories.d.ts +7 -1
  3. package/dist-sdk/src/catalog/factories.js +2 -0
  4. package/dist-sdk/src/catalog/index.d.ts +2 -2
  5. package/dist-sdk/src/catalog/index.js +1 -1
  6. package/dist-sdk/src/catalog/org.js +79 -34
  7. package/dist-sdk/src/catalog/token.js +5 -0
  8. package/dist-sdk/src/catalog/types.d.ts +7 -1
  9. package/dist-sdk/src/catalog/types.js +3 -0
  10. package/dist-sdk/src/core/system-fields.d.ts +1 -1
  11. package/dist-sdk/src/core/system-fields.js +1 -1
  12. package/dist-sdk/src/core/types/app.d.ts +6 -0
  13. package/dist-sdk/src/engine/build/context.d.ts +3 -0
  14. package/dist-sdk/src/engine/pull/authored-address.js +42 -6
  15. package/dist-sdk/src/engine/pull/authored-root.d.ts +88 -0
  16. package/dist-sdk/src/engine/pull/authored-root.js +306 -0
  17. package/dist-sdk/src/engine/pull/context.d.ts +10 -0
  18. package/dist-sdk/src/engine/pull/field-refs-registry.d.ts +114 -0
  19. package/dist-sdk/src/engine/pull/field-refs-registry.js +501 -0
  20. package/dist-sdk/src/engine/pull/org.d.ts +1 -0
  21. package/dist-sdk/src/engine/pull/org.js +2 -0
  22. package/dist-sdk/src/engine/pull/partition.d.ts +8 -0
  23. package/dist-sdk/src/engine/pull/partition.js +43 -0
  24. package/dist-sdk/src/engine/pull/pulled-roots.d.ts +89 -0
  25. package/dist-sdk/src/engine/pull/pulled-roots.js +203 -0
  26. package/dist-sdk/src/engine/pull/ref-only.d.ts +4 -0
  27. package/dist-sdk/src/engine/pull/ref-only.js +7 -1
  28. package/dist-sdk/src/engine/pull/source-resources.d.ts +39 -0
  29. package/dist-sdk/src/engine/pull/source-resources.js +110 -0
  30. package/dist-sdk/src/engine/pull/stream-root.d.ts +148 -0
  31. package/dist-sdk/src/engine/pull/stream-root.js +563 -0
  32. package/dist-sdk/src/engine/workspace/reserved-catalog-keys.d.ts +47 -0
  33. package/dist-sdk/src/engine/workspace/reserved-catalog-keys.js +115 -0
  34. package/dist-sdk/src/entities/access-policy/build.d.ts +2 -1
  35. package/dist-sdk/src/entities/access-policy/from-tf.d.ts +7 -0
  36. package/dist-sdk/src/entities/access-policy/from-tf.js +18 -3
  37. package/dist-sdk/src/entities/access-policy/to-tf.d.ts +19 -0
  38. package/dist-sdk/src/entities/access-policy/to-tf.js +65 -8
  39. package/dist-sdk/src/entities/agent/build.d.ts +52 -1
  40. package/dist-sdk/src/entities/agent/build.js +34 -0
  41. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +68 -0
  42. package/dist-sdk/src/entities/agent/from-tf.d.ts +34 -1
  43. package/dist-sdk/src/entities/agent/from-tf.js +262 -2
  44. package/dist-sdk/src/entities/agent/index.d.ts +1 -1
  45. package/dist-sdk/src/entities/agent/to-tf.d.ts +8 -10
  46. package/dist-sdk/src/entities/agent/to-tf.js +158 -14
  47. package/dist-sdk/src/entities/agent-tool/from-tf.js +6 -0
  48. package/dist-sdk/src/entities/agent-tool/to-tf.d.ts +2 -2
  49. package/dist-sdk/src/entities/agent-tool/to-tf.js +2 -2
  50. package/dist-sdk/src/entities/app/build.d.ts +33 -13
  51. package/dist-sdk/src/entities/app/build.js +14 -12
  52. package/dist-sdk/src/entities/app/from-tf.js +24 -9
  53. package/dist-sdk/src/entities/app/index.d.ts +1 -0
  54. package/dist-sdk/src/entities/app/index.js +1 -0
  55. package/dist-sdk/src/entities/app/module.js +1 -0
  56. package/dist-sdk/src/entities/app/stage-keys.d.ts +2 -2
  57. package/dist-sdk/src/entities/app/stage-keys.js +3 -3
  58. package/dist-sdk/src/entities/app/to-tf.js +1 -0
  59. package/dist-sdk/src/entities/approval-chain/build.d.ts +46 -11
  60. package/dist-sdk/src/entities/approval-chain/build.js +120 -8
  61. package/dist-sdk/src/entities/approval-chain/from-tf.d.ts +24 -2
  62. package/dist-sdk/src/entities/approval-chain/from-tf.js +148 -6
  63. package/dist-sdk/src/entities/approval-chain/index.d.ts +4 -1
  64. package/dist-sdk/src/entities/approval-chain/index.js +4 -1
  65. package/dist-sdk/src/entities/approval-chain/module.js +23 -5
  66. package/dist-sdk/src/entities/approval-chain/printer.d.ts +1 -1
  67. package/dist-sdk/src/entities/approval-chain/printer.js +91 -5
  68. package/dist-sdk/src/entities/approval-chain/steps-tf.d.ts +77 -0
  69. package/dist-sdk/src/entities/approval-chain/steps-tf.js +255 -0
  70. package/dist-sdk/src/entities/approval-chain/steps.d.ts +71 -0
  71. package/dist-sdk/src/entities/approval-chain/steps.js +83 -0
  72. package/dist-sdk/src/entities/approval-chain/to-tf.d.ts +16 -6
  73. package/dist-sdk/src/entities/approval-chain/to-tf.js +37 -4
  74. package/dist-sdk/src/entities/automation/build.js +70 -27
  75. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +20 -5
  76. package/dist-sdk/src/entities/automation/from-tf.d.ts +26 -5
  77. package/dist-sdk/src/entities/automation/from-tf.js +593 -101
  78. package/dist-sdk/src/entities/automation/index.d.ts +1 -0
  79. package/dist-sdk/src/entities/automation/index.js +1 -0
  80. package/dist-sdk/src/entities/automation/module.d.ts +4 -3
  81. package/dist-sdk/src/entities/automation/module.js +33 -12
  82. package/dist-sdk/src/entities/automation/refs-read.d.ts +19 -0
  83. package/dist-sdk/src/entities/automation/refs-read.js +48 -0
  84. package/dist-sdk/src/entities/automation/slack-message-refs.d.ts +9 -0
  85. package/dist-sdk/src/entities/automation/slack-message-refs.js +75 -0
  86. package/dist-sdk/src/entities/automation/task-output-labels.d.ts +11 -0
  87. package/dist-sdk/src/entities/automation/task-output-labels.js +23 -0
  88. package/dist-sdk/src/entities/automation/tasks.d.ts +29 -4
  89. package/dist-sdk/src/entities/automation/tasks.js +34 -11
  90. package/dist-sdk/src/entities/automation/to-tf.d.ts +3 -0
  91. package/dist-sdk/src/entities/automation/to-tf.js +337 -77
  92. package/dist-sdk/src/entities/automation/triggers.d.ts +0 -8
  93. package/dist-sdk/src/entities/automation/triggers.js +26 -11
  94. package/dist-sdk/src/entities/automation/unsafe-script-output.d.ts +50 -0
  95. package/dist-sdk/src/entities/automation/unsafe-script-output.js +92 -0
  96. package/dist-sdk/src/entities/chart/build.d.ts +9 -1
  97. package/dist-sdk/src/entities/chart/from-tf.js +3 -0
  98. package/dist-sdk/src/entities/chart/to-tf.js +19 -2
  99. package/dist-sdk/src/entities/dynamic-layout/build.d.ts +2 -0
  100. package/dist-sdk/src/entities/dynamic-layout/from-tf.js +44 -5
  101. package/dist-sdk/src/entities/dynamic-layout/to-tf.js +46 -12
  102. package/dist-sdk/src/entities/element/audit-field-guard.d.ts +9 -0
  103. package/dist-sdk/src/entities/element/audit-field-guard.js +88 -0
  104. package/dist-sdk/src/entities/element/build.js +2 -1
  105. package/dist-sdk/src/entities/element/from-tf.js +15 -2
  106. package/dist-sdk/src/entities/element/module.js +3 -0
  107. package/dist-sdk/src/entities/element/system-fields.d.ts +13 -0
  108. package/dist-sdk/src/entities/element/system-fields.js +23 -0
  109. package/dist-sdk/src/entities/element/to-tf.js +1 -0
  110. package/dist-sdk/src/entities/field/build.d.ts +9 -0
  111. package/dist-sdk/src/entities/field/from-tf.d.ts +29 -1
  112. package/dist-sdk/src/entities/field/from-tf.js +99 -6
  113. package/dist-sdk/src/entities/field/module.js +4 -0
  114. package/dist-sdk/src/entities/field/to-tf.d.ts +5 -1
  115. package/dist-sdk/src/entities/field/to-tf.js +33 -12
  116. package/dist-sdk/src/entities/org/category.js +1 -0
  117. package/dist-sdk/src/entities/org/index.d.ts +1 -0
  118. package/dist-sdk/src/entities/org/index.js +1 -0
  119. package/dist-sdk/src/entities/org/voice-auth.d.ts +5 -0
  120. package/dist-sdk/src/entities/org/voice-auth.js +33 -0
  121. package/dist-sdk/src/entities/organization/build.d.ts +3 -0
  122. package/dist-sdk/src/entities/organization/build.js +2 -0
  123. package/dist-sdk/src/entities/organization/index.d.ts +1 -0
  124. package/dist-sdk/src/entities/organization/index.js +1 -0
  125. package/dist-sdk/src/entities/organization/to-tf.d.ts +2 -0
  126. package/dist-sdk/src/entities/organization/to-tf.js +59 -1
  127. package/dist-sdk/src/entities/organization/voice-auth.d.ts +60 -0
  128. package/dist-sdk/src/entities/organization/voice-auth.js +60 -0
  129. package/dist-sdk/src/entities/phone-service/build.d.ts +9 -1
  130. package/dist-sdk/src/entities/phone-service/build.js +1 -0
  131. package/dist-sdk/src/entities/phone-service/deserializer/print-phone-service.js +3 -0
  132. package/dist-sdk/src/entities/phone-service/from-tf.d.ts +1 -0
  133. package/dist-sdk/src/entities/phone-service/from-tf.js +8 -1
  134. package/dist-sdk/src/entities/phone-service/to-tf.js +16 -0
  135. package/dist-sdk/src/entities/record-create-form/from-tf.js +2 -2
  136. package/dist-sdk/src/entities/relationship/from-tf.js +7 -2
  137. package/dist-sdk/src/entities/relationship/module.js +2 -2
  138. package/dist-sdk/src/entities/relationship/pair-check.d.ts +6 -2
  139. package/dist-sdk/src/entities/relationship/pair-check.js +105 -18
  140. package/dist-sdk/src/entities/relationship/to-tf.js +5 -2
  141. package/dist-sdk/src/entities/role/permission-levels.d.ts +7 -0
  142. package/dist-sdk/src/entities/role/permission-levels.js +78 -0
  143. package/dist-sdk/src/entities/role/to-tf.js +32 -2
  144. package/dist-sdk/src/entities/service-account/module.d.ts +2 -1
  145. package/dist-sdk/src/entities/service-account/module.js +4 -1
  146. package/dist-sdk/src/entities/service-account/resource-type.d.ts +2 -0
  147. package/dist-sdk/src/entities/service-account/resource-type.js +2 -0
  148. package/dist-sdk/src/entities/service-account/role-membership-check.d.ts +20 -0
  149. package/dist-sdk/src/entities/service-account/role-membership-check.js +80 -0
  150. package/dist-sdk/src/entities/shared/aspect-target-from-tf.d.ts +15 -0
  151. package/dist-sdk/src/entities/shared/aspect-target-from-tf.js +36 -0
  152. package/dist-sdk/src/entities/shared/filters/ast.d.ts +2 -0
  153. package/dist-sdk/src/entities/shared/filters/index.d.ts +85 -14
  154. package/dist-sdk/src/entities/shared/filters/index.js +18 -6
  155. package/dist-sdk/src/entities/shared/filters/option-lookups.d.ts +57 -0
  156. package/dist-sdk/src/entities/shared/filters/option-lookups.js +108 -0
  157. package/dist-sdk/src/entities/shared/filters/print.js +9 -5
  158. package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +12 -7
  159. package/dist-sdk/src/entities/shared/filters/tf-codec.js +9 -5
  160. package/dist-sdk/src/entities/shared/raw-option-id.d.ts +30 -0
  161. package/dist-sdk/src/entities/shared/raw-option-id.js +41 -0
  162. package/dist-sdk/src/entities/shared/tool-field-ref.js +6 -2
  163. package/dist-sdk/src/entities/skill/input-coverage.js +5 -2
  164. package/dist-sdk/src/entities/sla/build.d.ts +96 -0
  165. package/dist-sdk/src/entities/sla/build.js +124 -0
  166. package/dist-sdk/src/entities/sla/from-tf.d.ts +40 -0
  167. package/dist-sdk/src/entities/sla/from-tf.js +164 -0
  168. package/dist-sdk/src/entities/sla/index.d.ts +1 -0
  169. package/dist-sdk/src/entities/sla/index.js +1 -0
  170. package/dist-sdk/src/entities/sla/module.d.ts +4 -0
  171. package/dist-sdk/src/entities/sla/module.js +73 -0
  172. package/dist-sdk/src/entities/sla/printer.d.ts +2 -0
  173. package/dist-sdk/src/entities/sla/printer.js +84 -0
  174. package/dist-sdk/src/entities/sla/to-tf.d.ts +12 -0
  175. package/dist-sdk/src/entities/sla/to-tf.js +81 -0
  176. package/dist-sdk/src/entities/table/build.d.ts +7 -2
  177. package/dist-sdk/src/entities/table/build.js +22 -1
  178. package/dist-sdk/src/entities/table/deserializer/normalize.d.ts +4 -1
  179. package/dist-sdk/src/entities/table/from-tf.js +89 -14
  180. package/dist-sdk/src/entities/table/index.d.ts +1 -1
  181. package/dist-sdk/src/entities/table/to-tf.js +41 -2
  182. package/dist-sdk/src/entities/task/from-tf.js +2 -1
  183. package/dist-sdk/src/entities/view/from-tf.js +7 -4
  184. package/dist-sdk/src/entities/widget/from-tf.js +13 -2
  185. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.d.ts +49 -4
  186. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.js +2 -2
  187. package/dist-sdk/src/graph/index.d.ts +5 -0
  188. package/dist-sdk/src/graph/index.js +7 -0
  189. package/dist-sdk/src/graph/modules.js +11 -1
  190. package/dist-sdk/src/graph/resource-types.d.ts +47 -0
  191. package/dist-sdk/src/graph/resource-types.js +62 -0
  192. package/dist-sdk/src/graph/types.d.ts +78 -6
  193. package/package.json +6 -3
@@ -0,0 +1,255 @@
1
+ import { EntityTfError } from "../../engine/entity.js";
2
+ import { dataRefLabel, groupCatalogKey } from "../../engine/pull/context.js";
3
+ import { expr, sanitizeName, tfLabelToTsLabel } from "../../engine/tf/index.js";
4
+ import { filterFromTf } from "../access-policy/from-tf.js";
5
+ import { filterToTf } from "../access-policy/to-tf.js";
6
+ import { printFilter } from "../shared/filters/print.js";
7
+ import { APP_RESOURCE_TYPE } from "../app/module.js";
8
+ import { ELEMENT_RESOURCE_TYPE } from "../element/module.js";
9
+ function asWire(value, where) {
10
+ if (value === null || typeof value !== "object" || Array.isArray(value))
11
+ throw new EntityTfError(`${where}: not an approver step`);
12
+ return value;
13
+ }
14
+ function count(value, where) {
15
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 0)
16
+ throw new EntityTfError(`${where}: must be an integer >= 0`);
17
+ return value;
18
+ }
19
+ function countsToTf(wire, where) {
20
+ return {
21
+ approvals_required: count(wire["approvalsRequired"], `${where}.approvalsRequired`),
22
+ denials_required: count(wire["denialsRequired"], `${where}.denialsRequired`),
23
+ };
24
+ }
25
+ function filterValue(filter, where, ctx) {
26
+ return filterToTf(filter, where, {
27
+ ownerTfLabel: ctx.ownerTfLabel,
28
+ appRefName: ctx.appRefName,
29
+ ownerRefName: ctx.appRefName,
30
+ scope: ctx.scope,
31
+ objectId: expr.resourceAttr(ctx.scope === "element" ? ELEMENT_RESOURCE_TYPE : APP_RESOURCE_TYPE, ctx.ownerTfLabel, ["id"]),
32
+ diagnostics: ctx.diagnostics,
33
+ resolvePicklistLiterals: false,
34
+ ...(ctx.fieldAddresses !== undefined ? { fieldAddresses: ctx.fieldAddresses } : {}),
35
+ });
36
+ }
37
+ function userId(wire, where, ctx) {
38
+ const user = wire["user"];
39
+ if (user?.__kind !== "user" || typeof user.email !== "string" || !user.email.includes("@"))
40
+ throw new EntityTfError(`${where}: user must be userRef(email)`);
41
+ // The whole email names the lookup, as field allow-lists and ei export
42
+ // do, so two users sharing a local part never share one data address.
43
+ const label = sanitizeName(user.email);
44
+ if (label === "")
45
+ throw new EntityTfError(`${where}: user email ${JSON.stringify(user.email)} has no usable lookup label`);
46
+ ctx.orgRefs.rawLookup("elementum_user", label, user.email);
47
+ return expr.dataAttr("elementum_user", label, ["id"]);
48
+ }
49
+ function groupId(wire, where, ctx) {
50
+ const group = wire["group"];
51
+ if (group?.__kind !== "group" || typeof group.refName !== "string")
52
+ throw new EntityTfError(`${where}: group must be an org.groups token`);
53
+ const resolved = ctx.orgRefs.byTsLabel("groups", group.refName);
54
+ // A step cannot be dropped: the provider would delete it. Fail the build.
55
+ if (resolved === undefined)
56
+ throw new EntityTfError(`${where}: group token ${JSON.stringify(group.refName)} is not in org.ts`);
57
+ return expr.dataAttr("elementum_group", resolved.tfLabel, ["id"]);
58
+ }
59
+ function approverToTf(wire, where, ctx, conditional) {
60
+ switch (wire["kind"]) {
61
+ case "user":
62
+ return { user: { user_id: userId(wire, where, ctx) } };
63
+ case "group":
64
+ return { group: { group_id: groupId(wire, where, ctx), ...countsToTf(wire, where) } };
65
+ case "dynamicEmployee": {
66
+ const auto = wire["autoApproveOnMissing"];
67
+ if (typeof auto !== "boolean")
68
+ throw new EntityTfError(`${where}.autoApproveOnMissing: must be a boolean`);
69
+ return { dynamic_employee: { degrees_of_separation: count(wire["degreesOfSeparation"], `${where}.degreesOfSeparation`), auto_approve_on_missing: auto } };
70
+ }
71
+ case "resolvedQuery":
72
+ return {
73
+ resolved_query: {
74
+ ...(wire["filter"] !== undefined ? { filter: filterValue(wire["filter"], `${where}.filter`, ctx) } : {}),
75
+ ...countsToTf(wire, where),
76
+ },
77
+ };
78
+ case "runtimeUser":
79
+ if (!conditional)
80
+ throw new EntityTfError(`${where}: a runtime approver can only be a priorityGroup conditional`);
81
+ return { user: { supplied_at_runtime: true } };
82
+ case "runtimeGroup":
83
+ if (!conditional)
84
+ throw new EntityTfError(`${where}: a runtime approver can only be a priorityGroup conditional`);
85
+ return { group: { supplied_at_runtime: true, ...countsToTf(wire, where) } };
86
+ }
87
+ throw new EntityTfError(`${where}: unknown approver kind ${JSON.stringify(wire["kind"])}`);
88
+ }
89
+ /** The `approvers` list for authored steps. Any step that cannot be built fails the build. */
90
+ export function approvalStepsToTf(steps, where, ctx) {
91
+ return steps.map((raw, index) => {
92
+ const at = `${where}[${index}]`;
93
+ const wire = asWire(raw, at);
94
+ if (wire["kind"] !== "priorityGroup")
95
+ return approverToTf(wire, at, ctx, false);
96
+ const threshold = wire["threshold"];
97
+ const conditionals = wire["conditionals"];
98
+ if (!Array.isArray(conditionals) || conditionals.length === 0)
99
+ throw new EntityTfError(`${at}: a priorityGroup needs conditionals`);
100
+ return {
101
+ priority_group: {
102
+ ...(threshold !== undefined ? { threshold: countsToTf(asWire(threshold, `${at}.threshold`), `${at}.threshold`) } : {}),
103
+ conditionals: conditionals.map((rawConditional, k) => {
104
+ const cat = `${at}.conditionals[${k}]`;
105
+ const conditional = asWire(rawConditional, cat);
106
+ return {
107
+ ...(conditional["when"] !== undefined ? { conditions: filterValue(conditional["when"], `${cat}.when`, ctx) } : {}),
108
+ approver: approverToTf(asWire(conditional["approver"], `${cat}.approver`), `${cat}.approver`, ctx, true),
109
+ };
110
+ }),
111
+ },
112
+ };
113
+ });
114
+ }
115
+ function obj(value, where) {
116
+ if (value === null || value === undefined || typeof value !== "object" || Array.isArray(value))
117
+ throw new EntityTfError(`${where}: expected an object`);
118
+ if ("$expr" in value)
119
+ throw new EntityTfError(`${where}: expected an object`);
120
+ return value;
121
+ }
122
+ function pulledCount(attrs, key, where) {
123
+ const value = attrs[key];
124
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 0)
125
+ throw new EntityTfError(`${where}.${key}: must be a literal integer >= 0`);
126
+ return value;
127
+ }
128
+ function pulledCounts(attrs, where) {
129
+ return { approvalsRequired: pulledCount(attrs, "approvals_required", where), denialsRequired: pulledCount(attrs, "denials_required", where) };
130
+ }
131
+ function only(attrs, allowed, where) {
132
+ for (const key of Object.keys(attrs))
133
+ if (!allowed.includes(key))
134
+ throw new EntityTfError(`${where}: unknown attribute ${key}`);
135
+ }
136
+ // A filter field that is not a field of the owner (unresolved, or another
137
+ // kind of left side) cannot be authored. Throwing here leaves the whole step
138
+ // list out, unmanaged, with a diagnostic, never a placeholder in source.
139
+ function requireAuthorableFilter(filter, where) {
140
+ const check = (ref) => {
141
+ if (ref.kind === "field")
142
+ return "";
143
+ if (ref.kind === "unresolved")
144
+ throw new EntityTfError(`${where}: filter field ${JSON.stringify(ref.uuid)} is not a field this pull can name`);
145
+ throw new EntityTfError(`${where}: filter left-hand side kind ${JSON.stringify(ref.kind)} cannot be authored`);
146
+ };
147
+ printFilter(filter, { field: check, reference: check });
148
+ return filter;
149
+ }
150
+ function pulledFilter(value, where, owner, ctx) {
151
+ return requireAuthorableFilter(pulledFilterUnchecked(value, where, owner, ctx), where);
152
+ }
153
+ function pulledFilterUnchecked(value, where, owner, ctx) {
154
+ return filterFromTf(value, {
155
+ type: owner.scope === "element" ? "elementum_element" : "elementum_app",
156
+ label: owner.appTfLabel,
157
+ ownerRefName: tfLabelToTsLabel(owner.appTfLabel),
158
+ ownerTfLabel: owner.appTfLabel,
159
+ scope: owner.scope,
160
+ foreignFieldRefsById: ctx.foreignFieldRefsById,
161
+ }, where, ctx, { optionLookupsAsTextOption: true });
162
+ }
163
+ function pulledUser(value, where, ctx) {
164
+ const label = dataRefLabel(value, "elementum_user");
165
+ const email = label === undefined ? undefined : ctx.dataName("elementum_user", label);
166
+ if (email === undefined || !email.includes("@"))
167
+ throw new EntityTfError(`${where}: user_id is not a data.elementum_user lookup by email`);
168
+ return { __kind: "user", email: email.trim() };
169
+ }
170
+ function pulledGroup(value, where, ctx) {
171
+ const label = dataRefLabel(value, "elementum_group");
172
+ const name = label === undefined ? undefined : ctx.dataName("elementum_group", label);
173
+ if (label === undefined || name === undefined || name === "")
174
+ throw new EntityTfError(`${where}: group_id is not a data.elementum_group lookup by name`);
175
+ return { __kind: "group", refName: groupCatalogKey(ctx, label, name), name };
176
+ }
177
+ function pulledApprover(attrs, where, owner, ctx, conditional) {
178
+ const keys = Object.keys(attrs).filter((key) => attrs[key] !== null && attrs[key] !== undefined);
179
+ if (keys.length !== 1)
180
+ throw new EntityTfError(`${where}: must set exactly one approver`);
181
+ const key = keys[0];
182
+ const inner = obj(attrs[key], `${where}.${key}`);
183
+ const at = `${where}.${key}`;
184
+ switch (key) {
185
+ case "user":
186
+ if (inner["supplied_at_runtime"] === true && conditional) {
187
+ only(inner, ["supplied_at_runtime"], at);
188
+ return { kind: "runtimeUser" };
189
+ }
190
+ only(inner, ["user_id"], at);
191
+ return { kind: "user", user: pulledUser(inner["user_id"], at, ctx) };
192
+ case "group":
193
+ if (inner["supplied_at_runtime"] === true && conditional) {
194
+ only(inner, ["supplied_at_runtime", "approvals_required", "denials_required"], at);
195
+ return { kind: "runtimeGroup", ...pulledCounts(inner, at) };
196
+ }
197
+ only(inner, ["group_id", "approvals_required", "denials_required"], at);
198
+ return { kind: "group", group: pulledGroup(inner["group_id"], at, ctx), ...pulledCounts(inner, at) };
199
+ case "dynamic_employee": {
200
+ only(inner, ["degrees_of_separation", "auto_approve_on_missing"], at);
201
+ const auto = inner["auto_approve_on_missing"];
202
+ if (typeof auto !== "boolean")
203
+ throw new EntityTfError(`${at}.auto_approve_on_missing: must be a literal boolean`);
204
+ return { kind: "dynamicEmployee", degreesOfSeparation: pulledCount(inner, "degrees_of_separation", at), autoApproveOnMissing: auto };
205
+ }
206
+ case "resolved_query":
207
+ only(inner, ["filter", "approvals_required", "denials_required"], at);
208
+ return {
209
+ kind: "resolvedQuery",
210
+ ...(inner["filter"] !== undefined && inner["filter"] !== null ? { filter: pulledFilter(inner["filter"], `${at}.filter`, owner, ctx) } : {}),
211
+ ...pulledCounts(inner, at),
212
+ };
213
+ }
214
+ throw new EntityTfError(`${where}: unknown approver ${key}`);
215
+ }
216
+ /**
217
+ * The authored steps of a pulled `approvers` list. Throws when any step
218
+ * cannot be authored: the caller then leaves every step out (unmanaged)
219
+ * rather than author a list the provider would reconcile by deleting.
220
+ */
221
+ export function approvalStepsFromTf(value, where, owner, ctx) {
222
+ if (!Array.isArray(value))
223
+ throw new EntityTfError(`${where}: approvers must be a list`);
224
+ return value.map((raw, index) => {
225
+ const at = `${where}[${index}]`;
226
+ const attrs = obj(raw, at);
227
+ if (attrs["priority_group"] === undefined || attrs["priority_group"] === null) {
228
+ const approver = pulledApprover(attrs, at, owner, ctx, false);
229
+ if (approver.kind === "runtimeUser" || approver.kind === "runtimeGroup")
230
+ throw new EntityTfError(`${at}: a runtime approver cannot be a step`);
231
+ return approver;
232
+ }
233
+ if (Object.keys(attrs).length !== 1)
234
+ throw new EntityTfError(`${at}: must set exactly one approver`);
235
+ const group = obj(attrs["priority_group"], `${at}.priority_group`);
236
+ only(group, ["threshold", "conditionals"], `${at}.priority_group`);
237
+ const conditionals = group["conditionals"];
238
+ if (!Array.isArray(conditionals) || conditionals.length === 0)
239
+ throw new EntityTfError(`${at}.priority_group.conditionals: must be a non-empty list`);
240
+ return {
241
+ kind: "priorityGroup",
242
+ ...(group["threshold"] !== undefined && group["threshold"] !== null ? { threshold: pulledCounts(obj(group["threshold"], `${at}.priority_group.threshold`), `${at}.priority_group.threshold`) } : {}),
243
+ conditionals: conditionals.map((rawConditional, k) => {
244
+ const cat = `${at}.priority_group.conditionals[${k}]`;
245
+ const conditional = obj(rawConditional, cat);
246
+ only(conditional, ["conditions", "approver"], cat);
247
+ const when = conditional["conditions"];
248
+ return {
249
+ ...(when !== undefined && when !== null ? { when: pulledFilter(when, `${cat}.conditions`, owner, ctx) } : {}),
250
+ approver: pulledApprover(obj(conditional["approver"], `${cat}.approver`), `${cat}.approver`, owner, ctx, true),
251
+ };
252
+ }),
253
+ };
254
+ });
255
+ }
@@ -0,0 +1,71 @@
1
+ import type { GroupRef } from "../../catalog/index.js";
2
+ import type { Filter } from "../shared/filters/types.js";
3
+ import type { RoleUserRef } from "../role/build.js";
4
+ export interface ApprovalCounts {
5
+ readonly approvalsRequired: number;
6
+ readonly denialsRequired: number;
7
+ }
8
+ export interface ApprovalUserStep {
9
+ readonly kind: "user";
10
+ readonly user: RoleUserRef;
11
+ }
12
+ export interface ApprovalGroupStep extends ApprovalCounts {
13
+ readonly kind: "group";
14
+ readonly group: GroupRef;
15
+ }
16
+ export interface ApprovalDynamicEmployeeStep {
17
+ readonly kind: "dynamicEmployee";
18
+ /** Levels above the requester (1 is the direct manager). */
19
+ readonly degreesOfSeparation: number;
20
+ /** Approve the step when the requester has no manager at that level. */
21
+ readonly autoApproveOnMissing: boolean;
22
+ }
23
+ export interface ApprovalResolvedQueryStep extends ApprovalCounts {
24
+ readonly kind: "resolvedQuery";
25
+ /** The users the filter resolves approve. */
26
+ readonly filter?: Filter;
27
+ }
28
+ /** A user supplied when the approval starts; priority-group conditionals only. */
29
+ export interface ApprovalRuntimeUser {
30
+ readonly kind: "runtimeUser";
31
+ }
32
+ /** A group supplied when the approval starts; priority-group conditionals only. */
33
+ export interface ApprovalRuntimeGroup extends ApprovalCounts {
34
+ readonly kind: "runtimeGroup";
35
+ }
36
+ export type ApprovalConditionalApprover = ApprovalUserStep | ApprovalGroupStep | ApprovalDynamicEmployeeStep | ApprovalResolvedQueryStep | ApprovalRuntimeUser | ApprovalRuntimeGroup;
37
+ export interface ApprovalConditional {
38
+ /** The record conditions under which this approver applies; omit to always apply. */
39
+ readonly when?: Filter;
40
+ readonly approver: ApprovalConditionalApprover;
41
+ }
42
+ export interface ApprovalPriorityGroup {
43
+ readonly kind: "priorityGroup";
44
+ readonly threshold?: ApprovalCounts;
45
+ readonly conditionals: readonly ApprovalConditional[];
46
+ }
47
+ export type ApprovalStep = ApprovalUserStep | ApprovalGroupStep | ApprovalDynamicEmployeeStep | ApprovalResolvedQueryStep | ApprovalPriorityGroup;
48
+ /** A fixed user approves: `userStep(userRef("pat@example.com"))`. */
49
+ export declare function userStep(user: RoleUserRef): ApprovalUserStep;
50
+ /** A fixed group approves: `groupStep(org.groups.finance, { approvalsRequired: 1, denialsRequired: 1 })`. */
51
+ export declare function groupStep(group: GroupRef, options: ApprovalCounts): ApprovalGroupStep;
52
+ /** The requester's manager chain approves. */
53
+ export declare function dynamicEmployeeStep(options: {
54
+ readonly degreesOfSeparation: number;
55
+ readonly autoApproveOnMissing: boolean;
56
+ }): ApprovalDynamicEmployeeStep;
57
+ /** The users a filter resolves approve. */
58
+ export declare function resolvedQueryStep(options: ApprovalCounts & {
59
+ readonly filter?: Filter;
60
+ }): ApprovalResolvedQueryStep;
61
+ /** A user supplied when the approval starts. Only valid as a priority-group conditional approver. */
62
+ export declare function runtimeUser(): ApprovalRuntimeUser;
63
+ /** A group supplied when the approval starts. Only valid as a priority-group conditional approver. */
64
+ export declare function runtimeGroup(options: ApprovalCounts): ApprovalRuntimeGroup;
65
+ /** Conditional approvers; needs `dynamicChain: true` on the approval process. */
66
+ export declare function priorityGroup(options: {
67
+ readonly threshold?: ApprovalCounts;
68
+ readonly conditionals: readonly ApprovalConditional[];
69
+ }): ApprovalPriorityGroup;
70
+ /** Checks a step list: no runtime approver as a step, priority groups only on a dynamic chain. */
71
+ export declare function validateApprovalSteps(steps: readonly ApprovalStep[], dynamicChain: boolean): void;
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Approver steps of an approval process (edk#373), in order. Users are named
3
+ * by email (`userRef(...)`) and groups by their `org.groups` token, so the
4
+ * source carries no platform ids; build turns them into `data.elementum_user`
5
+ * and `data.elementum_group` lookups.
6
+ *
7
+ * A runtime approver (`runtimeUser()`, `runtimeGroup(...)`), supplied when the
8
+ * approval starts, is only valid as a priority-group conditional, and
9
+ * priority groups only on a dynamic chain (`dynamicChain: true`).
10
+ */
11
+ import { EdkValidationError } from "../../../elementum-entity-client/errors.js";
12
+ function counts(where, options) {
13
+ const { approvalsRequired, denialsRequired } = options;
14
+ for (const [name, value] of [["approvalsRequired", approvalsRequired], ["denialsRequired", denialsRequired]]) {
15
+ if (!Number.isInteger(value) || value < 0) {
16
+ throw new EdkValidationError(`${where}: ${name} must be an integer >= 0 (got ${String(value)}).`);
17
+ }
18
+ }
19
+ return { approvalsRequired, denialsRequired };
20
+ }
21
+ /** A fixed user approves: `userStep(userRef("pat@example.com"))`. */
22
+ export function userStep(user) {
23
+ if (user?.__kind !== "user" || typeof user.email !== "string" || !user.email.includes("@")) {
24
+ throw new EdkValidationError("userStep requires userRef(email).");
25
+ }
26
+ return { kind: "user", user };
27
+ }
28
+ /** A fixed group approves: `groupStep(org.groups.finance, { approvalsRequired: 1, denialsRequired: 1 })`. */
29
+ export function groupStep(group, options) {
30
+ if (group?.__kind !== "group")
31
+ throw new EdkValidationError("groupStep requires an org.groups token.");
32
+ return { kind: "group", group, ...counts("groupStep", options) };
33
+ }
34
+ /** The requester's manager chain approves. */
35
+ export function dynamicEmployeeStep(options) {
36
+ if (!Number.isInteger(options.degreesOfSeparation) || options.degreesOfSeparation < 0) {
37
+ throw new EdkValidationError("dynamicEmployeeStep: degreesOfSeparation must be an integer >= 0.");
38
+ }
39
+ return { kind: "dynamicEmployee", degreesOfSeparation: options.degreesOfSeparation, autoApproveOnMissing: options.autoApproveOnMissing };
40
+ }
41
+ /** The users a filter resolves approve. */
42
+ export function resolvedQueryStep(options) {
43
+ return { kind: "resolvedQuery", ...counts("resolvedQueryStep", options), ...(options.filter !== undefined ? { filter: options.filter } : {}) };
44
+ }
45
+ /** A user supplied when the approval starts. Only valid as a priority-group conditional approver. */
46
+ export function runtimeUser() {
47
+ return { kind: "runtimeUser" };
48
+ }
49
+ /** A group supplied when the approval starts. Only valid as a priority-group conditional approver. */
50
+ export function runtimeGroup(options) {
51
+ return { kind: "runtimeGroup", ...counts("runtimeGroup", options) };
52
+ }
53
+ /** Conditional approvers; needs `dynamicChain: true` on the approval process. */
54
+ export function priorityGroup(options) {
55
+ if (options.conditionals.length === 0)
56
+ throw new EdkValidationError("priorityGroup needs at least one conditional.");
57
+ for (const conditional of options.conditionals) {
58
+ const kind = conditional?.approver?.kind;
59
+ if (!["user", "group", "dynamicEmployee", "resolvedQuery", "runtimeUser", "runtimeGroup"].includes(String(kind))) {
60
+ throw new EdkValidationError("priorityGroup conditionals need an approver: userStep, groupStep, dynamicEmployeeStep, resolvedQueryStep, runtimeUser or runtimeGroup.");
61
+ }
62
+ }
63
+ return {
64
+ kind: "priorityGroup",
65
+ ...(options.threshold !== undefined ? { threshold: counts("priorityGroup threshold", options.threshold) } : {}),
66
+ conditionals: options.conditionals,
67
+ };
68
+ }
69
+ /** Checks a step list: no runtime approver as a step, priority groups only on a dynamic chain. */
70
+ export function validateApprovalSteps(steps, dynamicChain) {
71
+ steps.forEach((step, index) => {
72
+ const kind = step?.kind;
73
+ if (kind === "runtimeUser" || kind === "runtimeGroup") {
74
+ throw new EdkValidationError(`approvalProcess steps[${index}]: a runtime approver cannot be a step; use it as a priorityGroup conditional.`);
75
+ }
76
+ if (kind === "priorityGroup" && !dynamicChain) {
77
+ throw new EdkValidationError(`approvalProcess steps[${index}]: priorityGroup needs dynamicChain: true.`);
78
+ }
79
+ if (!["user", "group", "dynamicEmployee", "resolvedQuery", "priorityGroup"].includes(String(kind))) {
80
+ throw new EdkValidationError(`approvalProcess steps[${index}]: not an approver step (use userStep, groupStep, dynamicEmployeeStep, resolvedQueryStep or priorityGroup).`);
81
+ }
82
+ });
83
+ }
@@ -1,10 +1,20 @@
1
1
  import { type DeployableEntity } from "../../engine/entity.js";
2
- import { type TfBlock } from "../../engine/tf/index.js";
3
- export declare function approvalChainToTf(entity: DeployableEntity, ctx: {
4
- appRefName: string;
5
- appTfLabel: string;
6
- stageKeyByTfLabel?: ReadonlyMap<string, string>;
7
- }): {
2
+ import { type ChildResourceScope, type TfBlock } from "../../engine/tf/index.js";
3
+ import type { OrgRefLookup } from "../../engine/build/context.js";
4
+ export interface ApprovalChainToTfContext {
5
+ readonly appRefName: string;
6
+ /** The owning app's or element's Terraform label. */
7
+ readonly appTfLabel: string;
8
+ /** The owner kind (edk#373); defaults to app for direct callers. */
9
+ readonly parentScope?: ChildResourceScope;
10
+ readonly stageKeyByTfLabel?: ReadonlyMap<string, string>;
11
+ /** Resolves user and group lookups for approver steps (edk#373). */
12
+ readonly orgRefs?: OrgRefLookup;
13
+ readonly diagnostics?: string[];
14
+ /** Authored custom field addresses so a locked system-keyed field prefers the custom resource (#1871). */
15
+ readonly fieldAddresses?: ReadonlyMap<string, string>;
16
+ }
17
+ export declare function approvalChainToTf(entity: DeployableEntity, ctx: ApprovalChainToTfContext): {
8
18
  blocks: readonly TfBlock[];
9
19
  address: string;
10
20
  };
@@ -1,18 +1,26 @@
1
1
  import { isUnsafePlatformId } from "../../core/platform-id.js";
2
2
  import { EntityTfError } from "../../engine/entity.js";
3
- import { appScopedResourceLabel, expr, tsLabelToTfLabel } from "../../engine/tf/index.js";
3
+ import { expr, scopedResourceLabel, tsLabelToTfLabel } from "../../engine/tf/index.js";
4
4
  import { APP_RESOURCE_TYPE } from "../app/module.js";
5
+ import { ELEMENT_RESOURCE_TYPE } from "../element/module.js";
5
6
  import { fieldRefToTfValue } from "../field/to-tf.js";
6
7
  import { APPROVAL_PROCESS_RESOURCE_TYPE } from "./module.js";
8
+ import { approvalStepsToTf } from "./steps-tf.js";
7
9
  export function approvalChainToTf(entity, ctx) {
8
10
  if (entity.kind !== "approvalChain")
9
11
  throw new EntityTfError(`approvalChainToTf expects approvalChain; got ${entity.kind}`);
10
12
  const payload = entity.payload;
11
13
  if (payload === null)
12
14
  throw new EntityTfError(`approval chain ${entity.refName} has no payload`);
13
- const label = appScopedResourceLabel(ctx.appTfLabel, tsLabelToTfLabel(entity.refName));
15
+ const scope = ctx.parentScope ?? "app";
16
+ if (scope === "element" && (payload.stage !== undefined || payload.stageId !== undefined)) {
17
+ throw new EntityTfError(`approval chain ${entity.refName}: an element-owned approval process takes no stage`);
18
+ }
19
+ const label = scopedResourceLabel(ctx.appTfLabel, tsLabelToTfLabel(entity.refName), scope);
14
20
  const attrs = {
15
- app_id: expr.resourceAttr(APP_RESOURCE_TYPE, ctx.appTfLabel, ["id"]),
21
+ ...(scope === "element"
22
+ ? { element_id: expr.resourceAttr(ELEMENT_RESOURCE_TYPE, ctx.appTfLabel, ["id"]) }
23
+ : { app_id: expr.resourceAttr(APP_RESOURCE_TYPE, ctx.appTfLabel, ["id"]) }),
16
24
  name: payload.name ?? "",
17
25
  number_of_dynamic_approvers: payload.numberOfDynamicApprovers ?? 0,
18
26
  managed_by_automation: payload.managedByAutomation ?? false,
@@ -35,12 +43,37 @@ export function approvalChainToTf(entity, ctx) {
35
43
  return field.value;
36
44
  if (field.parent !== undefined && field.parent !== "" && field.parent !== ctx.appRefName)
37
45
  throw new EntityTfError(`approval chain ${entity.refName}: locked field belongs to ${field.parent}`);
38
- const value = fieldRefToTfValue(field.fieldType ?? "", field.refName ?? "", ctx.appTfLabel);
46
+ const value = fieldRefToTfValue(field.fieldType ?? "", field.refName ?? "", ctx.appTfLabel, scope, ctx.fieldAddresses, field.resourceKey);
39
47
  if (value === undefined)
40
48
  throw new EntityTfError(`approval chain ${entity.refName}: locked field ${field.refName ?? ""} has no Terraform mapping`);
41
49
  return value;
42
50
  });
43
51
  }
52
+ if (payload.dynamicChain === true)
53
+ attrs["dynamic_chain"] = true;
54
+ else if (payload.dynamicChain === false)
55
+ attrs["dynamic_chain"] = false;
56
+ if (payload.steps !== undefined) {
57
+ if (ctx.orgRefs === undefined)
58
+ throw new EntityTfError(`approval chain ${entity.refName}: approver steps need org lookups`);
59
+ attrs["approvers"] = approvalStepsToTf(payload.steps, `${APPROVAL_PROCESS_RESOURCE_TYPE}.${label}.approvers`, {
60
+ orgRefs: ctx.orgRefs, ownerTfLabel: ctx.appTfLabel, appRefName: ctx.appRefName, scope,
61
+ diagnostics: ctx.diagnostics ?? [], ...(ctx.fieldAddresses !== undefined ? { fieldAddresses: ctx.fieldAddresses } : {}),
62
+ });
63
+ }
64
+ if (payload.tableFilterFieldIds !== undefined) {
65
+ attrs["table_filter_field_ids"] = payload.tableFilterFieldIds.map((field) => {
66
+ if (isUnsafePlatformId(field))
67
+ return field.value;
68
+ if (field.parent !== undefined && field.parent !== "" && field.parent !== ctx.appRefName) {
69
+ throw new EntityTfError(`approval chain ${entity.refName}: table filter column belongs to ${field.parent}`);
70
+ }
71
+ const value = fieldRefToTfValue(field.fieldType ?? "", field.refName ?? "", ctx.appTfLabel, scope, ctx.fieldAddresses, field.resourceKey);
72
+ if (value === undefined)
73
+ throw new EntityTfError(`approval chain ${entity.refName}: table filter column ${field.refName ?? ""} has no Terraform mapping`);
74
+ return value;
75
+ });
76
+ }
44
77
  const block = { kind: "resource", type: APPROVAL_PROCESS_RESOURCE_TYPE, label, attrs };
45
78
  return { blocks: [block], address: `${APPROVAL_PROCESS_RESOURCE_TYPE}.${label}` };
46
79
  }