business-as-code 2.1.3 → 2.3.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 (260) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +2 -0
  3. package/package.json +16 -13
  4. package/src/dollar.ts +5 -2
  5. package/src/entities/organization.ts +31 -18
  6. package/src/goals.ts +78 -12
  7. package/src/index.ts +48 -18
  8. package/src/kpis.ts +62 -8
  9. package/src/metrics.ts +92 -79
  10. package/src/okrs.ts +120 -20
  11. package/src/organization.ts +12 -15
  12. package/src/process.ts +11 -12
  13. package/src/product.ts +8 -9
  14. package/src/queries.ts +238 -75
  15. package/src/roles.ts +62 -61
  16. package/src/workflow.ts +22 -15
  17. package/test/business.test.ts +282 -0
  18. package/test/dollar.test.ts +270 -0
  19. package/test/entities.test.ts +628 -0
  20. package/test/financials.test.ts +539 -0
  21. package/test/goals.test.ts +451 -0
  22. package/{src → test}/index.test.ts +1 -1
  23. package/test/kpis.test.ts +440 -0
  24. package/test/metrics.test.ts +744 -0
  25. package/test/okrs.test.ts +741 -0
  26. package/test/organization.test.ts +548 -0
  27. package/test/process.test.ts +503 -0
  28. package/test/product.test.ts +430 -0
  29. package/test/queries.test.ts +556 -0
  30. package/test/roles.test.ts +546 -0
  31. package/test/service.test.ts +450 -0
  32. package/test/types.test.ts +1141 -0
  33. package/test/vision.test.ts +214 -0
  34. package/test/workflow.test.ts +501 -0
  35. package/vitest.config.ts +47 -0
  36. package/.turbo/turbo-build.log +0 -5
  37. package/LICENSE +0 -21
  38. package/dist/business.d.ts +0 -62
  39. package/dist/business.d.ts.map +0 -1
  40. package/dist/business.js +0 -109
  41. package/dist/business.js.map +0 -1
  42. package/dist/canvas/activities.d.ts +0 -19
  43. package/dist/canvas/activities.d.ts.map +0 -1
  44. package/dist/canvas/activities.js +0 -20
  45. package/dist/canvas/activities.js.map +0 -1
  46. package/dist/canvas/channels.d.ts +0 -20
  47. package/dist/canvas/channels.d.ts.map +0 -1
  48. package/dist/canvas/channels.js +0 -21
  49. package/dist/canvas/channels.js.map +0 -1
  50. package/dist/canvas/relationships.d.ts +0 -20
  51. package/dist/canvas/relationships.d.ts.map +0 -1
  52. package/dist/canvas/relationships.js +0 -21
  53. package/dist/canvas/relationships.js.map +0 -1
  54. package/dist/canvas/resources.d.ts +0 -20
  55. package/dist/canvas/resources.d.ts.map +0 -1
  56. package/dist/canvas/resources.js +0 -30
  57. package/dist/canvas/resources.js.map +0 -1
  58. package/dist/canvas/revenue.d.ts +0 -22
  59. package/dist/canvas/revenue.d.ts.map +0 -1
  60. package/dist/canvas/revenue.js +0 -30
  61. package/dist/canvas/revenue.js.map +0 -1
  62. package/dist/canvas/segments.d.ts +0 -20
  63. package/dist/canvas/segments.d.ts.map +0 -1
  64. package/dist/canvas/segments.js +0 -28
  65. package/dist/canvas/segments.js.map +0 -1
  66. package/dist/canvas/types.d.ts +0 -232
  67. package/dist/canvas/types.d.ts.map +0 -1
  68. package/dist/canvas/types.js +0 -8
  69. package/dist/canvas/types.js.map +0 -1
  70. package/dist/canvas/value.d.ts +0 -20
  71. package/dist/canvas/value.d.ts.map +0 -1
  72. package/dist/canvas/value.js +0 -21
  73. package/dist/canvas/value.js.map +0 -1
  74. package/dist/dollar.d.ts +0 -60
  75. package/dist/dollar.d.ts.map +0 -1
  76. package/dist/dollar.js +0 -107
  77. package/dist/dollar.js.map +0 -1
  78. package/dist/entities/assets.d.ts +0 -21
  79. package/dist/entities/assets.d.ts.map +0 -1
  80. package/dist/entities/assets.js +0 -323
  81. package/dist/entities/assets.js.map +0 -1
  82. package/dist/entities/business.d.ts +0 -36
  83. package/dist/entities/business.d.ts.map +0 -1
  84. package/dist/entities/business.js +0 -370
  85. package/dist/entities/business.js.map +0 -1
  86. package/dist/entities/communication.d.ts +0 -21
  87. package/dist/entities/communication.d.ts.map +0 -1
  88. package/dist/entities/communication.js +0 -255
  89. package/dist/entities/communication.js.map +0 -1
  90. package/dist/entities/customers.d.ts +0 -58
  91. package/dist/entities/customers.d.ts.map +0 -1
  92. package/dist/entities/customers.js +0 -989
  93. package/dist/entities/customers.js.map +0 -1
  94. package/dist/entities/financials.d.ts +0 -59
  95. package/dist/entities/financials.d.ts.map +0 -1
  96. package/dist/entities/financials.js +0 -932
  97. package/dist/entities/financials.js.map +0 -1
  98. package/dist/entities/goals.d.ts +0 -58
  99. package/dist/entities/goals.d.ts.map +0 -1
  100. package/dist/entities/goals.js +0 -800
  101. package/dist/entities/goals.js.map +0 -1
  102. package/dist/entities/index.d.ts +0 -299
  103. package/dist/entities/index.d.ts.map +0 -1
  104. package/dist/entities/index.js +0 -198
  105. package/dist/entities/index.js.map +0 -1
  106. package/dist/entities/legal.d.ts +0 -21
  107. package/dist/entities/legal.d.ts.map +0 -1
  108. package/dist/entities/legal.js +0 -301
  109. package/dist/entities/legal.js.map +0 -1
  110. package/dist/entities/market.d.ts +0 -21
  111. package/dist/entities/market.d.ts.map +0 -1
  112. package/dist/entities/market.js +0 -301
  113. package/dist/entities/market.js.map +0 -1
  114. package/dist/entities/marketing.d.ts +0 -67
  115. package/dist/entities/marketing.d.ts.map +0 -1
  116. package/dist/entities/marketing.js +0 -1157
  117. package/dist/entities/marketing.js.map +0 -1
  118. package/dist/entities/offerings.d.ts +0 -51
  119. package/dist/entities/offerings.d.ts.map +0 -1
  120. package/dist/entities/offerings.js +0 -727
  121. package/dist/entities/offerings.js.map +0 -1
  122. package/dist/entities/operations.d.ts +0 -58
  123. package/dist/entities/operations.d.ts.map +0 -1
  124. package/dist/entities/operations.js +0 -787
  125. package/dist/entities/operations.js.map +0 -1
  126. package/dist/entities/organization.d.ts +0 -57
  127. package/dist/entities/organization.d.ts.map +0 -1
  128. package/dist/entities/organization.js +0 -807
  129. package/dist/entities/organization.js.map +0 -1
  130. package/dist/entities/partnerships.d.ts +0 -21
  131. package/dist/entities/partnerships.d.ts.map +0 -1
  132. package/dist/entities/partnerships.js +0 -300
  133. package/dist/entities/partnerships.js.map +0 -1
  134. package/dist/entities/planning.d.ts +0 -0
  135. package/dist/entities/planning.d.ts.map +0 -1
  136. package/dist/entities/planning.js +0 -271
  137. package/dist/entities/planning.js.map +0 -1
  138. package/dist/entities/projects.d.ts +0 -25
  139. package/dist/entities/projects.d.ts.map +0 -1
  140. package/dist/entities/projects.js +0 -349
  141. package/dist/entities/projects.js.map +0 -1
  142. package/dist/entities/risk.d.ts +0 -21
  143. package/dist/entities/risk.d.ts.map +0 -1
  144. package/dist/entities/risk.js +0 -293
  145. package/dist/entities/risk.js.map +0 -1
  146. package/dist/entities/sales.d.ts +0 -72
  147. package/dist/entities/sales.d.ts.map +0 -1
  148. package/dist/entities/sales.js +0 -1248
  149. package/dist/entities/sales.js.map +0 -1
  150. package/dist/financials.d.ts +0 -130
  151. package/dist/financials.d.ts.map +0 -1
  152. package/dist/financials.js +0 -297
  153. package/dist/financials.js.map +0 -1
  154. package/dist/goals.d.ts +0 -87
  155. package/dist/goals.d.ts.map +0 -1
  156. package/dist/goals.js +0 -215
  157. package/dist/goals.js.map +0 -1
  158. package/dist/index.d.ts +0 -97
  159. package/dist/index.d.ts.map +0 -1
  160. package/dist/index.js +0 -132
  161. package/dist/index.js.map +0 -1
  162. package/dist/kpis.d.ts +0 -118
  163. package/dist/kpis.d.ts.map +0 -1
  164. package/dist/kpis.js +0 -232
  165. package/dist/kpis.js.map +0 -1
  166. package/dist/metrics.d.ts +0 -448
  167. package/dist/metrics.d.ts.map +0 -1
  168. package/dist/metrics.js +0 -325
  169. package/dist/metrics.js.map +0 -1
  170. package/dist/okrs.d.ts +0 -123
  171. package/dist/okrs.d.ts.map +0 -1
  172. package/dist/okrs.js +0 -269
  173. package/dist/okrs.js.map +0 -1
  174. package/dist/organization.d.ts +0 -585
  175. package/dist/organization.d.ts.map +0 -1
  176. package/dist/organization.js +0 -173
  177. package/dist/organization.js.map +0 -1
  178. package/dist/process.d.ts +0 -112
  179. package/dist/process.d.ts.map +0 -1
  180. package/dist/process.js +0 -241
  181. package/dist/process.js.map +0 -1
  182. package/dist/product.d.ts +0 -85
  183. package/dist/product.d.ts.map +0 -1
  184. package/dist/product.js +0 -145
  185. package/dist/product.js.map +0 -1
  186. package/dist/queries.d.ts +0 -304
  187. package/dist/queries.d.ts.map +0 -1
  188. package/dist/queries.js +0 -415
  189. package/dist/queries.js.map +0 -1
  190. package/dist/roles.d.ts +0 -340
  191. package/dist/roles.d.ts.map +0 -1
  192. package/dist/roles.js +0 -255
  193. package/dist/roles.js.map +0 -1
  194. package/dist/service.d.ts +0 -61
  195. package/dist/service.d.ts.map +0 -1
  196. package/dist/service.js +0 -140
  197. package/dist/service.js.map +0 -1
  198. package/dist/types.d.ts +0 -459
  199. package/dist/types.d.ts.map +0 -1
  200. package/dist/types.js +0 -5
  201. package/dist/types.js.map +0 -1
  202. package/dist/vision.d.ts +0 -38
  203. package/dist/vision.d.ts.map +0 -1
  204. package/dist/vision.js +0 -68
  205. package/dist/vision.js.map +0 -1
  206. package/dist/workflow.d.ts +0 -115
  207. package/dist/workflow.d.ts.map +0 -1
  208. package/dist/workflow.js +0 -247
  209. package/dist/workflow.js.map +0 -1
  210. package/src/business.js +0 -108
  211. package/src/canvas/activities.ts +0 -32
  212. package/src/canvas/canvas.ts +0 -482
  213. package/src/canvas/channels.ts +0 -34
  214. package/src/canvas/costs.ts +0 -43
  215. package/src/canvas/economics.ts +0 -99
  216. package/src/canvas/index.ts +0 -206
  217. package/src/canvas/partnerships.ts +0 -34
  218. package/src/canvas/projections.ts +0 -141
  219. package/src/canvas/relationships.ts +0 -34
  220. package/src/canvas/resources.ts +0 -43
  221. package/src/canvas/revenue.ts +0 -56
  222. package/src/canvas/segments.ts +0 -42
  223. package/src/canvas/types.ts +0 -363
  224. package/src/canvas/value.ts +0 -34
  225. package/src/dollar.js +0 -106
  226. package/src/entities/assets.js +0 -322
  227. package/src/entities/business.js +0 -369
  228. package/src/entities/communication.js +0 -254
  229. package/src/entities/customers.js +0 -988
  230. package/src/entities/financials.js +0 -931
  231. package/src/entities/goals.js +0 -799
  232. package/src/entities/index.js +0 -197
  233. package/src/entities/legal.js +0 -300
  234. package/src/entities/market.js +0 -300
  235. package/src/entities/marketing.js +0 -1156
  236. package/src/entities/offerings.js +0 -726
  237. package/src/entities/operations.js +0 -786
  238. package/src/entities/organization.js +0 -806
  239. package/src/entities/partnerships.js +0 -299
  240. package/src/entities/planning.js +0 -270
  241. package/src/entities/projects.js +0 -348
  242. package/src/entities/risk.js +0 -292
  243. package/src/entities/sales.js +0 -1247
  244. package/src/financials.js +0 -296
  245. package/src/goals.js +0 -214
  246. package/src/index.js +0 -131
  247. package/src/index.test.js +0 -274
  248. package/src/kpis.js +0 -231
  249. package/src/metrics.js +0 -324
  250. package/src/okrs.js +0 -268
  251. package/src/organization.js +0 -172
  252. package/src/process.js +0 -240
  253. package/src/product.js +0 -144
  254. package/src/queries.js +0 -414
  255. package/src/roles.js +0 -254
  256. package/src/service.js +0 -139
  257. package/src/types.js +0 -4
  258. package/src/vision.js +0 -67
  259. package/src/workflow.js +0 -246
  260. package/tests/canvas.test.ts +0 -842
package/src/service.js DELETED
@@ -1,139 +0,0 @@
1
- /**
2
- * Service definition and management
3
- */
4
- /**
5
- * Define a service with pricing, SLA, and delivery information
6
- *
7
- * @example
8
- * ```ts
9
- * const service = Service({
10
- * name: 'Widget Consulting',
11
- * description: 'Expert widget implementation and optimization',
12
- * category: 'Consulting',
13
- * targetSegment: 'Enterprise',
14
- * valueProposition: 'Get expert help implementing widgets in 2 weeks',
15
- * pricingModel: 'fixed',
16
- * price: 5000,
17
- * currency: 'USD',
18
- * deliveryTime: '2 weeks',
19
- * sla: {
20
- * uptime: 99.9,
21
- * responseTime: '< 24 hours',
22
- * supportHours: 'Business hours (9-5 EST)',
23
- * penalties: '10% refund per day of delay',
24
- * },
25
- * })
26
- * ```
27
- */
28
- export function Service(definition) {
29
- if (!definition.name) {
30
- throw new Error('Service name is required');
31
- }
32
- return {
33
- ...definition,
34
- pricingModel: definition.pricingModel || 'hourly',
35
- currency: definition.currency || 'USD',
36
- metadata: definition.metadata || {},
37
- };
38
- }
39
- /**
40
- * Calculate service price based on hours (for hourly pricing)
41
- */
42
- export function calculateHourlyPrice(service, hours) {
43
- if (service.pricingModel !== 'hourly' || !service.price) {
44
- throw new Error('Service must use hourly pricing model');
45
- }
46
- return service.price * hours;
47
- }
48
- /**
49
- * Calculate monthly retainer equivalent
50
- */
51
- export function calculateMonthlyRetainer(service, hoursPerMonth) {
52
- if (service.pricingModel !== 'hourly' || !service.price) {
53
- throw new Error('Service must use hourly pricing model');
54
- }
55
- return service.price * hoursPerMonth;
56
- }
57
- /**
58
- * Check if service meets SLA uptime requirement
59
- */
60
- export function checkSLAUptime(service, actualUptime) {
61
- if (!service.sla?.uptime)
62
- return true;
63
- return actualUptime >= service.sla.uptime;
64
- }
65
- /**
66
- * Parse delivery time to days
67
- */
68
- export function parseDeliveryTimeToDays(deliveryTime) {
69
- if (!deliveryTime)
70
- return 0;
71
- const lower = deliveryTime.toLowerCase();
72
- // Parse patterns like "2 weeks", "3 days", "1 month"
73
- const match = lower.match(/(\d+)\s*(day|days|week|weeks|month|months|hour|hours)/);
74
- if (!match)
75
- return 0;
76
- const value = parseInt(match[1] || '0', 10);
77
- const unit = match[2];
78
- switch (unit) {
79
- case 'hour':
80
- case 'hours':
81
- return value / 24;
82
- case 'day':
83
- case 'days':
84
- return value;
85
- case 'week':
86
- case 'weeks':
87
- return value * 7;
88
- case 'month':
89
- case 'months':
90
- return value * 30;
91
- default:
92
- return 0;
93
- }
94
- }
95
- /**
96
- * Estimate service completion date
97
- */
98
- export function estimateCompletionDate(service, startDate) {
99
- const start = startDate || new Date();
100
- const days = parseDeliveryTimeToDays(service.deliveryTime);
101
- const completion = new Date(start);
102
- completion.setDate(completion.getDate() + days);
103
- return completion;
104
- }
105
- /**
106
- * Calculate service value (for value-based pricing)
107
- */
108
- export function calculateValueBasedPrice(service, customerValue, valueSharePercentage) {
109
- if (service.pricingModel !== 'value-based') {
110
- throw new Error('Service must use value-based pricing model');
111
- }
112
- if (valueSharePercentage < 0 || valueSharePercentage > 100) {
113
- throw new Error('Value share percentage must be between 0 and 100');
114
- }
115
- return customerValue * (valueSharePercentage / 100);
116
- }
117
- /**
118
- * Validate service definition
119
- */
120
- export function validateService(service) {
121
- const errors = [];
122
- if (!service.name) {
123
- errors.push('Service name is required');
124
- }
125
- if (service.price && service.price < 0) {
126
- errors.push('Service price cannot be negative');
127
- }
128
- if (service.sla?.uptime && (service.sla.uptime < 0 || service.sla.uptime > 100)) {
129
- errors.push('SLA uptime must be between 0 and 100');
130
- }
131
- if (service.pricingModel &&
132
- !['hourly', 'fixed', 'retainer', 'value-based'].includes(service.pricingModel)) {
133
- errors.push('Invalid pricing model');
134
- }
135
- return {
136
- valid: errors.length === 0,
137
- errors,
138
- };
139
- }
package/src/types.js DELETED
@@ -1,4 +0,0 @@
1
- /**
2
- * Core types for business-as-code primitives
3
- */
4
- export {};
package/src/vision.js DELETED
@@ -1,67 +0,0 @@
1
- /**
2
- * Vision statement definition
3
- */
4
- /**
5
- * Define a business vision statement with timeframe and success indicators
6
- *
7
- * @example
8
- * ```ts
9
- * const vision = Vision({
10
- * statement: 'To become the world\'s most trusted widget platform',
11
- * timeframe: '5 years',
12
- * successIndicators: [
13
- * '10M+ active users',
14
- * 'Present in 50+ countries',
15
- * 'Industry-leading NPS score',
16
- * '$1B+ annual revenue',
17
- * ],
18
- * })
19
- * ```
20
- */
21
- export function Vision(definition) {
22
- // Validate required fields
23
- if (!definition.statement) {
24
- throw new Error('Vision statement is required');
25
- }
26
- // Return validated vision definition
27
- return {
28
- ...definition,
29
- successIndicators: definition.successIndicators || [],
30
- metadata: definition.metadata || {},
31
- };
32
- }
33
- /**
34
- * Check if a success indicator has been achieved
35
- */
36
- export function checkIndicator(vision, indicator, currentMetrics) {
37
- // Simple check - would need more sophisticated parsing in production
38
- return Object.entries(currentMetrics).some(([key, value]) => {
39
- return indicator.toLowerCase().includes(key.toLowerCase()) && Boolean(value);
40
- });
41
- }
42
- /**
43
- * Calculate vision progress based on achieved indicators
44
- */
45
- export function calculateProgress(vision, currentMetrics) {
46
- if (!vision.successIndicators || vision.successIndicators.length === 0) {
47
- return 0;
48
- }
49
- const achieved = vision.successIndicators.filter(indicator => checkIndicator(vision, indicator, currentMetrics)).length;
50
- return (achieved / vision.successIndicators.length) * 100;
51
- }
52
- /**
53
- * Validate vision definition
54
- */
55
- export function validateVision(vision) {
56
- const errors = [];
57
- if (!vision.statement) {
58
- errors.push('Vision statement is required');
59
- }
60
- if (vision.statement && vision.statement.length < 10) {
61
- errors.push('Vision statement should be at least 10 characters');
62
- }
63
- return {
64
- valid: errors.length === 0,
65
- errors,
66
- };
67
- }
package/src/workflow.js DELETED
@@ -1,246 +0,0 @@
1
- /**
2
- * Workflow definition (automation sequences)
3
- */
4
- /**
5
- * Define an automated workflow with triggers and actions
6
- *
7
- * @example
8
- * ```ts
9
- * const workflow = Workflow({
10
- * name: 'New Customer Welcome',
11
- * description: 'Automated welcome sequence for new customers',
12
- * trigger: {
13
- * type: 'event',
14
- * event: 'Customer.created',
15
- * },
16
- * actions: [
17
- * {
18
- * order: 1,
19
- * type: 'send',
20
- * description: 'Send welcome email',
21
- * params: {
22
- * template: 'welcome_email',
23
- * to: '{{customer.email}}',
24
- * },
25
- * },
26
- * {
27
- * order: 2,
28
- * type: 'create',
29
- * description: 'Create onboarding task',
30
- * params: {
31
- * type: 'Task',
32
- * title: 'Onboard {{customer.name}}',
33
- * assignee: 'customer_success_team',
34
- * },
35
- * },
36
- * {
37
- * order: 3,
38
- * type: 'wait',
39
- * description: 'Wait 24 hours',
40
- * params: {
41
- * duration: '24 hours',
42
- * },
43
- * },
44
- * {
45
- * order: 4,
46
- * type: 'notify',
47
- * description: 'Send setup reminder',
48
- * params: {
49
- * channel: 'email',
50
- * message: 'Reminder to complete setup',
51
- * },
52
- * condition: 'customer.setupCompleted === false',
53
- * },
54
- * ],
55
- * })
56
- * ```
57
- */
58
- export function Workflow(definition) {
59
- if (!definition.name) {
60
- throw new Error('Workflow name is required');
61
- }
62
- if (!definition.trigger) {
63
- throw new Error('Workflow trigger is required');
64
- }
65
- return {
66
- ...definition,
67
- actions: definition.actions || [],
68
- metadata: definition.metadata || {},
69
- };
70
- }
71
- /**
72
- * Get actions in execution order
73
- */
74
- export function getActionsInOrder(workflow) {
75
- return [...(workflow.actions || [])].sort((a, b) => a.order - b.order);
76
- }
77
- /**
78
- * Get actions by type
79
- */
80
- export function getActionsByType(workflow, type) {
81
- return workflow.actions?.filter(action => action.type === type) || [];
82
- }
83
- /**
84
- * Get conditional actions
85
- */
86
- export function getConditionalActions(workflow) {
87
- return workflow.actions?.filter(action => action.condition) || [];
88
- }
89
- /**
90
- * Add action to workflow
91
- */
92
- export function addAction(workflow, action) {
93
- return {
94
- ...workflow,
95
- actions: [...(workflow.actions || []), action],
96
- };
97
- }
98
- /**
99
- * Remove action from workflow
100
- */
101
- export function removeAction(workflow, order) {
102
- return {
103
- ...workflow,
104
- actions: workflow.actions?.filter(a => a.order !== order),
105
- };
106
- }
107
- /**
108
- * Update action in workflow
109
- */
110
- export function updateAction(workflow, order, updates) {
111
- const actions = workflow.actions?.map(action => action.order === order ? { ...action, ...updates } : action);
112
- return {
113
- ...workflow,
114
- actions,
115
- };
116
- }
117
- /**
118
- * Check if trigger is event-based
119
- */
120
- export function isEventTrigger(trigger) {
121
- return trigger.type === 'event';
122
- }
123
- /**
124
- * Check if trigger is schedule-based
125
- */
126
- export function isScheduleTrigger(trigger) {
127
- return trigger.type === 'schedule';
128
- }
129
- /**
130
- * Check if trigger is webhook-based
131
- */
132
- export function isWebhookTrigger(trigger) {
133
- return trigger.type === 'webhook';
134
- }
135
- /**
136
- * Parse wait duration to milliseconds
137
- */
138
- export function parseWaitDuration(duration) {
139
- const match = duration.match(/(\d+)\s*(ms|millisecond|milliseconds|s|second|seconds|m|minute|minutes|h|hour|hours|d|day|days)/);
140
- if (!match)
141
- return 0;
142
- const value = parseInt(match[1] || '0', 10);
143
- const unit = match[2];
144
- switch (unit) {
145
- case 'ms':
146
- case 'millisecond':
147
- case 'milliseconds':
148
- return value;
149
- case 's':
150
- case 'second':
151
- case 'seconds':
152
- return value * 1000;
153
- case 'm':
154
- case 'minute':
155
- case 'minutes':
156
- return value * 60 * 1000;
157
- case 'h':
158
- case 'hour':
159
- case 'hours':
160
- return value * 60 * 60 * 1000;
161
- case 'd':
162
- case 'day':
163
- case 'days':
164
- return value * 24 * 60 * 60 * 1000;
165
- default:
166
- return 0;
167
- }
168
- }
169
- /**
170
- * Evaluate condition (simple implementation)
171
- */
172
- export function evaluateCondition(condition, context) {
173
- // This is a simplified implementation
174
- // In production, use a proper expression evaluator
175
- try {
176
- // Replace variable references with actual values
177
- let expression = condition;
178
- for (const [key, value] of Object.entries(context)) {
179
- const regex = new RegExp(`\\b${key}\\b`, 'g');
180
- expression = expression.replace(regex, JSON.stringify(value));
181
- }
182
- // Evaluate the expression (unsafe in production - use a proper evaluator)
183
- return Boolean(eval(expression));
184
- }
185
- catch {
186
- return false;
187
- }
188
- }
189
- /**
190
- * Fill template with context values
191
- */
192
- export function fillTemplate(template, context) {
193
- return template.replace(/\{\{([^}]+)\}\}/g, (_, path) => {
194
- const value = getNestedValue(context, path.trim());
195
- return String(value ?? '');
196
- });
197
- }
198
- /**
199
- * Get nested value from object by path
200
- */
201
- function getNestedValue(obj, path) {
202
- return path.split('.').reduce((acc, part) => acc?.[part], obj);
203
- }
204
- /**
205
- * Validate workflow definition
206
- */
207
- export function validateWorkflow(workflow) {
208
- const errors = [];
209
- if (!workflow.name) {
210
- errors.push('Workflow name is required');
211
- }
212
- if (!workflow.trigger) {
213
- errors.push('Workflow trigger is required');
214
- }
215
- else {
216
- if (workflow.trigger.type === 'event' && !workflow.trigger.event) {
217
- errors.push('Event trigger must specify an event name');
218
- }
219
- if (workflow.trigger.type === 'schedule' && !workflow.trigger.schedule) {
220
- errors.push('Schedule trigger must specify a schedule expression');
221
- }
222
- if (workflow.trigger.type === 'webhook' && !workflow.trigger.webhook) {
223
- errors.push('Webhook trigger must specify a webhook URL');
224
- }
225
- }
226
- if (workflow.actions) {
227
- const orders = new Set();
228
- for (const action of workflow.actions) {
229
- if (!action.type) {
230
- errors.push(`Action at order ${action.order} must have a type`);
231
- }
232
- if (orders.has(action.order)) {
233
- errors.push(`Duplicate action order: ${action.order}`);
234
- }
235
- orders.add(action.order);
236
- // Validate action-specific requirements
237
- if (action.type === 'wait' && !action.params?.duration) {
238
- errors.push(`Wait action at order ${action.order} must specify duration`);
239
- }
240
- }
241
- }
242
- return {
243
- valid: errors.length === 0,
244
- errors,
245
- };
246
- }