( domain: string, context?: TriageContext )
| 505 | * Checks the kill switch before proceeding. |
| 506 | */ |
| 507 | export async function triageAndCreateProspect( |
| 508 | domain: string, |
| 509 | context?: TriageContext |
| 510 | ): Promise<{ triaged: boolean; created: boolean; orgId?: string; result: TriageResult }> { |
| 511 | // Check kill switch |
| 512 | const enabled = await isTriageEnabled(); |
| 513 | if (!enabled) { |
| 514 | logger.debug({ domain }, 'Prospect triage disabled via system setting'); |
| 515 | return { |
| 516 | triaged: false, |
| 517 | created: false, |
| 518 | result: { action: 'skip', reason: 'triage_disabled', owner: 'addie', priority: 'standard', verdict: 'Automatic triage is disabled.' }, |
| 519 | }; |
| 520 | } |
| 521 | |
| 522 | let result: TriageResult; |
| 523 | |
| 524 | try { |
| 525 | result = await triageEmailDomain(domain, context); |
| 526 | } catch (err) { |
| 527 | logger.error({ err, domain }, 'Triage assessment failed'); |
| 528 | return { |
| 529 | triaged: false, |
| 530 | created: false, |
| 531 | result: { action: 'skip', reason: 'assessment_error', owner: 'human', priority: 'standard', verdict: '' }, |
| 532 | }; |
| 533 | } |
| 534 | |
| 535 | if (result.action === 'skip') { |
| 536 | handleSkipSideEffects(domain, result, context); |
| 537 | logger.debug({ domain, reason: result.reason }, 'Triage: skip'); |
| 538 | return { triaged: true, created: false, orgId: result.existingOrgId, result }; |
| 539 | } |
| 540 | |
| 541 | logger.info({ domain, owner: result.owner, priority: result.priority, companyName: result.companyName }, 'Triage: creating prospect'); |
| 542 | |
| 543 | const prospectResult = await createProspect({ |
| 544 | name: result.companyName || domain, |
| 545 | domain, |
| 546 | company_type: result.companyType, |
| 547 | prospect_status: 'prospect', |
| 548 | prospect_source: context?.source ?? 'inbound', |
| 549 | prospect_notes: result.verdict, |
| 550 | prospect_owner: result.owner === 'addie' ? 'addie' : undefined, |
| 551 | prospect_contact_email: context?.email, |
| 552 | prospect_contact_name: context?.name, |
| 553 | prospect_contact_title: context?.title, |
| 554 | }); |
| 555 | |
| 556 | if (!prospectResult.success) { |
| 557 | if (prospectResult.alreadyExists) { |
| 558 | logger.debug({ domain }, 'Triage: prospect already exists'); |
| 559 | return { triaged: true, created: false, orgId: prospectResult.organization?.workos_organization_id, result }; |
| 560 | } |
| 561 | logger.error({ domain, error: prospectResult.error }, 'Triage: failed to create prospect'); |
| 562 | return { triaged: true, created: false, result }; |
| 563 | } |
| 564 |
no test coverage detected