|
| 1 | +import { Connection, Messages } from '@salesforce/core'; |
| 2 | +import { Logger } from '../logger'; |
| 3 | +import { QueryTools } from '../query'; |
| 4 | +import { NetUtils } from '../net'; |
| 5 | + |
| 6 | +/** |
| 7 | + * OmniStudioMetadataCleanupService |
| 8 | + * |
| 9 | + * This service checks for records in four OmniStudio metadata tables and cleans them if found: |
| 10 | + * - OmniUiCardConfig |
| 11 | + * - OmniScriptConfig |
| 12 | + * - OmniIntegrationProcConfig |
| 13 | + * - OmniDataTransformConfig |
| 14 | + */ |
| 15 | + |
| 16 | +export class OmniStudioMetadataCleanupService { |
| 17 | + private static readonly CONFIG_TABLES = [ |
| 18 | + 'OmniUiCardConfig', |
| 19 | + 'OmniScriptConfig', |
| 20 | + 'OmniIntegrationProcConfig', |
| 21 | + 'OmniDataTransformConfig', |
| 22 | + ]; |
| 23 | + |
| 24 | + private readonly connection: Connection; |
| 25 | + private readonly messages: Messages; |
| 26 | + |
| 27 | + public constructor(connection: Connection, messages: Messages) { |
| 28 | + this.messages = messages; |
| 29 | + this.connection = connection; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Checks if all config tables are clean (have no records) |
| 34 | + * |
| 35 | + * @returns Promise<boolean> - true if all tables are empty, false if any table has records |
| 36 | + */ |
| 37 | + public async hasCleanOmniStudioMetadataTables(): Promise<boolean> { |
| 38 | + try { |
| 39 | + for (const tableName of OmniStudioMetadataCleanupService.CONFIG_TABLES) { |
| 40 | + const recordIds = await QueryTools.queryIds(this.connection, tableName); |
| 41 | + if (recordIds.length > 0) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + } |
| 45 | + return true; |
| 46 | + } catch (error) { |
| 47 | + Logger.error(this.messages.getMessage('errorCheckingMetadataTables', [String(error)])); |
| 48 | + return false; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Checks all config tables for records and cleans them if found |
| 54 | + * |
| 55 | + * @returns Promise<boolean> - true if cleanup was successful, false otherwise |
| 56 | + */ |
| 57 | + public async cleanupOmniStudioMetadataTables(): Promise<boolean> { |
| 58 | + try { |
| 59 | + Logger.log(this.messages.getMessage('startingMetadataCleanup')); |
| 60 | + |
| 61 | + let totalCleanedRecords = 0; |
| 62 | + const failedTables: string[] = []; |
| 63 | + |
| 64 | + for (const tableName of OmniStudioMetadataCleanupService.CONFIG_TABLES) { |
| 65 | + const recordCount = await this.cleanupOmniStudioMetadataTable(tableName); |
| 66 | + |
| 67 | + if (recordCount >= 0) { |
| 68 | + totalCleanedRecords += recordCount; |
| 69 | + } else { |
| 70 | + failedTables.push(tableName); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (failedTables.length > 0) { |
| 75 | + Logger.error(this.messages.getMessage('failedToCleanTables', [failedTables.join(', ')])); |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + Logger.log(this.messages.getMessage('metadataCleanupCompleted', [totalCleanedRecords])); |
| 80 | + return true; |
| 81 | + } catch (error) { |
| 82 | + Logger.error(this.messages.getMessage('errorCheckingMetadataTables', [String(error)])); |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Checks a specific table for records and cleans them if found |
| 89 | + * |
| 90 | + * @param tableName - Name of the table to check and clean |
| 91 | + * @returns Promise<number> - number of cleaned records, or -1 if failed |
| 92 | + */ |
| 93 | + private async cleanupOmniStudioMetadataTable(tableName: string): Promise<number> { |
| 94 | + const recordIds = await QueryTools.queryIds(this.connection, tableName); |
| 95 | + |
| 96 | + if (recordIds.length === 0) { |
| 97 | + return 0; |
| 98 | + } |
| 99 | + |
| 100 | + const deleteSuccess = await NetUtils.delete(this.connection, recordIds); |
| 101 | + return deleteSuccess ? recordIds.length : -1; |
| 102 | + } |
| 103 | +} |
0 commit comments