@elevasis/core 0.15.0 → 0.16.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 (88) hide show
  1. package/dist/index.d.ts +1718 -19
  2. package/dist/index.js +369 -25
  3. package/dist/organization-model/index.d.ts +1718 -19
  4. package/dist/organization-model/index.js +369 -25
  5. package/dist/test-utils/index.d.ts +1108 -371
  6. package/dist/test-utils/index.js +357 -17
  7. package/package.json +5 -1
  8. package/src/__tests__/publish.test.ts +14 -13
  9. package/src/__tests__/template-core-compatibility.test.ts +4 -4
  10. package/src/_gen/__tests__/__snapshots__/contracts.md.snap +1109 -882
  11. package/src/auth/multi-tenancy/index.ts +3 -0
  12. package/src/auth/multi-tenancy/theme-presets.ts +45 -0
  13. package/src/auth/multi-tenancy/types.ts +57 -83
  14. package/src/auth/multi-tenancy/users/api-schemas.ts +165 -194
  15. package/src/business/acquisition/activity-events.ts +13 -4
  16. package/src/business/acquisition/api-schemas.test.ts +315 -4
  17. package/src/business/acquisition/api-schemas.ts +122 -8
  18. package/src/business/acquisition/build-templates.ts +44 -0
  19. package/src/business/acquisition/crm-next-action.test.ts +262 -0
  20. package/src/business/acquisition/crm-next-action.ts +220 -0
  21. package/src/business/acquisition/crm-priority.test.ts +216 -0
  22. package/src/business/acquisition/crm-priority.ts +349 -0
  23. package/src/business/acquisition/crm-state-actions.test.ts +151 -160
  24. package/src/business/acquisition/deal-ownership.test.ts +351 -0
  25. package/src/business/acquisition/deal-ownership.ts +120 -0
  26. package/src/business/acquisition/derive-actions.test.ts +101 -37
  27. package/src/business/acquisition/derive-actions.ts +102 -87
  28. package/src/business/acquisition/index.ts +10 -0
  29. package/src/business/acquisition/types.ts +400 -366
  30. package/src/business/crm/api-schemas.ts +40 -0
  31. package/src/business/crm/index.ts +1 -0
  32. package/src/business/deals/api-schemas.ts +79 -0
  33. package/src/business/deals/index.ts +1 -0
  34. package/src/business/projects/types.ts +124 -88
  35. package/src/execution/core/runner-types.ts +61 -80
  36. package/src/execution/engine/index.ts +4 -3
  37. package/src/execution/engine/tools/integration/server/adapters/gmail/gmail-tools.ts +105 -104
  38. package/src/execution/engine/tools/integration/server/adapters/instantly/instantly-tools.ts +1474 -1473
  39. package/src/execution/engine/tools/integration/server/adapters/millionverifier/millionverifier-tools.ts +103 -102
  40. package/src/execution/engine/tools/integration/server/adapters/signature-api/signature-api-tools.ts +182 -179
  41. package/src/execution/engine/tools/integration/server/adapters/stripe/stripe-tools.ts +310 -309
  42. package/src/execution/engine/tools/integration/tool.ts +255 -253
  43. package/src/execution/engine/tools/lead-service-types.ts +939 -924
  44. package/src/execution/engine/tools/messages.ts +43 -0
  45. package/src/execution/engine/tools/platform/acquisition/list-tools.ts +6 -5
  46. package/src/execution/engine/tools/platform/acquisition/types.ts +5 -2
  47. package/src/execution/engine/tools/platform/email/types.ts +97 -96
  48. package/src/execution/engine/tools/registry.ts +4 -3
  49. package/src/execution/engine/tools/tool-maps.ts +3 -1
  50. package/src/execution/engine/tools/types.ts +234 -233
  51. package/src/execution/engine/workflow/types.ts +195 -193
  52. package/src/execution/external/api-schemas.ts +40 -0
  53. package/src/execution/external/index.ts +1 -0
  54. package/src/knowledge/README.md +32 -0
  55. package/src/knowledge/__tests__/queries.test.ts +504 -0
  56. package/src/knowledge/format.ts +99 -0
  57. package/src/knowledge/index.ts +5 -0
  58. package/src/knowledge/queries.ts +256 -0
  59. package/src/organization-model/__tests__/defaults.test.ts +172 -172
  60. package/src/organization-model/__tests__/foundation.test.ts +7 -7
  61. package/src/organization-model/__tests__/icons.test.ts +27 -0
  62. package/src/organization-model/__tests__/knowledge.test.ts +214 -0
  63. package/src/organization-model/contracts.ts +17 -15
  64. package/src/organization-model/defaults.ts +74 -19
  65. package/src/organization-model/domains/knowledge.ts +53 -0
  66. package/src/organization-model/domains/navigation.ts +416 -399
  67. package/src/organization-model/domains/prospecting.ts +204 -1
  68. package/src/organization-model/domains/sales.test.ts +29 -0
  69. package/src/organization-model/domains/sales.ts +102 -0
  70. package/src/organization-model/domains/shared.ts +6 -5
  71. package/src/organization-model/foundation.ts +10 -6
  72. package/src/organization-model/graph/build.ts +209 -182
  73. package/src/organization-model/graph/schema.ts +37 -34
  74. package/src/organization-model/graph/types.ts +47 -31
  75. package/src/organization-model/icons.ts +81 -0
  76. package/src/organization-model/index.ts +8 -3
  77. package/src/organization-model/organization-model.mdx +1 -1
  78. package/src/organization-model/published.ts +103 -86
  79. package/src/organization-model/schema.ts +90 -85
  80. package/src/organization-model/types.ts +42 -35
  81. package/src/platform/constants/versions.ts +1 -1
  82. package/src/platform/index.ts +23 -27
  83. package/src/platform/registry/index.ts +0 -4
  84. package/src/platform/registry/resource-registry.ts +0 -77
  85. package/src/platform/registry/serialized-types.ts +148 -219
  86. package/src/platform/registry/stats-types.ts +60 -60
  87. package/src/reference/_generated/contracts.md +829 -595
  88. package/src/supabase/database.types.ts +2978 -2958
@@ -0,0 +1,351 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { getDealOwnership } from './deal-ownership'
3
+
4
+ // ---------------------------------------------------------------------------
5
+ // Helpers
6
+ // ---------------------------------------------------------------------------
7
+
8
+ function makeEvent(type: string, timestamp: string): Record<string, unknown> {
9
+ return { type, timestamp }
10
+ }
11
+
12
+ // ---------------------------------------------------------------------------
13
+ // Closed deals
14
+ // ---------------------------------------------------------------------------
15
+
16
+ describe('getDealOwnership — closed deals', () => {
17
+ it('returns null for closed_won regardless of activity_log', () => {
18
+ const deal = {
19
+ state_key: 'closed_won',
20
+ activity_log: [makeEvent('reply_received', '2026-01-10T10:00:00Z')]
21
+ }
22
+ expect(getDealOwnership(deal)).toBeNull()
23
+ })
24
+
25
+ it('returns null for closed_lost regardless of activity_log', () => {
26
+ const deal = {
27
+ state_key: 'closed_lost',
28
+ activity_log: [makeEvent('reply_received', '2026-01-10T10:00:00Z')]
29
+ }
30
+ expect(getDealOwnership(deal)).toBeNull()
31
+ })
32
+ })
33
+
34
+ // ---------------------------------------------------------------------------
35
+ // Empty / missing activity log
36
+ // ---------------------------------------------------------------------------
37
+
38
+ describe('getDealOwnership — empty activity log', () => {
39
+ it('returns null when activity_log is an empty array', () => {
40
+ expect(getDealOwnership({ state_key: 'discovery_replied', activity_log: [] })).toBeNull()
41
+ })
42
+
43
+ it('returns null when activity_log is null', () => {
44
+ expect(getDealOwnership({ state_key: 'discovery_replied', activity_log: null })).toBeNull()
45
+ })
46
+
47
+ it('returns null when activity_log is undefined', () => {
48
+ expect(getDealOwnership({ state_key: 'discovery_replied', activity_log: undefined })).toBeNull()
49
+ })
50
+
51
+ it('returns null when activity_log is a non-array value', () => {
52
+ expect(getDealOwnership({ state_key: 'discovery_replied', activity_log: 'bad' })).toBeNull()
53
+ })
54
+
55
+ it('returns null when activity_log contains no inbound or outbound events', () => {
56
+ const deal = {
57
+ state_key: 'discovery_replied',
58
+ activity_log: [
59
+ makeEvent('deal_created', '2026-01-01T00:00:00Z'),
60
+ makeEvent('stage_change', '2026-01-02T00:00:00Z')
61
+ ]
62
+ }
63
+ expect(getDealOwnership(deal)).toBeNull()
64
+ })
65
+ })
66
+
67
+ // ---------------------------------------------------------------------------
68
+ // us ownership (inbound is most recent)
69
+ // ---------------------------------------------------------------------------
70
+
71
+ describe('getDealOwnership — us ownership', () => {
72
+ it('returns "us" when inbound event is newer than outbound event', () => {
73
+ const deal = {
74
+ state_key: 'discovery_replied',
75
+ activity_log: [
76
+ makeEvent('reply_sent_to_lead', '2026-01-10T10:00:00Z'),
77
+ makeEvent('reply_received', '2026-01-11T10:00:00Z')
78
+ ]
79
+ }
80
+ expect(getDealOwnership(deal)).toBe('us')
81
+ })
82
+
83
+ it('returns "us" when only a reply_received event exists', () => {
84
+ const deal = {
85
+ state_key: 'discovery_replied',
86
+ activity_log: [makeEvent('reply_received', '2026-01-10T10:00:00Z')]
87
+ }
88
+ expect(getDealOwnership(deal)).toBe('us')
89
+ })
90
+
91
+ it('returns "us" with mixed irrelevant events present', () => {
92
+ const deal = {
93
+ state_key: 'discovery_replied',
94
+ activity_log: [
95
+ makeEvent('deal_created', '2026-01-01T00:00:00Z'),
96
+ makeEvent('reply_sent_to_lead', '2026-01-10T08:00:00Z'),
97
+ makeEvent('state_change', '2026-01-10T09:00:00Z'),
98
+ makeEvent('reply_received', '2026-01-10T12:00:00Z')
99
+ ]
100
+ }
101
+ expect(getDealOwnership(deal)).toBe('us')
102
+ })
103
+
104
+ it('returns "us" picking the latest of multiple inbound events', () => {
105
+ const deal = {
106
+ state_key: 'discovery_replied',
107
+ activity_log: [
108
+ makeEvent('reply_sent_to_lead', '2026-01-10T10:00:00Z'),
109
+ makeEvent('reply_received', '2026-01-09T10:00:00Z'),
110
+ makeEvent('reply_received', '2026-01-11T10:00:00Z')
111
+ ]
112
+ }
113
+ expect(getDealOwnership(deal)).toBe('us')
114
+ })
115
+ })
116
+
117
+ // ---------------------------------------------------------------------------
118
+ // them ownership (outbound is most recent or tied)
119
+ // ---------------------------------------------------------------------------
120
+
121
+ describe('getDealOwnership — them ownership', () => {
122
+ it('returns "them" when outbound event is newer than inbound event', () => {
123
+ const deal = {
124
+ state_key: 'discovery_replied',
125
+ activity_log: [
126
+ makeEvent('reply_received', '2026-01-10T10:00:00Z'),
127
+ makeEvent('reply_sent_to_lead', '2026-01-11T10:00:00Z')
128
+ ]
129
+ }
130
+ expect(getDealOwnership(deal)).toBe('them')
131
+ })
132
+
133
+ it('returns "them" on exact timestamp tie (outbound wins tie-break)', () => {
134
+ const deal = {
135
+ state_key: 'discovery_replied',
136
+ activity_log: [
137
+ makeEvent('reply_received', '2026-01-10T10:00:00Z'),
138
+ makeEvent('reply_sent_to_lead', '2026-01-10T10:00:00Z')
139
+ ]
140
+ }
141
+ expect(getDealOwnership(deal)).toBe('them')
142
+ })
143
+
144
+ it('returns "them" when only outbound events exist', () => {
145
+ const deal = {
146
+ state_key: 'discovery_replied',
147
+ activity_log: [makeEvent('booking_nudge_sent', '2026-01-10T10:00:00Z')]
148
+ }
149
+ expect(getDealOwnership(deal)).toBe('them')
150
+ })
151
+
152
+ it('returns "them" for each outbound type independently', () => {
153
+ const outboundTypes = [
154
+ 'reply_sent_to_lead',
155
+ 'booking_nudge_sent',
156
+ 'reminder_sent',
157
+ 'rebook_sent',
158
+ 'followup_email_sent',
159
+ 'reply_followup_sent',
160
+ 'lead_deferred_next_step'
161
+ ] as const
162
+
163
+ for (const type of outboundTypes) {
164
+ const deal = {
165
+ state_key: 'discovery_replied',
166
+ activity_log: [makeEvent(type, '2026-01-10T10:00:00Z')]
167
+ }
168
+ expect(getDealOwnership(deal), `expected "them" for outbound type: ${type}`).toBe('them')
169
+ }
170
+ })
171
+
172
+ it('returns "them" picking the latest of multiple outbound events', () => {
173
+ const deal = {
174
+ state_key: 'discovery_replied',
175
+ activity_log: [
176
+ makeEvent('reply_received', '2026-01-10T10:00:00Z'),
177
+ makeEvent('reply_sent_to_lead', '2026-01-09T10:00:00Z'),
178
+ makeEvent('booking_nudge_sent', '2026-01-11T10:00:00Z')
179
+ ]
180
+ }
181
+ expect(getDealOwnership(deal)).toBe('them')
182
+ })
183
+
184
+ it('treats canonical and legacy follow-up events as outbound ownership inputs', () => {
185
+ const canonical = {
186
+ state_key: 'followup_1_sent',
187
+ activity_log: [
188
+ makeEvent('reply_received', '2026-01-10T10:00:00Z'),
189
+ makeEvent('followup_email_sent', '2026-01-11T10:00:00Z')
190
+ ]
191
+ }
192
+ const legacy = {
193
+ state_key: 'followup_1_sent',
194
+ activity_log: [
195
+ makeEvent('reply_received', '2026-01-10T10:00:00Z'),
196
+ makeEvent('reply_followup_sent', '2026-01-11T10:00:00Z')
197
+ ]
198
+ }
199
+
200
+ expect(getDealOwnership(canonical)).toBe('them')
201
+ expect(getDealOwnership(legacy)).toBe('them')
202
+ })
203
+ })
204
+
205
+ // ---------------------------------------------------------------------------
206
+ // Timestamp COALESCE — alternate timestamp field names
207
+ // ---------------------------------------------------------------------------
208
+
209
+ describe('getDealOwnership — timestamp field coalesce', () => {
210
+ it('resolves timestamp from occurredAt field', () => {
211
+ const deal = {
212
+ state_key: 'discovery_replied',
213
+ activity_log: [
214
+ { type: 'reply_received', occurredAt: '2026-01-11T10:00:00Z' },
215
+ { type: 'reply_sent_to_lead', occurredAt: '2026-01-10T10:00:00Z' }
216
+ ]
217
+ }
218
+ expect(getDealOwnership(deal)).toBe('us')
219
+ })
220
+
221
+ it('resolves timestamp from createdAt field', () => {
222
+ const deal = {
223
+ state_key: 'discovery_replied',
224
+ activity_log: [{ type: 'reply_received', createdAt: '2026-01-10T10:00:00Z' }]
225
+ }
226
+ expect(getDealOwnership(deal)).toBe('us')
227
+ })
228
+
229
+ it('resolves timestamp from updatedAt field', () => {
230
+ const deal = {
231
+ state_key: 'discovery_replied',
232
+ activity_log: [{ type: 'reply_sent_to_lead', updatedAt: '2026-01-10T10:00:00Z' }]
233
+ }
234
+ expect(getDealOwnership(deal)).toBe('them')
235
+ })
236
+
237
+ it('resolves timestamp from sentAt field', () => {
238
+ const deal = {
239
+ state_key: 'discovery_replied',
240
+ activity_log: [{ type: 'reply_sent_to_lead', sentAt: '2026-01-10T10:00:00Z' }]
241
+ }
242
+ expect(getDealOwnership(deal)).toBe('them')
243
+ })
244
+
245
+ it('prefers timestamp over occurredAt when both present', () => {
246
+ // outbound has timestamp=newer, inbound has occurredAt=older → "them"
247
+ const deal = {
248
+ state_key: 'discovery_replied',
249
+ activity_log: [
250
+ { type: 'reply_received', timestamp: '2026-01-09T00:00:00Z', occurredAt: '2026-01-12T00:00:00Z' },
251
+ { type: 'reply_sent_to_lead', timestamp: '2026-01-10T00:00:00Z' }
252
+ ]
253
+ }
254
+ // inbound timestamp=Jan 9, outbound timestamp=Jan 10 → outbound newer → "them"
255
+ expect(getDealOwnership(deal)).toBe('them')
256
+ })
257
+ })
258
+
259
+ // ---------------------------------------------------------------------------
260
+ // Malformed entries — must be silently skipped
261
+ // ---------------------------------------------------------------------------
262
+
263
+ describe('getDealOwnership — malformed events skipped', () => {
264
+ it('skips entries with no timestamp field', () => {
265
+ const deal = {
266
+ state_key: 'discovery_replied',
267
+ activity_log: [
268
+ { type: 'reply_received' }, // no timestamp
269
+ makeEvent('reply_sent_to_lead', '2026-01-10T10:00:00Z')
270
+ ]
271
+ }
272
+ // Only the outbound event has a valid timestamp — result is "them"
273
+ expect(getDealOwnership(deal)).toBe('them')
274
+ })
275
+
276
+ it('skips entries with an invalid timestamp string', () => {
277
+ const deal = {
278
+ state_key: 'discovery_replied',
279
+ activity_log: [
280
+ { type: 'reply_received', timestamp: 'not-a-date' },
281
+ makeEvent('reply_sent_to_lead', '2026-01-10T10:00:00Z')
282
+ ]
283
+ }
284
+ expect(getDealOwnership(deal)).toBe('them')
285
+ })
286
+
287
+ it('skips null entries in the array', () => {
288
+ const deal = {
289
+ state_key: 'discovery_replied',
290
+ activity_log: [null, makeEvent('reply_received', '2026-01-10T10:00:00Z')]
291
+ }
292
+ expect(getDealOwnership(deal)).toBe('us')
293
+ })
294
+
295
+ it('skips non-object entries in the array', () => {
296
+ const deal = {
297
+ state_key: 'discovery_replied',
298
+ activity_log: ['string', 42, makeEvent('reply_sent_to_lead', '2026-01-10T10:00:00Z')]
299
+ }
300
+ expect(getDealOwnership(deal)).toBe('them')
301
+ })
302
+
303
+ it('skips array-in-array entries', () => {
304
+ const deal = {
305
+ state_key: 'discovery_replied',
306
+ activity_log: [[], makeEvent('reply_received', '2026-01-10T10:00:00Z')]
307
+ }
308
+ expect(getDealOwnership(deal)).toBe('us')
309
+ })
310
+
311
+ it('skips entries with missing type field', () => {
312
+ const deal = {
313
+ state_key: 'discovery_replied',
314
+ activity_log: [
315
+ { timestamp: '2026-01-10T10:00:00Z' }, // no type
316
+ makeEvent('reply_sent_to_lead', '2026-01-11T10:00:00Z')
317
+ ]
318
+ }
319
+ expect(getDealOwnership(deal)).toBe('them')
320
+ })
321
+
322
+ it('returns null when all entries are malformed', () => {
323
+ const deal = {
324
+ state_key: 'discovery_replied',
325
+ activity_log: [null, 'bad', { type: 'reply_received' }]
326
+ }
327
+ expect(getDealOwnership(deal)).toBeNull()
328
+ })
329
+ })
330
+
331
+ // ---------------------------------------------------------------------------
332
+ // state_key variants (non-closed)
333
+ // ---------------------------------------------------------------------------
334
+
335
+ describe('getDealOwnership — non-closed state_key variants', () => {
336
+ it('evaluates ownership for null state_key', () => {
337
+ const deal = {
338
+ state_key: null,
339
+ activity_log: [makeEvent('reply_received', '2026-01-10T10:00:00Z')]
340
+ }
341
+ expect(getDealOwnership(deal)).toBe('us')
342
+ })
343
+
344
+ it('evaluates ownership for discovery_link_sent state', () => {
345
+ const deal = {
346
+ state_key: 'discovery_link_sent',
347
+ activity_log: [makeEvent('booking_nudge_sent', '2026-01-10T10:00:00Z')]
348
+ }
349
+ expect(getDealOwnership(deal)).toBe('them')
350
+ })
351
+ })
@@ -0,0 +1,120 @@
1
+ // ---------------------------------------------------------------------------
2
+ // Deal ownership axis — derived from activity_log (Pattern A, approved).
3
+ //
4
+ // Ownership reflects who owes the next move, not a stored column.
5
+ // Closed deals always return null — ownership is meaningless once won/lost.
6
+ // ---------------------------------------------------------------------------
7
+
8
+ export type DealOwnership = 'us' | 'them' | null
9
+
10
+ // Inbound events: lead acted, ball is in our court.
11
+ const INBOUND_EVENT_TYPES = ['reply_received'] as const
12
+
13
+ // Outbound events: we acted, ball is in their court.
14
+ const OUTBOUND_EVENT_TYPES = [
15
+ 'reply_sent_to_lead',
16
+ 'booking_nudge_sent',
17
+ 'reminder_sent',
18
+ 'rebook_sent',
19
+ 'followup_email_sent',
20
+ 'reply_followup_sent',
21
+ 'lead_deferred_next_step'
22
+ ] as const
23
+
24
+ type InboundEventType = (typeof INBOUND_EVENT_TYPES)[number]
25
+ type OutboundEventType = (typeof OUTBOUND_EVENT_TYPES)[number]
26
+
27
+ const INBOUND_SET = new Set<string>(INBOUND_EVENT_TYPES)
28
+ const OUTBOUND_SET = new Set<string>(OUTBOUND_EVENT_TYPES)
29
+
30
+ interface DealOwnershipInput {
31
+ state_key: string | null
32
+ activity_log: unknown
33
+ }
34
+
35
+ /**
36
+ * Derives who owes the next move from the activity log.
37
+ *
38
+ * - `'us'` — the lead replied last; we owe a response.
39
+ * - `'them'` — we sent something last; waiting on their reply.
40
+ * - `null` — closed deal, or no inbound/outbound events present.
41
+ *
42
+ * Timestamp resolution matches the canonical pattern in crm-priority.ts:
43
+ * COALESCE across `timestamp ?? occurredAt ?? createdAt ?? updatedAt ?? sentAt`.
44
+ */
45
+ export function getDealOwnership(deal: DealOwnershipInput): DealOwnership {
46
+ if (deal.state_key === 'closed_won' || deal.state_key === 'closed_lost') {
47
+ return null
48
+ }
49
+
50
+ if (!Array.isArray(deal.activity_log)) return null
51
+
52
+ let latestInboundMs: number | null = null
53
+ let latestOutboundMs: number | null = null
54
+
55
+ for (const entry of deal.activity_log) {
56
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) continue
57
+
58
+ const record = entry as Record<string, unknown>
59
+ const type = typeof record.type === 'string' ? record.type : null
60
+ if (!type) continue
61
+
62
+ const isInbound = INBOUND_SET.has(type)
63
+ const isOutbound = OUTBOUND_SET.has(type)
64
+ if (!isInbound && !isOutbound) continue
65
+
66
+ const occurredAt = resolveTimestamp(record)
67
+ if (occurredAt === null) continue
68
+
69
+ if (isInbound && (latestInboundMs === null || occurredAt > latestInboundMs)) {
70
+ latestInboundMs = occurredAt
71
+ }
72
+
73
+ if (isOutbound && (latestOutboundMs === null || occurredAt > latestOutboundMs)) {
74
+ latestOutboundMs = occurredAt
75
+ }
76
+ }
77
+
78
+ if (latestInboundMs === null && latestOutboundMs === null) return null
79
+
80
+ // Tie-break: outbound wins ("after we send, ball is in their court")
81
+ if (latestOutboundMs !== null && (latestInboundMs === null || latestOutboundMs >= latestInboundMs)) {
82
+ return 'them'
83
+ }
84
+
85
+ return 'us'
86
+ }
87
+
88
+ // ---------------------------------------------------------------------------
89
+ // Internal helpers
90
+ // ---------------------------------------------------------------------------
91
+
92
+ /**
93
+ * COALESCE timestamp fields in the same order as getParsedActivities in
94
+ * crm-priority.ts: timestamp → occurredAt → createdAt → updatedAt → sentAt.
95
+ * Returns milliseconds since epoch, or null if no valid date found.
96
+ */
97
+ function resolveTimestamp(record: Record<string, unknown>): number | null {
98
+ return (
99
+ parseMs(record.timestamp) ??
100
+ parseMs(record.occurredAt) ??
101
+ parseMs(record.createdAt) ??
102
+ parseMs(record.updatedAt) ??
103
+ parseMs(record.sentAt) ??
104
+ null
105
+ )
106
+ }
107
+
108
+ function parseMs(value: unknown): number | null {
109
+ if (value instanceof Date) {
110
+ const ms = value.getTime()
111
+ return Number.isNaN(ms) ? null : ms
112
+ }
113
+ if (typeof value !== 'string') return null
114
+ const ms = new Date(value).getTime()
115
+ return Number.isNaN(ms) ? null : ms
116
+ }
117
+
118
+ // Re-export event type lists for consumers that want to introspect classification.
119
+ export { INBOUND_EVENT_TYPES, OUTBOUND_EVENT_TYPES }
120
+ export type { InboundEventType, OutboundEventType }
@@ -12,7 +12,9 @@ import { DEFAULT_CRM_ACTIONS, SendReplyActionPayloadSchema, deriveActions } from
12
12
  * Only stage_key and state_key affect availability logic; all other required
13
13
  * columns are filled with safe, inert defaults.
14
14
  */
15
- function makeDeal(overrides: Partial<Pick<AcqDealRow, 'stage_key' | 'state_key'>> = {}): AcqDealRow {
15
+ function makeDeal(
16
+ overrides: Partial<AcqDealRow> & { ownership?: 'us' | 'them' | null; nextAction?: string | null } = {}
17
+ ): AcqDealRow & { ownership?: 'us' | 'them' | null; nextAction?: string | null } {
16
18
  return {
17
19
  id: 'deal-fixture-id',
18
20
  organization_id: 'org-fixture-id',
@@ -392,8 +394,10 @@ describe('deriveActions() — custom action set isolation', () => {
392
394
  // ---------------------------------------------------------------------------
393
395
 
394
396
  describe('deriveActions() — action metadata preservation', () => {
395
- it('send_reply action includes payloadSchema in output', () => {
396
- const actions = deriveActions(makeDeal({ stage_key: 'interested', state_key: 'discovery_replied' }))
397
+ it('send_reply action includes payloadSchema in output', () => {
398
+ const actions = deriveActions(
399
+ makeDeal({ stage_key: 'interested', state_key: 'discovery_replied', nextAction: 'send_reply' })
400
+ )
397
401
  const sendReply = actions.find((a) => a.key === 'send_reply')
398
402
  expect(sendReply).toBeDefined()
399
403
  expect(sendReply?.payloadSchema).toBeDefined()
@@ -459,40 +463,100 @@ describe('deriveActions() — multi-state discovery path coverage', () => {
459
463
  ])
460
464
  })
461
465
 
462
- it('interested + discovery_replied: base actions plus send_reply and send_link', () => {
463
- expect(deriveActions(makeDeal({ stage_key: 'interested', state_key: 'discovery_replied' }))).toEqual([
464
- { key: 'move_to_proposal', label: 'Move to Proposal' },
465
- { key: 'move_to_closed_lost', label: 'Close Lost' },
466
- { key: 'move_to_nurturing', label: 'Move to Nurturing' },
467
- { key: 'send_reply', label: 'Send Reply', payloadSchema: SendReplyActionPayloadSchema },
468
- { key: 'send_link', label: 'Send Booking Link' }
469
- ])
470
- })
471
-
472
- it('interested + discovery_link_sent: base actions plus send_reply and send_nudge', () => {
473
- expect(deriveActions(makeDeal({ stage_key: 'interested', state_key: 'discovery_link_sent' }))).toEqual([
474
- { key: 'move_to_proposal', label: 'Move to Proposal' },
475
- { key: 'move_to_closed_lost', label: 'Close Lost' },
476
- { key: 'move_to_nurturing', label: 'Move to Nurturing' },
477
- { key: 'send_reply', label: 'Send Reply', payloadSchema: SendReplyActionPayloadSchema },
478
- { key: 'send_nudge', label: 'Send Nudge' }
479
- ])
480
- })
481
-
482
- it('interested + discovery_nudging: base actions plus send_reply, send_nudge, and mark_no_show', () => {
483
- expect(deriveActions(makeDeal({ stage_key: 'interested', state_key: 'discovery_nudging' }))).toEqual([
484
- { key: 'move_to_proposal', label: 'Move to Proposal' },
485
- { key: 'move_to_closed_lost', label: 'Close Lost' },
486
- { key: 'move_to_nurturing', label: 'Move to Nurturing' },
487
- { key: 'send_reply', label: 'Send Reply', payloadSchema: SendReplyActionPayloadSchema },
488
- { key: 'send_nudge', label: 'Send Nudge' },
489
- { key: 'mark_no_show', label: 'Mark No-Show' }
490
- ])
491
- })
492
- })
493
-
494
- // ---------------------------------------------------------------------------
495
- // Type-level smoke tests (compile-time only, no runtime assertions)
466
+ it('interested + discovery_replied: base actions plus send_link when no next move signal is available', () => {
467
+ expect(deriveActions(makeDeal({ stage_key: 'interested', state_key: 'discovery_replied' }))).toEqual([
468
+ { key: 'move_to_proposal', label: 'Move to Proposal' },
469
+ { key: 'move_to_closed_lost', label: 'Close Lost' },
470
+ { key: 'move_to_nurturing', label: 'Move to Nurturing' },
471
+ { key: 'send_link', label: 'Send Booking Link' }
472
+ ])
473
+ })
474
+
475
+ it('interested + discovery_link_sent: base actions plus send_nudge', () => {
476
+ expect(deriveActions(makeDeal({ stage_key: 'interested', state_key: 'discovery_link_sent' }))).toEqual([
477
+ { key: 'move_to_proposal', label: 'Move to Proposal' },
478
+ { key: 'move_to_closed_lost', label: 'Close Lost' },
479
+ { key: 'move_to_nurturing', label: 'Move to Nurturing' },
480
+ { key: 'send_nudge', label: 'Send Nudge' }
481
+ ])
482
+ })
483
+
484
+ it('interested + discovery_nudging: base actions plus send_nudge and mark_no_show', () => {
485
+ expect(deriveActions(makeDeal({ stage_key: 'interested', state_key: 'discovery_nudging' }))).toEqual([
486
+ { key: 'move_to_proposal', label: 'Move to Proposal' },
487
+ { key: 'move_to_closed_lost', label: 'Close Lost' },
488
+ { key: 'move_to_nurturing', label: 'Move to Nurturing' },
489
+ { key: 'send_nudge', label: 'Send Nudge' },
490
+ { key: 'mark_no_show', label: 'Mark No-Show' }
491
+ ])
492
+ })
493
+ })
494
+
495
+ // ---------------------------------------------------------------------------
496
+ // deriveActions() - ownership-gated reply action
497
+ // ---------------------------------------------------------------------------
498
+
499
+ describe('deriveActions() - ownership-gated reply action', () => {
500
+ it('lead asks us to respond: send_reply is available and ownership is our move', () => {
501
+ const actions = deriveActions(
502
+ makeDeal({
503
+ stage_key: 'interested',
504
+ state_key: 'discovery_replied',
505
+ activity_log: [{ type: 'reply_received', timestamp: '2026-01-10T10:00:00Z' }]
506
+ })
507
+ )
508
+
509
+ expect(actions).toContainEqual({ key: 'send_reply', label: 'Send Reply', payloadSchema: SendReplyActionPayloadSchema })
510
+ })
511
+
512
+ it('lead deferred next step: send_reply is not available while it is their move', () => {
513
+ const actions = deriveActions(
514
+ makeDeal({
515
+ stage_key: 'interested',
516
+ state_key: 'discovery_replied',
517
+ activity_log: [
518
+ { type: 'reply_received', timestamp: '2026-01-10T10:00:00Z' },
519
+ { type: 'lead_deferred_next_step', timestamp: '2026-01-10T10:05:00Z' }
520
+ ]
521
+ })
522
+ )
523
+
524
+ expect(actions.map((action) => action.key)).not.toContain('send_reply')
525
+ expect(actions).toContainEqual({ key: 'send_link', label: 'Send Booking Link' })
526
+ })
527
+
528
+ it('outbound after lead response: send_reply is not available while it is their move', () => {
529
+ const actions = deriveActions(
530
+ makeDeal({
531
+ stage_key: 'interested',
532
+ state_key: 'discovery_replied',
533
+ activity_log: [
534
+ { type: 'reply_received', timestamp: '2026-01-10T10:00:00Z' },
535
+ { type: 'reply_sent_to_lead', timestamp: '2026-01-10T10:05:00Z' }
536
+ ]
537
+ })
538
+ )
539
+
540
+ expect(actions.map((action) => action.key)).not.toContain('send_reply')
541
+ expect(actions).toContainEqual({ key: 'send_link', label: 'Send Booking Link' })
542
+ })
543
+
544
+ it('uses projected nextAction before deriving from activity_log', () => {
545
+ const actions = deriveActions(
546
+ makeDeal({
547
+ stage_key: 'interested',
548
+ state_key: 'discovery_replied',
549
+ nextAction: null,
550
+ activity_log: [{ type: 'reply_received', timestamp: '2026-01-10T10:00:00Z' }]
551
+ })
552
+ )
553
+
554
+ expect(actions.map((action) => action.key)).not.toContain('send_reply')
555
+ })
556
+ })
557
+
558
+ // ---------------------------------------------------------------------------
559
+ // Type-level smoke tests (compile-time only, no runtime assertions)
496
560
  // ---------------------------------------------------------------------------
497
561
 
498
562
  describe('type-level smoke', () => {