@pattern-stack/codegen 0.11.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -0
- package/dist/runtime/subsystems/index.d.ts +7 -3
- package/dist/runtime/subsystems/index.js +993 -19
- package/dist/runtime/subsystems/index.js.map +1 -1
- package/dist/runtime/subsystems/integration/entity-change-source-registry.memory.d.ts +25 -0
- package/dist/runtime/subsystems/integration/entity-change-source-registry.memory.js +34 -0
- package/dist/runtime/subsystems/integration/entity-change-source-registry.memory.js.map +1 -0
- package/dist/runtime/subsystems/integration/entity-change-source-registry.protocol.d.ts +53 -0
- package/dist/runtime/subsystems/integration/entity-change-source-registry.protocol.js +13 -0
- package/dist/runtime/subsystems/integration/entity-change-source-registry.protocol.js.map +1 -0
- package/dist/runtime/subsystems/integration/execute-integration.use-case.js.map +1 -1
- package/dist/runtime/subsystems/integration/index.d.ts +3 -1
- package/dist/runtime/subsystems/integration/index.js +35 -0
- package/dist/runtime/subsystems/integration/index.js.map +1 -1
- package/dist/runtime/subsystems/integration/integration-cursor-store.drizzle-backend.js.map +1 -1
- package/dist/runtime/subsystems/integration/integration-run-recorder.drizzle-backend.js.map +1 -1
- package/dist/runtime/subsystems/integration/integration.module.js.map +1 -1
- package/dist/runtime/subsystems/integration/integration.tokens.d.ts +14 -1
- package/dist/runtime/subsystems/integration/integration.tokens.js +2 -0
- package/dist/runtime/subsystems/integration/integration.tokens.js.map +1 -1
- package/dist/runtime/subsystems/observability/index.js.map +1 -1
- package/dist/runtime/subsystems/observability/observability.module.js.map +1 -1
- package/dist/runtime/subsystems/observability/observability.service.js.map +1 -1
- package/dist/src/cli/index.js +1100 -108
- package/dist/src/cli/index.js.map +1 -1
- package/dist/src/index.d.ts +48 -0
- package/dist/src/index.js +99 -3
- package/dist/src/index.js.map +1 -1
- package/package.json +9 -1
- package/runtime/subsystems/index.ts +15 -0
- package/runtime/subsystems/integration/entity-change-source-registry.memory.ts +40 -0
- package/runtime/subsystems/integration/entity-change-source-registry.protocol.ts +59 -0
- package/runtime/subsystems/integration/index.ts +9 -0
- package/runtime/subsystems/integration/integration.tokens.ts +14 -0
- package/templates/entity/new/clean-lite-ps/entity.ejs.t +12 -3
- package/templates/entity/new/clean-lite-ps/prompt-extension.js +212 -29
- package/templates/entity/new/backend/modules/core/integration-source.providers.ejs.t +0 -18
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { IChangeSource } from './integration-change-source.protocol.js';
|
|
2
|
+
import { IEntityChangeSourceRegistry } from './entity-change-source-registry.protocol.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Integration subsystem — in-memory entity-change-source registry
|
|
6
|
+
*
|
|
7
|
+
* Default `IEntityChangeSourceRegistry` backed by a `Map<entityName, source>`.
|
|
8
|
+
* Track D's codegen-emitted aggregator folds per-provider adapter
|
|
9
|
+
* contributions into one of these and binds it under
|
|
10
|
+
* `ENTITY_CHANGE_SOURCE_REGISTRY` (RFC-0001 §3); tests and simple consumers
|
|
11
|
+
* construct it directly.
|
|
12
|
+
*
|
|
13
|
+
* See {@link ./entity-change-source-registry.protocol} for the contract and
|
|
14
|
+
* #336 for scope.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
declare class MemoryEntityChangeSourceRegistry implements IEntityChangeSourceRegistry {
|
|
18
|
+
private readonly sources;
|
|
19
|
+
constructor(sources: Map<string, IChangeSource<unknown>>);
|
|
20
|
+
get<T = unknown>(name: string): IChangeSource<T>;
|
|
21
|
+
has(name: string): boolean;
|
|
22
|
+
entities(): readonly string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { MemoryEntityChangeSourceRegistry };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// runtime/subsystems/integration/entity-change-source-registry.protocol.ts
|
|
2
|
+
var UnknownEntityError = class extends Error {
|
|
3
|
+
constructor(entity, available) {
|
|
4
|
+
super(
|
|
5
|
+
`No change source registered for entity '${entity}'. Available: ${available.join(", ")}`
|
|
6
|
+
);
|
|
7
|
+
this.name = "UnknownEntityError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// runtime/subsystems/integration/entity-change-source-registry.memory.ts
|
|
12
|
+
var MemoryEntityChangeSourceRegistry = class {
|
|
13
|
+
constructor(sources) {
|
|
14
|
+
this.sources = sources;
|
|
15
|
+
}
|
|
16
|
+
sources;
|
|
17
|
+
get(name) {
|
|
18
|
+
const source = this.sources.get(name);
|
|
19
|
+
if (!source) {
|
|
20
|
+
throw new UnknownEntityError(name, [...this.sources.keys()]);
|
|
21
|
+
}
|
|
22
|
+
return source;
|
|
23
|
+
}
|
|
24
|
+
has(name) {
|
|
25
|
+
return this.sources.has(name);
|
|
26
|
+
}
|
|
27
|
+
entities() {
|
|
28
|
+
return [...this.sources.keys()];
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
export {
|
|
32
|
+
MemoryEntityChangeSourceRegistry
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=entity-change-source-registry.memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../runtime/subsystems/integration/entity-change-source-registry.protocol.ts","../../../../runtime/subsystems/integration/entity-change-source-registry.memory.ts"],"sourcesContent":["/**\n * Integration subsystem — entity-keyed change-source registry (port)\n *\n * `IEntityChangeSourceRegistry` resolves an `IChangeSource<T>` by entity name.\n * It generalizes today's per-entity DI tokens (`ACCOUNT_POLL_FETCH_REGISTRY`,\n * `CONTACT_POLL_FETCH_REGISTRY`, …) into one entity-keyed registry, so the L3\n * composing port (`<Surface>Port`, Track C C6) can be entity-agnostic at the\n * type level instead of enumerating entities (epic #328 locked decision #5).\n *\n * This lives in L1 (the integration subsystem) rather than in a per-surface\n * package because the same shape applies across surfaces — CRM (`account`,\n * `contact`, `deal`), Mail (`email`, `thread`, `label`), Transcript\n * (`transcript`, `speaker`, `utterance`), Meeting (`meeting`, `attendee`).\n * Cross-surface plumbing belongs at L1 (epic #328 locked decision #6).\n *\n * Scope (Track C · C7): this is purely the L1 type + memory impl. Codegen does\n * NOT yet emit this registry, and the existing per-entity tokens keep emitting\n * unchanged — the retarget (and the per-entity-token deprecation) is Track D\n * D3/D4 (RFC-0001 §3/§8).\n *\n * See #336 (this issue), #328 (parent epic), RFC-0001 §3 (the registry\n * contract Track D emits the wiring for).\n */\n\nimport type { IChangeSource } from './integration-change-source.protocol';\n\n/**\n * Entity-keyed resolver for change sources. The orchestrator (and the L3\n * surface port) consume this, agnostic to whether a source came from a\n * hand-written adapter or a configured `PollChangeSource<T>`.\n */\nexport interface IEntityChangeSourceRegistry {\n /**\n * Resolve a change source for a given entity name.\n * Throws {@link UnknownEntityError} if the entity isn't registered.\n */\n get<T = unknown>(entityName: string): IChangeSource<T>;\n\n /** True if the entity is registered. */\n has(entityName: string): boolean;\n\n /** List all entity names this registry serves. */\n entities(): readonly string[];\n}\n\n/**\n * Thrown by {@link IEntityChangeSourceRegistry.get} when no source is\n * registered for the requested entity. The message enumerates the available\n * entities so a misconfiguration (typo'd entity name, missing adapter\n * contribution) is diagnosable from the error alone.\n */\nexport class UnknownEntityError extends Error {\n constructor(entity: string, available: readonly string[]) {\n super(\n `No change source registered for entity '${entity}'. Available: ${available.join(', ')}`,\n );\n this.name = 'UnknownEntityError';\n }\n}\n","/**\n * Integration subsystem — in-memory entity-change-source registry\n *\n * Default `IEntityChangeSourceRegistry` backed by a `Map<entityName, source>`.\n * Track D's codegen-emitted aggregator folds per-provider adapter\n * contributions into one of these and binds it under\n * `ENTITY_CHANGE_SOURCE_REGISTRY` (RFC-0001 §3); tests and simple consumers\n * construct it directly.\n *\n * See {@link ./entity-change-source-registry.protocol} for the contract and\n * #336 for scope.\n */\n\nimport type { IChangeSource } from './integration-change-source.protocol';\nimport {\n type IEntityChangeSourceRegistry,\n UnknownEntityError,\n} from './entity-change-source-registry.protocol';\n\nexport class MemoryEntityChangeSourceRegistry\n implements IEntityChangeSourceRegistry\n{\n constructor(private readonly sources: Map<string, IChangeSource<unknown>>) {}\n\n get<T = unknown>(name: string): IChangeSource<T> {\n const source = this.sources.get(name);\n if (!source) {\n throw new UnknownEntityError(name, [...this.sources.keys()]);\n }\n return source as IChangeSource<T>;\n }\n\n has(name: string): boolean {\n return this.sources.has(name);\n }\n\n entities(): readonly string[] {\n return [...this.sources.keys()];\n }\n}\n"],"mappings":";AAmDO,IAAM,qBAAN,cAAiC,MAAM;AAAA,EAC5C,YAAY,QAAgB,WAA8B;AACxD;AAAA,MACE,2CAA2C,MAAM,iBAAiB,UAAU,KAAK,IAAI,CAAC;AAAA,IACxF;AACA,SAAK,OAAO;AAAA,EACd;AACF;;;ACvCO,IAAM,mCAAN,MAEP;AAAA,EACE,YAA6B,SAA8C;AAA9C;AAAA,EAA+C;AAAA,EAA/C;AAAA,EAE7B,IAAiB,MAAgC;AAC/C,UAAM,SAAS,KAAK,QAAQ,IAAI,IAAI;AACpC,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI,mBAAmB,MAAM,CAAC,GAAG,KAAK,QAAQ,KAAK,CAAC,CAAC;AAAA,IAC7D;AACA,WAAO;AAAA,EACT;AAAA,EAEA,IAAI,MAAuB;AACzB,WAAO,KAAK,QAAQ,IAAI,IAAI;AAAA,EAC9B;AAAA,EAEA,WAA8B;AAC5B,WAAO,CAAC,GAAG,KAAK,QAAQ,KAAK,CAAC;AAAA,EAChC;AACF;","names":[]}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { IChangeSource } from './integration-change-source.protocol.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Integration subsystem — entity-keyed change-source registry (port)
|
|
5
|
+
*
|
|
6
|
+
* `IEntityChangeSourceRegistry` resolves an `IChangeSource<T>` by entity name.
|
|
7
|
+
* It generalizes today's per-entity DI tokens (`ACCOUNT_POLL_FETCH_REGISTRY`,
|
|
8
|
+
* `CONTACT_POLL_FETCH_REGISTRY`, …) into one entity-keyed registry, so the L3
|
|
9
|
+
* composing port (`<Surface>Port`, Track C C6) can be entity-agnostic at the
|
|
10
|
+
* type level instead of enumerating entities (epic #328 locked decision #5).
|
|
11
|
+
*
|
|
12
|
+
* This lives in L1 (the integration subsystem) rather than in a per-surface
|
|
13
|
+
* package because the same shape applies across surfaces — CRM (`account`,
|
|
14
|
+
* `contact`, `deal`), Mail (`email`, `thread`, `label`), Transcript
|
|
15
|
+
* (`transcript`, `speaker`, `utterance`), Meeting (`meeting`, `attendee`).
|
|
16
|
+
* Cross-surface plumbing belongs at L1 (epic #328 locked decision #6).
|
|
17
|
+
*
|
|
18
|
+
* Scope (Track C · C7): this is purely the L1 type + memory impl. Codegen does
|
|
19
|
+
* NOT yet emit this registry, and the existing per-entity tokens keep emitting
|
|
20
|
+
* unchanged — the retarget (and the per-entity-token deprecation) is Track D
|
|
21
|
+
* D3/D4 (RFC-0001 §3/§8).
|
|
22
|
+
*
|
|
23
|
+
* See #336 (this issue), #328 (parent epic), RFC-0001 §3 (the registry
|
|
24
|
+
* contract Track D emits the wiring for).
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Entity-keyed resolver for change sources. The orchestrator (and the L3
|
|
29
|
+
* surface port) consume this, agnostic to whether a source came from a
|
|
30
|
+
* hand-written adapter or a configured `PollChangeSource<T>`.
|
|
31
|
+
*/
|
|
32
|
+
interface IEntityChangeSourceRegistry {
|
|
33
|
+
/**
|
|
34
|
+
* Resolve a change source for a given entity name.
|
|
35
|
+
* Throws {@link UnknownEntityError} if the entity isn't registered.
|
|
36
|
+
*/
|
|
37
|
+
get<T = unknown>(entityName: string): IChangeSource<T>;
|
|
38
|
+
/** True if the entity is registered. */
|
|
39
|
+
has(entityName: string): boolean;
|
|
40
|
+
/** List all entity names this registry serves. */
|
|
41
|
+
entities(): readonly string[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Thrown by {@link IEntityChangeSourceRegistry.get} when no source is
|
|
45
|
+
* registered for the requested entity. The message enumerates the available
|
|
46
|
+
* entities so a misconfiguration (typo'd entity name, missing adapter
|
|
47
|
+
* contribution) is diagnosable from the error alone.
|
|
48
|
+
*/
|
|
49
|
+
declare class UnknownEntityError extends Error {
|
|
50
|
+
constructor(entity: string, available: readonly string[]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { type IEntityChangeSourceRegistry, UnknownEntityError };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// runtime/subsystems/integration/entity-change-source-registry.protocol.ts
|
|
2
|
+
var UnknownEntityError = class extends Error {
|
|
3
|
+
constructor(entity, available) {
|
|
4
|
+
super(
|
|
5
|
+
`No change source registered for entity '${entity}'. Available: ${available.join(", ")}`
|
|
6
|
+
);
|
|
7
|
+
this.name = "UnknownEntityError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
export {
|
|
11
|
+
UnknownEntityError
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=entity-change-source-registry.protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../runtime/subsystems/integration/entity-change-source-registry.protocol.ts"],"sourcesContent":["/**\n * Integration subsystem — entity-keyed change-source registry (port)\n *\n * `IEntityChangeSourceRegistry` resolves an `IChangeSource<T>` by entity name.\n * It generalizes today's per-entity DI tokens (`ACCOUNT_POLL_FETCH_REGISTRY`,\n * `CONTACT_POLL_FETCH_REGISTRY`, …) into one entity-keyed registry, so the L3\n * composing port (`<Surface>Port`, Track C C6) can be entity-agnostic at the\n * type level instead of enumerating entities (epic #328 locked decision #5).\n *\n * This lives in L1 (the integration subsystem) rather than in a per-surface\n * package because the same shape applies across surfaces — CRM (`account`,\n * `contact`, `deal`), Mail (`email`, `thread`, `label`), Transcript\n * (`transcript`, `speaker`, `utterance`), Meeting (`meeting`, `attendee`).\n * Cross-surface plumbing belongs at L1 (epic #328 locked decision #6).\n *\n * Scope (Track C · C7): this is purely the L1 type + memory impl. Codegen does\n * NOT yet emit this registry, and the existing per-entity tokens keep emitting\n * unchanged — the retarget (and the per-entity-token deprecation) is Track D\n * D3/D4 (RFC-0001 §3/§8).\n *\n * See #336 (this issue), #328 (parent epic), RFC-0001 §3 (the registry\n * contract Track D emits the wiring for).\n */\n\nimport type { IChangeSource } from './integration-change-source.protocol';\n\n/**\n * Entity-keyed resolver for change sources. The orchestrator (and the L3\n * surface port) consume this, agnostic to whether a source came from a\n * hand-written adapter or a configured `PollChangeSource<T>`.\n */\nexport interface IEntityChangeSourceRegistry {\n /**\n * Resolve a change source for a given entity name.\n * Throws {@link UnknownEntityError} if the entity isn't registered.\n */\n get<T = unknown>(entityName: string): IChangeSource<T>;\n\n /** True if the entity is registered. */\n has(entityName: string): boolean;\n\n /** List all entity names this registry serves. */\n entities(): readonly string[];\n}\n\n/**\n * Thrown by {@link IEntityChangeSourceRegistry.get} when no source is\n * registered for the requested entity. The message enumerates the available\n * entities so a misconfiguration (typo'd entity name, missing adapter\n * contribution) is diagnosable from the error alone.\n */\nexport class UnknownEntityError extends Error {\n constructor(entity: string, available: readonly string[]) {\n super(\n `No change source registered for entity '${entity}'. Available: ${available.join(', ')}`,\n );\n this.name = 'UnknownEntityError';\n }\n}\n"],"mappings":";AAmDO,IAAM,qBAAN,cAAiC,MAAM;AAAA,EAC5C,YAAY,QAAgB,WAA8B;AACxD;AAAA,MACE,2CAA2C,MAAM,iBAAiB,UAAU,KAAK,IAAI,CAAC;AAAA,IACxF;AACA,SAAK,OAAO;AAAA,EACd;AACF;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../runtime/subsystems/integration/execute-integration.use-case.ts","../../../../runtime/subsystems/integration/integration-errors.ts","../../../../runtime/subsystems/integration/integration.tokens.ts"],"sourcesContent":["/**\n * ExecuteIntegrationUseCase — the generic integration orchestrator (SYNC-5).\n *\n * One class. Reused across every `(provider, detection-mode, canonical-entity)`\n * tuple. Parameterized over `T` so canonical records stay typed end-to-end.\n *\n * Flow per run:\n *\n * 1. `recorder.startRun(...)` — opens a `integration_runs` row in 'running'.\n * 2. for each change yielded by `source.listChanges(subscription, cursorBefore)`:\n * a. differ.diff(existing, incoming) → 'noop' short-circuits to\n * a noop audit row (no sink write).\n * b. sink.upsertByExternalId / softDeleteByExternalId → records\n * the local id on the audit row.\n * c. per-item try/catch — a failed item increments the failed\n * counter and records `status: 'failed'` with `error`, but\n * does NOT abort the run.\n * d. advance `latestCursor = change.cursor` as the iterator moves.\n * 3. `cursors.put(subscription.id, latestCursor)` when the loop completes\n * AND at least one cursor advance happened. On exceptions from the\n * source iterator (auth expiry, network error), we persist the\n * last-good cursor so the next run resumes from the last known\n * successful position.\n * 4. `finally { recorder.completeRun(...) }` — always terminates the run.\n *\n * Loopback suppression — when a consumer's writes echo back on the next\n * inbound poll/CDC/webhook — is composed into the source's middleware\n * chain via `createLoopbackMiddleware(store)` (#226-5 / ADR-033). The\n * orchestrator no longer special-cases echoes: middleware drops them\n * before they reach this loop. Consumers that don't have outbound\n * writeback paths simply omit the middleware.\n *\n * ## Generics\n *\n * - `T` = canonical record shape from the adapter side. Same `T` flows\n * through `IChangeSource<T>`, `IFieldDiffer<T>`, `IIntegrationSink<T>`.\n *\n * ## No CRM bleed\n *\n * Per the SYNC-5 issue's extraction notes (HS-9 finding), this orchestrator\n * is strictly provider-agnostic:\n * - `entityType` is `string` throughout; no `'opportunity' | 'account' | ...`\n * narrowing leaks into the use case\n * - the upstream consumer's `IntegrationRunRecorderService` class injection replaced with the\n * `IIntegrationRunRecorder` protocol (backend lands in SYNC-4)\n */\nimport { Inject, Injectable, Logger, Optional } from '@nestjs/common';\nimport type { IChangeSource, Change } from './integration-change-source.protocol';\nimport type { ICursorStore } from './integration-cursor-store.protocol';\nimport type { IFieldDiffer, FieldDiff } from './integration-field-diff.protocol';\nimport type { IIntegrationSink } from './integration-sink.protocol';\nimport type { IIntegrationRunRecorder } from './integration-run-recorder.protocol';\nimport { assertTenantId } from './integration-errors';\nimport {\n INTEGRATION_CHANGE_SOURCE,\n INTEGRATION_CURSOR_STORE,\n INTEGRATION_FIELD_DIFFER,\n INTEGRATION_MULTI_TENANT,\n INTEGRATION_RUN_RECORDER,\n INTEGRATION_SINK,\n} from './integration.tokens';\n\n// ============================================================================\n// Inputs + result\n// ============================================================================\n\nexport interface ExecuteIntegrationInput<T> {\n /** The subscription whose cursor/identity frames this run. */\n readonly subscription: {\n readonly id: string;\n readonly domain: string; // entityType — used on audit rows\n readonly externalRef?: string | null;\n };\n /** Per-run user context; threaded through sink writes. */\n readonly userId: string;\n /** Provider label persisted on saved rows, e.g. `'salesforce-crm'`. */\n readonly provider: string;\n /** Run direction — almost always `'inbound'`. Reserved for writeback. */\n readonly direction: 'inbound' | 'outbound';\n /** Detection mode — maps 1:1 to `integration_runs.action`. */\n readonly action: 'poll' | 'cdc' | 'webhook' | 'manual' | 'writeback';\n /** Multi-tenant deployments pass the tenant id through. */\n readonly tenantId?: string | null;\n /**\n * Optional override — inject a specific change source for this run when\n * the DI-bound source is not the one to use (e.g. manual backfill with\n * a custom cursor). Defaults to the DI-resolved `INTEGRATION_CHANGE_SOURCE`.\n */\n readonly sourceOverride?: IChangeSource<T>;\n}\n\nexport interface ExecuteIntegrationResult {\n readonly runId: string;\n readonly status: 'success' | 'no_changes' | 'failed';\n readonly recordsFound: number;\n readonly recordsProcessed: number;\n readonly recordsFailed: number;\n readonly cursorBefore: unknown | null;\n readonly cursorAfter: unknown | null;\n readonly durationMs: number;\n readonly error?: string | null;\n}\n\n// ============================================================================\n// ExecuteIntegrationUseCase\n// ============================================================================\n\n@Injectable()\nexport class ExecuteIntegrationUseCase<T extends Record<string, unknown>> {\n private readonly logger = new Logger(ExecuteIntegrationUseCase.name);\n\n constructor(\n @Inject(INTEGRATION_CHANGE_SOURCE) private readonly source: IChangeSource<T>,\n @Inject(INTEGRATION_CURSOR_STORE) private readonly cursors: ICursorStore,\n @Inject(INTEGRATION_FIELD_DIFFER) private readonly differ: IFieldDiffer<T>,\n @Inject(INTEGRATION_SINK) private readonly sink: IIntegrationSink<T>,\n @Inject(INTEGRATION_RUN_RECORDER) private readonly recorder: IIntegrationRunRecorder,\n @Optional()\n @Inject(INTEGRATION_MULTI_TENANT)\n private readonly multiTenant: boolean = false,\n ) {}\n\n async execute(input: ExecuteIntegrationInput<T>): Promise<ExecuteIntegrationResult> {\n // Defense-in-depth tenancy guard — fire BEFORE startRun so a rejected\n // input never leaves a dangling `status=running` row. Backends also\n // enforce (SYNC-4), but failing fast at the orchestrator boundary is\n // cheaper for observability, metrics, and manual cleanup.\n assertTenantId(input.tenantId, {\n multiTenant: this.multiTenant,\n operation: 'execute',\n });\n\n const source = input.sourceOverride ?? this.source;\n const startedAt = Date.now();\n const cursorBefore = await this.cursors.get(input.subscription.id, input.tenantId);\n\n const { id: runId } = await this.recorder.startRun({\n subscriptionId: input.subscription.id,\n direction: input.direction,\n action: input.action,\n cursorBefore,\n tenantId: input.tenantId,\n });\n\n let recordsFound = 0;\n let recordsProcessed = 0;\n let recordsFailed = 0;\n let latestCursor: unknown | null = cursorBefore;\n let cursorAdvanced = false;\n let runError: string | null = null;\n let status: 'success' | 'no_changes' | 'failed' = 'no_changes';\n\n try {\n for await (const change of source.listChanges(input.subscription, cursorBefore)) {\n recordsFound++;\n latestCursor = change.cursor;\n cursorAdvanced = true;\n\n try {\n await this.processChange(runId, input, change);\n recordsProcessed++;\n } catch (err) {\n recordsFailed++;\n const message = err instanceof Error ? err.message : String(err);\n this.logger.warn(\n `integration item failed: subscription=${input.subscription.id} externalId=${change.externalId}: ${message}`,\n );\n await this.recorder.recordItem({\n integrationRunId: runId,\n entityType: input.subscription.domain,\n externalId: change.externalId,\n operation: change.operation === 'deleted' ? 'deleted' : 'updated',\n status: 'failed',\n changedFields: {},\n error: message,\n tenantId: input.tenantId,\n });\n }\n }\n\n if (recordsFailed > 0 && recordsProcessed === 0 && recordsFound > 0) {\n // Every record we saw failed — call the run a failure, not a\n // success. Partial success (some processed, some failed) still\n // counts as 'success' so the cursor advances.\n status = 'failed';\n runError = `all ${recordsFailed} records failed`;\n } else if (recordsFound === 0) {\n status = 'no_changes';\n } else {\n status = 'success';\n }\n } catch (err) {\n // Source iterator itself threw — cursor DOES NOT advance past the\n // last-successful cursor. `latestCursor` still holds the last\n // `change.cursor` we observed, which is the furthest we know to\n // have delivered. Persist it (below) so next run resumes there.\n status = 'failed';\n runError = err instanceof Error ? err.message : String(err);\n this.logger.error(\n `integration source failed: subscription=${input.subscription.id}: ${runError}`,\n );\n }\n\n // Persist cursor advance only when something actually moved. Never\n // overwrite a valid cursor with `null` on a no-change run.\n if (cursorAdvanced && latestCursor !== null && latestCursor !== undefined) {\n try {\n await this.cursors.put(input.subscription.id, latestCursor, input.tenantId);\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err);\n this.logger.error(\n `cursor put failed: subscription=${input.subscription.id}: ${message}`,\n );\n if (status !== 'failed') {\n status = 'failed';\n runError = `cursor put failed: ${message}`;\n }\n }\n }\n\n const durationMs = Date.now() - startedAt;\n\n await this.recorder.completeRun(runId, {\n status,\n recordsFound,\n recordsProcessed,\n cursorAfter: cursorAdvanced ? latestCursor : cursorBefore,\n durationMs,\n error: runError,\n });\n\n return {\n runId,\n status,\n recordsFound,\n recordsProcessed,\n recordsFailed,\n cursorBefore,\n cursorAfter: cursorAdvanced ? latestCursor : cursorBefore,\n durationMs,\n error: runError,\n };\n }\n\n private async processChange(\n runId: string,\n input: ExecuteIntegrationInput<T>,\n change: Change<T>,\n ): Promise<void> {\n // Deletion branch — no diff, no upsert; soft-delete via sink.\n if (change.operation === 'deleted') {\n const result = await this.sink.softDeleteByExternalId(\n input.userId,\n change.externalId,\n );\n await this.recorder.recordItem({\n integrationRunId: runId,\n entityType: input.subscription.domain,\n externalId: change.externalId,\n localId: result?.id ?? null,\n operation: result ? 'deleted' : 'noop',\n status: 'success',\n changedFields: {},\n tenantId: input.tenantId,\n });\n return;\n }\n\n // Create/update path — diff against local state, short-circuit on noop.\n const existing = await this.sink.findByExternalId(\n input.userId,\n change.externalId,\n );\n const diff = this.differ.diff(\n existing,\n change.record,\n change.providerChangedFields,\n );\n\n if (diff === 'noop') {\n // Sinks that declare `reprojectsOnNoop` reproject side data the differ\n // can't see (e.g. EAV field_values) — so fall through to the idempotent\n // upsert instead of short-circuiting. The canonical state is unchanged,\n // so the audit `operation` stays `'noop'`, but we capture the local id\n // returned by the upsert. Sinks without the flag keep today's behavior.\n if (!this.sink.reprojectsOnNoop) {\n await this.recorder.recordItem({\n integrationRunId: runId,\n entityType: input.subscription.domain,\n externalId: change.externalId,\n localId: null,\n operation: 'noop',\n status: 'success',\n changedFields: {},\n tenantId: input.tenantId,\n });\n return;\n }\n\n const { id: noopLocalId } = await this.sink.upsertByExternalId(\n input.userId,\n change.record,\n input.provider,\n );\n await this.recorder.recordItem({\n integrationRunId: runId,\n entityType: input.subscription.domain,\n externalId: change.externalId,\n localId: noopLocalId,\n operation: 'noop',\n status: 'success',\n changedFields: {},\n tenantId: input.tenantId,\n });\n return;\n }\n\n const { id: localId } = await this.sink.upsertByExternalId(\n input.userId,\n change.record,\n input.provider,\n );\n\n await this.recorder.recordItem({\n integrationRunId: runId,\n entityType: input.subscription.domain,\n externalId: change.externalId,\n localId,\n operation: existing === null ? 'created' : 'updated',\n status: 'success',\n changedFields: diff as FieldDiff,\n tenantId: input.tenantId,\n });\n }\n}\n","/**\n * Typed errors + shared boundary helpers for the integration subsystem.\n *\n * Classes (not bare Error) so consumers can `instanceof` them in catch\n * blocks and exception filters can map them to HTTP codes.\n *\n * Mirrors the shape of `events-errors.ts` and `jobs-errors.ts`.\n */\n\n/**\n * Thrown by the Drizzle cursor-store / run-recorder backends AND by the\n * orchestrator entry point when `INTEGRATION_MULTI_TENANT` is enabled but the\n * caller did not supply a non-null `tenantId`. Strict enforcement at the\n * boundary — explicit `null` still throws.\n *\n * Disable multi-tenancy on the module (`multiTenant: false`, the default)\n * to opt out of the requirement entirely.\n *\n * `operation` identifies the call site (e.g. `'cursor.put'`,\n * `'startRun'`, `'execute'`) so the stack-trace message points at the\n * specific boundary that rejected the input.\n */\nexport class MissingTenantIdError extends Error {\n override readonly name = 'MissingTenantIdError';\n constructor(operation: string) {\n super(\n `Missing tenantId for integration operation '${operation}'. IntegrationModule is ` +\n `configured with multiTenant: true — every call must include a ` +\n `non-null tenantId. Either pass the tenantId or disable multi-` +\n `tenancy on the module.`,\n );\n }\n}\n\n/**\n * Shared boundary guard — used at the orchestrator entry AND inside the\n * Drizzle backends. Keeping the check in one function guarantees every\n * `MissingTenantIdError` carries the same message shape regardless of the\n * site that raised it, which makes it easier for consumers to pattern-\n * match on the error in logs/metrics.\n *\n * When `multiTenant` is false, the function is a no-op — `tenantId` may\n * be anything (including `undefined`). When true, `undefined` or `null`\n * throws.\n */\nexport function assertTenantId(\n tenantId: string | null | undefined,\n options: { multiTenant: boolean; operation: string },\n): asserts tenantId is string {\n if (!options.multiTenant) return;\n if (tenantId === undefined || tenantId === null) {\n throw new MissingTenantIdError(options.operation);\n }\n}\n","/**\n * Integration subsystem — DI tokens\n *\n * String constants (not Symbols) so they match by value across import\n * boundaries — same convention as the events subsystem (`EVENT_BUS`). The\n * jobs subsystem uses Symbols for its analogous tokens; events and integration\n * stay internally consistent with strings.\n *\n * Usage in use cases:\n * ```ts\n * constructor(\n * @Inject(INTEGRATION_CHANGE_SOURCE) private readonly source: IChangeSource<CanonicalOpportunity>,\n * @Inject(INTEGRATION_CURSOR_STORE) private readonly cursors: ICursorStore,\n * @Inject(INTEGRATION_FIELD_DIFFER) private readonly differ: IFieldDiffer<CanonicalOpportunity>,\n * @Inject(INTEGRATION_SINK) private readonly sink: IIntegrationSink<CanonicalOpportunity>,\n * @Inject(INTEGRATION_RUN_RECORDER) private readonly recorder: IIntegrationRunRecorder,\n * ) {}\n * ```\n *\n * Concrete bindings are registered by `IntegrationModule.forRoot(...)` (SYNC-6).\n */\n\nexport const INTEGRATION_CHANGE_SOURCE = 'INTEGRATION_CHANGE_SOURCE' as const;\nexport const INTEGRATION_CURSOR_STORE = 'INTEGRATION_CURSOR_STORE' as const;\nexport const INTEGRATION_FIELD_DIFFER = 'INTEGRATION_FIELD_DIFFER' as const;\nexport const INTEGRATION_SINK = 'INTEGRATION_SINK' as const;\n\n/**\n * Run-recorder token (SYNC-5). Backed by `IIntegrationRunRecorder`. Drizzle impl\n * lands in SYNC-4; tests provide inline fakes.\n */\nexport const INTEGRATION_RUN_RECORDER = 'INTEGRATION_RUN_RECORDER' as const;\n\n/**\n * Injection token for the resolved `IntegrationModuleOptions` object (SYNC-6).\n *\n * Backends that need to observe module configuration (e.g. `multiTenant`\n * flag, pool filters) inject via this token. Provided automatically by\n * `IntegrationModule.forRoot(...)` / `IntegrationModule.forRootAsync(...)`.\n */\nexport const INTEGRATION_MODULE_OPTIONS = 'INTEGRATION_MODULE_OPTIONS' as const;\n\n/**\n * Injection token for the resolved multi-tenancy flag (SYNC-6).\n *\n * Provided by `IntegrationModule.forRoot(...)` as `options.multiTenant ?? false`.\n * Consumed by `ExecuteIntegrationUseCase` to enforce the tenantId-is-required rule.\n */\nexport const INTEGRATION_MULTI_TENANT = 'INTEGRATION_MULTI_TENANT' as const;\n"],"mappings":";;;;;;;;;;;;;AA8CA,SAAS,QAAQ,YAAY,QAAQ,gBAAgB;;;ACxB9C,IAAM,uBAAN,cAAmC,MAAM;AAAA,EAC5B,OAAO;AAAA,EACzB,YAAY,WAAmB;AAC7B;AAAA,MACE,+CAA+C,SAAS;AAAA,IAI1D;AAAA,EACF;AACF;AAaO,SAAS,eACd,UACA,SAC4B;AAC5B,MAAI,CAAC,QAAQ,YAAa;AAC1B,MAAI,aAAa,UAAa,aAAa,MAAM;AAC/C,UAAM,IAAI,qBAAqB,QAAQ,SAAS;AAAA,EAClD;AACF;;;AC/BO,IAAM,4BAA4B;AAClC,IAAM,2BAA2B;AACjC,IAAM,2BAA2B;AACjC,IAAM,mBAAmB;AAMzB,IAAM,2BAA2B;AAiBjC,IAAM,2BAA2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AS;AAAA,QACT,WAAW;AAAA,QACX,QAAQ;AAAA,QACR,eAAe,CAAC;AAAA,QAChB,UAAU,MAAM;AAAA,MAClB,CAAC;AACD;AAAA,IACF;AAEA,UAAM,EAAE,IAAI,QAAQ,IAAI,MAAM,KAAK,KAAK;AAAA,MACtC,MAAM;AAAA,MACN,OAAO;AAAA,MACP,MAAM;AAAA,IACR;AAEA,UAAM,KAAK,SAAS,WAAW;AAAA,MAC7B,kBAAkB;AAAA,MAClB,YAAY,MAAM,aAAa;AAAA,MAC/B,YAAY,OAAO;AAAA,MACnB;AAAA,MACA,WAAW,aAAa,OAAO,YAAY;AAAA,MAC3C,QAAQ;AAAA,MACR,eAAe;AAAA,MACf,UAAU,MAAM;AAAA,IAClB,CAAC;AAAA,EACH;AACF;AAlOa,4BAAN;AAAA,EADN,WAAW;AAAA,EAKP,0BAAO,yBAAyB;AAAA,EAChC,0BAAO,wBAAwB;AAAA,EAC/B,0BAAO,wBAAwB;AAAA,EAC/B,0BAAO,gBAAgB;AAAA,EACvB,0BAAO,wBAAwB;AAAA,EAC/B,4BAAS;AAAA,EACT,0BAAO,wBAAwB;AAAA,GAVvB;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../../runtime/subsystems/integration/execute-integration.use-case.ts","../../../../runtime/subsystems/integration/integration-errors.ts","../../../../runtime/subsystems/integration/integration.tokens.ts"],"sourcesContent":["/**\n * ExecuteIntegrationUseCase — the generic integration orchestrator (SYNC-5).\n *\n * One class. Reused across every `(provider, detection-mode, canonical-entity)`\n * tuple. Parameterized over `T` so canonical records stay typed end-to-end.\n *\n * Flow per run:\n *\n * 1. `recorder.startRun(...)` — opens a `integration_runs` row in 'running'.\n * 2. for each change yielded by `source.listChanges(subscription, cursorBefore)`:\n * a. differ.diff(existing, incoming) → 'noop' short-circuits to\n * a noop audit row (no sink write).\n * b. sink.upsertByExternalId / softDeleteByExternalId → records\n * the local id on the audit row.\n * c. per-item try/catch — a failed item increments the failed\n * counter and records `status: 'failed'` with `error`, but\n * does NOT abort the run.\n * d. advance `latestCursor = change.cursor` as the iterator moves.\n * 3. `cursors.put(subscription.id, latestCursor)` when the loop completes\n * AND at least one cursor advance happened. On exceptions from the\n * source iterator (auth expiry, network error), we persist the\n * last-good cursor so the next run resumes from the last known\n * successful position.\n * 4. `finally { recorder.completeRun(...) }` — always terminates the run.\n *\n * Loopback suppression — when a consumer's writes echo back on the next\n * inbound poll/CDC/webhook — is composed into the source's middleware\n * chain via `createLoopbackMiddleware(store)` (#226-5 / ADR-033). The\n * orchestrator no longer special-cases echoes: middleware drops them\n * before they reach this loop. Consumers that don't have outbound\n * writeback paths simply omit the middleware.\n *\n * ## Generics\n *\n * - `T` = canonical record shape from the adapter side. Same `T` flows\n * through `IChangeSource<T>`, `IFieldDiffer<T>`, `IIntegrationSink<T>`.\n *\n * ## No CRM bleed\n *\n * Per the SYNC-5 issue's extraction notes (HS-9 finding), this orchestrator\n * is strictly provider-agnostic:\n * - `entityType` is `string` throughout; no `'opportunity' | 'account' | ...`\n * narrowing leaks into the use case\n * - the upstream consumer's `IntegrationRunRecorderService` class injection replaced with the\n * `IIntegrationRunRecorder` protocol (backend lands in SYNC-4)\n */\nimport { Inject, Injectable, Logger, Optional } from '@nestjs/common';\nimport type { IChangeSource, Change } from './integration-change-source.protocol';\nimport type { ICursorStore } from './integration-cursor-store.protocol';\nimport type { IFieldDiffer, FieldDiff } from './integration-field-diff.protocol';\nimport type { IIntegrationSink } from './integration-sink.protocol';\nimport type { IIntegrationRunRecorder } from './integration-run-recorder.protocol';\nimport { assertTenantId } from './integration-errors';\nimport {\n INTEGRATION_CHANGE_SOURCE,\n INTEGRATION_CURSOR_STORE,\n INTEGRATION_FIELD_DIFFER,\n INTEGRATION_MULTI_TENANT,\n INTEGRATION_RUN_RECORDER,\n INTEGRATION_SINK,\n} from './integration.tokens';\n\n// ============================================================================\n// Inputs + result\n// ============================================================================\n\nexport interface ExecuteIntegrationInput<T> {\n /** The subscription whose cursor/identity frames this run. */\n readonly subscription: {\n readonly id: string;\n readonly domain: string; // entityType — used on audit rows\n readonly externalRef?: string | null;\n };\n /** Per-run user context; threaded through sink writes. */\n readonly userId: string;\n /** Provider label persisted on saved rows, e.g. `'salesforce-crm'`. */\n readonly provider: string;\n /** Run direction — almost always `'inbound'`. Reserved for writeback. */\n readonly direction: 'inbound' | 'outbound';\n /** Detection mode — maps 1:1 to `integration_runs.action`. */\n readonly action: 'poll' | 'cdc' | 'webhook' | 'manual' | 'writeback';\n /** Multi-tenant deployments pass the tenant id through. */\n readonly tenantId?: string | null;\n /**\n * Optional override — inject a specific change source for this run when\n * the DI-bound source is not the one to use (e.g. manual backfill with\n * a custom cursor). Defaults to the DI-resolved `INTEGRATION_CHANGE_SOURCE`.\n */\n readonly sourceOverride?: IChangeSource<T>;\n}\n\nexport interface ExecuteIntegrationResult {\n readonly runId: string;\n readonly status: 'success' | 'no_changes' | 'failed';\n readonly recordsFound: number;\n readonly recordsProcessed: number;\n readonly recordsFailed: number;\n readonly cursorBefore: unknown | null;\n readonly cursorAfter: unknown | null;\n readonly durationMs: number;\n readonly error?: string | null;\n}\n\n// ============================================================================\n// ExecuteIntegrationUseCase\n// ============================================================================\n\n@Injectable()\nexport class ExecuteIntegrationUseCase<T extends Record<string, unknown>> {\n private readonly logger = new Logger(ExecuteIntegrationUseCase.name);\n\n constructor(\n @Inject(INTEGRATION_CHANGE_SOURCE) private readonly source: IChangeSource<T>,\n @Inject(INTEGRATION_CURSOR_STORE) private readonly cursors: ICursorStore,\n @Inject(INTEGRATION_FIELD_DIFFER) private readonly differ: IFieldDiffer<T>,\n @Inject(INTEGRATION_SINK) private readonly sink: IIntegrationSink<T>,\n @Inject(INTEGRATION_RUN_RECORDER) private readonly recorder: IIntegrationRunRecorder,\n @Optional()\n @Inject(INTEGRATION_MULTI_TENANT)\n private readonly multiTenant: boolean = false,\n ) {}\n\n async execute(input: ExecuteIntegrationInput<T>): Promise<ExecuteIntegrationResult> {\n // Defense-in-depth tenancy guard — fire BEFORE startRun so a rejected\n // input never leaves a dangling `status=running` row. Backends also\n // enforce (SYNC-4), but failing fast at the orchestrator boundary is\n // cheaper for observability, metrics, and manual cleanup.\n assertTenantId(input.tenantId, {\n multiTenant: this.multiTenant,\n operation: 'execute',\n });\n\n const source = input.sourceOverride ?? this.source;\n const startedAt = Date.now();\n const cursorBefore = await this.cursors.get(input.subscription.id, input.tenantId);\n\n const { id: runId } = await this.recorder.startRun({\n subscriptionId: input.subscription.id,\n direction: input.direction,\n action: input.action,\n cursorBefore,\n tenantId: input.tenantId,\n });\n\n let recordsFound = 0;\n let recordsProcessed = 0;\n let recordsFailed = 0;\n let latestCursor: unknown | null = cursorBefore;\n let cursorAdvanced = false;\n let runError: string | null = null;\n let status: 'success' | 'no_changes' | 'failed' = 'no_changes';\n\n try {\n for await (const change of source.listChanges(input.subscription, cursorBefore)) {\n recordsFound++;\n latestCursor = change.cursor;\n cursorAdvanced = true;\n\n try {\n await this.processChange(runId, input, change);\n recordsProcessed++;\n } catch (err) {\n recordsFailed++;\n const message = err instanceof Error ? err.message : String(err);\n this.logger.warn(\n `integration item failed: subscription=${input.subscription.id} externalId=${change.externalId}: ${message}`,\n );\n await this.recorder.recordItem({\n integrationRunId: runId,\n entityType: input.subscription.domain,\n externalId: change.externalId,\n operation: change.operation === 'deleted' ? 'deleted' : 'updated',\n status: 'failed',\n changedFields: {},\n error: message,\n tenantId: input.tenantId,\n });\n }\n }\n\n if (recordsFailed > 0 && recordsProcessed === 0 && recordsFound > 0) {\n // Every record we saw failed — call the run a failure, not a\n // success. Partial success (some processed, some failed) still\n // counts as 'success' so the cursor advances.\n status = 'failed';\n runError = `all ${recordsFailed} records failed`;\n } else if (recordsFound === 0) {\n status = 'no_changes';\n } else {\n status = 'success';\n }\n } catch (err) {\n // Source iterator itself threw — cursor DOES NOT advance past the\n // last-successful cursor. `latestCursor` still holds the last\n // `change.cursor` we observed, which is the furthest we know to\n // have delivered. Persist it (below) so next run resumes there.\n status = 'failed';\n runError = err instanceof Error ? err.message : String(err);\n this.logger.error(\n `integration source failed: subscription=${input.subscription.id}: ${runError}`,\n );\n }\n\n // Persist cursor advance only when something actually moved. Never\n // overwrite a valid cursor with `null` on a no-change run.\n if (cursorAdvanced && latestCursor !== null && latestCursor !== undefined) {\n try {\n await this.cursors.put(input.subscription.id, latestCursor, input.tenantId);\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err);\n this.logger.error(\n `cursor put failed: subscription=${input.subscription.id}: ${message}`,\n );\n if (status !== 'failed') {\n status = 'failed';\n runError = `cursor put failed: ${message}`;\n }\n }\n }\n\n const durationMs = Date.now() - startedAt;\n\n await this.recorder.completeRun(runId, {\n status,\n recordsFound,\n recordsProcessed,\n cursorAfter: cursorAdvanced ? latestCursor : cursorBefore,\n durationMs,\n error: runError,\n });\n\n return {\n runId,\n status,\n recordsFound,\n recordsProcessed,\n recordsFailed,\n cursorBefore,\n cursorAfter: cursorAdvanced ? latestCursor : cursorBefore,\n durationMs,\n error: runError,\n };\n }\n\n private async processChange(\n runId: string,\n input: ExecuteIntegrationInput<T>,\n change: Change<T>,\n ): Promise<void> {\n // Deletion branch — no diff, no upsert; soft-delete via sink.\n if (change.operation === 'deleted') {\n const result = await this.sink.softDeleteByExternalId(\n input.userId,\n change.externalId,\n );\n await this.recorder.recordItem({\n integrationRunId: runId,\n entityType: input.subscription.domain,\n externalId: change.externalId,\n localId: result?.id ?? null,\n operation: result ? 'deleted' : 'noop',\n status: 'success',\n changedFields: {},\n tenantId: input.tenantId,\n });\n return;\n }\n\n // Create/update path — diff against local state, short-circuit on noop.\n const existing = await this.sink.findByExternalId(\n input.userId,\n change.externalId,\n );\n const diff = this.differ.diff(\n existing,\n change.record,\n change.providerChangedFields,\n );\n\n if (diff === 'noop') {\n // Sinks that declare `reprojectsOnNoop` reproject side data the differ\n // can't see (e.g. EAV field_values) — so fall through to the idempotent\n // upsert instead of short-circuiting. The canonical state is unchanged,\n // so the audit `operation` stays `'noop'`, but we capture the local id\n // returned by the upsert. Sinks without the flag keep today's behavior.\n if (!this.sink.reprojectsOnNoop) {\n await this.recorder.recordItem({\n integrationRunId: runId,\n entityType: input.subscription.domain,\n externalId: change.externalId,\n localId: null,\n operation: 'noop',\n status: 'success',\n changedFields: {},\n tenantId: input.tenantId,\n });\n return;\n }\n\n const { id: noopLocalId } = await this.sink.upsertByExternalId(\n input.userId,\n change.record,\n input.provider,\n );\n await this.recorder.recordItem({\n integrationRunId: runId,\n entityType: input.subscription.domain,\n externalId: change.externalId,\n localId: noopLocalId,\n operation: 'noop',\n status: 'success',\n changedFields: {},\n tenantId: input.tenantId,\n });\n return;\n }\n\n const { id: localId } = await this.sink.upsertByExternalId(\n input.userId,\n change.record,\n input.provider,\n );\n\n await this.recorder.recordItem({\n integrationRunId: runId,\n entityType: input.subscription.domain,\n externalId: change.externalId,\n localId,\n operation: existing === null ? 'created' : 'updated',\n status: 'success',\n changedFields: diff as FieldDiff,\n tenantId: input.tenantId,\n });\n }\n}\n","/**\n * Typed errors + shared boundary helpers for the integration subsystem.\n *\n * Classes (not bare Error) so consumers can `instanceof` them in catch\n * blocks and exception filters can map them to HTTP codes.\n *\n * Mirrors the shape of `events-errors.ts` and `jobs-errors.ts`.\n */\n\n/**\n * Thrown by the Drizzle cursor-store / run-recorder backends AND by the\n * orchestrator entry point when `INTEGRATION_MULTI_TENANT` is enabled but the\n * caller did not supply a non-null `tenantId`. Strict enforcement at the\n * boundary — explicit `null` still throws.\n *\n * Disable multi-tenancy on the module (`multiTenant: false`, the default)\n * to opt out of the requirement entirely.\n *\n * `operation` identifies the call site (e.g. `'cursor.put'`,\n * `'startRun'`, `'execute'`) so the stack-trace message points at the\n * specific boundary that rejected the input.\n */\nexport class MissingTenantIdError extends Error {\n override readonly name = 'MissingTenantIdError';\n constructor(operation: string) {\n super(\n `Missing tenantId for integration operation '${operation}'. IntegrationModule is ` +\n `configured with multiTenant: true — every call must include a ` +\n `non-null tenantId. Either pass the tenantId or disable multi-` +\n `tenancy on the module.`,\n );\n }\n}\n\n/**\n * Shared boundary guard — used at the orchestrator entry AND inside the\n * Drizzle backends. Keeping the check in one function guarantees every\n * `MissingTenantIdError` carries the same message shape regardless of the\n * site that raised it, which makes it easier for consumers to pattern-\n * match on the error in logs/metrics.\n *\n * When `multiTenant` is false, the function is a no-op — `tenantId` may\n * be anything (including `undefined`). When true, `undefined` or `null`\n * throws.\n */\nexport function assertTenantId(\n tenantId: string | null | undefined,\n options: { multiTenant: boolean; operation: string },\n): asserts tenantId is string {\n if (!options.multiTenant) return;\n if (tenantId === undefined || tenantId === null) {\n throw new MissingTenantIdError(options.operation);\n }\n}\n","/**\n * Integration subsystem — DI tokens\n *\n * String constants (not Symbols) so they match by value across import\n * boundaries — same convention as the events subsystem (`EVENT_BUS`). The\n * jobs subsystem uses Symbols for its analogous tokens; events and integration\n * stay internally consistent with strings.\n *\n * Usage in use cases:\n * ```ts\n * constructor(\n * @Inject(INTEGRATION_CHANGE_SOURCE) private readonly source: IChangeSource<CanonicalOpportunity>,\n * @Inject(INTEGRATION_CURSOR_STORE) private readonly cursors: ICursorStore,\n * @Inject(INTEGRATION_FIELD_DIFFER) private readonly differ: IFieldDiffer<CanonicalOpportunity>,\n * @Inject(INTEGRATION_SINK) private readonly sink: IIntegrationSink<CanonicalOpportunity>,\n * @Inject(INTEGRATION_RUN_RECORDER) private readonly recorder: IIntegrationRunRecorder,\n * ) {}\n * ```\n *\n * Concrete bindings are registered by `IntegrationModule.forRoot(...)` (SYNC-6).\n */\n\nexport const INTEGRATION_CHANGE_SOURCE = 'INTEGRATION_CHANGE_SOURCE' as const;\nexport const INTEGRATION_CURSOR_STORE = 'INTEGRATION_CURSOR_STORE' as const;\nexport const INTEGRATION_FIELD_DIFFER = 'INTEGRATION_FIELD_DIFFER' as const;\nexport const INTEGRATION_SINK = 'INTEGRATION_SINK' as const;\n\n/**\n * Run-recorder token (SYNC-5). Backed by `IIntegrationRunRecorder`. Drizzle impl\n * lands in SYNC-4; tests provide inline fakes.\n */\nexport const INTEGRATION_RUN_RECORDER = 'INTEGRATION_RUN_RECORDER' as const;\n\n/**\n * Injection token for the resolved `IntegrationModuleOptions` object (SYNC-6).\n *\n * Backends that need to observe module configuration (e.g. `multiTenant`\n * flag, pool filters) inject via this token. Provided automatically by\n * `IntegrationModule.forRoot(...)` / `IntegrationModule.forRootAsync(...)`.\n */\nexport const INTEGRATION_MODULE_OPTIONS = 'INTEGRATION_MODULE_OPTIONS' as const;\n\n/**\n * Injection token for the resolved multi-tenancy flag (SYNC-6).\n *\n * Provided by `IntegrationModule.forRoot(...)` as `options.multiTenant ?? false`.\n * Consumed by `ExecuteIntegrationUseCase` to enforce the tenantId-is-required rule.\n */\nexport const INTEGRATION_MULTI_TENANT = 'INTEGRATION_MULTI_TENANT' as const;\n\n/**\n * Injection token for the entity-keyed `IEntityChangeSourceRegistry` (C7,\n * #336). Bound to the codegen-emitted aggregator that folds per-provider\n * adapter contributions into one registry (RFC-0001 §3, emitted by Track D\n * D3/D4).\n *\n * A string constant, not `Symbol.for(...)`, to match this subsystem's token\n * convention (see file header). The originating issue's code block proposed a\n * `Symbol.for('@pattern-stack/codegen.entity-change-source-registry')` key,\n * predating the sync→integration consolidation onto string tokens; kept as a\n * string here for internal consistency with the other INTEGRATION_* tokens.\n */\nexport const ENTITY_CHANGE_SOURCE_REGISTRY = 'ENTITY_CHANGE_SOURCE_REGISTRY' as const;\n"],"mappings":";;;;;;;;;;;;;AA8CA,SAAS,QAAQ,YAAY,QAAQ,gBAAgB;;;ACxB9C,IAAM,uBAAN,cAAmC,MAAM;AAAA,EAC5B,OAAO;AAAA,EACzB,YAAY,WAAmB;AAC7B;AAAA,MACE,+CAA+C,SAAS;AAAA,IAI1D;AAAA,EACF;AACF;AAaO,SAAS,eACd,UACA,SAC4B;AAC5B,MAAI,CAAC,QAAQ,YAAa;AAC1B,MAAI,aAAa,UAAa,aAAa,MAAM;AAC/C,UAAM,IAAI,qBAAqB,QAAQ,SAAS;AAAA,EAClD;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;AAAA,MACR;AACA,YAAM,KAAK,SAAS,WAAW;AAAA,QAC7B,kBAAkB;AAAA,QAClB,YAAY,MAAM,aAAa;AAAA,QAC/B,YAAY,OAAO;AAAA,QACnB,SAAS;AAAA,QACT,WAAW;AAAA,QACX,QAAQ;AAAA,QACR,eAAe,CAAC;AAAA,QAChB,UAAU,MAAM;AAAA,MAClB,CAAC;AACD;AAAA,IACF;AAEA,UAAM,EAAE,IAAI,QAAQ,IAAI,MAAM,KAAK,KAAK;AAAA,MACtC,MAAM;AAAA,MACN,OAAO;AAAA,MACP,MAAM;AAAA,IACR;AAEA,UAAM,KAAK,SAAS,WAAW;AAAA,MAC7B,kBAAkB;AAAA,MAClB,YAAY,MAAM,aAAa;AAAA,MAC/B,YAAY,OAAO;AAAA,MACnB;AAAA,MACA,WAAW,aAAa,OAAO,YAAY;AAAA,MAC3C,QAAQ;AAAA,MACR,eAAe;AAAA,MACf,UAAU,MAAM;AAAA,IAClB,CAAC;AAAA,EACH;AACF;AAlOa,4BAAN;AAAA,EADN,WAAW;AAAA,EAKP,0BAAO,yBAAyB;AAAA,EAChC,0BAAO,wBAAwB;AAAA,EAC/B,0BAAO,wBAAwB;AAAA,EAC/B,0BAAO,gBAAgB;AAAA,EACvB,0BAAO,wBAAwB;AAAA,EAC/B,4BAAS;AAAA,EACT,0BAAO,wBAAwB;AAAA,GAVvB;","names":[]}
|
|
@@ -4,13 +4,15 @@ export { DiffResult, FieldDiff, FieldDiffSchema, FieldDiffValue, FieldDiffValueS
|
|
|
4
4
|
export { IIntegrationSink } from './integration-sink.protocol.js';
|
|
5
5
|
export { CompleteRunInput, IIntegrationRunRecorder, IntegrationRunSummary, RecordItemInput, StartRunInput } from './integration-run-recorder.protocol.js';
|
|
6
6
|
export { ILoopbackFingerprintStore } from './integration-loopback.protocol.js';
|
|
7
|
+
export { IEntityChangeSourceRegistry, UnknownEntityError } from './entity-change-source-registry.protocol.js';
|
|
8
|
+
export { MemoryEntityChangeSourceRegistry } from './entity-change-source-registry.memory.js';
|
|
7
9
|
export { CursorStrategy, CursorStrategySchema, DetectionConfig, DetectionConfigSchema, FieldMapping, FieldMappingSchema, PollDetection, PollDetectionSchema, ResolvedFilter, ResolvedFilterSchema, WebhookDetection, WebhookDetectionSchema } from './detection-config.schema.js';
|
|
8
10
|
export { ChangeIterator, ChangeMiddleware, ComposeChangeMiddleware } from './integration-middleware.protocol.js';
|
|
9
11
|
export { createLoopbackMiddleware } from './loopback.middleware.js';
|
|
10
12
|
export { PollChangeSource, PollChangeSourceOptions, PollCursor, PollFetchCallback, PollFetchContext } from './poll-change-source.js';
|
|
11
13
|
export { WebhookChangeSource, WebhookChangeSourceOptions, WebhookCursor, WebhookFetchCallback, WebhookFetchContext } from './webhook-change-source.js';
|
|
12
14
|
export { buildChangeSource } from './build-change-source.js';
|
|
13
|
-
export { INTEGRATION_CHANGE_SOURCE, INTEGRATION_CURSOR_STORE, INTEGRATION_FIELD_DIFFER, INTEGRATION_MODULE_OPTIONS, INTEGRATION_MULTI_TENANT, INTEGRATION_RUN_RECORDER, INTEGRATION_SINK } from './integration.tokens.js';
|
|
15
|
+
export { ENTITY_CHANGE_SOURCE_REGISTRY, INTEGRATION_CHANGE_SOURCE, INTEGRATION_CURSOR_STORE, INTEGRATION_FIELD_DIFFER, INTEGRATION_MODULE_OPTIONS, INTEGRATION_MULTI_TENANT, INTEGRATION_RUN_RECORDER, INTEGRATION_SINK } from './integration.tokens.js';
|
|
14
16
|
export { MissingTenantIdError, assertTenantId } from './integration-errors.js';
|
|
15
17
|
export { IntegrationRunItemRow, IntegrationRunRow, IntegrationSubscriptionRow, integrationRunActionEnum, integrationRunDirectionEnum, integrationRunItemOperationEnum, integrationRunItemStatusEnum, integrationRunItems, integrationRunStatusEnum, integrationRuns, integrationSubscriptions } from './integration-audit.schema.js';
|
|
16
18
|
export { MemoryCursorStore } from './integration-cursor-store.memory-backend.js';
|
|
@@ -18,6 +18,37 @@ var FieldDiffValueSchema = z.object({
|
|
|
18
18
|
});
|
|
19
19
|
var FieldDiffSchema = z.record(z.string(), FieldDiffValueSchema);
|
|
20
20
|
|
|
21
|
+
// runtime/subsystems/integration/entity-change-source-registry.protocol.ts
|
|
22
|
+
var UnknownEntityError = class extends Error {
|
|
23
|
+
constructor(entity, available) {
|
|
24
|
+
super(
|
|
25
|
+
`No change source registered for entity '${entity}'. Available: ${available.join(", ")}`
|
|
26
|
+
);
|
|
27
|
+
this.name = "UnknownEntityError";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// runtime/subsystems/integration/entity-change-source-registry.memory.ts
|
|
32
|
+
var MemoryEntityChangeSourceRegistry = class {
|
|
33
|
+
constructor(sources) {
|
|
34
|
+
this.sources = sources;
|
|
35
|
+
}
|
|
36
|
+
sources;
|
|
37
|
+
get(name) {
|
|
38
|
+
const source = this.sources.get(name);
|
|
39
|
+
if (!source) {
|
|
40
|
+
throw new UnknownEntityError(name, [...this.sources.keys()]);
|
|
41
|
+
}
|
|
42
|
+
return source;
|
|
43
|
+
}
|
|
44
|
+
has(name) {
|
|
45
|
+
return this.sources.has(name);
|
|
46
|
+
}
|
|
47
|
+
entities() {
|
|
48
|
+
return [...this.sources.keys()];
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
21
52
|
// runtime/subsystems/integration/detection-config.schema.ts
|
|
22
53
|
import { z as z2 } from "zod";
|
|
23
54
|
var FieldMappingSchema = z2.object({
|
|
@@ -280,6 +311,7 @@ var INTEGRATION_SINK = "INTEGRATION_SINK";
|
|
|
280
311
|
var INTEGRATION_RUN_RECORDER = "INTEGRATION_RUN_RECORDER";
|
|
281
312
|
var INTEGRATION_MODULE_OPTIONS = "INTEGRATION_MODULE_OPTIONS";
|
|
282
313
|
var INTEGRATION_MULTI_TENANT = "INTEGRATION_MULTI_TENANT";
|
|
314
|
+
var ENTITY_CHANGE_SOURCE_REGISTRY = "ENTITY_CHANGE_SOURCE_REGISTRY";
|
|
283
315
|
|
|
284
316
|
// runtime/subsystems/integration/integration-errors.ts
|
|
285
317
|
var MissingTenantIdError = class extends Error {
|
|
@@ -1158,6 +1190,7 @@ export {
|
|
|
1158
1190
|
DeepEqualDiffer,
|
|
1159
1191
|
DetectionConfigSchema,
|
|
1160
1192
|
DrizzleIntegrationRunRecorder,
|
|
1193
|
+
ENTITY_CHANGE_SOURCE_REGISTRY,
|
|
1161
1194
|
ExecuteIntegrationUseCase,
|
|
1162
1195
|
FieldDiffSchema,
|
|
1163
1196
|
FieldDiffValueSchema,
|
|
@@ -1171,12 +1204,14 @@ export {
|
|
|
1171
1204
|
INTEGRATION_SINK,
|
|
1172
1205
|
IntegrationModule,
|
|
1173
1206
|
MemoryCursorStore,
|
|
1207
|
+
MemoryEntityChangeSourceRegistry,
|
|
1174
1208
|
MemoryRunRecorder,
|
|
1175
1209
|
MissingTenantIdError,
|
|
1176
1210
|
PollChangeSource,
|
|
1177
1211
|
PollDetectionSchema,
|
|
1178
1212
|
PostgresCursorStore,
|
|
1179
1213
|
ResolvedFilterSchema,
|
|
1214
|
+
UnknownEntityError,
|
|
1180
1215
|
WebhookChangeSource,
|
|
1181
1216
|
WebhookDetectionSchema,
|
|
1182
1217
|
assertTenantId,
|