@elevasis/core 0.13.0 → 0.15.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 (42) 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 +463 -377
  6. package/dist/test-utils/index.js +9 -2
  7. package/package.json +1 -1
  8. package/src/_gen/__tests__/__snapshots__/contracts.md.snap +2336 -0
  9. package/src/business/acquisition/activity-events.test.ts +250 -0
  10. package/src/business/acquisition/activity-events.ts +7 -65
  11. package/src/business/acquisition/api-schemas.test.ts +1180 -0
  12. package/src/business/acquisition/api-schemas.ts +317 -73
  13. package/src/business/acquisition/crm-state-actions.test.ts +160 -0
  14. package/src/business/acquisition/derive-actions.test.ts +518 -0
  15. package/src/business/acquisition/derive-actions.ts +101 -78
  16. package/src/business/acquisition/index.ts +51 -9
  17. package/src/business/acquisition/stateful.ts +30 -0
  18. package/src/business/acquisition/types.ts +48 -80
  19. package/src/execution/engine/index.ts +437 -434
  20. package/src/execution/engine/tools/integration/server/adapters/attio/__tests__/attio-crud.integration.test.ts +363 -360
  21. package/src/execution/engine/tools/integration/server/adapters/attio/fetch/get-record/index.test.ts +162 -186
  22. package/src/execution/engine/tools/integration/server/adapters/attio/fetch/list-records/index.test.ts +316 -338
  23. package/src/execution/engine/tools/integration/server/adapters/gmail/gmail-adapter.ts +204 -210
  24. package/src/execution/engine/tools/integration/server/adapters/resend/fetch/send-email/index.test.ts +88 -0
  25. package/src/execution/engine/tools/integration/server/adapters/resend/fetch/send-email/index.ts +141 -134
  26. package/src/execution/engine/tools/integration/server/adapters/resend/fetch/utils/types.ts +76 -75
  27. package/src/execution/engine/tools/integration/service.test.ts +34 -9
  28. package/src/execution/engine/tools/integration/service.ts +6 -3
  29. package/src/execution/engine/tools/lead-service-types.ts +934 -874
  30. package/src/execution/engine/tools/platform/acquisition/types.ts +266 -260
  31. package/src/execution/engine/tools/registry.ts +701 -699
  32. package/src/execution/engine/tools/tool-maps.ts +30 -2
  33. package/src/execution/engine/workflow/types.ts +11 -0
  34. package/src/organization-model/contracts.ts +4 -4
  35. package/src/organization-model/domains/navigation.ts +62 -62
  36. package/src/organization-model/domains/sales.test.ts +189 -0
  37. package/src/organization-model/domains/sales.ts +456 -94
  38. package/src/organization-model/published.ts +21 -21
  39. package/src/organization-model/resolve.ts +21 -8
  40. package/src/platform/constants/versions.ts +1 -1
  41. package/src/reference/_generated/contracts.md +2336 -0
  42. package/src/supabase/database.types.ts +2958 -2886
@@ -1,473 +1,513 @@
1
- /**
2
- * Lead Service Types
3
- * CRUD operation types for the acquisition platform (lists, companies, contacts, deals)
4
- *
5
- * Implementation: apps/api/src/acquisition/lead-service.ts (LeadService class)
6
- */
7
-
8
- import type { Json } from '../../../supabase'
9
-
10
- // Re-export acquisition domain types from the authoritative source
11
- import type {
12
- AcqList,
13
- AcqCompany,
14
- AcqContact,
15
- AcqDealTask,
16
- AcqDealTaskKind,
17
- CompanyListStage,
18
- ContactListStage,
19
- ListConfig,
20
- ListTelemetry
1
+ /**
2
+ * Lead Service Types
3
+ * CRUD operation types for the acquisition platform (lists, companies, contacts, deals)
4
+ *
5
+ * Implementation: apps/api/src/acquisition/lead-service.ts (LeadService class)
6
+ */
7
+
8
+ import type { Json } from '../../../supabase'
9
+
10
+ // Re-export acquisition domain types from the authoritative source
11
+ import type {
12
+ AcqList,
13
+ AcqCompany,
14
+ AcqContact,
15
+ AcqDealTask,
16
+ AcqDealTaskKind,
17
+ ListStatus,
18
+ ScrapingConfig,
19
+ IcpRubric,
20
+ PipelineConfig,
21
+ ListTelemetry,
22
+ DealDetail
21
23
  } from '../../../business/acquisition/types'
22
-
23
- export type {
24
- AcqList,
25
- AcqCompany,
26
- AcqContact,
27
- AcqDealTask,
28
- AcqDealTaskKind,
29
- CompanyListStage,
30
- ContactListStage,
31
- ListConfig,
32
- ListTelemetry
33
- }
34
-
35
- // Pagination types
36
- export interface PaginationParams {
37
- limit: number
38
- offset: number
39
- }
40
-
41
- export interface PaginatedResult<T> {
42
- data: T[]
43
- total: number
44
- limit: number
45
- offset: number
46
- }
47
-
48
- // List params
49
- export interface CreateListParams {
50
- organizationId: string
51
- name: string
52
- description?: string
53
- type?: string
54
- batchIds?: string[]
55
- instantlyCampaignId?: string
56
- status?: string
57
- metadata?: Record<string, unknown>
58
- config?: ListConfig
59
- }
60
-
61
- export interface UpdateListParams {
62
- name?: string
63
- description?: string
64
- status?: string
65
- batchIds?: string[]
66
- }
67
-
68
- // Company params
69
- export interface CreateCompanyParams {
70
- organizationId: string
71
- name: string
72
- domain?: string
73
- linkedinUrl?: string
74
- website?: string
75
- numEmployees?: number
76
- foundedYear?: number
77
- locationCity?: string
78
- locationState?: string
79
- category?: string
80
- source?: string
81
- batchId?: string
82
- verticalResearch?: string
83
- }
84
-
85
- export interface UpdateCompanyParams {
86
- name?: string
87
- domain?: string
88
- linkedinUrl?: string
89
- website?: string
90
- numEmployees?: number
91
- foundedYear?: number
92
- locationCity?: string
93
- locationState?: string
94
- category?: string
95
- segment?: string
96
- pipelineStatus?: Record<string, unknown>
97
- enrichmentData?: Record<string, unknown>
98
- source?: string
99
- batchId?: string
100
- status?: 'active' | 'invalid'
101
- verticalResearch?: string | null
102
- }
103
-
104
- export type UpsertCompanyParams = CreateCompanyParams
105
- // Upsert by domain - uses same fields as create
106
-
107
- export interface CompanyFilters {
108
- listId?: string // Filter to companies in a specific list (via acq_list_companies)
109
- search?: string
110
- domain?: string
111
- website?: string
112
- segment?: string
113
- category?: string
114
- pipelineStatus?: Record<string, unknown>
115
- /** Exclude companies whose pipeline_status contains this value (PostgREST NOT contains) */
116
- pipelineStatusNot?: Record<string, unknown>
117
- batchId?: string
118
- status?: 'active' | 'invalid'
119
- includeAll?: boolean
120
- excludeColumns?: Array<'enrichmentData' | 'pipelineStatus'>
121
- }
122
-
123
- // Contact params
124
- export interface CreateContactParams {
125
- organizationId: string
126
- email: string
127
- companyId?: string
128
- firstName?: string
129
- lastName?: string
130
- linkedinUrl?: string
131
- title?: string
132
- source?: string
133
- sourceId?: string
134
- batchId?: string
135
- }
136
-
137
- export interface UpdateContactParams {
138
- companyId?: string
139
- emailValid?: 'VALID' | 'INVALID' | 'RISKY' | 'UNKNOWN'
140
- firstName?: string
141
- lastName?: string
142
- linkedinUrl?: string
143
- title?: string
144
- headline?: string
145
- filterReason?: string
146
- openingLine?: string
147
- pipelineStatus?: Record<string, unknown>
148
- enrichmentData?: Record<string, unknown>
149
- status?: 'active' | 'invalid'
150
- }
151
-
152
- export type UpsertContactParams = CreateContactParams
153
- // Upsert by email - uses same fields as create
154
-
155
- export interface ContactFilters {
156
- listId?: string // Filter to contacts in a specific list (via acq_list_members)
157
- search?: string
158
- openingLineIsNull?: boolean // Filter to contacts without personalization
159
- pipelineStatus?: Record<string, unknown>
160
- batchId?: string
161
- contactStatus?: 'active' | 'invalid' // Filter by contact status (soft-delete flag)
162
- }
163
-
164
- // Deal params (for acq_deals table)
165
- export interface UpsertDealParams {
166
- organizationId: string
167
- /** Contact email — dedupe key together with organization_id */
168
- contactEmail: string
169
- /** Optional contact ID for foreign key join */
170
- contactId?: string
171
- /** Campaign list that generated this deal (FK to acq_lists) */
172
- sourceListId?: string
173
- /** Deal origin: 'instantly', 'referral', 'inbound', 'manual' */
174
- sourceType?: 'instantly' | 'referral' | 'inbound' | 'manual'
175
- /** Optional discovery data JSONB to set on upsert */
176
- discoveryData?: unknown
177
- /** Optional proposal data JSONB to set on upsert */
178
- proposalData?: unknown
179
- }
180
-
181
- export interface UpdateDiscoveryDataParams {
182
- organizationId: string
183
- contactEmail: string
184
- discoveryData: unknown
185
- submittedBy?: string
186
- }
187
-
188
- export interface UpdateProposalDataParams {
189
- organizationId: string
190
- contactEmail: string
191
- proposalData: unknown
192
- proposalPdfUrl?: string
193
- }
194
-
195
- export interface MarkProposalSentParams {
196
- organizationId: string
197
- contactEmail: string
198
- }
199
-
200
- export interface MarkProposalReviewedParams {
201
- organizationId: string
202
- contactEmail: string
203
- reviewedBy: string
204
- proposalData?: unknown
205
- }
206
-
207
- export interface UpdateCloseLostReasonParams {
208
- organizationId: string
209
- dealId: string
210
- reason: string
211
- }
212
-
213
- export interface UpdateFeesParams {
214
- organizationId: string
215
- contactEmail?: string
216
- dealId?: string
217
- initialFee?: number
218
- monthlyFee?: number
219
- }
220
-
221
- export interface TransitionItemParams {
222
- organizationId: string
223
- dealId: string
224
- pipelineKey: string
225
- stageKey: string
226
- stateKey?: string | null
227
- reason?: string
228
- expectedUpdatedAt?: string
229
- }
230
-
231
- export interface SetContactNurtureParams {
232
- organizationId: string
233
- contactEmail: string
234
- nurture?: boolean
235
- }
236
-
237
- export interface DeactivateContactsByCompanyParams {
238
- organizationId: string
239
- companyId: string
240
- }
241
-
242
- export interface DeactivateContactsByCompanyResult {
243
- deactivated: number
244
- }
245
-
246
- export interface CancelSchedulesAndHitlByEmailParams {
247
- organizationId: string
248
- email: string
249
- }
250
-
251
- export interface CancelHitlByDealIdParams {
252
- organizationId: string
253
- dealId: string
254
- }
255
-
256
- export interface ClearDealFieldsParams {
257
- organizationId: string
258
- contactEmail?: string
259
- dealId?: string
260
- fields: (
261
- | 'proposalPdfUrl'
262
- | 'proposalGeneratedAt'
263
- | 'initialFee'
264
- | 'monthlyFee'
265
- | 'closedLostReason'
266
- | 'closedLostAt'
267
- | 'discoveryData'
268
- | 'discoverySubmittedAt'
269
- )[]
270
- }
271
-
272
- export interface DeleteDealParams {
273
- organizationId: string
274
- dealId: string
275
- }
276
-
277
- export interface GetDealByIdParams {
278
- dealId: string
279
- organizationId: string
280
- }
281
-
282
- export interface GetContactByIdParams {
283
- contactId: string
284
- organizationId: string
285
- }
286
-
287
- export interface GetCompanyByIdParams {
288
- companyId: string
289
- organizationId: string
290
- }
291
-
292
- // Social monitoring params (acq_social_posts table)
293
- export interface UpsertSocialPostParams {
294
- organizationId: string
295
- platform: string
296
- platformPostId: string
297
- authorName: string
298
- authorUrl?: string | null
299
- postTitle: string
300
- postText: string
301
- postUrl: string
302
- engagementCount?: number
303
- commentsCount?: number
304
- postedAt: string
305
- metadata?: Record<string, unknown>
306
- relevanceScore?: number
307
- matchedKeywords?: string[]
308
- matchedQuery?: string | null
309
- initialDraft?: string | null
310
- finalResponse?: string | null
311
- sourceCategory?: string | null
312
- }
313
-
314
- export interface UpsertSocialPostsParams {
315
- organizationId: string
316
- posts: Omit<UpsertSocialPostParams, 'organizationId'>[]
317
- }
318
-
319
- export interface UpsertSocialPostsResult {
320
- inserted: number
321
- duplicatesSkipped: number
322
- }
323
-
324
- export interface AcqDeal {
325
- id: string
326
- organizationId: string
327
- contactEmail: string
328
- pipelineKey: string
329
- stageKey?: string | null
330
- stateKey?: string | null
331
- discoveryData?: Json | null
332
- proposalData?: Json | null
333
- proposalSentAt?: string | null
334
- proposalPdfUrl?: string | null
335
- signatureEnvelopeId?: string | null
336
- sourceListId?: string | null
337
- sourceType?: string | null
338
- activityLog: DealActivityEntry[]
339
- createdAt: Date
340
- updatedAt: Date
341
- }
342
-
343
- export interface DealActivityEntry {
344
- type: string
345
- title: string
346
- description?: string
347
- payload?: Record<string, unknown>
348
- occurredAt: string
349
- }
350
-
351
- export interface AcqDealNote {
352
- id: string
353
- dealId: string
354
- organizationId: string
355
- authorUserId: string | null
356
- body: string
357
- createdAt: string
358
- updatedAt: string
359
- }
360
-
361
- export interface CreateDealNoteParams {
362
- organizationId: string
363
- dealId: string
364
- body: string
365
- authorUserId?: string
366
- }
367
-
368
- export interface ListDealNotesParams {
369
- organizationId: string
370
- dealId: string
371
- }
372
-
373
- export interface CreateDealTaskParams {
374
- organizationId: string
375
- dealId: string
376
- title: string
377
- description?: string | null
378
- kind?: AcqDealTaskKind
379
- dueAt?: string | null
380
- assigneeUserId?: string | null
381
- createdByUserId?: string | null
382
- }
383
-
384
- export interface ListDealTasksParams {
385
- organizationId: string
386
- dealId: string
387
- }
388
-
389
- export interface ListDealTasksDueParams {
390
- organizationId: string
391
- assigneeUserId?: string | null
392
- /** Window filter: 'overdue' = past due, 'today' = due today only, 'today_and_overdue' (default) = both, 'upcoming' = future */
393
- window?: 'overdue' | 'today' | 'today_and_overdue' | 'upcoming'
394
- }
395
-
396
- export interface CompleteDealTaskParams {
397
- organizationId: string
398
- taskId: string
399
- completedByUserId: string | null
400
- }
401
-
402
- export interface RecordDealActivityParams {
403
- organizationId: string
404
- dealId: string
405
- type: string
406
- title: string
407
- description?: string
408
- payload?: Record<string, unknown>
409
- }
410
-
411
- // Deal analytics types (for /meta status and platform-status workflow)
412
-
413
- export interface DealStageSummary {
414
- stage: string
415
- count: number
416
- oldestUpdatedAt: string | null
417
- newestUpdatedAt: string | null
418
- }
419
-
420
- export interface StaleDeal {
421
- id: string
422
- contactEmail: string
423
- stageKey: string
424
- updatedAt: string
425
- daysStale: number
426
- }
427
-
428
- export interface DealPipelineAnalytics {
429
- totalDeals: number
430
- stageSummary: DealStageSummary[]
431
- staleDeals: StaleDeal[]
432
- recentActivity: AcqDeal[]
433
- }
434
-
435
- export interface DealAnalyticsParams {
436
- organizationId: string
437
- recentLimit?: number
438
- }
439
-
440
- export interface DealFilters {
441
- stage?: string
442
- search?: string
443
- limit?: number
444
- offset?: number
445
- }
446
-
447
- export interface AddContactsToListParams {
448
- organizationId: string
449
- listId: string
450
- contactIds: string[]
451
- }
452
-
453
- export interface AddContactsToListResult {
454
- added: number
455
- alreadyExisted: number
456
- }
457
-
458
- // List config/progress/executions params
459
- export interface UpdateListConfigParams {
460
- organizationId: string
461
- listId: string
462
- /** Deep-partial patch — any subtree that is present is replaced at that level. */
463
- configPatch: Record<string, unknown>
464
- }
465
-
24
+ import type { ListProgress, ProcessingStageStatus } from '../../../business/acquisition/api-schemas'
25
+
26
+ export type {
27
+ AcqList,
28
+ AcqCompany,
29
+ AcqContact,
30
+ AcqDealTask,
31
+ AcqDealTaskKind,
32
+ ListStatus,
33
+ ScrapingConfig,
34
+ IcpRubric,
35
+ PipelineConfig,
36
+ ProcessingStageStatus,
37
+ ListTelemetry,
38
+ DealDetail,
39
+ ListProgress
40
+ }
41
+
42
+ // Pagination types
43
+ export interface PaginationParams {
44
+ limit: number
45
+ offset: number
46
+ }
47
+
48
+ export interface PaginatedResult<T> {
49
+ data: T[]
50
+ total: number
51
+ limit: number
52
+ offset: number
53
+ }
54
+
55
+ // List params
56
+ export interface CreateListParams {
57
+ organizationId: string
58
+ name: string
59
+ description?: string
60
+ type?: string
61
+ batchIds?: string[]
62
+ instantlyCampaignId?: string
63
+ status?: ListStatus
64
+ metadata?: Record<string, unknown>
65
+ scrapingConfig?: ScrapingConfig
66
+ icp?: IcpRubric
67
+ pipelineConfig?: PipelineConfig
68
+ }
69
+
70
+ export interface UpdateListParams {
71
+ name?: string
72
+ description?: string
73
+ batchIds?: string[]
74
+ }
75
+
76
+ export interface UpdateListStatusParams {
77
+ organizationId: string
78
+ listId: string
79
+ status: ListStatus
80
+ }
81
+
82
+ // Company params
83
+ export interface CreateCompanyParams {
84
+ organizationId: string
85
+ name: string
86
+ domain?: string
87
+ linkedinUrl?: string
88
+ website?: string
89
+ numEmployees?: number
90
+ foundedYear?: number
91
+ locationCity?: string
92
+ locationState?: string
93
+ category?: string
94
+ source?: string
95
+ batchId?: string
96
+ verticalResearch?: string
97
+ }
98
+
99
+ export interface UpdateCompanyParams {
100
+ name?: string
101
+ domain?: string
102
+ linkedinUrl?: string
103
+ website?: string
104
+ numEmployees?: number
105
+ foundedYear?: number
106
+ locationCity?: string
107
+ locationState?: string
108
+ category?: string
109
+ segment?: string
110
+ pipelineStatus?: Record<string, unknown>
111
+ enrichmentData?: Record<string, unknown>
112
+ source?: string
113
+ batchId?: string
114
+ status?: 'active' | 'invalid'
115
+ verticalResearch?: string | null
116
+ /** Track A: flat qualification score column (null until a scoring rubric is defined) */
117
+ qualificationScore?: number | null
118
+ /** Track A: flat qualification signals jsonb — mirrors the former pipeline_status.qualification shape */
119
+ qualificationSignals?: Record<string, unknown> | null
120
+ /** Track A: key identifying the rubric used for qualification */
121
+ qualificationRubricKey?: string | null
122
+ }
123
+
124
+ export type UpsertCompanyParams = CreateCompanyParams
125
+ // Upsert by domain - uses same fields as create
126
+
127
+ export interface CompanyFilters {
128
+ listId?: string // Filter to companies in a specific list (via acq_list_companies)
129
+ search?: string
130
+ domain?: string
131
+ website?: string
132
+ segment?: string
133
+ category?: string
134
+ pipelineStatus?: Record<string, unknown>
135
+ /** Exclude companies whose pipeline_status contains this value (PostgREST NOT contains) */
136
+ pipelineStatusNot?: Record<string, unknown>
137
+ batchId?: string
138
+ status?: 'active' | 'invalid'
139
+ includeAll?: boolean
140
+ excludeColumns?: Array<'enrichmentData' | 'pipelineStatus'>
141
+ }
142
+
143
+ // Contact params
144
+ export interface CreateContactParams {
145
+ organizationId: string
146
+ email: string
147
+ companyId?: string
148
+ firstName?: string
149
+ lastName?: string
150
+ linkedinUrl?: string
151
+ title?: string
152
+ source?: string
153
+ sourceId?: string
154
+ batchId?: string
155
+ }
156
+
157
+ export interface UpdateContactParams {
158
+ companyId?: string
159
+ emailValid?: 'VALID' | 'INVALID' | 'RISKY' | 'UNKNOWN'
160
+ firstName?: string
161
+ lastName?: string
162
+ linkedinUrl?: string
163
+ title?: string
164
+ headline?: string
165
+ filterReason?: string
166
+ openingLine?: string
167
+ pipelineStatus?: Record<string, unknown>
168
+ enrichmentData?: Record<string, unknown>
169
+ status?: 'active' | 'invalid'
170
+ }
171
+
172
+ export type UpsertContactParams = CreateContactParams
173
+ // Upsert by email - uses same fields as create
174
+
175
+ export interface ContactFilters {
176
+ listId?: string // Filter to contacts in a specific list (via acq_list_members)
177
+ search?: string
178
+ openingLineIsNull?: boolean // Filter to contacts without personalization
179
+ pipelineStatus?: Record<string, unknown>
180
+ batchId?: string
181
+ contactStatus?: 'active' | 'invalid' // Filter by contact status (soft-delete flag)
182
+ }
183
+
184
+ // Deal params (for acq_deals table)
185
+ export interface UpsertDealParams {
186
+ organizationId: string
187
+ /** Contact email — dedupe key together with organization_id */
188
+ contactEmail: string
189
+ /** Optional contact ID for foreign key join */
190
+ contactId?: string
191
+ /** Campaign list that generated this deal (FK to acq_lists) */
192
+ sourceListId?: string
193
+ /** Deal origin: 'instantly', 'referral', 'inbound', 'manual' */
194
+ sourceType?: 'instantly' | 'referral' | 'inbound' | 'manual'
195
+ /** Optional discovery data JSONB to set on upsert */
196
+ discoveryData?: unknown
197
+ /** Optional proposal data JSONB to set on upsert */
198
+ proposalData?: unknown
199
+ }
200
+
201
+ export interface UpdateDiscoveryDataParams {
202
+ organizationId: string
203
+ contactEmail: string
204
+ discoveryData: unknown
205
+ submittedBy?: string
206
+ }
207
+
208
+ export interface UpdateProposalDataParams {
209
+ organizationId: string
210
+ contactEmail: string
211
+ proposalData: unknown
212
+ proposalPdfUrl?: string
213
+ }
214
+
215
+ export interface MarkProposalSentParams {
216
+ organizationId: string
217
+ contactEmail: string
218
+ }
219
+
220
+ export interface MarkProposalReviewedParams {
221
+ organizationId: string
222
+ contactEmail: string
223
+ reviewedBy: string
224
+ proposalData?: unknown
225
+ }
226
+
227
+ export interface UpdateCloseLostReasonParams {
228
+ organizationId: string
229
+ dealId: string
230
+ reason: string
231
+ }
232
+
233
+ export interface UpdateFeesParams {
234
+ organizationId: string
235
+ contactEmail?: string
236
+ dealId?: string
237
+ initialFee?: number
238
+ monthlyFee?: number
239
+ }
240
+
241
+ export interface TransitionItemParams {
242
+ organizationId: string
243
+ dealId: string
244
+ pipelineKey: string
245
+ stageKey: string
246
+ stateKey?: string | null
247
+ reason?: string
248
+ expectedUpdatedAt?: string
249
+ }
250
+
251
+ export interface SetContactNurtureParams {
252
+ organizationId: string
253
+ contactEmail: string
254
+ nurture?: boolean
255
+ }
256
+
257
+ export interface DeactivateContactsByCompanyParams {
258
+ organizationId: string
259
+ companyId: string
260
+ }
261
+
262
+ export interface DeactivateContactsByCompanyResult {
263
+ deactivated: number
264
+ }
265
+
266
+ export interface CancelSchedulesAndHitlByEmailParams {
267
+ organizationId: string
268
+ email: string
269
+ }
270
+
271
+ export interface CancelHitlByDealIdParams {
272
+ organizationId: string
273
+ dealId: string
274
+ }
275
+
276
+ export interface ClearDealFieldsParams {
277
+ organizationId: string
278
+ contactEmail?: string
279
+ dealId?: string
280
+ fields: (
281
+ | 'proposalPdfUrl'
282
+ | 'proposalGeneratedAt'
283
+ | 'initialFee'
284
+ | 'monthlyFee'
285
+ | 'closedLostReason'
286
+ | 'closedLostAt'
287
+ | 'discoveryData'
288
+ | 'discoverySubmittedAt'
289
+ )[]
290
+ }
291
+
292
+ export interface DeleteDealParams {
293
+ organizationId: string
294
+ dealId: string
295
+ }
296
+
297
+ export interface GetDealByIdParams {
298
+ dealId: string
299
+ organizationId: string
300
+ }
301
+
302
+ export interface GetContactByIdParams {
303
+ contactId: string
304
+ organizationId: string
305
+ }
306
+
307
+ export interface GetCompanyByIdParams {
308
+ companyId: string
309
+ organizationId: string
310
+ }
311
+
312
+ // Social monitoring params (acq_social_posts table)
313
+ export interface UpsertSocialPostParams {
314
+ organizationId: string
315
+ platform: string
316
+ platformPostId: string
317
+ authorName: string
318
+ authorUrl?: string | null
319
+ postTitle: string
320
+ postText: string
321
+ postUrl: string
322
+ engagementCount?: number
323
+ commentsCount?: number
324
+ postedAt: string
325
+ metadata?: Record<string, unknown>
326
+ relevanceScore?: number
327
+ matchedKeywords?: string[]
328
+ matchedQuery?: string | null
329
+ initialDraft?: string | null
330
+ finalResponse?: string | null
331
+ sourceCategory?: string | null
332
+ }
333
+
334
+ export interface UpsertSocialPostsParams {
335
+ organizationId: string
336
+ posts: Omit<UpsertSocialPostParams, 'organizationId'>[]
337
+ }
338
+
339
+ export interface UpsertSocialPostsResult {
340
+ inserted: number
341
+ duplicatesSkipped: number
342
+ }
343
+
344
+ export interface AcqDeal {
345
+ id: string
346
+ organizationId: string
347
+ contactEmail: string
348
+ pipelineKey: string
349
+ stageKey?: string | null
350
+ stateKey?: string | null
351
+ discoveryData?: Json | null
352
+ proposalData?: Json | null
353
+ proposalSentAt?: string | null
354
+ proposalPdfUrl?: string | null
355
+ signatureEnvelopeId?: string | null
356
+ sourceListId?: string | null
357
+ sourceType?: string | null
358
+ activityLog: DealActivityEntry[]
359
+ createdAt: Date
360
+ updatedAt: Date
361
+ }
362
+
363
+ export interface DealActivityEntry {
364
+ type: string
365
+ title: string
366
+ description?: string
367
+ payload?: Record<string, unknown>
368
+ occurredAt: string
369
+ }
370
+
371
+ export interface AcqDealNote {
372
+ id: string
373
+ dealId: string
374
+ organizationId: string
375
+ authorUserId: string | null
376
+ body: string
377
+ createdAt: string
378
+ updatedAt: string
379
+ }
380
+
381
+ export interface CreateDealNoteParams {
382
+ organizationId: string
383
+ dealId: string
384
+ body: string
385
+ authorUserId?: string
386
+ }
387
+
388
+ export interface ListDealNotesParams {
389
+ organizationId: string
390
+ dealId: string
391
+ }
392
+
393
+ export interface CreateDealTaskParams {
394
+ organizationId: string
395
+ dealId: string
396
+ title: string
397
+ description?: string | null
398
+ kind?: AcqDealTaskKind
399
+ dueAt?: string | null
400
+ assigneeUserId?: string | null
401
+ createdByUserId?: string | null
402
+ }
403
+
404
+ export interface ListDealTasksParams {
405
+ organizationId: string
406
+ dealId: string
407
+ }
408
+
409
+ export interface ListDealTasksDueParams {
410
+ organizationId: string
411
+ assigneeUserId?: string | null
412
+ /** Window filter: 'overdue' = past due, 'today' = due today only, 'today_and_overdue' (default) = both, 'upcoming' = future */
413
+ window?: 'overdue' | 'today' | 'today_and_overdue' | 'upcoming'
414
+ }
415
+
416
+ export interface CompleteDealTaskParams {
417
+ organizationId: string
418
+ taskId: string
419
+ completedByUserId: string | null
420
+ }
421
+
422
+ export interface RecordDealActivityParams {
423
+ organizationId: string
424
+ dealId: string
425
+ type: string
426
+ title: string
427
+ description?: string
428
+ payload?: Record<string, unknown>
429
+ }
430
+
431
+ export interface SetDealStateKeyParams {
432
+ organizationId: string
433
+ dealId: string
434
+ stateKey: string
435
+ }
436
+
437
+ export interface TransitionDealParams {
438
+ organizationId: string
439
+ dealId: string
440
+ toStage: string
441
+ toState?: string
442
+ }
443
+
444
+ export interface LoadDealParams {
445
+ organizationId: string
446
+ dealId: string
447
+ }
448
+
449
+ // Deal analytics types (for /meta status and platform-status workflow)
450
+
451
+ export interface DealStageSummary {
452
+ stage: string
453
+ count: number
454
+ oldestUpdatedAt: string | null
455
+ newestUpdatedAt: string | null
456
+ }
457
+
458
+ export interface StaleDeal {
459
+ id: string
460
+ contactEmail: string
461
+ stageKey: string
462
+ updatedAt: string
463
+ daysStale: number
464
+ }
465
+
466
+ export interface DealPipelineAnalytics {
467
+ totalDeals: number
468
+ stageSummary: DealStageSummary[]
469
+ staleDeals: StaleDeal[]
470
+ recentActivity: AcqDeal[]
471
+ }
472
+
473
+ export interface DealAnalyticsParams {
474
+ organizationId: string
475
+ recentLimit?: number
476
+ }
477
+
478
+ export interface DealFilters {
479
+ stage?: string
480
+ search?: string
481
+ limit?: number
482
+ offset?: number
483
+ }
484
+
485
+ export interface AddContactsToListParams {
486
+ organizationId: string
487
+ listId: string
488
+ contactIds: string[]
489
+ }
490
+
491
+ export interface AddContactsToListResult {
492
+ added: number
493
+ alreadyExisted: number
494
+ }
495
+
496
+ // List config/progress/executions params
497
+ export interface UpdateListConfigParams {
498
+ organizationId: string
499
+ listId: string
500
+ scrapingConfig?: ScrapingConfig
501
+ icp?: IcpRubric
502
+ pipelineConfig?: PipelineConfig
503
+ }
504
+
466
505
  export interface UpdateCompanyStageParams {
467
506
  organizationId: string
468
507
  listId: string
469
508
  companyId: string
470
- stage: CompanyListStage
509
+ stage: string
510
+ status?: ProcessingStageStatus
471
511
  executionId?: string
472
512
  }
473
513
 
@@ -475,414 +515,434 @@ export interface UpdateContactStageParams {
475
515
  organizationId: string
476
516
  listId: string
477
517
  contactId: string
478
- stage: ContactListStage
518
+ stage: string
519
+ status?: ProcessingStageStatus
479
520
  executionId?: string
480
521
  }
481
-
482
- export interface AddCompaniesToListParams {
483
- organizationId: string
484
- listId: string
485
- companyIds: string[]
486
- }
487
-
488
- export interface AddCompaniesToListResult {
489
- added: number
490
- alreadyExisted: number
491
- }
492
-
493
- export interface RemoveCompaniesFromListParams {
494
- organizationId: string
495
- listId: string
496
- companyIds: string[]
497
- }
498
-
499
- export interface RemoveCompaniesFromListResult {
500
- removed: number
501
- }
502
-
503
- export interface RecordListExecutionParams {
504
- organizationId: string
505
- listId: string
506
- executionId: string
507
- configSnapshot?: Record<string, unknown>
508
- }
509
-
510
- export interface ListExecutionSummary {
511
- executionId: string
512
- resourceId: string
513
- status: string
514
- createdAt: string
515
- completedAt: string | null
516
- durationMs: number | null
517
- }
518
-
519
- // Bulk import (contacts)
520
- export interface BulkImportParams {
521
- organizationId: string
522
- contacts: CreateContactParams[]
523
- listId?: string
524
- }
525
-
526
- export interface BulkImportResult {
527
- created: number
528
- updated: number
529
- errors: Array<{ email: string; error: string }>
530
- }
531
-
532
- // Bulk import (companies)
533
- export interface BulkImportCompanyEntry {
534
- name: string
535
- domain: string
536
- website?: string
537
- locationCity?: string
538
- locationState?: string
539
- category?: string
540
- source?: string
541
- enrichmentData?: Record<string, unknown>
542
- pipelineStatus?: Record<string, unknown>
543
- }
544
-
545
- export interface BulkImportCompaniesParams {
546
- organizationId: string
547
- batchId: string
548
- companies: BulkImportCompanyEntry[]
549
- }
550
-
551
- export interface BulkImportCompaniesResult {
552
- created: number
553
- skipped: number
554
- errors: Array<{ companyName: string; error: string }>
555
- }
556
-
557
- /**
558
- * Lead Service interface for acquisition platform tools.
559
- * Provides CRUD operations for lists, companies, and contacts.
560
- *
561
- * Implementation: apps/api/src/acquisition/lead-service.ts (LeadService class)
562
- *
563
- * Multi-tenancy: All operations require organizationId for tenant isolation.
564
- * All queries are filtered by organizationId via RLS policies.
565
- */
566
- export interface ILeadService {
567
- // List operations
568
- /**
569
- * Create a new list
570
- * @see LeadService.createList (apps/api/src/acquisition/lead-service.ts)
571
- */
572
- createList(params: CreateListParams): Promise<AcqList>
573
-
574
- /**
575
- * Update an existing list
576
- * @see LeadService.updateList (apps/api/src/acquisition/lead-service.ts)
577
- */
578
- updateList(id: string, params: UpdateListParams): Promise<AcqList>
579
-
580
- /**
581
- * Delete a list
582
- * @see LeadService.deleteList (apps/api/src/acquisition/lead-service.ts)
583
- */
584
- deleteList(id: string, organizationId: string): Promise<void>
585
-
586
- /**
587
- * Add contacts to a list (upsert — idempotent on re-runs)
588
- * @see LeadService.addContactsToList (apps/api/src/business/acquisition/lead-service.ts)
589
- */
590
- addContactsToList(params: AddContactsToListParams): Promise<AddContactsToListResult>
591
-
592
- /**
593
- * List all lists for an organization
594
- * @see LeadService.listLists (apps/api/src/acquisition/lead-service.ts)
595
- */
596
- listLists(organizationId: string): Promise<AcqList[]>
597
-
598
- /**
599
- * Get a single list by ID.
600
- */
601
- getList(id: string, organizationId: string): Promise<AcqList | null>
602
-
603
- /**
604
- * Deep-merge patch the jsonb `config` column. Patch keys at any depth
605
- * replace the corresponding subtree.
606
- */
607
- updateListConfig(params: UpdateListConfigParams): Promise<AcqList>
608
-
609
- /**
610
- * Add companies to a list via the acq_list_companies junction.
611
- * Idempotent on (list_id, company_id).
612
- */
613
- addCompaniesToList(params: AddCompaniesToListParams): Promise<AddCompaniesToListResult>
614
-
615
- /**
616
- * Remove companies from a list (delete junction rows only — company rows untouched).
617
- */
618
- removeCompaniesFromList(params: RemoveCompaniesFromListParams): Promise<RemoveCompaniesFromListResult>
619
-
620
- /**
621
- * Live org-wide list telemetry — computed on demand from acq_companies
622
- * and acq_contacts joined through the acq_list_companies / acq_list_members
623
- * junctions. Replaces the batch-scoped getBatchTelemetry.
624
- */
625
- getListsTelemetry(organizationId: string): Promise<ListTelemetry[]>
626
-
627
- /**
628
- * Single live rollup read from list junction stage columns.
629
- */
630
- getListProgress(listId: string, organizationId: string): Promise<ListTelemetry | null>
631
-
632
- /**
633
- * Advance a company row within a list's explicit stage journey.
634
- */
635
- updateCompanyStage(params: UpdateCompanyStageParams): Promise<void>
636
-
637
- /**
638
- * Advance a contact row within a list's explicit stage journey.
639
- */
640
- updateContactStage(params: UpdateContactStageParams): Promise<void>
641
-
642
- /**
643
- * Per-list execution history — reads via the feature-owned
644
- * acq_list_executions junction, joined to execution_logs for details.
645
- */
646
- getListExecutions(listId: string, organizationId: string): Promise<ListExecutionSummary[]>
647
-
648
- /**
649
- * Write a junction row linking (listId, executionId). Called by the
650
- * workflow layer at execution start in Step 4. No-op if the row exists.
651
- */
652
- recordListExecution(params: RecordListExecutionParams): Promise<void>
653
-
654
- // Company operations
655
- /**
656
- * Create a new company
657
- * @see LeadService.createCompany (apps/api/src/acquisition/lead-service.ts)
658
- */
659
- createCompany(params: CreateCompanyParams): Promise<AcqCompany>
660
-
661
- /**
662
- * Update an existing company
663
- * @see LeadService.updateCompany (apps/api/src/acquisition/lead-service.ts)
664
- */
665
- updateCompany(id: string, params: UpdateCompanyParams): Promise<AcqCompany>
666
-
667
- /**
668
- * Upsert a company by domain
669
- * @see LeadService.upsertCompany (apps/api/src/acquisition/lead-service.ts)
670
- */
671
- upsertCompany(params: UpsertCompanyParams): Promise<AcqCompany>
672
-
673
- /**
674
- * Get a company by ID
675
- * @see LeadService.getCompany (apps/api/src/acquisition/lead-service.ts)
676
- */
677
- getCompany(id: string, organizationId: string): Promise<AcqCompany | null>
678
-
679
- /**
680
- * List companies with optional filters
681
- * @see LeadService.listCompanies (apps/api/src/acquisition/lead-service.ts)
682
- */
683
- listCompanies(organizationId: string, filters: CompanyFilters): Promise<AcqCompany[]>
684
-
685
- /**
686
- * Delete a company
687
- * @see LeadService.deleteCompany (apps/api/src/acquisition/lead-service.ts)
688
- */
689
- deleteCompany(id: string, organizationId: string): Promise<void>
690
-
691
- // Contact operations
692
- /**
693
- * Create a new contact
694
- * @see LeadService.createContact (apps/api/src/acquisition/lead-service.ts)
695
- */
696
- createContact(params: CreateContactParams): Promise<AcqContact>
697
-
698
- /**
699
- * Update an existing contact
700
- * @see LeadService.updateContact (apps/api/src/acquisition/lead-service.ts)
701
- */
702
- updateContact(id: string, params: UpdateContactParams): Promise<AcqContact>
703
-
704
- /**
705
- * Upsert a contact by email
706
- * @see LeadService.upsertContact (apps/api/src/acquisition/lead-service.ts)
707
- */
708
- upsertContact(params: UpsertContactParams): Promise<AcqContact>
709
-
710
- /**
711
- * Get a contact by ID
712
- * @see LeadService.getContact (apps/api/src/acquisition/lead-service.ts)
713
- */
714
- getContact(id: string, organizationId: string): Promise<AcqContact | null>
715
-
716
- /**
717
- * List contacts with pagination and filters
718
- * @see LeadService.listContacts (apps/api/src/acquisition/lead-service.ts)
719
- */
720
- listContacts(
721
- organizationId: string,
722
- filters: ContactFilters,
723
- pagination: PaginationParams
724
- ): Promise<PaginatedResult<AcqContact>>
725
-
726
- /**
727
- * Delete a contact
728
- * @see LeadService.deleteContact (apps/api/src/acquisition/lead-service.ts)
729
- */
730
- deleteContact(id: string, organizationId: string): Promise<void>
731
-
732
- /**
733
- * Bulk import contacts
734
- * @see LeadService.bulkImportContacts (apps/api/src/acquisition/lead-service.ts)
735
- */
736
- bulkImportContacts(params: BulkImportParams): Promise<BulkImportResult>
737
-
738
- /**
739
- * Bulk import companies with domain dedup (skips existing domains).
740
- * Inserts in batches using Supabase bulk insert + ON CONFLICT.
741
- * @see LeadService.bulkImportCompanies (apps/api/src/business/acquisition/lead-service.ts)
742
- */
743
- bulkImportCompanies(params: BulkImportCompaniesParams): Promise<BulkImportCompaniesResult>
744
-
745
- /**
746
- * Deactivate all active contacts belonging to a company.
747
- * Used by qualification workflow to cascade company disqualification to contacts.
748
- */
749
- deactivateContactsByCompany(params: DeactivateContactsByCompanyParams): Promise<DeactivateContactsByCompanyResult>
750
-
751
- /**
752
- * Get a contact by email address
753
- * Used for looking up existing leads when they reply to outreach
754
- * @see LeadService.getContactByEmail (apps/api/src/acquisition/lead-service.ts)
755
- */
756
- getContactByEmail(email: string, organizationId: string): Promise<AcqContact | null>
757
-
758
- // Deal operations (acq_deals table)
759
- /**
760
- * Upsert a deal by Attio Deal ID
761
- * Creates or updates acq_deals record linking Attio Deal to Supabase
762
- * @see LeadService.upsertDeal (apps/api/src/acquisition/lead-service.ts)
763
- */
764
- upsertDeal(params: UpsertDealParams): Promise<AcqDeal>
765
-
766
- /**
767
- * Get a deal by contact email
768
- * Used for looking up existing deals when leads book via email (fallback lookup)
769
- * @see LeadService.getDealByEmail (apps/api/src/acquisition/lead-service.ts)
770
- */
771
- getDealByEmail(email: string, organizationId: string): Promise<AcqDeal | null>
772
-
773
- /**
774
- * Get a deal by SignatureAPI envelope ID
775
- * Used by webhook handler to find deal when contract is signed
776
- * @see LeadService.getDealByEnvelopeId (apps/api/src/acquisition/lead-service.ts)
777
- */
778
- getDealByEnvelopeId(envelopeId: string, organizationId: string): Promise<AcqDeal | null>
779
-
780
- /**
781
- * Update deal with signature envelope ID
782
- * Called when proposal is sent via SignatureAPI
783
- * @see LeadService.updateDealEnvelopeId (apps/api/src/acquisition/lead-service.ts)
784
- */
785
- updateDealEnvelopeId(dealId: string, envelopeId: string, organizationId: string): Promise<AcqDeal | null>
786
-
787
- // Deal-sync operations (mirror deal-sync.ts utilities as server-side methods)
788
-
789
- getDealById(params: GetDealByIdParams): Promise<AcqDeal | null>
790
-
791
- getContactById(params: GetContactByIdParams): Promise<AcqContact | null>
792
-
793
- getCompanyById(params: GetCompanyByIdParams): Promise<AcqCompany | null>
794
-
795
- updateDiscoveryData(params: UpdateDiscoveryDataParams): Promise<void>
796
-
797
- updateProposalData(params: UpdateProposalDataParams): Promise<void>
798
-
799
- markProposalSent(params: MarkProposalSentParams): Promise<void>
800
-
801
- markProposalReviewed(params: MarkProposalReviewedParams): Promise<void>
802
-
803
- updateCloseLostReason(params: UpdateCloseLostReasonParams): Promise<void>
804
-
805
- updateFees(params: UpdateFeesParams): Promise<void>
806
-
807
- transitionItem(params: TransitionItemParams): Promise<void>
808
-
809
- setContactNurture(params: SetContactNurtureParams): Promise<void>
810
-
811
- cancelSchedulesAndHitlByEmail(
812
- params: CancelSchedulesAndHitlByEmailParams
813
- ): Promise<{ schedulesCancelled: number; hitlDeleted: number }>
814
-
815
- cancelHitlByDealId(params: CancelHitlByDealIdParams): Promise<{ hitlDeleted: number }>
816
-
817
- clearDealFields(params: ClearDealFieldsParams): Promise<void>
818
-
819
- deleteDeal(params: DeleteDealParams): Promise<void>
820
-
821
- listDeals(organizationId: string, filters?: DealFilters): Promise<AcqDeal[]>
822
-
823
- getDealPipelineAnalytics(organizationId: string, recentLimit?: number): Promise<DealPipelineAnalytics>
824
-
825
- /**
826
- * Deep-merge enrichment data into a company or contact record.
827
- * Merges per source key rather than wholesale replacing the JSONB object.
828
- * @see LeadService.mergeEnrichmentData (apps/api/src/business/acquisition/lead-service.ts)
829
- */
830
- mergeEnrichmentData(
831
- id: string,
832
- orgId: string,
833
- table: 'acq_companies' | 'acq_contacts',
834
- data: Record<string, unknown>
835
- ): Promise<void>
836
-
837
- // Deal note operations (acq_deal_notes table)
838
- /**
839
- * Create a human-authored note on a deal
840
- * @see LeadService.createDealNote (apps/api/src/business/acquisition/lead-service.ts)
841
- */
842
- createDealNote(params: CreateDealNoteParams): Promise<AcqDealNote>
843
-
844
- /**
845
- * List notes for a deal, ordered by created_at DESC
846
- * @see LeadService.listDealNotes (apps/api/src/business/acquisition/lead-service.ts)
847
- */
848
- listDealNotes(params: ListDealNotesParams): Promise<AcqDealNote[]>
849
-
850
- // Deal task operations (acq_deal_tasks table)
851
- /**
852
- * Creates a new task attached to a deal.
853
- * @see LeadService.createDealTask (apps/api/src/business/acquisition/lead-service.ts)
854
- */
855
- createDealTask(params: CreateDealTaskParams): Promise<AcqDealTask>
856
-
857
- /**
858
- * Lists all tasks for a given deal, ordered by due date.
859
- * @see LeadService.listDealTasks (apps/api/src/business/acquisition/lead-service.ts)
860
- */
861
- listDealTasks(params: ListDealTasksParams): Promise<AcqDealTask[]>
862
-
863
- /**
864
- * Lists open (uncompleted) tasks within a date window across all deals.
865
- * @see LeadService.listDealTasksDue (apps/api/src/business/acquisition/lead-service.ts)
866
- */
867
- listDealTasksDue(params: ListDealTasksDueParams): Promise<AcqDealTask[]>
868
-
869
- /**
870
- * Marks a task as completed.
871
- * @see LeadService.completeDealTask (apps/api/src/business/acquisition/lead-service.ts)
872
- */
873
- completeDealTask(params: CompleteDealTaskParams): Promise<AcqDealTask>
874
-
875
- /**
876
- * Record a deal activity entry by deal ID.
877
- * Generic method for any activity type — used by SDK workflows.
878
- * @see LeadService.recordDealActivity (apps/api/src/business/acquisition/lead-service.ts)
879
- */
880
- recordDealActivity(params: RecordDealActivityParams): Promise<void>
881
-
882
- // Social monitoring operations (acq_social_posts table)
883
- /**
884
- * Bulk upsert social posts (deduplicate by platform + platform_post_id)
885
- * @see LeadService.upsertSocialPosts (apps/api/src/business/acquisition/lead-service.ts)
886
- */
887
- upsertSocialPosts(params: UpsertSocialPostsParams): Promise<UpsertSocialPostsResult>
888
- }
522
+
523
+ export interface AddCompaniesToListParams {
524
+ organizationId: string
525
+ listId: string
526
+ companyIds: string[]
527
+ }
528
+
529
+ export interface AddCompaniesToListResult {
530
+ added: number
531
+ alreadyExisted: number
532
+ }
533
+
534
+ export interface RemoveCompaniesFromListParams {
535
+ organizationId: string
536
+ listId: string
537
+ companyIds: string[]
538
+ }
539
+
540
+ export interface RemoveCompaniesFromListResult {
541
+ removed: number
542
+ }
543
+
544
+ export interface RecordListExecutionParams {
545
+ organizationId: string
546
+ listId: string
547
+ executionId: string
548
+ configSnapshot?: Record<string, unknown>
549
+ }
550
+
551
+ export interface ListExecutionSummary {
552
+ executionId: string
553
+ resourceId: string
554
+ status: string
555
+ createdAt: string
556
+ completedAt: string | null
557
+ durationMs: number | null
558
+ }
559
+
560
+ // Bulk import (contacts)
561
+ export interface BulkImportParams {
562
+ organizationId: string
563
+ contacts: CreateContactParams[]
564
+ listId?: string
565
+ }
566
+
567
+ export interface BulkImportResult {
568
+ created: number
569
+ updated: number
570
+ errors: Array<{ email: string; error: string }>
571
+ }
572
+
573
+ // Bulk import (companies)
574
+ export interface BulkImportCompanyEntry {
575
+ name: string
576
+ domain: string
577
+ website?: string
578
+ locationCity?: string
579
+ locationState?: string
580
+ category?: string
581
+ source?: string
582
+ enrichmentData?: Record<string, unknown>
583
+ pipelineStatus?: Record<string, unknown>
584
+ }
585
+
586
+ export interface BulkImportCompaniesParams {
587
+ organizationId: string
588
+ batchId: string
589
+ companies: BulkImportCompanyEntry[]
590
+ }
591
+
592
+ export interface BulkImportCompaniesResult {
593
+ created: number
594
+ skipped: number
595
+ errors: Array<{ companyName: string; error: string }>
596
+ }
597
+
598
+ /**
599
+ * Lead Service interface for acquisition platform tools.
600
+ * Provides CRUD operations for lists, companies, and contacts.
601
+ *
602
+ * Implementation: apps/api/src/acquisition/lead-service.ts (LeadService class)
603
+ *
604
+ * Multi-tenancy: All operations require organizationId for tenant isolation.
605
+ * All queries are filtered by organizationId via RLS policies.
606
+ */
607
+ export interface ILeadService {
608
+ // List operations
609
+ /**
610
+ * Create a new list
611
+ * @see LeadService.createList (apps/api/src/acquisition/lead-service.ts)
612
+ */
613
+ createList(params: CreateListParams): Promise<AcqList>
614
+
615
+ /**
616
+ * Update an existing list
617
+ * @see LeadService.updateList (apps/api/src/acquisition/lead-service.ts)
618
+ */
619
+ updateList(id: string, params: UpdateListParams): Promise<AcqList>
620
+
621
+ /**
622
+ * Delete a list
623
+ * @see LeadService.deleteList (apps/api/src/acquisition/lead-service.ts)
624
+ */
625
+ deleteList(id: string, organizationId: string): Promise<void>
626
+
627
+ /**
628
+ * Add contacts to a list (upsert — idempotent on re-runs)
629
+ * @see LeadService.addContactsToList (apps/api/src/business/acquisition/lead-service.ts)
630
+ */
631
+ addContactsToList(params: AddContactsToListParams): Promise<AddContactsToListResult>
632
+
633
+ /**
634
+ * List all lists for an organization
635
+ * @see LeadService.listLists (apps/api/src/acquisition/lead-service.ts)
636
+ */
637
+ listLists(organizationId: string): Promise<AcqList[]>
638
+
639
+ /**
640
+ * Get a single list by ID.
641
+ */
642
+ getList(id: string, organizationId: string): Promise<AcqList | null>
643
+
644
+ /**
645
+ * Deep-merge patch the jsonb `config` column. Patch keys at any depth
646
+ * replace the corresponding subtree.
647
+ */
648
+ updateListConfig(params: UpdateListConfigParams): Promise<AcqList>
649
+
650
+ /**
651
+ * Add companies to a list via the acq_list_companies junction.
652
+ * Idempotent on (list_id, company_id).
653
+ */
654
+ addCompaniesToList(params: AddCompaniesToListParams): Promise<AddCompaniesToListResult>
655
+
656
+ /**
657
+ * Remove companies from a list (delete junction rows only — company rows untouched).
658
+ */
659
+ removeCompaniesFromList(params: RemoveCompaniesFromListParams): Promise<RemoveCompaniesFromListResult>
660
+
661
+ /**
662
+ * Live org-wide list telemetry — computed on demand from acq_companies
663
+ * and acq_contacts joined through the acq_list_companies / acq_list_members
664
+ * junctions. Replaces the batch-scoped getBatchTelemetry.
665
+ */
666
+ getListsTelemetry(organizationId: string): Promise<ListTelemetry[]>
667
+
668
+ /**
669
+ * On-demand progress aggregation: COUNT(*) FILTER over processing_state flags,
670
+ * keyed by the list's pipeline_config.stages[].key (Decision #4 + #7).
671
+ */
672
+ getListProgress(listId: string, organizationId: string): Promise<ListProgress | null>
673
+
674
+ /**
675
+ * Advance a company row within a list's explicit stage journey.
676
+ */
677
+ updateCompanyStage(params: UpdateCompanyStageParams): Promise<void>
678
+
679
+ /**
680
+ * Advance a contact row within a list's explicit stage journey.
681
+ */
682
+ updateContactStage(params: UpdateContactStageParams): Promise<void>
683
+
684
+ /**
685
+ * Per-list execution history reads via the feature-owned
686
+ * acq_list_executions junction, joined to execution_logs for details.
687
+ */
688
+ getListExecutions(listId: string, organizationId: string): Promise<ListExecutionSummary[]>
689
+
690
+ /**
691
+ * Write a junction row linking (listId, executionId). Called by the
692
+ * workflow layer at execution start in Step 4. No-op if the row exists.
693
+ */
694
+ recordListExecution(params: RecordListExecutionParams): Promise<void>
695
+
696
+ // Company operations
697
+ /**
698
+ * Create a new company
699
+ * @see LeadService.createCompany (apps/api/src/acquisition/lead-service.ts)
700
+ */
701
+ createCompany(params: CreateCompanyParams): Promise<AcqCompany>
702
+
703
+ /**
704
+ * Update an existing company
705
+ * @see LeadService.updateCompany (apps/api/src/acquisition/lead-service.ts)
706
+ */
707
+ updateCompany(id: string, params: UpdateCompanyParams): Promise<AcqCompany>
708
+
709
+ /**
710
+ * Upsert a company by domain
711
+ * @see LeadService.upsertCompany (apps/api/src/acquisition/lead-service.ts)
712
+ */
713
+ upsertCompany(params: UpsertCompanyParams): Promise<AcqCompany>
714
+
715
+ /**
716
+ * Get a company by ID
717
+ * @see LeadService.getCompany (apps/api/src/acquisition/lead-service.ts)
718
+ */
719
+ getCompany(id: string, organizationId: string): Promise<AcqCompany | null>
720
+
721
+ /**
722
+ * List companies with optional filters
723
+ * @see LeadService.listCompanies (apps/api/src/acquisition/lead-service.ts)
724
+ */
725
+ listCompanies(organizationId: string, filters: CompanyFilters): Promise<AcqCompany[]>
726
+
727
+ /**
728
+ * Delete a company
729
+ * @see LeadService.deleteCompany (apps/api/src/acquisition/lead-service.ts)
730
+ */
731
+ deleteCompany(id: string, organizationId: string): Promise<void>
732
+
733
+ // Contact operations
734
+ /**
735
+ * Create a new contact
736
+ * @see LeadService.createContact (apps/api/src/acquisition/lead-service.ts)
737
+ */
738
+ createContact(params: CreateContactParams): Promise<AcqContact>
739
+
740
+ /**
741
+ * Update an existing contact
742
+ * @see LeadService.updateContact (apps/api/src/acquisition/lead-service.ts)
743
+ */
744
+ updateContact(id: string, params: UpdateContactParams): Promise<AcqContact>
745
+
746
+ /**
747
+ * Upsert a contact by email
748
+ * @see LeadService.upsertContact (apps/api/src/acquisition/lead-service.ts)
749
+ */
750
+ upsertContact(params: UpsertContactParams): Promise<AcqContact>
751
+
752
+ /**
753
+ * Get a contact by ID
754
+ * @see LeadService.getContact (apps/api/src/acquisition/lead-service.ts)
755
+ */
756
+ getContact(id: string, organizationId: string): Promise<AcqContact | null>
757
+
758
+ /**
759
+ * List contacts with pagination and filters
760
+ * @see LeadService.listContacts (apps/api/src/acquisition/lead-service.ts)
761
+ */
762
+ listContacts(
763
+ organizationId: string,
764
+ filters: ContactFilters,
765
+ pagination: PaginationParams
766
+ ): Promise<PaginatedResult<AcqContact>>
767
+
768
+ /**
769
+ * Delete a contact
770
+ * @see LeadService.deleteContact (apps/api/src/acquisition/lead-service.ts)
771
+ */
772
+ deleteContact(id: string, organizationId: string): Promise<void>
773
+
774
+ /**
775
+ * Bulk import contacts
776
+ * @see LeadService.bulkImportContacts (apps/api/src/acquisition/lead-service.ts)
777
+ */
778
+ bulkImportContacts(params: BulkImportParams): Promise<BulkImportResult>
779
+
780
+ /**
781
+ * Bulk import companies with domain dedup (skips existing domains).
782
+ * Inserts in batches using Supabase bulk insert + ON CONFLICT.
783
+ * @see LeadService.bulkImportCompanies (apps/api/src/business/acquisition/lead-service.ts)
784
+ */
785
+ bulkImportCompanies(params: BulkImportCompaniesParams): Promise<BulkImportCompaniesResult>
786
+
787
+ /**
788
+ * Deactivate all active contacts belonging to a company.
789
+ * Used by qualification workflow to cascade company disqualification to contacts.
790
+ */
791
+ deactivateContactsByCompany(params: DeactivateContactsByCompanyParams): Promise<DeactivateContactsByCompanyResult>
792
+
793
+ /**
794
+ * Get a contact by email address
795
+ * Used for looking up existing leads when they reply to outreach
796
+ * @see LeadService.getContactByEmail (apps/api/src/acquisition/lead-service.ts)
797
+ */
798
+ getContactByEmail(email: string, organizationId: string): Promise<AcqContact | null>
799
+
800
+ // Deal operations (acq_deals table)
801
+ /**
802
+ * Upsert a deal by Attio Deal ID
803
+ * Creates or updates acq_deals record linking Attio Deal to Supabase
804
+ * @see LeadService.upsertDeal (apps/api/src/acquisition/lead-service.ts)
805
+ */
806
+ upsertDeal(params: UpsertDealParams): Promise<AcqDeal>
807
+
808
+ /**
809
+ * Get a deal by contact email
810
+ * Used for looking up existing deals when leads book via email (fallback lookup)
811
+ * @see LeadService.getDealByEmail (apps/api/src/acquisition/lead-service.ts)
812
+ */
813
+ getDealByEmail(email: string, organizationId: string): Promise<AcqDeal | null>
814
+
815
+ /**
816
+ * Get a deal by SignatureAPI envelope ID
817
+ * Used by webhook handler to find deal when contract is signed
818
+ * @see LeadService.getDealByEnvelopeId (apps/api/src/acquisition/lead-service.ts)
819
+ */
820
+ getDealByEnvelopeId(envelopeId: string, organizationId: string): Promise<AcqDeal | null>
821
+
822
+ /**
823
+ * Update deal with signature envelope ID
824
+ * Called when proposal is sent via SignatureAPI
825
+ * @see LeadService.updateDealEnvelopeId (apps/api/src/acquisition/lead-service.ts)
826
+ */
827
+ updateDealEnvelopeId(dealId: string, envelopeId: string, organizationId: string): Promise<AcqDeal | null>
828
+
829
+ // Deal-sync operations (mirror deal-sync.ts utilities as server-side methods)
830
+
831
+ getDealById(params: GetDealByIdParams): Promise<AcqDeal | null>
832
+
833
+ getContactById(params: GetContactByIdParams): Promise<AcqContact | null>
834
+
835
+ getCompanyById(params: GetCompanyByIdParams): Promise<AcqCompany | null>
836
+
837
+ updateDiscoveryData(params: UpdateDiscoveryDataParams): Promise<void>
838
+
839
+ updateProposalData(params: UpdateProposalDataParams): Promise<void>
840
+
841
+ markProposalSent(params: MarkProposalSentParams): Promise<void>
842
+
843
+ markProposalReviewed(params: MarkProposalReviewedParams): Promise<void>
844
+
845
+ updateCloseLostReason(params: UpdateCloseLostReasonParams): Promise<void>
846
+
847
+ updateFees(params: UpdateFeesParams): Promise<void>
848
+
849
+ transitionItem(params: TransitionItemParams): Promise<void>
850
+
851
+ setContactNurture(params: SetContactNurtureParams): Promise<void>
852
+
853
+ cancelSchedulesAndHitlByEmail(
854
+ params: CancelSchedulesAndHitlByEmailParams
855
+ ): Promise<{ schedulesCancelled: number; hitlDeleted: number }>
856
+
857
+ cancelHitlByDealId(params: CancelHitlByDealIdParams): Promise<{ hitlDeleted: number }>
858
+
859
+ clearDealFields(params: ClearDealFieldsParams): Promise<void>
860
+
861
+ deleteDeal(params: DeleteDealParams): Promise<void>
862
+
863
+ listDeals(organizationId: string, filters?: DealFilters): Promise<AcqDeal[]>
864
+
865
+ getDealPipelineAnalytics(organizationId: string, recentLimit?: number): Promise<DealPipelineAnalytics>
866
+
867
+ /**
868
+ * Deep-merge enrichment data into a company or contact record.
869
+ * Merges per source key rather than wholesale replacing the JSONB object.
870
+ * @see LeadService.mergeEnrichmentData (apps/api/src/business/acquisition/lead-service.ts)
871
+ */
872
+ mergeEnrichmentData(
873
+ id: string,
874
+ orgId: string,
875
+ table: 'acq_companies' | 'acq_contacts',
876
+ data: Record<string, unknown>
877
+ ): Promise<void>
878
+
879
+ // Deal note operations (acq_deal_notes table)
880
+ /**
881
+ * Create a human-authored note on a deal
882
+ * @see LeadService.createDealNote (apps/api/src/business/acquisition/lead-service.ts)
883
+ */
884
+ createDealNote(params: CreateDealNoteParams): Promise<AcqDealNote>
885
+
886
+ /**
887
+ * List notes for a deal, ordered by created_at DESC
888
+ * @see LeadService.listDealNotes (apps/api/src/business/acquisition/lead-service.ts)
889
+ */
890
+ listDealNotes(params: ListDealNotesParams): Promise<AcqDealNote[]>
891
+
892
+ // Deal task operations (acq_deal_tasks table)
893
+ /**
894
+ * Creates a new task attached to a deal.
895
+ * @see LeadService.createDealTask (apps/api/src/business/acquisition/lead-service.ts)
896
+ */
897
+ createDealTask(params: CreateDealTaskParams): Promise<AcqDealTask>
898
+
899
+ /**
900
+ * Lists all tasks for a given deal, ordered by due date.
901
+ * @see LeadService.listDealTasks (apps/api/src/business/acquisition/lead-service.ts)
902
+ */
903
+ listDealTasks(params: ListDealTasksParams): Promise<AcqDealTask[]>
904
+
905
+ /**
906
+ * Lists open (uncompleted) tasks within a date window across all deals.
907
+ * @see LeadService.listDealTasksDue (apps/api/src/business/acquisition/lead-service.ts)
908
+ */
909
+ listDealTasksDue(params: ListDealTasksDueParams): Promise<AcqDealTask[]>
910
+
911
+ /**
912
+ * Marks a task as completed.
913
+ * @see LeadService.completeDealTask (apps/api/src/business/acquisition/lead-service.ts)
914
+ */
915
+ completeDealTask(params: CompleteDealTaskParams): Promise<AcqDealTask>
916
+
917
+ /**
918
+ * Record a deal activity entry by deal ID.
919
+ * Generic method for any activity type — used by SDK workflows.
920
+ * @see LeadService.recordDealActivity (apps/api/src/business/acquisition/lead-service.ts)
921
+ */
922
+ recordDealActivity(params: RecordDealActivityParams): Promise<void>
923
+
924
+ /**
925
+ * Update the state_key on an acq_deals row.
926
+ * @see LeadService.setDealStateKey (apps/api/src/business/acquisition/lead-service.ts)
927
+ */
928
+ setDealStateKey(params: SetDealStateKeyParams): Promise<{ ok: true }>
929
+
930
+ /**
931
+ * Transition a deal to a new stage, resolving pipeline_key automatically.
932
+ * @see LeadService.transitionDeal (apps/api/src/business/acquisition/lead-service.ts)
933
+ */
934
+ transitionDeal(params: TransitionDealParams): Promise<{ deal: AcqDeal }>
935
+
936
+ /**
937
+ * Load a deal with its joined contact and company data.
938
+ * @see LeadService.loadDeal (apps/api/src/business/acquisition/lead-service.ts)
939
+ */
940
+ loadDeal(params: LoadDealParams): Promise<DealDetail | null>
941
+
942
+ // Social monitoring operations (acq_social_posts table)
943
+ /**
944
+ * Bulk upsert social posts (deduplicate by platform + platform_post_id)
945
+ * @see LeadService.upsertSocialPosts (apps/api/src/business/acquisition/lead-service.ts)
946
+ */
947
+ upsertSocialPosts(params: UpsertSocialPostsParams): Promise<UpsertSocialPostsResult>
948
+ }