74e37f3e76
Work in progress from the Codex session, committed so nothing sits undeployed. Verified before committing: typecheck clean across all packages, 139 unit tests and the e2e suite green, migrations apply to an empty Postgres. Adds the HubSpot integration boundary (OAuth, client, contracts, webhook signature verification, sync), a growth route, customer-lifecycle service, Piggy lifecycle tools, a Growth page, and shared lifecycle/hubspot types. Two things are deliberately incomplete and should not be mistaken for finished: `packages/db/src/schema/hubspot.ts` is NOT exported from the schema index, so it is inert — no tables, no migration. That is the correct order (the shape can settle before it becomes a migration), but it does mean the HubSpot routes have no persistence behind them yet. `pnpm-workspace.yaml` and `pnpm-lock.yaml` are left uncommitted on purpose. The workspace file contains a literal unanswered placeholder — "esbuild: set this to true or false" — and this repository installs with npm, which is also what CI runs. Committing a second package manager's lockfile would make the install ambiguous. If the move to pnpm is intended it should be a deliberate change that updates CI and the Dockerfile together. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
102 lines
2.9 KiB
TypeScript
102 lines
2.9 KiB
TypeScript
import { createHash } from 'node:crypto';
|
|
import type { HubSpotObjectType, HubSpotRecord } from './contracts';
|
|
|
|
export interface HubSpotSyncCursor {
|
|
after: string | null;
|
|
phase: 'initial' | 'reconcile';
|
|
}
|
|
|
|
export interface HubSpotSyncStore {
|
|
getCursor(connectionId: string, objectType: HubSpotObjectType): Promise<HubSpotSyncCursor>;
|
|
/** Records and the next cursor must commit in the same transaction. */
|
|
commitPage(input: {
|
|
connectionId: string;
|
|
objectType: HubSpotObjectType;
|
|
phase: 'initial' | 'reconcile';
|
|
expectedAfter: string | null;
|
|
nextAfter: string | null;
|
|
records: readonly HubSpotSyncRecord[];
|
|
completedAt: Date;
|
|
}): Promise<void>;
|
|
}
|
|
|
|
export interface HubSpotSyncTokenProvider {
|
|
getAccessToken(connectionId: string, signal?: AbortSignal): Promise<string>;
|
|
}
|
|
|
|
export interface HubSpotSyncCrmClient {
|
|
listObjects(
|
|
accessToken: string,
|
|
objectType: HubSpotObjectType,
|
|
options?: { after?: string | null; signal?: AbortSignal },
|
|
): Promise<{ results: HubSpotRecord[]; nextAfter: string | null }>;
|
|
}
|
|
|
|
export interface HubSpotSyncRecord extends HubSpotRecord {
|
|
contentHash: string;
|
|
fetchedAt: Date;
|
|
}
|
|
|
|
export interface HubSpotSyncPageResult {
|
|
objectType: HubSpotObjectType;
|
|
records: number;
|
|
nextAfter: string | null;
|
|
complete: boolean;
|
|
}
|
|
|
|
export class HubSpotSyncService {
|
|
constructor(
|
|
private readonly store: HubSpotSyncStore,
|
|
private readonly tokens: HubSpotSyncTokenProvider,
|
|
private readonly crm: HubSpotSyncCrmClient,
|
|
private readonly now: () => Date = () => new Date(),
|
|
) {}
|
|
|
|
async syncNextPage(
|
|
connectionId: string,
|
|
objectType: HubSpotObjectType,
|
|
signal?: AbortSignal,
|
|
): Promise<HubSpotSyncPageResult> {
|
|
const cursor = await this.store.getCursor(connectionId, objectType);
|
|
const accessToken = await this.tokens.getAccessToken(connectionId, signal);
|
|
const page = await this.crm.listObjects(accessToken, objectType, {
|
|
after: cursor.after,
|
|
signal,
|
|
});
|
|
const fetchedAt = this.now();
|
|
const records = page.results.map((record) => ({
|
|
...record,
|
|
fetchedAt,
|
|
contentHash: hashHubSpotRecord(record),
|
|
}));
|
|
await this.store.commitPage({
|
|
connectionId,
|
|
objectType,
|
|
phase: cursor.phase,
|
|
expectedAfter: cursor.after,
|
|
nextAfter: page.nextAfter,
|
|
records,
|
|
completedAt: fetchedAt,
|
|
});
|
|
return {
|
|
objectType,
|
|
records: records.length,
|
|
nextAfter: page.nextAfter,
|
|
complete: page.nextAfter === null,
|
|
};
|
|
}
|
|
}
|
|
|
|
export function hashHubSpotRecord(record: HubSpotRecord): string {
|
|
const properties = Object.fromEntries(
|
|
Object.entries(record.properties).sort(([left], [right]) => left.localeCompare(right)),
|
|
);
|
|
return createHash('sha256').update(JSON.stringify({
|
|
id: record.id,
|
|
properties,
|
|
createdAt: record.createdAt,
|
|
updatedAt: record.updatedAt,
|
|
archived: record.archived,
|
|
})).digest('hex');
|
|
}
|