/** * `connectors/migration-studio.ts` — HSP-085: Migration Studio i kundportalen. * * T01: an EJ importerbart objekt döljs inte ur nämnaren — objects that * CANNOT be imported remain in the denominator; the import-ratio is * calculated over the FULL set (importable + non-importable), so hiding * failures would falsify the ratio. * T02: en uppgift kan återupptas efter sessionbyte — the Studio's state * (mapping, checkpoint, decided conflicts) persists in a resumable session * record keyed by migrationId; a session change picks up where the last * session stopped. * T03: en importerad CRM-lista marknadsförs INTE som komplett Microsoft * 365-ersättning — the coverage claim is SCOPED (what was imported, what * the coverage covers), never a generic total badge. */ export interface MigrationSourceObject { objectId: string; name: string; /** Can this object be imported into the target? */ importable: boolean; /** Why NOT importable (T01: the reason is ALWAYS recorded for non-importable). */ nonImportableReason?: string; /** The object's tenant (T03: cross-tenant objects are non-importable). */ tenantId: string; } export interface ConflictRecord { conflictId: string; objectId: string; kind: 'name_collision' | 'missing_field' | 'type_mismatch'; /** Has the customer decided on this conflict? */ resolved: boolean; resolution?: 'skip' | 'overwrite' | 'merge'; } export interface MigrationSession { migrationId: string; tenantId: string; /** T02: the checkpoint (batch position) — resumable across sessions. */ checkpointCursor: string; /** T02: decided conflicts persist across sessions. */ resolvedConflicts: ReadonlyArray; state: 'mapping' | 'dry_run' | 'conflict_review' | 'reconciliation' | 'final_approval'; } // ----------------------------------------------------------------------------- // T01: the import ratio counts EVERY source object in the denominator // ----------------------------------------------------------------------------- export interface ImportRatio { totalSourceObjects: number; importableCount: number; nonImportableCount: number; /** importable / total — non-importable objects stay in the DENOMINATOR. */ ratio: number; /** The non-importable objects with their reasons (always visible). */ nonImportable: ReadonlyArray<{ objectId: string; reason: string }>; } export function computeImportRatio(source: ReadonlyArray): ImportRatio { const importable = source.filter((o) => o.importable); const nonImportable = source.filter((o) => !o.importable); const total = source.length; return { totalSourceObjects: total, importableCount: importable.length, nonImportableCount: nonImportable.length, ratio: total === 0 ? 0 : Math.round((importable.length / total) * 100) / 100, nonImportable: nonImportable.map((o) => ({ objectId: o.objectId, reason: o.nonImportableReason ?? 'anledning ej angiven', })), }; } // ----------------------------------------------------------------------------- // T02: session resume // ----------------------------------------------------------------------------- export interface ResumeDecision { state: 'resumed' | 'fresh'; checkpointCursor: string; resolvedConflicts: ReadonlyArray; } /** * T02: resume a migration after a session change. The session record * (checkpoint + decided conflicts) persists; a new session picks up EXACTLY * where the old one left off. */ export function resumeMigration( stored: MigrationSession | undefined, requestingTenantId: string, ): ResumeDecision { if (!stored || stored.tenantId !== requestingTenantId) { return { state: 'fresh', checkpointCursor: '0', resolvedConflicts: [] }; } return { state: 'resumed', checkpointCursor: stored.checkpointCursor, resolvedConflicts: [...stored.resolvedConflicts], }; } // ----------------------------------------------------------------------------- // T03: scoped coverage claim // ----------------------------------------------------------------------------- export interface CoverageClaim { /** T03: the claim is SCOPED — what WAS imported, not a generic total. */ scope: string; importedCount: number; /** The denominator INCLUDES non-importable objects (T01). */ totalSourceObjects: number; nonImportableCount: number; } /** * T03: the coverage claim is SCOPED (a defined scope, never a total generic * badge). "100% CRM-listor" means CRM lists specifically — never "complete * Microsoft 365 replacement". */ export function buildCoverageClaim( scope: string, ratio: ImportRatio, ): CoverageClaim { return { scope: `Migration coverage for '${scope}' — ${ratio.importableCount} av ${ratio.totalSourceObjects} objekt importerade (${ratio.nonImportableCount} ej importerbarta, redovisade)`, importedCount: ratio.importableCount, totalSourceObjects: ratio.totalSourceObjects, nonImportableCount: ratio.nonImportableCount, }; }