@elevasis/core 0.14.0 → 0.15.1
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.
- package/dist/index.d.ts +60 -0
- package/dist/index.js +198 -1
- package/dist/organization-model/index.d.ts +60 -0
- package/dist/organization-model/index.js +198 -1
- package/dist/test-utils/index.d.ts +399 -363
- package/dist/test-utils/index.js +198 -1
- package/package.json +3 -3
- package/src/_gen/__tests__/__snapshots__/contracts.md.snap +444 -309
- package/src/business/acquisition/activity-events.ts +12 -3
- package/src/business/acquisition/api-schemas.test.ts +315 -4
- package/src/business/acquisition/api-schemas.ts +140 -17
- package/src/business/acquisition/build-templates.ts +44 -0
- package/src/business/acquisition/crm-next-action.test.ts +262 -0
- package/src/business/acquisition/crm-next-action.ts +220 -0
- package/src/business/acquisition/crm-priority.test.ts +216 -0
- package/src/business/acquisition/crm-priority.ts +349 -0
- package/src/business/acquisition/crm-state-actions.test.ts +12 -21
- package/src/business/acquisition/deal-ownership.test.ts +351 -0
- package/src/business/acquisition/deal-ownership.ts +120 -0
- package/src/business/acquisition/derive-actions.test.ts +101 -37
- package/src/business/acquisition/derive-actions.ts +49 -24
- package/src/business/acquisition/index.ts +163 -149
- package/src/business/acquisition/types.ts +48 -4
- package/src/execution/engine/index.ts +4 -3
- package/src/execution/engine/tools/lead-service-types.ts +68 -51
- package/src/execution/engine/tools/platform/acquisition/list-tools.ts +6 -5
- package/src/execution/engine/tools/platform/acquisition/types.ts +3 -1
- package/src/execution/engine/tools/registry.ts +4 -3
- package/src/execution/engine/tools/tool-maps.ts +821 -816
- package/src/organization-model/domains/prospecting.ts +204 -1
- package/src/organization-model/domains/sales.test.ts +218 -0
- package/src/organization-model/domains/sales.ts +558 -366
- package/src/organization-model/types.ts +2 -2
- package/src/platform/constants/versions.ts +1 -1
- package/src/reference/_generated/contracts.md +444 -309
- 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(
|
|
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(
|
|
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
|
|
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: '
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
{ key: '
|
|
475
|
-
{ key: '
|
|
476
|
-
{ key: '
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
{ key: '
|
|
485
|
-
{ key: '
|
|
486
|
-
{ key: '
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
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', () => {
|