99 */
1010import * as os from 'os' ;
1111import { flags } from '@salesforce/command' ;
12- import { Messages } from '@salesforce/core' ;
13- import { ExecuteAnonymousResult } from 'jsforce' ;
12+ import { Connection , Messages } from '@salesforce/core' ;
1413import OmniStudioBaseCommand from '../../basecommand' ;
1514import { DataRaptorMigrationTool } from '../../../migration/dataraptor' ;
1615import { DebugTimer , MigratedObject , MigratedRecordInfo } from '../../../utils' ;
@@ -24,8 +23,8 @@ import { generatePackageXml } from '../../../utils/generatePackageXml';
2423import { OmnistudioOrgDetails , OrgUtils } from '../../../utils/orgUtils' ;
2524import { Constants } from '../../../utils/constants/stringContants' ;
2625import { OrgPreferences } from '../../../utils/orgPreferences' ;
27- import { AnonymousApexRunner } from '../../../utils/apex/executor/AnonymousApexRunner' ;
2826import { ProjectPathUtil } from '../../../utils/projectPathUtil' ;
27+ import { PostMigrate } from '../../../migration/postMigrate' ;
2928
3029// Initialize Messages with the current plugin directory
3130Messages . importMessagesDirectory ( __dirname ) ;
@@ -85,7 +84,6 @@ export default class Migrate extends OmniStudioBaseCommand {
8584 const migrateOnly = ( this . flags . only || '' ) as string ;
8685 const allVersions = this . flags . allversions || ( false as boolean ) ;
8786 const relatedObjects = ( this . flags . relatedobjects || '' ) as string ;
88-
8987 // this.org is guaranteed because requiresUsername=true, as opposed to supportsUsername
9088 const conn = this . org . getConnection ( ) ;
9189 if ( apiVersion ) {
@@ -124,9 +122,10 @@ export default class Migrate extends OmniStudioBaseCommand {
124122 let projectPath : string ;
125123 let objectsToProcess : string [ ] = [ ] ;
126124 let targetApexNamespace : string ;
125+ const isExperienceBundleMetadataAPIProgramaticallyEnabled : { value : boolean } = { value : false } ;
127126 if ( relatedObjects ) {
128127 // To-Do: Add LWC to valid options when GA is released
129- const validOptions = [ Constants . Apex , Constants . FlexiPage ] ;
128+ const validOptions = [ Constants . Apex , Constants . ExpSites , Constants . FlexiPage ] ;
130129 objectsToProcess = relatedObjects . split ( ',' ) . map ( ( obj ) => obj . trim ( ) ) ;
131130 // Validate input
132131 for ( const obj of objectsToProcess ) {
@@ -141,7 +140,15 @@ export default class Migrate extends OmniStudioBaseCommand {
141140 // Use ProjectPathUtil for APEX project folder selection (matches assess.ts logic)
142141 projectPath = await ProjectPathUtil . getProjectPath ( messages , true ) ;
143142 targetApexNamespace = await this . getTargetApexNamespace ( objectsToProcess , targetApexNamespace ) ;
144- }
143+ await this . handleExperienceSitePrerequisites (
144+ objectsToProcess ,
145+ conn ,
146+ isExperienceBundleMetadataAPIProgramaticallyEnabled
147+ ) ;
148+ Logger . logVerbose (
149+ 'The objects to process after handleExpSitePrerequisite are ' + JSON . stringify ( objectsToProcess )
150+ ) ;
151+ } // TODO - What if general consent is no
145152 }
146153
147154 Logger . log ( messages . getMessage ( 'migrationInitialization' , [ String ( namespace ) ] ) ) ;
@@ -182,8 +189,20 @@ export default class Migrate extends OmniStudioBaseCommand {
182189 relatedObjectMigrationResult . flexipageAssessmentInfos
183190 ) ;
184191
192+ // POST MIGRATION
185193 let actionItems = [ ] ;
186- actionItems = await this . setDesignersToUseStandardDataModel ( namespace ) ;
194+ const postMigrate : PostMigrate = new PostMigrate (
195+ this . org ,
196+ namespace ,
197+ conn ,
198+ this . logger ,
199+ messages ,
200+ this . ux ,
201+ objectsToProcess
202+ ) ;
203+
204+ actionItems = await postMigrate . setDesignersToUseStandardDataModel ( namespace ) ;
205+ await postMigrate . restoreExperienceAPIMetadataSettings ( isExperienceBundleMetadataAPIProgramaticallyEnabled ) ;
187206
188207 await ResultsBuilder . generateReport (
189208 objectMigrationResults ,
@@ -201,27 +220,47 @@ export default class Migrate extends OmniStudioBaseCommand {
201220 return { objectMigrationResults } ;
202221 }
203222
204- private async setDesignersToUseStandardDataModel ( namespace : string ) : Promise < string [ ] > {
205- const userActionMessage : string [ ] = [ ] ;
206- try {
207- Logger . logVerbose ( 'Setting designers to use the standard data model' ) ;
208- const apexCode = `
209- ${ namespace } .OmniStudioPostInstallClass.useStandardDataModel();
210- ` ;
211-
212- const result : ExecuteAnonymousResult = await AnonymousApexRunner . run ( this . org , apexCode ) ;
213- if ( result ?. success === false ) {
214- const message = result ?. exceptionStackTrace ;
215- Logger . error ( `Error occurred while setting designers to use the standard data model ${ message } ` ) ;
216- userActionMessage . push ( messages . getMessage ( 'manuallySwitchDesignerToStandardDataModel' ) ) ;
217- } else if ( result ?. success === true ) {
218- Logger . logVerbose ( 'Successfully executed setDesignersToUseStandardDataModel' ) ;
223+ private async handleExperienceSitePrerequisites (
224+ objectsToProcess : string [ ] ,
225+ conn : Connection ,
226+ isExperienceBundleMetadataAPIProgramaticallyEnabled : { value : boolean }
227+ ) : Promise < void > {
228+ if ( objectsToProcess . includes ( Constants . ExpSites ) ) {
229+ const expMetadataApiConsent = await this . getExpSiteMetadataEnableConsent ( ) ;
230+ Logger . logVerbose ( `The consent for exp site is ${ expMetadataApiConsent } ` ) ;
231+
232+ if ( expMetadataApiConsent === false ) {
233+ Logger . warn ( 'Consent for experience sites is not provided. Experience sites will not be processed' ) ;
234+ this . removeKeyFromRelatedObjectsToProcess ( Constants . ExpSites , objectsToProcess ) ;
235+ Logger . logVerbose ( `Objects to process after removing expsite are ${ JSON . stringify ( objectsToProcess ) } ` ) ;
236+ return ;
237+ }
238+
239+ const isMetadataAPIPreEnabled = await OrgPreferences . isExperienceBundleMetadataAPIEnabled ( conn ) ;
240+ if ( isMetadataAPIPreEnabled === true ) {
241+ Logger . logVerbose ( 'ExperienceBundle metadata api is already enabled' ) ;
242+ return ;
219243 }
220- } catch ( ex ) {
221- Logger . error ( `Exception occurred while setting designers to use the standard data model ${ JSON . stringify ( ex ) } ` ) ;
222- userActionMessage . push ( messages . getMessage ( 'manuallySwitchDesignerToStandardDataModel' ) ) ;
244+
245+ Logger . logVerbose ( 'ExperienceBundle metadata api needs to be programatically enabled' ) ;
246+ isExperienceBundleMetadataAPIProgramaticallyEnabled . value = await OrgPreferences . setExperienceBundleMetadataAPI (
247+ conn ,
248+ true
249+ ) ;
250+ if ( isExperienceBundleMetadataAPIProgramaticallyEnabled . value === false ) {
251+ this . removeKeyFromRelatedObjectsToProcess ( Constants . ExpSites , objectsToProcess ) ;
252+ Logger . warn ( 'Since the api could not able enabled the experience sites would not be processed' ) ;
253+ }
254+
255+ Logger . logVerbose ( `Objects to process are ${ JSON . stringify ( objectsToProcess ) } ` ) ;
256+ }
257+ }
258+
259+ private removeKeyFromRelatedObjectsToProcess ( keyToRemove : string , relatedObjects : string [ ] ) : void {
260+ const index = relatedObjects . indexOf ( Constants . ExpSites ) ;
261+ if ( index > - 1 ) {
262+ relatedObjects . splice ( index , 1 ) ;
223263 }
224- return userActionMessage ;
225264 }
226265
227266 private async truncateObjects ( migrationObjects : MigrationTool [ ] , debugTimer : DebugTimer ) : Promise < MigratedObject [ ] > {
@@ -355,6 +394,23 @@ export default class Migrate extends OmniStudioBaseCommand {
355394 return consent ;
356395 }
357396
397+ private async getExpSiteMetadataEnableConsent ( ) : Promise < boolean > {
398+ let consent : boolean | null = null ;
399+
400+ while ( consent === null ) {
401+ try {
402+ consent = await Logger . confirm (
403+ 'By proceeding further, you hereby consent to enable digital experience metadata api(y/n). If y sites will be processed, if n expsites will not be processed'
404+ ) ;
405+ } catch ( error ) {
406+ Logger . log ( messages . getMessage ( 'invalidYesNoResponse' ) ) ;
407+ consent = null ;
408+ }
409+ }
410+
411+ return consent ;
412+ }
413+
358414 private mergeRecordAndUploadResults (
359415 migrationResults : MigrationResult ,
360416 migrationTool : MigrationTool
0 commit comments