fullstackgtm 0.45.0 → 0.47.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.
- package/CHANGELOG.md +115 -1
- package/INSTALL_FOR_AGENTS.md +13 -11
- package/README.md +27 -18
- package/dist/backfill.d.ts +86 -0
- package/dist/backfill.js +251 -0
- package/dist/bin.js +4 -1
- package/dist/cli/auth.d.ts +2 -0
- package/dist/cli/auth.js +119 -12
- package/dist/cli/backfill.d.ts +1 -0
- package/dist/cli/backfill.js +125 -0
- package/dist/cli/backfillRuns.d.ts +1 -0
- package/dist/cli/backfillRuns.js +187 -0
- package/dist/cli/call.js +23 -9
- package/dist/cli/enrich.js +28 -10
- package/dist/cli/fix.js +9 -7
- package/dist/cli/help.js +60 -23
- package/dist/cli/plans.js +13 -11
- package/dist/cli/shared.d.ts +4 -3
- package/dist/cli/shared.js +31 -20
- package/dist/cli/ui.d.ts +21 -0
- package/dist/cli/ui.js +53 -1
- package/dist/cli.js +37 -41
- package/dist/connector.d.ts +8 -0
- package/dist/connector.js +104 -21
- package/dist/connectors/hubspot.d.ts +8 -1
- package/dist/connectors/hubspot.js +406 -13
- package/dist/connectors/hubspotAuth.js +5 -1
- package/dist/connectors/linkedin.js +14 -14
- package/dist/connectors/salesforce.d.ts +8 -1
- package/dist/connectors/salesforce.js +163 -2
- package/dist/connectors/stripe.d.ts +37 -0
- package/dist/connectors/stripe.js +103 -31
- package/dist/enrich.d.ts +7 -0
- package/dist/enrich.js +7 -0
- package/dist/health.d.ts +11 -69
- package/dist/health.js +4 -134
- package/dist/healthScore.d.ts +71 -0
- package/dist/healthScore.js +143 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/llm.d.ts +29 -0
- package/dist/llm.js +206 -0
- package/dist/market.d.ts +39 -1
- package/dist/market.js +116 -44
- package/dist/marketClassify.d.ts +9 -1
- package/dist/marketClassify.js +10 -1
- package/dist/marketTaxonomy.d.ts +6 -1
- package/dist/marketTaxonomy.js +4 -2
- package/dist/mcp-bin.js +29 -0
- package/dist/mcp.js +117 -4
- package/dist/progress.d.ts +96 -0
- package/dist/progress.js +142 -0
- package/dist/runReport.d.ts +24 -0
- package/dist/runReport.js +139 -4
- package/dist/types.d.ts +33 -1
- package/docs/api.md +4 -2
- package/docs/architecture.md +2 -0
- package/docs/linkedin-connector-spec.md +1 -1
- package/llms.txt +3 -3
- package/package.json +10 -3
- package/skills/fullstackgtm/SKILL.md +1 -0
- package/src/backfill.ts +340 -0
- package/src/bin.ts +5 -1
- package/src/cli/auth.ts +135 -15
- package/src/cli/backfill.ts +156 -0
- package/src/cli/backfillRuns.ts +198 -0
- package/src/cli/call.ts +26 -10
- package/src/cli/enrich.ts +33 -10
- package/src/cli/fix.ts +11 -10
- package/src/cli/help.ts +61 -23
- package/src/cli/plans.ts +15 -14
- package/src/cli/shared.ts +44 -27
- package/src/cli/ui.ts +72 -1
- package/src/cli.ts +38 -41
- package/src/connector.ts +110 -16
- package/src/connectors/hubspot.ts +423 -14
- package/src/connectors/hubspotAuth.ts +5 -1
- package/src/connectors/linkedin.ts +14 -14
- package/src/connectors/salesforce.ts +168 -3
- package/src/connectors/stripe.ts +154 -34
- package/src/enrich.ts +13 -0
- package/src/health.ts +14 -213
- package/src/healthScore.ts +223 -0
- package/src/index.ts +28 -1
- package/src/llm.ts +239 -0
- package/src/market.ts +157 -44
- package/src/marketClassify.ts +18 -1
- package/src/marketTaxonomy.ts +10 -2
- package/src/mcp-bin.ts +32 -0
- package/src/mcp.ts +140 -6
- package/src/progress.ts +197 -0
- package/src/runReport.ts +159 -4
- package/src/types.ts +35 -2
- package/docs/dx-punch-list.md +0 -87
- package/docs/roadmap-to-1.0.md +0 -211
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HUBSPOT_DEFAULT_FIELD_MAPPINGS, mappedField, mappedFields, readMappedValue, } from "../mappings.js";
|
|
2
|
+
import { SNAPSHOT_PULL_STAGES } from "../progress.js";
|
|
2
3
|
const DEFAULT_API_BASE_URL = "https://api.hubapi.com";
|
|
3
4
|
const OBJECT_PATHS = {
|
|
4
5
|
account: "companies",
|
|
@@ -10,6 +11,14 @@ const MAPPING_OBJECT_TYPES = {
|
|
|
10
11
|
contact: "contacts",
|
|
11
12
|
deal: "deals",
|
|
12
13
|
};
|
|
14
|
+
// SnapshotProgress object types → the shared SNAPSHOT_PULL_STAGES names, so
|
|
15
|
+
// the CLI checklist and the app StageTimeline read the same stage labels.
|
|
16
|
+
const PULL_STAGE_BY_TYPE = {
|
|
17
|
+
user: "owners",
|
|
18
|
+
account: "accounts",
|
|
19
|
+
contact: "contacts",
|
|
20
|
+
deal: "deals",
|
|
21
|
+
};
|
|
13
22
|
/**
|
|
14
23
|
* Reference connector for HubSpot.
|
|
15
24
|
*
|
|
@@ -30,6 +39,32 @@ export function createHubspotConnector(options) {
|
|
|
30
39
|
// match value (email): HubSpot search is eventually consistent, so a contact
|
|
31
40
|
// created earlier in this apply run is invisible to a later search.
|
|
32
41
|
const createdContactsByMatch = new Map();
|
|
42
|
+
// Same-run dedup for `create_record` deal creates, keyed by
|
|
43
|
+
// matchKey:matchValue (e.g. stripe_invoice_id:in_123) — same
|
|
44
|
+
// eventual-consistency rationale as the contact cache.
|
|
45
|
+
const createdDealsByMatch = new Map();
|
|
46
|
+
// One /crm/v3/pipelines/deals read per connector lifetime (one apply run):
|
|
47
|
+
// pipeline/stage metadata doesn't change mid-run, and deal creates resolve
|
|
48
|
+
// the closed-won stage from it per operation.
|
|
49
|
+
let dealPipelinesCache;
|
|
50
|
+
// Custom deal dedupe properties ensured (or confirmed missing) this run.
|
|
51
|
+
const ensuredDealProperties = new Map();
|
|
52
|
+
// Per-page snapshot-pull progress: the legacy onProgress callback plus the
|
|
53
|
+
// shared emitter (stage on the first page of each object type, items per
|
|
54
|
+
// page). Both are presentation-only — callers already swallow errors.
|
|
55
|
+
let lastPullStage;
|
|
56
|
+
const pullProgress = (objectType, fetched) => {
|
|
57
|
+
options.onProgress?.({ objectType, fetched });
|
|
58
|
+
const emitter = options.progress;
|
|
59
|
+
if (!emitter)
|
|
60
|
+
return;
|
|
61
|
+
const stage = PULL_STAGE_BY_TYPE[objectType];
|
|
62
|
+
if (stage !== lastPullStage) {
|
|
63
|
+
lastPullStage = stage;
|
|
64
|
+
emitter.stage(stage, SNAPSHOT_PULL_STAGES.indexOf(stage), SNAPSHOT_PULL_STAGES.length);
|
|
65
|
+
}
|
|
66
|
+
emitter.items(fetched);
|
|
67
|
+
};
|
|
33
68
|
async function request(path, init = {}) {
|
|
34
69
|
const token = await options.getAccessToken();
|
|
35
70
|
let response;
|
|
@@ -115,7 +150,7 @@ export function createHubspotConnector(options) {
|
|
|
115
150
|
return results;
|
|
116
151
|
}
|
|
117
152
|
async function assembleSnapshot(fetchObjects) {
|
|
118
|
-
const owners = await list("/crm/v3/owners?limit=100", (fetched) =>
|
|
153
|
+
const owners = await list("/crm/v3/owners?limit=100", (fetched) => pullProgress("user", fetched));
|
|
119
154
|
const users = owners
|
|
120
155
|
.filter((owner) => owner.id)
|
|
121
156
|
.map((owner) => ({
|
|
@@ -228,6 +263,8 @@ export function createHubspotConnector(options) {
|
|
|
228
263
|
raw: deal,
|
|
229
264
|
};
|
|
230
265
|
});
|
|
266
|
+
// Deliver any throttled trailing items heartbeat before the pull returns.
|
|
267
|
+
options.progress?.flush();
|
|
231
268
|
return {
|
|
232
269
|
generatedAt: new Date().toISOString(),
|
|
233
270
|
provider: "hubspot",
|
|
@@ -242,7 +279,7 @@ export function createHubspotConnector(options) {
|
|
|
242
279
|
}
|
|
243
280
|
async function fetchSnapshot() {
|
|
244
281
|
const canonicalType = { companies: "account", contacts: "contact", deals: "deal" };
|
|
245
|
-
return assembleSnapshot((objectType, properties, withAssociations) => list(`/crm/v3/objects/${objectType}?limit=100&properties=${properties}${withAssociations ? "&associations=companies" : ""}`, (fetched) =>
|
|
282
|
+
return assembleSnapshot((objectType, properties, withAssociations) => list(`/crm/v3/objects/${objectType}?limit=100&properties=${properties}${withAssociations ? "&associations=companies" : ""}`, (fetched) => pullProgress(canonicalType[objectType], fetched)));
|
|
246
283
|
}
|
|
247
284
|
const MODIFIED_DATE_PROPERTIES = {
|
|
248
285
|
companies: "hs_lastmodifieddate",
|
|
@@ -506,12 +543,199 @@ export function createHubspotConnector(options) {
|
|
|
506
543
|
createdCompaniesByName.set(cacheKey, id);
|
|
507
544
|
return id;
|
|
508
545
|
}
|
|
546
|
+
/** Exact-value deal lookup (by any searchable property) for resolve-first creates. */
|
|
547
|
+
async function searchDealsBy(property, value) {
|
|
548
|
+
const data = await request(`/crm/v3/objects/deals/search`, {
|
|
549
|
+
method: "POST",
|
|
550
|
+
body: JSON.stringify({
|
|
551
|
+
filterGroups: [{ filters: [{ propertyName: property, operator: "EQ", value }] }],
|
|
552
|
+
properties: [property],
|
|
553
|
+
limit: 3,
|
|
554
|
+
}),
|
|
555
|
+
});
|
|
556
|
+
return (data?.results ?? []).map((row) => String(row.id));
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Make sure the deal dedupe property (e.g. "stripe_invoice_id") exists so
|
|
560
|
+
* both the resolve-first search and the stamped value are real. Best-effort
|
|
561
|
+
* create on a confirmed miss; returns false when the property can neither
|
|
562
|
+
* be read nor created — the caller must SKIP then (creating a deal without
|
|
563
|
+
* its dedupe key would make every replay a duplicate).
|
|
564
|
+
*/
|
|
565
|
+
async function ensureDealProperty(name) {
|
|
566
|
+
const cached = ensuredDealProperties.get(name);
|
|
567
|
+
if (cached !== undefined)
|
|
568
|
+
return cached;
|
|
569
|
+
let ok;
|
|
570
|
+
try {
|
|
571
|
+
await request(`/crm/v3/properties/deals/${encodeURIComponent(name)}`);
|
|
572
|
+
ok = true;
|
|
573
|
+
}
|
|
574
|
+
catch {
|
|
575
|
+
// Missing (404) or unreadable — try to create it; a failure here means
|
|
576
|
+
// we cannot guarantee the dedupe key, so report false.
|
|
577
|
+
try {
|
|
578
|
+
await request(`/crm/v3/properties/deals`, {
|
|
579
|
+
method: "POST",
|
|
580
|
+
body: JSON.stringify({
|
|
581
|
+
name,
|
|
582
|
+
label: name,
|
|
583
|
+
type: "string",
|
|
584
|
+
fieldType: "text",
|
|
585
|
+
groupName: "dealinformation",
|
|
586
|
+
hasUniqueValue: false,
|
|
587
|
+
}),
|
|
588
|
+
});
|
|
589
|
+
ok = true;
|
|
590
|
+
}
|
|
591
|
+
catch {
|
|
592
|
+
ok = false;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
ensuredDealProperties.set(name, ok);
|
|
596
|
+
return ok;
|
|
597
|
+
}
|
|
509
598
|
/**
|
|
510
|
-
*
|
|
511
|
-
*
|
|
512
|
-
*
|
|
513
|
-
*
|
|
514
|
-
*
|
|
599
|
+
* Resolve the target pipeline and its closed-won stage from the portal's
|
|
600
|
+
* own pipeline metadata (one read per run). The pipeline is picked by id or
|
|
601
|
+
* case-insensitive label (`hint`), else the default (lowest displayOrder);
|
|
602
|
+
* the closed-won stage is the one whose metadata says isClosed with
|
|
603
|
+
* probability 1 — NEVER a substring guess against stage names. Returns null
|
|
604
|
+
* when either is unresolvable so the caller skips instead of guessing.
|
|
605
|
+
*/
|
|
606
|
+
async function resolveClosedWonStage(hint) {
|
|
607
|
+
if (!dealPipelinesCache) {
|
|
608
|
+
dealPipelinesCache = request("/crm/v3/pipelines/deals").then((data) => (data?.results ?? []));
|
|
609
|
+
// A failed read must not poison the cache for the rest of the run.
|
|
610
|
+
dealPipelinesCache = dealPipelinesCache.catch(() => []);
|
|
611
|
+
}
|
|
612
|
+
const pipelines = await dealPipelinesCache;
|
|
613
|
+
if (pipelines.length === 0)
|
|
614
|
+
return null;
|
|
615
|
+
let pipeline;
|
|
616
|
+
if (hint) {
|
|
617
|
+
const wanted = hint.trim().toLowerCase();
|
|
618
|
+
pipeline = pipelines.find((candidate) => String(candidate?.id ?? "").toLowerCase() === wanted ||
|
|
619
|
+
String(candidate?.label ?? "").trim().toLowerCase() === wanted);
|
|
620
|
+
if (!pipeline)
|
|
621
|
+
return null; // an explicit hint that matches nothing is an error, not "use default"
|
|
622
|
+
}
|
|
623
|
+
else {
|
|
624
|
+
pipeline = [...pipelines].sort((a, b) => Number(a?.displayOrder ?? 0) - Number(b?.displayOrder ?? 0))[0];
|
|
625
|
+
}
|
|
626
|
+
if (!pipeline?.id)
|
|
627
|
+
return null;
|
|
628
|
+
const stage = (pipeline.stages ?? []).find((candidate) => {
|
|
629
|
+
const meta = candidate?.metadata ?? {};
|
|
630
|
+
return (candidate?.id &&
|
|
631
|
+
String(meta.isClosed).toLowerCase() === "true" &&
|
|
632
|
+
Number(meta.probability) === 1);
|
|
633
|
+
});
|
|
634
|
+
if (!stage)
|
|
635
|
+
return null;
|
|
636
|
+
return { pipelineId: String(pipeline.id), stageId: String(stage.id) };
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Deal branch of create_record (backfill: one closed-won deal per paid
|
|
640
|
+
* invoice). Resolve-first on the custom dedupe property (ensured to exist
|
|
641
|
+
* first), stage resolved from pipeline metadata, company association
|
|
642
|
+
* best-effort — same contract as the contact branch.
|
|
643
|
+
*/
|
|
644
|
+
async function createDealRecord(operation, payload, matchValue) {
|
|
645
|
+
const matchKey = String(payload.matchKey ?? "").trim();
|
|
646
|
+
if (!matchKey) {
|
|
647
|
+
return { operationId: operation.id, status: "skipped", detail: "create_record for deals needs a matchKey (the dedupe property, e.g. stripe_invoice_id)." };
|
|
648
|
+
}
|
|
649
|
+
const dedupeKey = `${matchKey}:${matchValue.toLowerCase()}`;
|
|
650
|
+
const alreadyCreated = createdDealsByMatch.get(dedupeKey);
|
|
651
|
+
if (alreadyCreated) {
|
|
652
|
+
return { operationId: operation.id, status: "skipped", detail: `Deal ${matchKey}=${matchValue} already created earlier in this run; not duplicating.`, providerData: { id: alreadyCreated, existing: true } };
|
|
653
|
+
}
|
|
654
|
+
// The dedupe property is usually CUSTOM — ensure it exists before the
|
|
655
|
+
// search; searching a property HubSpot doesn't have 400s, and creating a
|
|
656
|
+
// deal without the key stamped would defeat every future resolve-first.
|
|
657
|
+
const propertyOk = await ensureDealProperty(matchKey);
|
|
658
|
+
if (!propertyOk) {
|
|
659
|
+
return { operationId: operation.id, status: "skipped", detail: `Deal dedupe property "${matchKey}" does not exist and could not be created; refusing to create a deal without its dedupe key.` };
|
|
660
|
+
}
|
|
661
|
+
const existing = await searchDealsBy(matchKey, matchValue);
|
|
662
|
+
if (existing.length > 0) {
|
|
663
|
+
createdDealsByMatch.set(dedupeKey, existing[0]);
|
|
664
|
+
return { operationId: operation.id, status: "skipped", detail: `Deal ${matchKey}=${matchValue} already exists (${existing.join(", ")}); resolve-first declined to create.`, providerData: { id: existing[0], existing: true } };
|
|
665
|
+
}
|
|
666
|
+
// Stage: only the provider-neutral "closed_won" sentinel is understood,
|
|
667
|
+
// and it must resolve to a real stage id from pipeline metadata.
|
|
668
|
+
if (payload.dealStage !== "closed_won") {
|
|
669
|
+
return { operationId: operation.id, status: "skipped", detail: 'create_record for deals needs dealStage "closed_won" (the connector resolves the real stage id from pipeline metadata).' };
|
|
670
|
+
}
|
|
671
|
+
const resolved = await resolveClosedWonStage(payload.dealPipeline);
|
|
672
|
+
if (!resolved) {
|
|
673
|
+
return {
|
|
674
|
+
operationId: operation.id,
|
|
675
|
+
status: "skipped",
|
|
676
|
+
detail: payload.dealPipeline
|
|
677
|
+
? `Could not resolve pipeline "${payload.dealPipeline}" (by id or label) to a closed-won stage; not guessing a stage.`
|
|
678
|
+
: "Could not resolve the default deal pipeline's closed-won stage (isClosed + probability 1) from pipeline metadata; not guessing a stage.",
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
const properties = {
|
|
682
|
+
...payload.properties,
|
|
683
|
+
[matchKey]: matchValue,
|
|
684
|
+
pipeline: resolved.pipelineId,
|
|
685
|
+
dealstage: resolved.stageId,
|
|
686
|
+
};
|
|
687
|
+
if (payload.ownerId && !properties.hubspot_owner_id) {
|
|
688
|
+
properties.hubspot_owner_id = String(payload.ownerId);
|
|
689
|
+
}
|
|
690
|
+
let created;
|
|
691
|
+
try {
|
|
692
|
+
created = await request(`/crm/v3/objects/deals`, {
|
|
693
|
+
method: "POST",
|
|
694
|
+
body: JSON.stringify({ properties: { ...properties, hs_object_source_detail_2: `fullstackgtm backfill (${operation.id})` } }),
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
catch {
|
|
698
|
+
// Some portals reject writes to source-detail properties — the provenance
|
|
699
|
+
// stamp is best-effort, the create is not.
|
|
700
|
+
created = await request(`/crm/v3/objects/deals`, {
|
|
701
|
+
method: "POST",
|
|
702
|
+
body: JSON.stringify({ properties }),
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
const dealId = String(created.id);
|
|
706
|
+
createdDealsByMatch.set(dedupeKey, dealId);
|
|
707
|
+
let companyNote = "";
|
|
708
|
+
if (payload.associateCompanyName) {
|
|
709
|
+
try {
|
|
710
|
+
const companyId = await resolveOrCreateCompany(payload.associateCompanyName, payload.associateCompanyDomain, operation.id);
|
|
711
|
+
if (companyId) {
|
|
712
|
+
await request(`/crm/v4/objects/deals/${encodeURIComponent(dealId)}/associations/default/companies/${encodeURIComponent(companyId)}`, { method: "PUT" });
|
|
713
|
+
const domainNote = payload.associateCompanyDomain ? ` (${payload.associateCompanyDomain})` : "";
|
|
714
|
+
companyNote = ` Linked to company "${payload.associateCompanyName}"${domainNote} (${companyId}).`;
|
|
715
|
+
}
|
|
716
|
+
else {
|
|
717
|
+
companyNote = ` Company "${payload.associateCompanyName}" was ambiguous; left unlinked.`;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
catch (error) {
|
|
721
|
+
// Association is best-effort — the deal create already succeeded.
|
|
722
|
+
companyNote = ` (company link failed: ${error instanceof Error ? error.message : String(error)})`;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
return {
|
|
726
|
+
operationId: operation.id,
|
|
727
|
+
status: "applied",
|
|
728
|
+
detail: `Created deal ${matchKey}=${matchValue} (${dealId}) in stage ${resolved.stageId}.${companyNote}`,
|
|
729
|
+
providerData: { id: dealId, created: true, pipelineId: resolved.pipelineId, stageId: resolved.stageId },
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Create a NET-NEW record (a sourced lead, or a backfilled deal).
|
|
734
|
+
* Resolve-first: re-checks the dedupe key against the live CRM (the
|
|
735
|
+
* plan-time snapshot can be stale) and creates ONLY on a confirmed miss — a
|
|
736
|
+
* record a concurrent writer already added is returned as `skipped`, never
|
|
737
|
+
* duplicated. Contacts and deals can be linked to a resolved-or-created
|
|
738
|
+
* company in the same step.
|
|
515
739
|
*/
|
|
516
740
|
async function createRecord(operation) {
|
|
517
741
|
const payload = operation.afterValue;
|
|
@@ -519,13 +743,16 @@ export function createHubspotConnector(options) {
|
|
|
519
743
|
return { operationId: operation.id, status: "skipped", detail: "create_record needs a CreateRecordPayload afterValue." };
|
|
520
744
|
}
|
|
521
745
|
const objectPath = OBJECT_PATHS[operation.objectType];
|
|
522
|
-
if (operation.objectType !== "contact" && operation.objectType !== "account") {
|
|
523
|
-
return { operationId: operation.id, status: "skipped", detail: "create_record supports contacts and
|
|
746
|
+
if (operation.objectType !== "contact" && operation.objectType !== "account" && operation.objectType !== "deal") {
|
|
747
|
+
return { operationId: operation.id, status: "skipped", detail: "create_record supports contacts, accounts, and deals." };
|
|
524
748
|
}
|
|
525
749
|
const matchValue = String(payload.matchValue ?? "").trim();
|
|
526
750
|
if (!matchValue) {
|
|
527
751
|
return { operationId: operation.id, status: "skipped", detail: "create_record needs a non-empty matchValue to resolve-first." };
|
|
528
752
|
}
|
|
753
|
+
if (operation.objectType === "deal") {
|
|
754
|
+
return createDealRecord(operation, payload, matchValue);
|
|
755
|
+
}
|
|
529
756
|
if (operation.objectType === "account") {
|
|
530
757
|
const id = await resolveOrCreateCompany(matchValue, stringOrUndefined(payload.properties?.domain), operation.id);
|
|
531
758
|
if (id === null) {
|
|
@@ -830,6 +1057,173 @@ export function createHubspotConnector(options) {
|
|
|
830
1057
|
detail: `Archived ${objectPath}/${operation.objectId}.`,
|
|
831
1058
|
};
|
|
832
1059
|
}
|
|
1060
|
+
function fieldMapping(operation) {
|
|
1061
|
+
const objectPath = OBJECT_PATHS[operation.objectType];
|
|
1062
|
+
const mappingType = MAPPING_OBJECT_TYPES[operation.objectType];
|
|
1063
|
+
if (!objectPath || !mappingType || !operation.field)
|
|
1064
|
+
return null;
|
|
1065
|
+
const defaults = HUBSPOT_DEFAULT_FIELD_MAPPINGS[mappingType] ?? {};
|
|
1066
|
+
return { objectPath, property: mappedField(mappings, mappingType, operation.field, defaults[operation.field] ?? operation.field) };
|
|
1067
|
+
}
|
|
1068
|
+
function normalizeHubspotReadValue(field, value) {
|
|
1069
|
+
if (field === "closeDate" && typeof value === "string")
|
|
1070
|
+
return value.split("T")[0];
|
|
1071
|
+
return value ?? null;
|
|
1072
|
+
}
|
|
1073
|
+
function normalizeForComparison(value) {
|
|
1074
|
+
if (value === undefined || value === null || value === "")
|
|
1075
|
+
return null;
|
|
1076
|
+
return String(value);
|
|
1077
|
+
}
|
|
1078
|
+
function batchErrorIds(error) {
|
|
1079
|
+
const context = error?.context ?? {};
|
|
1080
|
+
const ids = context.ids ?? context.id ?? error?.id ?? error?.objectId;
|
|
1081
|
+
if (Array.isArray(ids))
|
|
1082
|
+
return ids.map(String);
|
|
1083
|
+
if (ids !== undefined && ids !== null)
|
|
1084
|
+
return [String(ids)];
|
|
1085
|
+
return [];
|
|
1086
|
+
}
|
|
1087
|
+
function batchErrorDetail(error) {
|
|
1088
|
+
const category = error?.category ? `${String(error.category)}: ` : "";
|
|
1089
|
+
return `${category}${String(error?.message ?? "HubSpot batch row failed.")}`;
|
|
1090
|
+
}
|
|
1091
|
+
async function applySetFieldBatch(operations) {
|
|
1092
|
+
const out = new Map();
|
|
1093
|
+
const mapped = operations.map((operation) => ({ operation, mapping: fieldMapping(operation) }));
|
|
1094
|
+
for (const item of mapped) {
|
|
1095
|
+
if (!item.mapping) {
|
|
1096
|
+
out.set(item.operation.id, {
|
|
1097
|
+
operationId: item.operation.id,
|
|
1098
|
+
status: "skipped",
|
|
1099
|
+
detail: "Field writes are only supported for accounts, contacts, and deals with an explicit field.",
|
|
1100
|
+
});
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
const clean = mapped.filter((item) => Boolean(item.mapping));
|
|
1104
|
+
if (clean.length === 0)
|
|
1105
|
+
return operations.map((operation) => out.get(operation.id));
|
|
1106
|
+
const objectPath = clean[0].mapping.objectPath;
|
|
1107
|
+
const properties = [...new Set(clean.map((item) => item.mapping.property))];
|
|
1108
|
+
const liveById = new Map();
|
|
1109
|
+
const ids = [...new Set(clean.map((item) => item.operation.objectId))];
|
|
1110
|
+
for (let i = 0; i < ids.length; i += 100) {
|
|
1111
|
+
const chunkIds = ids.slice(i, i + 100);
|
|
1112
|
+
const data = await request(`/crm/v3/objects/${objectPath}/batch/read`, {
|
|
1113
|
+
method: "POST",
|
|
1114
|
+
body: JSON.stringify({ properties, inputs: chunkIds.map((id) => ({ id })) }),
|
|
1115
|
+
});
|
|
1116
|
+
for (const row of data?.results ?? []) {
|
|
1117
|
+
if (row?.id)
|
|
1118
|
+
liveById.set(String(row.id), row.properties ?? {});
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
const toWrite = [];
|
|
1122
|
+
for (const { operation, mapping } of clean) {
|
|
1123
|
+
const props = liveById.get(operation.objectId);
|
|
1124
|
+
const current = normalizeHubspotReadValue(operation.field, props?.[mapping.property] ?? null);
|
|
1125
|
+
const expected = normalizeForComparison(operation.beforeValue);
|
|
1126
|
+
const found = normalizeForComparison(current);
|
|
1127
|
+
if (!props || expected !== found) {
|
|
1128
|
+
out.set(operation.id, {
|
|
1129
|
+
operationId: operation.id,
|
|
1130
|
+
status: "conflict",
|
|
1131
|
+
detail: `Value drifted since the plan was proposed: expected ${expected ?? "∅"}, found ${found ?? "∅"}. Re-run the audit.`,
|
|
1132
|
+
providerData: { currentValue: current ?? null },
|
|
1133
|
+
});
|
|
1134
|
+
continue;
|
|
1135
|
+
}
|
|
1136
|
+
toWrite.push({ operation, property: mapping.property });
|
|
1137
|
+
}
|
|
1138
|
+
for (let i = 0; i < toWrite.length; i += 100) {
|
|
1139
|
+
const chunk = toWrite.slice(i, i + 100);
|
|
1140
|
+
let data;
|
|
1141
|
+
try {
|
|
1142
|
+
data = await request(`/crm/v3/objects/${objectPath}/batch/update`, {
|
|
1143
|
+
method: "POST",
|
|
1144
|
+
body: JSON.stringify({
|
|
1145
|
+
inputs: chunk.map(({ operation, property }) => ({
|
|
1146
|
+
id: operation.objectId,
|
|
1147
|
+
properties: { [property]: operation.operation === "clear_field" ? "" : String(operation.afterValue ?? "") },
|
|
1148
|
+
})),
|
|
1149
|
+
}),
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
catch (error) {
|
|
1153
|
+
for (const { operation } of chunk)
|
|
1154
|
+
out.set(operation.id, { operationId: operation.id, status: "failed", detail: error instanceof Error ? error.message : String(error) });
|
|
1155
|
+
continue;
|
|
1156
|
+
}
|
|
1157
|
+
const opByObjectId = new Map(chunk.map(({ operation }) => [operation.objectId, operation]));
|
|
1158
|
+
for (const error of data?.errors ?? []) {
|
|
1159
|
+
for (const id of batchErrorIds(error)) {
|
|
1160
|
+
const operation = opByObjectId.get(id);
|
|
1161
|
+
if (operation)
|
|
1162
|
+
out.set(operation.id, { operationId: operation.id, status: "failed", detail: batchErrorDetail(error) });
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
for (const result of data?.results ?? []) {
|
|
1166
|
+
const operation = opByObjectId.get(String(result?.id));
|
|
1167
|
+
if (operation && !out.has(operation.id))
|
|
1168
|
+
out.set(operation.id, { operationId: operation.id, status: "applied", detail: `Set ${chunk.find((item) => item.operation.id === operation.id)?.property ?? "field"} on ${objectPath}/${operation.objectId}.`, providerData: { id: result?.id } });
|
|
1169
|
+
}
|
|
1170
|
+
for (const { operation, property } of chunk) {
|
|
1171
|
+
if (!out.has(operation.id))
|
|
1172
|
+
out.set(operation.id, { operationId: operation.id, status: "applied", detail: `Set ${property} on ${objectPath}/${operation.objectId}.` });
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
return operations.map((operation) => out.get(operation.id) ?? { operationId: operation.id, status: "failed", detail: "Batch update did not process this operation." });
|
|
1176
|
+
}
|
|
1177
|
+
async function applyArchiveBatch(operations) {
|
|
1178
|
+
const objectPath = OBJECT_PATHS[operations[0]?.objectType];
|
|
1179
|
+
if (!objectPath)
|
|
1180
|
+
return operations.map((operation) => ({ operationId: operation.id, status: "skipped", detail: "archive_record is supported for accounts, contacts, and deals." }));
|
|
1181
|
+
const out = new Map();
|
|
1182
|
+
for (let i = 0; i < operations.length; i += 100) {
|
|
1183
|
+
const chunk = operations.slice(i, i + 100);
|
|
1184
|
+
let data;
|
|
1185
|
+
try {
|
|
1186
|
+
data = await request(`/crm/v3/objects/${objectPath}/batch/archive`, {
|
|
1187
|
+
method: "POST",
|
|
1188
|
+
body: JSON.stringify({ inputs: chunk.map((operation) => ({ id: operation.objectId })) }),
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
catch (error) {
|
|
1192
|
+
for (const operation of chunk)
|
|
1193
|
+
out.set(operation.id, { operationId: operation.id, status: "failed", detail: error instanceof Error ? error.message : String(error) });
|
|
1194
|
+
continue;
|
|
1195
|
+
}
|
|
1196
|
+
const opByObjectId = new Map(chunk.map((operation) => [operation.objectId, operation]));
|
|
1197
|
+
for (const error of data?.errors ?? []) {
|
|
1198
|
+
for (const id of batchErrorIds(error)) {
|
|
1199
|
+
const operation = opByObjectId.get(id);
|
|
1200
|
+
if (operation)
|
|
1201
|
+
out.set(operation.id, { operationId: operation.id, status: "failed", detail: batchErrorDetail(error) });
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
for (const result of data?.results ?? []) {
|
|
1205
|
+
const operation = opByObjectId.get(String(result?.id));
|
|
1206
|
+
if (operation && !out.has(operation.id))
|
|
1207
|
+
out.set(operation.id, { operationId: operation.id, status: "applied", detail: `Archived ${objectPath}/${operation.objectId}.` });
|
|
1208
|
+
}
|
|
1209
|
+
for (const operation of chunk) {
|
|
1210
|
+
if (!out.has(operation.id))
|
|
1211
|
+
out.set(operation.id, { operationId: operation.id, status: "applied", detail: `Archived ${objectPath}/${operation.objectId}.` });
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
return operations.map((operation) => out.get(operation.id) ?? { operationId: operation.id, status: "failed", detail: "Batch archive did not process this operation." });
|
|
1215
|
+
}
|
|
1216
|
+
async function applyBatch(operations) {
|
|
1217
|
+
if (operations.length === 0)
|
|
1218
|
+
return [];
|
|
1219
|
+
if (operations.every((operation) => (operation.operation === "set_field" || operation.operation === "clear_field") && operation.objectType === operations[0].objectType)) {
|
|
1220
|
+
return applySetFieldBatch(operations);
|
|
1221
|
+
}
|
|
1222
|
+
if (operations.every((operation) => operation.operation === "archive_record" && operation.objectType === operations[0].objectType)) {
|
|
1223
|
+
return applyArchiveBatch(operations);
|
|
1224
|
+
}
|
|
1225
|
+
return operations.map((operation) => ({ operationId: operation.id, status: "skipped", detail: "This mixed operation batch is not supported." }));
|
|
1226
|
+
}
|
|
833
1227
|
/**
|
|
834
1228
|
* Merge a duplicate group into the approved survivor via HubSpot's v3
|
|
835
1229
|
* merge API (supported for contacts, companies, deals, and tickets).
|
|
@@ -955,10 +1349,7 @@ export function createHubspotConnector(options) {
|
|
|
955
1349
|
// single-object read here returns HubSpot's raw "2026-03-07T00:00:00Z", which
|
|
956
1350
|
// made compare-and-set see a spurious drift and refuse every date-field write.
|
|
957
1351
|
// Mirror the snapshot's normalization so the comparison is apples-to-apples.
|
|
958
|
-
|
|
959
|
-
return value.split("T")[0];
|
|
960
|
-
}
|
|
961
|
-
return value;
|
|
1352
|
+
return normalizeHubspotReadValue(field, value);
|
|
962
1353
|
}
|
|
963
1354
|
return {
|
|
964
1355
|
provider: "hubspot",
|
|
@@ -966,6 +1357,8 @@ export function createHubspotConnector(options) {
|
|
|
966
1357
|
fetchChanges,
|
|
967
1358
|
applyOperation,
|
|
968
1359
|
applyCreateContactsBatch,
|
|
1360
|
+
applyBatch,
|
|
1361
|
+
applyBatchLimit: 100,
|
|
969
1362
|
readField,
|
|
970
1363
|
};
|
|
971
1364
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createServer } from "node:http";
|
|
2
1
|
import { randomBytes } from "node:crypto";
|
|
3
2
|
/**
|
|
4
3
|
* HubSpot CLI authentication.
|
|
@@ -101,6 +100,11 @@ export async function runHubspotLoopbackLogin(options) {
|
|
|
101
100
|
const log = options.log ?? ((message) => console.error(message));
|
|
102
101
|
const redirectUri = `http://localhost:${port}/callback`;
|
|
103
102
|
const state = randomBytes(16).toString("hex");
|
|
103
|
+
// Deferred: `node:http` transitively compiles node's bundled undici on
|
|
104
|
+
// import (~10ms+, 12% of a demo-audit run's CPU profile). This module is in
|
|
105
|
+
// the eager graph of every CLI invocation via help.ts/auth.ts, but the
|
|
106
|
+
// server is only needed during the interactive OAuth loopback login.
|
|
107
|
+
const { createServer } = await import("node:http");
|
|
104
108
|
const code = await new Promise((resolve, reject) => {
|
|
105
109
|
const server = createServer((request, response) => {
|
|
106
110
|
const url = new URL(request.url ?? "/", `http://localhost:${port}`);
|
|
@@ -146,37 +146,37 @@ export function normalizeHeyReachLead(raw) {
|
|
|
146
146
|
// ── Fake provider (tests / dry-run with no key, no network) ─────────────────
|
|
147
147
|
export const FAKE_LINKEDIN_PROSPECTS = [
|
|
148
148
|
{
|
|
149
|
-
firstName: "
|
|
150
|
-
lastName: "
|
|
151
|
-
fullName: "
|
|
149
|
+
firstName: "Example",
|
|
150
|
+
lastName: "ProspectAlpha",
|
|
151
|
+
fullName: "Example Prospect Alpha",
|
|
152
152
|
headline: "VP RevOps",
|
|
153
153
|
jobTitle: "VP Revenue Operations",
|
|
154
154
|
company: "Northwind Logistics",
|
|
155
|
-
profileUrl: "https://
|
|
155
|
+
profileUrl: "https://example.com/linkedin/example-prospect-alpha",
|
|
156
156
|
location: "Chicago, IL",
|
|
157
|
-
email: "
|
|
157
|
+
email: "prospect.alpha@example.com",
|
|
158
158
|
emailStatus: "verified",
|
|
159
159
|
},
|
|
160
160
|
{
|
|
161
|
-
firstName: "
|
|
162
|
-
lastName: "
|
|
163
|
-
fullName: "
|
|
161
|
+
firstName: "Example",
|
|
162
|
+
lastName: "ProspectBeta",
|
|
163
|
+
fullName: "Example Prospect Beta",
|
|
164
164
|
headline: "Head of Sales Ops",
|
|
165
165
|
jobTitle: "Head of Sales Operations",
|
|
166
166
|
company: "Cobalt Health",
|
|
167
|
-
profileUrl: "https://
|
|
167
|
+
profileUrl: "https://example.com/linkedin/example-prospect-beta",
|
|
168
168
|
location: "Austin, TX",
|
|
169
169
|
},
|
|
170
170
|
{
|
|
171
|
-
firstName: "
|
|
172
|
-
lastName: "
|
|
173
|
-
fullName: "
|
|
171
|
+
firstName: "Example",
|
|
172
|
+
lastName: "ProspectGamma",
|
|
173
|
+
fullName: "Example Prospect Gamma",
|
|
174
174
|
headline: "RevOps Manager",
|
|
175
175
|
jobTitle: "Revenue Operations Manager",
|
|
176
176
|
company: "Ferro Systems",
|
|
177
|
-
profileUrl: "https://
|
|
177
|
+
profileUrl: "https://example.com/linkedin/example-prospect-gamma",
|
|
178
178
|
location: "Remote",
|
|
179
|
-
email: "
|
|
179
|
+
email: "prospect.gamma@example.com",
|
|
180
180
|
emailStatus: "guessed",
|
|
181
181
|
},
|
|
182
182
|
];
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type FieldMappings } from "../mappings.ts";
|
|
2
2
|
import type { GtmConnector, SnapshotProgress } from "../types.ts";
|
|
3
|
+
import { type ProgressEmitter } from "../progress.ts";
|
|
3
4
|
export type SalesforceConnection = {
|
|
4
5
|
accessToken: string;
|
|
5
6
|
/** e.g. https://yourorg.my.salesforce.com */
|
|
@@ -15,6 +16,12 @@ export type SalesforceConnectorOptions = {
|
|
|
15
16
|
fetchImpl?: typeof fetch;
|
|
16
17
|
/** Per-page snapshot-pull progress (presentation only — errors are swallowed). */
|
|
17
18
|
onProgress?: (progress: SnapshotProgress) => void;
|
|
19
|
+
/**
|
|
20
|
+
* Shared progress vocabulary (src/progress.ts): the snapshot pull emits a
|
|
21
|
+
* `stage` per object type and an `items` heartbeat per page. Additive
|
|
22
|
+
* alongside the legacy `onProgress` callback; both are presentation-only.
|
|
23
|
+
*/
|
|
24
|
+
progress?: ProgressEmitter;
|
|
18
25
|
};
|
|
19
26
|
/**
|
|
20
27
|
* Reference connector for Salesforce.
|
|
@@ -25,4 +32,4 @@ export type SalesforceConnectorOptions = {
|
|
|
25
32
|
* surface the gaps). Probabilities are normalized to 0..1 to match the
|
|
26
33
|
* canonical model.
|
|
27
34
|
*/
|
|
28
|
-
export declare function createSalesforceConnector(options: SalesforceConnectorOptions):
|
|
35
|
+
export declare function createSalesforceConnector(options: SalesforceConnectorOptions): GtmConnector;
|