/** * Setup Schema Mappings Script * * Configures schema mappings for object types based on the provided configuration. * Run with: npm run setup-schema-mappings */ import { schemaMappingService } from '../src/services/schemaMappingService.js'; import { logger } from '../src/services/logger.js'; import { JIRA_NAME_TO_TYPE } from '../src/generated/jira-schema.js'; // Configuration: Schema ID -> Array of object type display names const SCHEMA_MAPPINGS: Record = { '8': ['User'], '6': [ 'Application Component', 'Flows', 'Server', 'AzureSubscription', 'Certificate', 'Domain', 'Package', 'PackageBuild', 'Privileged User', 'Software', 'SoftwarePatch', 'Supplier', 'Application Management - Subteam', 'Application Management - Team', 'Measures', 'Rebootgroups', 'Application Management - Hosting', 'Application Management - Number of Users', 'Application Management - TAM', 'Application Management - Application Type', 'Application Management - Complexity Factor', 'Application Management - Dynamics Factor', 'ApplicationFunction', 'ApplicationFunctionCategory', 'Business Impact Analyse', 'Business Importance', 'Certificate ClassificationType', 'Certificate Type', 'Hosting Type', 'ICT Governance Model', 'Organisation', ], }; async function setupSchemaMappings() { logger.info('Setting up schema mappings...'); try { let totalMappings = 0; let skippedMappings = 0; let errors = 0; for (const [schemaId, objectTypeNames] of Object.entries(SCHEMA_MAPPINGS)) { logger.info(`\nConfiguring schema ${schemaId} with ${objectTypeNames.length} object types...`); for (const displayName of objectTypeNames) { try { // Convert display name to typeName let typeName: string; if (displayName === 'User') { // User might not be in the generated schema, use 'User' directly typeName = 'User'; // First, ensure User exists in object_types table const { normalizedCacheStore } = await import('../src/services/normalizedCacheStore.js'); const db = (normalizedCacheStore as any).db; await db.ensureInitialized?.(); // Check if User exists in object_types const existing = await db.queryOne<{ type_name: string }>(` SELECT type_name FROM object_types WHERE type_name = ? `, [typeName]); if (!existing) { // Insert User into object_types (we'll use a placeholder jira_type_id) // The actual jira_type_id will be discovered during schema discovery logger.info(` ℹ️ Adding "User" to object_types table...`); try { await db.execute(` INSERT INTO object_types (jira_type_id, type_name, display_name, description, sync_priority, object_count, discovered_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(jira_type_id) DO NOTHING `, [ 999999, // Placeholder ID - will be updated during schema discovery 'User', 'User', 'User object type from schema 8', 0, 0, new Date().toISOString(), new Date().toISOString() ]); // Also try with type_name as unique constraint await db.execute(` INSERT INTO object_types (jira_type_id, type_name, display_name, description, sync_priority, object_count, discovered_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(type_name) DO UPDATE SET display_name = excluded.display_name, updated_at = excluded.updated_at `, [ 999999, 'User', 'User', 'User object type from schema 8', 0, 0, new Date().toISOString(), new Date().toISOString() ]); logger.info(` ✓ Added "User" to object_types table`); } catch (error: any) { // If it already exists, that's fine if (error.message?.includes('UNIQUE constraint') || error.message?.includes('duplicate key')) { logger.info(` ℹ️ "User" already exists in object_types table`); } else { throw error; } } } } else { // Look up typeName from JIRA_NAME_TO_TYPE mapping typeName = JIRA_NAME_TO_TYPE[displayName]; if (!typeName) { logger.warn(` ⚠️ Skipping "${displayName}" - typeName not found in schema`); skippedMappings++; continue; } } // Set the mapping await schemaMappingService.setMapping(typeName, schemaId, true); logger.info(` ✓ Mapped ${typeName} (${displayName}) -> Schema ${schemaId}`); totalMappings++; } catch (error) { logger.error(` ✗ Failed to map "${displayName}" to schema ${schemaId}:`, error); errors++; } } } logger.info(`\n✅ Schema mappings setup complete!`); logger.info(` - Total mappings created: ${totalMappings}`); if (skippedMappings > 0) { logger.info(` - Skipped (not found in schema): ${skippedMappings}`); } if (errors > 0) { logger.info(` - Errors: ${errors}`); } // Clear cache to ensure fresh lookups schemaMappingService.clearCache(); logger.info(`\n💾 Cache cleared - mappings are now active`); } catch (error) { logger.error('Failed to setup schema mappings:', error); process.exit(1); } } // Run the script setupSchemaMappings() .then(() => { logger.info('\n✨ Done!'); process.exit(0); }) .catch((error) => { logger.error('Script failed:', error); process.exit(1); });