@elevasis/core 0.12.0 → 0.13.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.
@@ -1,885 +1,888 @@
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
21
- } 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
-
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
21
+ } 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
61
  export interface UpdateListParams {
62
62
  name?: string
63
63
  description?: string
64
64
  status?: string
65
65
  batchIds?: string[]
66
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
- proposalStatus?: string
194
- }
195
-
196
- export interface MarkProposalSentParams {
197
- organizationId: string
198
- contactEmail: string
199
- }
200
-
201
- export interface MarkProposalReviewedParams {
202
- organizationId: string
203
- contactEmail: string
204
- reviewedBy: string
205
- proposalData?: unknown
206
- }
207
-
208
- export interface UpdateCloseLostReasonParams {
209
- organizationId: string
210
- dealId: string
211
- reason: string
212
- }
213
-
214
- export interface UpdateFeesParams {
215
- organizationId: string
216
- contactEmail?: string
217
- dealId?: string
218
- initialFee?: number
219
- monthlyFee?: number
220
- }
221
-
222
- export interface SyncDealStageParams {
223
- organizationId: string
224
- dealId: string
225
- stage: string
226
- }
227
-
228
- export interface SetContactNurtureParams {
229
- organizationId: string
230
- contactEmail: string
231
- nurture?: boolean
232
- }
233
-
234
- export interface DeactivateContactsByCompanyParams {
235
- organizationId: string
236
- companyId: string
237
- }
238
-
239
- export interface DeactivateContactsByCompanyResult {
240
- deactivated: number
241
- }
242
-
243
- export interface CancelSchedulesAndHitlByEmailParams {
244
- organizationId: string
245
- email: string
246
- }
247
-
248
- export interface CancelHitlByDealIdParams {
249
- organizationId: string
250
- dealId: string
251
- }
252
-
253
- export interface ClearDealFieldsParams {
254
- organizationId: string
255
- contactEmail?: string
256
- dealId?: string
257
- fields: (
258
- | 'proposalPdfUrl'
259
- | 'proposalStatus'
260
- | 'proposalGeneratedAt'
261
- | 'initialFee'
262
- | 'monthlyFee'
263
- | 'closedLostReason'
264
- | 'closedLostAt'
265
- | 'discoveryData'
266
- | 'discoverySubmittedAt'
267
- )[]
268
- }
269
-
270
- export interface DeleteDealParams {
271
- organizationId: string
272
- dealId: string
273
- }
274
-
275
- export interface GetDealByIdParams {
276
- dealId: string
277
- organizationId: string
278
- }
279
-
280
- export interface GetContactByIdParams {
281
- contactId: string
282
- organizationId: string
283
- }
284
-
285
- export interface GetCompanyByIdParams {
286
- companyId: string
287
- organizationId: string
288
- }
289
-
290
- // Social monitoring params (acq_social_posts table)
291
- export interface UpsertSocialPostParams {
292
- organizationId: string
293
- platform: string
294
- platformPostId: string
295
- authorName: string
296
- authorUrl?: string | null
297
- postTitle: string
298
- postText: string
299
- postUrl: string
300
- engagementCount?: number
301
- commentsCount?: number
302
- postedAt: string
303
- metadata?: Record<string, unknown>
304
- relevanceScore?: number
305
- matchedKeywords?: string[]
306
- matchedQuery?: string | null
307
- initialDraft?: string | null
308
- finalResponse?: string | null
309
- sourceCategory?: string | null
310
- }
311
-
312
- export interface UpsertSocialPostsParams {
313
- organizationId: string
314
- posts: Omit<UpsertSocialPostParams, 'organizationId'>[]
315
- }
316
-
317
- export interface UpsertSocialPostsResult {
318
- inserted: number
319
- duplicatesSkipped: number
320
- }
321
-
322
- export interface AcqDeal {
323
- id: string
324
- organizationId: string
325
- contactEmail: string
326
- cachedStage?: string | null
327
- discoveryData?: Json | null
328
- proposalData?: Json | null
329
- proposalStatus?: string | null
330
- proposalSentAt?: string | null
331
- proposalPdfUrl?: string | null
332
- signatureEnvelopeId?: string | null
333
- sourceListId?: string | null
334
- sourceType?: string | null
335
- activityLog: DealActivityEntry[]
336
- createdAt: Date
337
- updatedAt: Date
338
- }
339
-
340
- export interface DealActivityEntry {
341
- type: string
342
- title: string
343
- description?: string
344
- payload?: Record<string, unknown>
345
- occurredAt: string
346
- }
347
-
348
- export interface AcqDealNote {
349
- id: string
350
- dealId: string
351
- organizationId: string
352
- authorUserId: string | null
353
- body: string
354
- createdAt: string
355
- updatedAt: string
356
- }
357
-
358
- export interface CreateDealNoteParams {
359
- organizationId: string
360
- dealId: string
361
- body: string
362
- authorUserId?: string
363
- }
364
-
365
- export interface ListDealNotesParams {
366
- organizationId: string
367
- dealId: string
368
- }
369
-
370
- export interface CreateDealTaskParams {
371
- organizationId: string
372
- dealId: string
373
- title: string
374
- description?: string | null
375
- kind?: AcqDealTaskKind
376
- dueAt?: string | null
377
- assigneeUserId?: string | null
378
- createdByUserId?: string | null
379
- }
380
-
381
- export interface ListDealTasksParams {
382
- organizationId: string
383
- dealId: string
384
- }
385
-
386
- export interface ListDealTasksDueParams {
387
- organizationId: string
388
- assigneeUserId?: string | null
389
- /** Window filter: 'overdue' = past due, 'today' = due today only, 'today_and_overdue' (default) = both, 'upcoming' = future */
390
- window?: 'overdue' | 'today' | 'today_and_overdue' | 'upcoming'
391
- }
392
-
393
- export interface CompleteDealTaskParams {
394
- organizationId: string
395
- taskId: string
396
- completedByUserId: string | null
397
- }
398
-
399
- export interface RecordDealActivityParams {
400
- organizationId: string
401
- dealId: string
402
- type: string
403
- title: string
404
- description?: string
405
- payload?: Record<string, unknown>
406
- }
407
-
408
- // Deal analytics types (for /meta status and platform-status workflow)
409
-
410
- export interface DealStageSummary {
411
- stage: string
412
- count: number
413
- oldestUpdatedAt: string | null
414
- newestUpdatedAt: string | null
415
- }
416
-
417
- export interface StaleDeal {
418
- id: string
419
- contactEmail: string
420
- cachedStage: string
421
- updatedAt: string
422
- daysStale: number
423
- }
424
-
425
- export interface DealPipelineAnalytics {
426
- totalDeals: number
427
- stageSummary: DealStageSummary[]
428
- staleDeals: StaleDeal[]
429
- recentActivity: AcqDeal[]
430
- }
431
-
432
- export interface DealAnalyticsParams {
433
- organizationId: string
434
- recentLimit?: number
435
- }
436
-
437
- export interface DealFilters {
438
- stage?: string
439
- search?: string
440
- limit?: number
441
- offset?: number
442
- }
443
-
444
- export interface AddContactsToListParams {
445
- organizationId: string
446
- listId: string
447
- contactIds: string[]
448
- }
449
-
450
- export interface AddContactsToListResult {
451
- added: number
452
- alreadyExisted: number
453
- }
454
-
455
- // List config/progress/executions params
456
- export interface UpdateListConfigParams {
457
- organizationId: string
458
- listId: string
459
- /** Deep-partial patch — any subtree that is present is replaced at that level. */
460
- configPatch: Record<string, unknown>
461
- }
462
-
463
- export interface UpdateCompanyStageParams {
464
- organizationId: string
465
- listId: string
466
- companyId: string
467
- stage: CompanyListStage
468
- executionId?: string
469
- }
470
-
471
- export interface UpdateContactStageParams {
472
- organizationId: string
473
- listId: string
474
- contactId: string
475
- stage: ContactListStage
476
- executionId?: string
477
- }
478
-
479
- export interface AddCompaniesToListParams {
480
- organizationId: string
481
- listId: string
482
- companyIds: string[]
483
- }
484
-
485
- export interface AddCompaniesToListResult {
486
- added: number
487
- alreadyExisted: number
488
- }
489
-
490
- export interface RemoveCompaniesFromListParams {
491
- organizationId: string
492
- listId: string
493
- companyIds: string[]
494
- }
495
-
496
- export interface RemoveCompaniesFromListResult {
497
- removed: number
498
- }
499
-
500
- export interface RecordListExecutionParams {
501
- organizationId: string
502
- listId: string
503
- executionId: string
504
- configSnapshot?: Record<string, unknown>
505
- }
506
-
507
- export interface ListExecutionSummary {
508
- executionId: string
509
- resourceId: string
510
- status: string
511
- createdAt: string
512
- completedAt: string | null
513
- durationMs: number | null
514
- }
515
-
516
- // Bulk import (contacts)
517
- export interface BulkImportParams {
518
- organizationId: string
519
- contacts: CreateContactParams[]
520
- listId?: string
521
- }
522
-
523
- export interface BulkImportResult {
524
- created: number
525
- updated: number
526
- errors: Array<{ email: string; error: string }>
527
- }
528
-
529
- // Bulk import (companies)
530
- export interface BulkImportCompanyEntry {
531
- name: string
532
- domain: string
533
- website?: string
534
- locationCity?: string
535
- locationState?: string
536
- category?: string
537
- source?: string
538
- enrichmentData?: Record<string, unknown>
539
- pipelineStatus?: Record<string, unknown>
540
- }
541
-
542
- export interface BulkImportCompaniesParams {
543
- organizationId: string
544
- batchId: string
545
- companies: BulkImportCompanyEntry[]
546
- }
547
-
548
- export interface BulkImportCompaniesResult {
549
- created: number
550
- skipped: number
551
- errors: Array<{ companyName: string; error: string }>
552
- }
553
-
554
- /**
555
- * Lead Service interface for acquisition platform tools.
556
- * Provides CRUD operations for lists, companies, and contacts.
557
- *
558
- * Implementation: apps/api/src/acquisition/lead-service.ts (LeadService class)
559
- *
560
- * Multi-tenancy: All operations require organizationId for tenant isolation.
561
- * All queries are filtered by organizationId via RLS policies.
562
- */
563
- export interface ILeadService {
564
- // List operations
565
- /**
566
- * Create a new list
567
- * @see LeadService.createList (apps/api/src/acquisition/lead-service.ts)
568
- */
569
- createList(params: CreateListParams): Promise<AcqList>
570
-
571
- /**
572
- * Update an existing list
573
- * @see LeadService.updateList (apps/api/src/acquisition/lead-service.ts)
574
- */
575
- updateList(id: string, params: UpdateListParams): Promise<AcqList>
576
-
577
- /**
578
- * Delete a list
579
- * @see LeadService.deleteList (apps/api/src/acquisition/lead-service.ts)
580
- */
581
- deleteList(id: string, organizationId: string): Promise<void>
582
-
583
- /**
584
- * Add contacts to a list (upsert idempotent on re-runs)
585
- * @see LeadService.addContactsToList (apps/api/src/business/acquisition/lead-service.ts)
586
- */
587
- addContactsToList(params: AddContactsToListParams): Promise<AddContactsToListResult>
588
-
589
- /**
590
- * List all lists for an organization
591
- * @see LeadService.listLists (apps/api/src/acquisition/lead-service.ts)
592
- */
593
- listLists(organizationId: string): Promise<AcqList[]>
594
-
595
- /**
596
- * Get a single list by ID.
597
- */
598
- getList(id: string, organizationId: string): Promise<AcqList | null>
599
-
600
- /**
601
- * Deep-merge patch the jsonb `config` column. Patch keys at any depth
602
- * replace the corresponding subtree.
603
- */
604
- updateListConfig(params: UpdateListConfigParams): Promise<AcqList>
605
-
606
- /**
607
- * Add companies to a list via the acq_list_companies junction.
608
- * Idempotent on (list_id, company_id).
609
- */
610
- addCompaniesToList(params: AddCompaniesToListParams): Promise<AddCompaniesToListResult>
611
-
612
- /**
613
- * Remove companies from a list (delete junction rows only — company rows untouched).
614
- */
615
- removeCompaniesFromList(params: RemoveCompaniesFromListParams): Promise<RemoveCompaniesFromListResult>
616
-
617
- /**
618
- * Live org-wide list telemetry — computed on demand from acq_companies
619
- * and acq_contacts joined through the acq_list_companies / acq_list_members
620
- * junctions. Replaces the batch-scoped getBatchTelemetry.
621
- */
622
- getListsTelemetry(organizationId: string): Promise<ListTelemetry[]>
623
-
624
- /**
625
- * Single live rollup read from list junction stage columns.
626
- */
627
- getListProgress(listId: string, organizationId: string): Promise<ListTelemetry | null>
628
-
629
- /**
630
- * Advance a company row within a list's explicit stage journey.
631
- */
632
- updateCompanyStage(params: UpdateCompanyStageParams): Promise<void>
633
-
634
- /**
635
- * Advance a contact row within a list's explicit stage journey.
636
- */
637
- updateContactStage(params: UpdateContactStageParams): Promise<void>
638
-
639
- /**
640
- * Per-list execution history — reads via the feature-owned
641
- * acq_list_executions junction, joined to execution_logs for details.
642
- */
643
- getListExecutions(listId: string, organizationId: string): Promise<ListExecutionSummary[]>
644
-
645
- /**
646
- * Write a junction row linking (listId, executionId). Called by the
647
- * workflow layer at execution start in Step 4. No-op if the row exists.
648
- */
649
- recordListExecution(params: RecordListExecutionParams): Promise<void>
650
-
651
- // Company operations
652
- /**
653
- * Create a new company
654
- * @see LeadService.createCompany (apps/api/src/acquisition/lead-service.ts)
655
- */
656
- createCompany(params: CreateCompanyParams): Promise<AcqCompany>
657
-
658
- /**
659
- * Update an existing company
660
- * @see LeadService.updateCompany (apps/api/src/acquisition/lead-service.ts)
661
- */
662
- updateCompany(id: string, params: UpdateCompanyParams): Promise<AcqCompany>
663
-
664
- /**
665
- * Upsert a company by domain
666
- * @see LeadService.upsertCompany (apps/api/src/acquisition/lead-service.ts)
667
- */
668
- upsertCompany(params: UpsertCompanyParams): Promise<AcqCompany>
669
-
670
- /**
671
- * Get a company by ID
672
- * @see LeadService.getCompany (apps/api/src/acquisition/lead-service.ts)
673
- */
674
- getCompany(id: string, organizationId: string): Promise<AcqCompany | null>
675
-
676
- /**
677
- * List companies with optional filters
678
- * @see LeadService.listCompanies (apps/api/src/acquisition/lead-service.ts)
679
- */
680
- listCompanies(organizationId: string, filters: CompanyFilters): Promise<AcqCompany[]>
681
-
682
- /**
683
- * Delete a company
684
- * @see LeadService.deleteCompany (apps/api/src/acquisition/lead-service.ts)
685
- */
686
- deleteCompany(id: string, organizationId: string): Promise<void>
687
-
688
- // Contact operations
689
- /**
690
- * Create a new contact
691
- * @see LeadService.createContact (apps/api/src/acquisition/lead-service.ts)
692
- */
693
- createContact(params: CreateContactParams): Promise<AcqContact>
694
-
695
- /**
696
- * Update an existing contact
697
- * @see LeadService.updateContact (apps/api/src/acquisition/lead-service.ts)
698
- */
699
- updateContact(id: string, params: UpdateContactParams): Promise<AcqContact>
700
-
701
- /**
702
- * Upsert a contact by email
703
- * @see LeadService.upsertContact (apps/api/src/acquisition/lead-service.ts)
704
- */
705
- upsertContact(params: UpsertContactParams): Promise<AcqContact>
706
-
707
- /**
708
- * Get a contact by ID
709
- * @see LeadService.getContact (apps/api/src/acquisition/lead-service.ts)
710
- */
711
- getContact(id: string, organizationId: string): Promise<AcqContact | null>
712
-
713
- /**
714
- * List contacts with pagination and filters
715
- * @see LeadService.listContacts (apps/api/src/acquisition/lead-service.ts)
716
- */
717
- listContacts(
718
- organizationId: string,
719
- filters: ContactFilters,
720
- pagination: PaginationParams
721
- ): Promise<PaginatedResult<AcqContact>>
722
-
723
- /**
724
- * Delete a contact
725
- * @see LeadService.deleteContact (apps/api/src/acquisition/lead-service.ts)
726
- */
727
- deleteContact(id: string, organizationId: string): Promise<void>
728
-
729
- /**
730
- * Bulk import contacts
731
- * @see LeadService.bulkImportContacts (apps/api/src/acquisition/lead-service.ts)
732
- */
733
- bulkImportContacts(params: BulkImportParams): Promise<BulkImportResult>
734
-
735
- /**
736
- * Bulk import companies with domain dedup (skips existing domains).
737
- * Inserts in batches using Supabase bulk insert + ON CONFLICT.
738
- * @see LeadService.bulkImportCompanies (apps/api/src/business/acquisition/lead-service.ts)
739
- */
740
- bulkImportCompanies(params: BulkImportCompaniesParams): Promise<BulkImportCompaniesResult>
741
-
742
- /**
743
- * Deactivate all active contacts belonging to a company.
744
- * Used by qualification workflow to cascade company disqualification to contacts.
745
- */
746
- deactivateContactsByCompany(params: DeactivateContactsByCompanyParams): Promise<DeactivateContactsByCompanyResult>
747
-
748
- /**
749
- * Get a contact by email address
750
- * Used for looking up existing leads when they reply to outreach
751
- * @see LeadService.getContactByEmail (apps/api/src/acquisition/lead-service.ts)
752
- */
753
- getContactByEmail(email: string, organizationId: string): Promise<AcqContact | null>
754
-
755
- // Deal operations (acq_deals table)
756
- /**
757
- * Upsert a deal by Attio Deal ID
758
- * Creates or updates acq_deals record linking Attio Deal to Supabase
759
- * @see LeadService.upsertDeal (apps/api/src/acquisition/lead-service.ts)
760
- */
761
- upsertDeal(params: UpsertDealParams): Promise<AcqDeal>
762
-
763
- /**
764
- * Get a deal by contact email
765
- * Used for looking up existing deals when leads book via email (fallback lookup)
766
- * @see LeadService.getDealByEmail (apps/api/src/acquisition/lead-service.ts)
767
- */
768
- getDealByEmail(email: string, organizationId: string): Promise<AcqDeal | null>
769
-
770
- /**
771
- * Get a deal by SignatureAPI envelope ID
772
- * Used by webhook handler to find deal when contract is signed
773
- * @see LeadService.getDealByEnvelopeId (apps/api/src/acquisition/lead-service.ts)
774
- */
775
- getDealByEnvelopeId(envelopeId: string, organizationId: string): Promise<AcqDeal | null>
776
-
777
- /**
778
- * Update deal with signature envelope ID
779
- * Called when proposal is sent via SignatureAPI
780
- * @see LeadService.updateDealEnvelopeId (apps/api/src/acquisition/lead-service.ts)
781
- */
782
- updateDealEnvelopeId(dealId: string, envelopeId: string, organizationId: string): Promise<AcqDeal | null>
783
-
784
- // Deal-sync operations (mirror deal-sync.ts utilities as server-side methods)
785
-
786
- getDealById(params: GetDealByIdParams): Promise<AcqDeal | null>
787
-
788
- getContactById(params: GetContactByIdParams): Promise<AcqContact | null>
789
-
790
- getCompanyById(params: GetCompanyByIdParams): Promise<AcqCompany | null>
791
-
792
- updateDiscoveryData(params: UpdateDiscoveryDataParams): Promise<void>
793
-
794
- updateProposalData(params: UpdateProposalDataParams): Promise<void>
795
-
796
- markProposalSent(params: MarkProposalSentParams): Promise<void>
797
-
798
- markProposalReviewed(params: MarkProposalReviewedParams): Promise<void>
799
-
800
- updateCloseLostReason(params: UpdateCloseLostReasonParams): Promise<void>
801
-
802
- updateFees(params: UpdateFeesParams): Promise<void>
803
-
804
- syncDealStage(params: SyncDealStageParams): Promise<void>
805
-
806
- setContactNurture(params: SetContactNurtureParams): Promise<void>
807
-
808
- cancelSchedulesAndHitlByEmail(
809
- params: CancelSchedulesAndHitlByEmailParams
810
- ): Promise<{ schedulesCancelled: number; hitlDeleted: number }>
811
-
812
- cancelHitlByDealId(params: CancelHitlByDealIdParams): Promise<{ hitlDeleted: number }>
813
-
814
- clearDealFields(params: ClearDealFieldsParams): Promise<void>
815
-
816
- deleteDeal(params: DeleteDealParams): Promise<void>
817
-
818
- listDeals(organizationId: string, filters?: DealFilters): Promise<AcqDeal[]>
819
-
820
- getDealPipelineAnalytics(organizationId: string, recentLimit?: number): Promise<DealPipelineAnalytics>
821
-
822
- /**
823
- * Deep-merge enrichment data into a company or contact record.
824
- * Merges per source key rather than wholesale replacing the JSONB object.
825
- * @see LeadService.mergeEnrichmentData (apps/api/src/business/acquisition/lead-service.ts)
826
- */
827
- mergeEnrichmentData(
828
- id: string,
829
- orgId: string,
830
- table: 'acq_companies' | 'acq_contacts',
831
- data: Record<string, unknown>
832
- ): Promise<void>
833
-
834
- // Deal note operations (acq_deal_notes table)
835
- /**
836
- * Create a human-authored note on a deal
837
- * @see LeadService.createDealNote (apps/api/src/business/acquisition/lead-service.ts)
838
- */
839
- createDealNote(params: CreateDealNoteParams): Promise<AcqDealNote>
840
-
841
- /**
842
- * List notes for a deal, ordered by created_at DESC
843
- * @see LeadService.listDealNotes (apps/api/src/business/acquisition/lead-service.ts)
844
- */
845
- listDealNotes(params: ListDealNotesParams): Promise<AcqDealNote[]>
846
-
847
- // Deal task operations (acq_deal_tasks table)
848
- /**
849
- * Creates a new task attached to a deal.
850
- * @see LeadService.createDealTask (apps/api/src/business/acquisition/lead-service.ts)
851
- */
852
- createDealTask(params: CreateDealTaskParams): Promise<AcqDealTask>
853
-
854
- /**
855
- * Lists all tasks for a given deal, ordered by due date.
856
- * @see LeadService.listDealTasks (apps/api/src/business/acquisition/lead-service.ts)
857
- */
858
- listDealTasks(params: ListDealTasksParams): Promise<AcqDealTask[]>
859
-
860
- /**
861
- * Lists open (uncompleted) tasks within a date window across all deals.
862
- * @see LeadService.listDealTasksDue (apps/api/src/business/acquisition/lead-service.ts)
863
- */
864
- listDealTasksDue(params: ListDealTasksDueParams): Promise<AcqDealTask[]>
865
-
866
- /**
867
- * Marks a task as completed.
868
- * @see LeadService.completeDealTask (apps/api/src/business/acquisition/lead-service.ts)
869
- */
870
- completeDealTask(params: CompleteDealTaskParams): Promise<AcqDealTask>
871
-
872
- /**
873
- * Record a deal activity entry by deal ID.
874
- * Generic method for any activity type — used by SDK workflows.
875
- * @see LeadService.recordDealActivity (apps/api/src/business/acquisition/lead-service.ts)
876
- */
877
- recordDealActivity(params: RecordDealActivityParams): Promise<void>
878
-
879
- // Social monitoring operations (acq_social_posts table)
880
- /**
881
- * Bulk upsert social posts (deduplicate by platform + platform_post_id)
882
- * @see LeadService.upsertSocialPosts (apps/api/src/business/acquisition/lead-service.ts)
883
- */
884
- upsertSocialPosts(params: UpsertSocialPostsParams): Promise<UpsertSocialPostsResult>
885
- }
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
+
466
+ export interface UpdateCompanyStageParams {
467
+ organizationId: string
468
+ listId: string
469
+ companyId: string
470
+ stage: CompanyListStage
471
+ executionId?: string
472
+ }
473
+
474
+ export interface UpdateContactStageParams {
475
+ organizationId: string
476
+ listId: string
477
+ contactId: string
478
+ stage: ContactListStage
479
+ executionId?: string
480
+ }
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
+ }