@elevasis/core 0.12.0 → 0.14.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 (50) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.js +9 -2
  3. package/dist/organization-model/index.d.ts +1 -1
  4. package/dist/organization-model/index.js +9 -2
  5. package/dist/test-utils/index.d.ts +480 -389
  6. package/dist/test-utils/index.js +28 -2
  7. package/package.json +1 -1
  8. package/src/_gen/__tests__/__snapshots__/contracts.md.snap +2324 -0
  9. package/src/auth/multi-tenancy/credentials/__tests__/encryption.test.ts +217 -216
  10. package/src/auth/multi-tenancy/credentials/server/encryption.ts +5 -19
  11. package/src/auth/multi-tenancy/credentials/server/kek-loader.ts +3 -13
  12. package/src/auth/multi-tenancy/permissions.ts +12 -5
  13. package/src/business/acquisition/activity-events.test.ts +250 -0
  14. package/src/business/acquisition/activity-events.ts +84 -0
  15. package/src/business/acquisition/api-schemas.test.ts +1180 -0
  16. package/src/business/acquisition/api-schemas.ts +456 -235
  17. package/src/business/acquisition/crm-state-actions.test.ts +160 -0
  18. package/src/business/acquisition/derive-actions.test.ts +518 -0
  19. package/src/business/acquisition/derive-actions.ts +103 -0
  20. package/src/business/acquisition/index.ts +51 -11
  21. package/src/business/acquisition/stateful.ts +30 -0
  22. package/src/business/acquisition/types.ts +44 -77
  23. package/src/execution/engine/index.ts +4 -1
  24. package/src/execution/engine/tools/integration/server/adapters/apify/__tests__/apify-run-actor.integration.test.ts +1 -2
  25. package/src/execution/engine/tools/integration/server/adapters/attio/__tests__/attio-crud.integration.test.ts +363 -361
  26. package/src/execution/engine/tools/integration/server/adapters/attio/fetch/get-record/index.test.ts +162 -186
  27. package/src/execution/engine/tools/integration/server/adapters/attio/fetch/list-records/index.test.ts +316 -338
  28. package/src/execution/engine/tools/integration/server/adapters/gmail/gmail-adapter.ts +204 -210
  29. package/src/execution/engine/tools/integration/server/adapters/resend/fetch/send-email/index.test.ts +88 -0
  30. package/src/execution/engine/tools/integration/server/adapters/resend/fetch/send-email/index.ts +141 -134
  31. package/src/execution/engine/tools/integration/server/adapters/resend/fetch/utils/types.ts +76 -75
  32. package/src/execution/engine/tools/integration/service.test.ts +34 -9
  33. package/src/execution/engine/tools/integration/service.ts +6 -3
  34. package/src/execution/engine/tools/lead-service-types.ts +90 -30
  35. package/src/execution/engine/tools/platform/acquisition/types.ts +266 -260
  36. package/src/execution/engine/tools/registry.ts +5 -4
  37. package/src/execution/engine/tools/tool-maps.ts +43 -21
  38. package/src/execution/engine/workflow/types.ts +11 -0
  39. package/src/organization-model/contracts.ts +4 -4
  40. package/src/organization-model/domains/navigation.ts +62 -62
  41. package/src/organization-model/domains/sales.ts +272 -0
  42. package/src/organization-model/organization-graph.mdx +2 -2
  43. package/src/organization-model/published.ts +21 -21
  44. package/src/organization-model/resolve.ts +21 -8
  45. package/src/platform/constants/versions.ts +1 -1
  46. package/src/reference/_generated/contracts.md +2324 -0
  47. package/src/scaffold-registry/index.ts +10 -9
  48. package/src/scaffold-registry/schema.ts +68 -62
  49. package/src/supabase/database.types.ts +2958 -2884
  50. package/src/test-utils/rls/RLSTestContext.ts +585 -553
@@ -0,0 +1,250 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { ActivityEventSchema } from './activity-events'
3
+ import type { ActivityEvent } from './activity-events'
4
+
5
+ const TIMESTAMP = '2026-04-28T12:00:00.000Z'
6
+
7
+ function makeEvent<T extends ActivityEvent['type']>(
8
+ type: T,
9
+ overrides: Record<string, unknown> = {}
10
+ ): Record<string, unknown> {
11
+ const bases: Record<string, Record<string, unknown>> = {
12
+ stage_change: { type, timestamp: TIMESTAMP, stageBefore: 'interested', stageAfter: 'proposal' },
13
+ state_change: { type, timestamp: TIMESTAMP, stateBefore: null, stateAfter: 'discovery_replied' },
14
+ action_taken: { type, timestamp: TIMESTAMP, actionKey: 'send_link' },
15
+ approval_created: { type, timestamp: TIMESTAMP, commandId: 'cmd_abc' },
16
+ approval_resolved: { type, timestamp: TIMESTAMP, commandId: 'cmd_abc', resolution: 'superseded' },
17
+ approval_stale: { type, timestamp: TIMESTAMP, commandId: 'cmd_abc' },
18
+ task_created: { type, timestamp: TIMESTAMP, taskId: 'task_001' },
19
+ deal_created: { type, timestamp: TIMESTAMP }
20
+ }
21
+ return { ...bases[type], ...overrides }
22
+ }
23
+
24
+ function without(event: Record<string, unknown>, key: string): Record<string, unknown> {
25
+ const copy = { ...event }
26
+ delete copy[key]
27
+ return copy
28
+ }
29
+
30
+ describe('ActivityEventSchema', () => {
31
+ describe('positive cases — all 8 variants with minimal-valid payloads', () => {
32
+ const variants: ActivityEvent['type'][] = [
33
+ 'stage_change',
34
+ 'state_change',
35
+ 'action_taken',
36
+ 'approval_created',
37
+ 'approval_resolved',
38
+ 'approval_stale',
39
+ 'task_created',
40
+ 'deal_created'
41
+ ]
42
+
43
+ it.each(variants)('accepts minimal valid %s event', (type) => {
44
+ expect(ActivityEventSchema.safeParse(makeEvent(type)).success).toBe(true)
45
+ })
46
+ })
47
+
48
+ describe('optional fields', () => {
49
+ it('accepts stage_change without reason', () => {
50
+ expect(ActivityEventSchema.safeParse(makeEvent('stage_change')).success).toBe(true)
51
+ })
52
+
53
+ it('accepts stage_change with reason', () => {
54
+ expect(ActivityEventSchema.safeParse(makeEvent('stage_change', { reason: 'qualified' })).success).toBe(true)
55
+ })
56
+
57
+ it('accepts state_change without reason', () => {
58
+ expect(ActivityEventSchema.safeParse(makeEvent('state_change')).success).toBe(true)
59
+ })
60
+
61
+ it('accepts state_change with reason', () => {
62
+ expect(ActivityEventSchema.safeParse(makeEvent('state_change', { reason: 'manual override' })).success).toBe(true)
63
+ })
64
+
65
+ it('accepts action_taken without payload', () => {
66
+ expect(ActivityEventSchema.safeParse(makeEvent('action_taken')).success).toBe(true)
67
+ })
68
+
69
+ it('accepts action_taken with payload', () => {
70
+ expect(
71
+ ActivityEventSchema.safeParse(makeEvent('action_taken', { payload: { channel: 'email', count: 3 } })).success
72
+ ).toBe(true)
73
+ })
74
+
75
+ it('accepts approval_created without dealStageBefore and dealStageAfter', () => {
76
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_created')).success).toBe(true)
77
+ })
78
+
79
+ it('accepts approval_created with both stage fields', () => {
80
+ expect(
81
+ ActivityEventSchema.safeParse(
82
+ makeEvent('approval_created', { dealStageBefore: 'interested', dealStageAfter: 'proposal' })
83
+ ).success
84
+ ).toBe(true)
85
+ })
86
+
87
+ it('accepts approval_resolved without originResourceType', () => {
88
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_resolved')).success).toBe(true)
89
+ })
90
+
91
+ it('accepts approval_resolved with originResourceType', () => {
92
+ expect(
93
+ ActivityEventSchema.safeParse(makeEvent('approval_resolved', { originResourceType: 'crm_action' })).success
94
+ ).toBe(true)
95
+ })
96
+
97
+ it('accepts approval_stale without dealStageBefore and dealStageAfter', () => {
98
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_stale')).success).toBe(true)
99
+ })
100
+
101
+ it('accepts approval_stale with both stage fields', () => {
102
+ expect(
103
+ ActivityEventSchema.safeParse(
104
+ makeEvent('approval_stale', { dealStageBefore: 'interested', dealStageAfter: 'proposal' })
105
+ ).success
106
+ ).toBe(true)
107
+ })
108
+ })
109
+
110
+ describe('state_change nullable fields', () => {
111
+ it('accepts stateBefore: null and stateAfter: null', () => {
112
+ expect(
113
+ ActivityEventSchema.safeParse(makeEvent('state_change', { stateBefore: null, stateAfter: null })).success
114
+ ).toBe(true)
115
+ })
116
+
117
+ it('accepts stateBefore as string and stateAfter as string', () => {
118
+ expect(
119
+ ActivityEventSchema.safeParse(
120
+ makeEvent('state_change', { stateBefore: 'discovery_link_sent', stateAfter: 'discovery_replied' })
121
+ ).success
122
+ ).toBe(true)
123
+ })
124
+ })
125
+
126
+ describe('required-field enforcement', () => {
127
+ it('rejects stage_change missing stageBefore', () => {
128
+ expect(ActivityEventSchema.safeParse(without(makeEvent('stage_change'), 'stageBefore')).success).toBe(false)
129
+ })
130
+
131
+ it('rejects stage_change missing stageAfter', () => {
132
+ expect(ActivityEventSchema.safeParse(without(makeEvent('stage_change'), 'stageAfter')).success).toBe(false)
133
+ })
134
+
135
+ it('rejects stage_change missing timestamp', () => {
136
+ expect(ActivityEventSchema.safeParse(without(makeEvent('stage_change'), 'timestamp')).success).toBe(false)
137
+ })
138
+
139
+ it('rejects state_change missing stateBefore', () => {
140
+ expect(ActivityEventSchema.safeParse(without(makeEvent('state_change'), 'stateBefore')).success).toBe(false)
141
+ })
142
+
143
+ it('rejects state_change missing stateAfter', () => {
144
+ expect(ActivityEventSchema.safeParse(without(makeEvent('state_change'), 'stateAfter')).success).toBe(false)
145
+ })
146
+
147
+ it('rejects action_taken missing actionKey', () => {
148
+ expect(ActivityEventSchema.safeParse(without(makeEvent('action_taken'), 'actionKey')).success).toBe(false)
149
+ })
150
+
151
+ it('rejects approval_created missing commandId', () => {
152
+ expect(ActivityEventSchema.safeParse(without(makeEvent('approval_created'), 'commandId')).success).toBe(false)
153
+ })
154
+
155
+ it('rejects approval_resolved missing commandId', () => {
156
+ expect(ActivityEventSchema.safeParse(without(makeEvent('approval_resolved'), 'commandId')).success).toBe(false)
157
+ })
158
+
159
+ it('rejects approval_resolved missing resolution', () => {
160
+ expect(ActivityEventSchema.safeParse(without(makeEvent('approval_resolved'), 'resolution')).success).toBe(false)
161
+ })
162
+
163
+ it('rejects approval_stale missing commandId', () => {
164
+ expect(ActivityEventSchema.safeParse(without(makeEvent('approval_stale'), 'commandId')).success).toBe(false)
165
+ })
166
+
167
+ it('rejects task_created missing taskId', () => {
168
+ expect(ActivityEventSchema.safeParse(without(makeEvent('task_created'), 'taskId')).success).toBe(false)
169
+ })
170
+ })
171
+
172
+ describe('approval_resolved enum constraint', () => {
173
+ it('accepts resolution: superseded', () => {
174
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_resolved', { resolution: 'superseded' })).success).toBe(
175
+ true
176
+ )
177
+ })
178
+
179
+ it('rejects resolution: approved', () => {
180
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_resolved', { resolution: 'approved' })).success).toBe(
181
+ false
182
+ )
183
+ })
184
+
185
+ it('rejects resolution: rejected', () => {
186
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_resolved', { resolution: 'rejected' })).success).toBe(
187
+ false
188
+ )
189
+ })
190
+
191
+ it('rejects resolution: pending', () => {
192
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_resolved', { resolution: 'pending' })).success).toBe(
193
+ false
194
+ )
195
+ })
196
+ })
197
+
198
+ describe('timestamp validation', () => {
199
+ it('accepts a full ISO datetime string', () => {
200
+ expect(ActivityEventSchema.safeParse(makeEvent('deal_created', { timestamp: TIMESTAMP })).success).toBe(true)
201
+ })
202
+
203
+ it('rejects a non-date string', () => {
204
+ expect(ActivityEventSchema.safeParse(makeEvent('deal_created', { timestamp: 'not-a-date' })).success).toBe(false)
205
+ })
206
+
207
+ it('rejects a date-only string without time component', () => {
208
+ expect(ActivityEventSchema.safeParse(makeEvent('deal_created', { timestamp: '2026-04-28' })).success).toBe(false)
209
+ })
210
+
211
+ it('rejects an empty timestamp string', () => {
212
+ expect(ActivityEventSchema.safeParse(makeEvent('deal_created', { timestamp: '' })).success).toBe(false)
213
+ })
214
+ })
215
+
216
+ describe('discriminator strictness', () => {
217
+ it('rejects an unknown type value', () => {
218
+ expect(ActivityEventSchema.safeParse({ type: 'booking_nudge_sent', timestamp: TIMESTAMP }).success).toBe(false)
219
+ })
220
+
221
+ it('rejects a payload with no type field', () => {
222
+ expect(ActivityEventSchema.safeParse({ timestamp: TIMESTAMP }).success).toBe(false)
223
+ })
224
+
225
+ it('rejects a payload where type is null', () => {
226
+ expect(ActivityEventSchema.safeParse({ type: null, timestamp: TIMESTAMP }).success).toBe(false)
227
+ })
228
+
229
+ it('rejects a completely empty object', () => {
230
+ expect(ActivityEventSchema.safeParse({}).success).toBe(false)
231
+ })
232
+ })
233
+
234
+ describe('round-trip serialization', () => {
235
+ it.each([
236
+ 'stage_change',
237
+ 'state_change',
238
+ 'action_taken',
239
+ 'approval_created',
240
+ 'approval_resolved',
241
+ 'approval_stale',
242
+ 'task_created',
243
+ 'deal_created'
244
+ ] as const)('round-trips %s through JSON.stringify → JSON.parse', (type) => {
245
+ const original = makeEvent(type)
246
+ const parsed = JSON.parse(JSON.stringify(original))
247
+ expect(ActivityEventSchema.safeParse(parsed).success).toBe(true)
248
+ })
249
+ })
250
+ })
@@ -0,0 +1,84 @@
1
+ import { z } from 'zod'
2
+
3
+ // ---------------------------------------------------------------------------
4
+ // Platform event member schemas (8 members — closed union, stays in @repo/core)
5
+ //
6
+ // Domain-specific events (reply_received, booking_nudge_sent, etc.) live in
7
+ // external/elevasis/operations as CrmDomainActivityEventSchema.
8
+ // See Open Decision #5 in crm-action-system.mdx for rationale.
9
+ // ---------------------------------------------------------------------------
10
+
11
+ const StageChangeEventSchema = z.object({
12
+ type: z.literal('stage_change'),
13
+ timestamp: z.string().datetime(),
14
+ stageBefore: z.string(),
15
+ stageAfter: z.string(),
16
+ reason: z.string().optional()
17
+ })
18
+
19
+ const StateChangeEventSchema = z.object({
20
+ type: z.literal('state_change'),
21
+ timestamp: z.string().datetime(),
22
+ stateBefore: z.string().nullable(),
23
+ stateAfter: z.string().nullable(),
24
+ reason: z.string().optional()
25
+ })
26
+
27
+ const ActionTakenEventSchema = z.object({
28
+ type: z.literal('action_taken'),
29
+ timestamp: z.string().datetime(),
30
+ actionKey: z.string(),
31
+ payload: z.record(z.string(), z.unknown()).optional()
32
+ })
33
+
34
+ const ApprovalCreatedEventSchema = z.object({
35
+ type: z.literal('approval_created'),
36
+ timestamp: z.string().datetime(),
37
+ commandId: z.string(),
38
+ dealStageBefore: z.string().optional(),
39
+ dealStageAfter: z.string().optional()
40
+ })
41
+
42
+ const ApprovalResolvedEventSchema = z.object({
43
+ type: z.literal('approval_resolved'),
44
+ timestamp: z.string().datetime(),
45
+ commandId: z.string(),
46
+ resolution: z.enum(['superseded']),
47
+ originResourceType: z.string().optional()
48
+ })
49
+
50
+ const ApprovalStaleEventSchema = z.object({
51
+ type: z.literal('approval_stale'),
52
+ timestamp: z.string().datetime(),
53
+ commandId: z.string(),
54
+ dealStageBefore: z.string().optional(),
55
+ dealStageAfter: z.string().optional()
56
+ })
57
+
58
+ const TaskCreatedEventSchema = z.object({
59
+ type: z.literal('task_created'),
60
+ timestamp: z.string().datetime(),
61
+ taskId: z.string()
62
+ })
63
+
64
+ const DealCreatedEventSchema = z.object({
65
+ type: z.literal('deal_created'),
66
+ timestamp: z.string().datetime()
67
+ })
68
+
69
+ // ---------------------------------------------------------------------------
70
+ // Union — closed 8-member platform union
71
+ // ---------------------------------------------------------------------------
72
+
73
+ export const ActivityEventSchema = z.discriminatedUnion('type', [
74
+ StageChangeEventSchema,
75
+ StateChangeEventSchema,
76
+ ActionTakenEventSchema,
77
+ ApprovalCreatedEventSchema,
78
+ ApprovalResolvedEventSchema,
79
+ ApprovalStaleEventSchema,
80
+ TaskCreatedEventSchema,
81
+ DealCreatedEventSchema
82
+ ])
83
+
84
+ export type ActivityEvent = z.infer<typeof ActivityEventSchema>