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 ] ;
128+ const validOptions = [ Constants . Apex , Constants . ExpSites ] ;
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 ) ] ) ) ;
@@ -181,8 +188,20 @@ export default class Migrate extends OmniStudioBaseCommand {
181188 relatedObjectMigrationResult . lwcAssessmentInfos
182189 ) ;
183190
191+ // POST MIGRATION
184192 let actionItems = [ ] ;
185- actionItems = await this . setDesignersToUseStandardDataModel ( namespace ) ;
193+ const postMigrate : PostMigrate = new PostMigrate (
194+ this . org ,
195+ namespace ,
196+ conn ,
197+ this . logger ,
198+ messages ,
199+ this . ux ,
200+ objectsToProcess
201+ ) ;
202+
203+ actionItems = await postMigrate . setDesignersToUseStandardDataModel ( namespace ) ;
204+ await postMigrate . restoreExperienceAPIMetadataSettings ( isExperienceBundleMetadataAPIProgramaticallyEnabled ) ;
186205
187206 await ResultsBuilder . generateReport (
188207 objectMigrationResults ,
@@ -200,27 +219,47 @@ export default class Migrate extends OmniStudioBaseCommand {
200219 return { objectMigrationResults } ;
201220 }
202221
203- private async setDesignersToUseStandardDataModel ( namespace : string ) : Promise < string [ ] > {
204- const userActionMessage : string [ ] = [ ] ;
205- try {
206- Logger . logVerbose ( 'Setting designers to use the standard data model' ) ;
207- const apexCode = `
208- ${ namespace } .OmniStudioPostInstallClass.useStandardDataModel();
209- ` ;
210-
211- const result : ExecuteAnonymousResult = await AnonymousApexRunner . run ( this . org , apexCode ) ;
212- if ( result ?. success === false ) {
213- const message = result ?. exceptionStackTrace ;
214- Logger . error ( `Error occurred while setting designers to use the standard data model ${ message } ` ) ;
215- userActionMessage . push ( messages . getMessage ( 'manuallySwitchDesignerToStandardDataModel' ) ) ;
216- } else if ( result ?. success === true ) {
217- Logger . logVerbose ( 'Successfully executed setDesignersToUseStandardDataModel' ) ;
222+ private async handleExperienceSitePrerequisites (
223+ objectsToProcess : string [ ] ,
224+ conn : Connection ,
225+ isExperienceBundleMetadataAPIProgramaticallyEnabled : { value : boolean }
226+ ) : Promise < void > {
227+ if ( objectsToProcess . includes ( Constants . ExpSites ) ) {
228+ const expMetadataApiConsent = await this . getExpSiteMetadataEnableConsent ( ) ;
229+ Logger . logVerbose ( `The consent for exp site is ${ expMetadataApiConsent } ` ) ;
230+
231+ if ( expMetadataApiConsent === false ) {
232+ Logger . warn ( 'Consent for experience sites is not provided. Experience sites will not be processed' ) ;
233+ this . removeKeyFromRelatedObjectsToProcess ( Constants . ExpSites , objectsToProcess ) ;
234+ Logger . logVerbose ( `Objects to process after removing expsite are ${ JSON . stringify ( objectsToProcess ) } ` ) ;
235+ return ;
236+ }
237+
238+ const isMetadataAPIPreEnabled = await OrgPreferences . isExperienceBundleMetadataAPIEnabled ( conn ) ;
239+ if ( isMetadataAPIPreEnabled === true ) {
240+ Logger . logVerbose ( 'ExperienceBundle metadata api is already enabled' ) ;
241+ return ;
218242 }
219- } catch ( ex ) {
220- Logger . error ( `Exception occurred while setting designers to use the standard data model ${ JSON . stringify ( ex ) } ` ) ;
221- userActionMessage . push ( messages . getMessage ( 'manuallySwitchDesignerToStandardDataModel' ) ) ;
243+
244+ Logger . logVerbose ( 'ExperienceBundle metadata api needs to be programatically enabled' ) ;
245+ isExperienceBundleMetadataAPIProgramaticallyEnabled . value = await OrgPreferences . setExperienceBundleMetadataAPI (
246+ conn ,
247+ true
248+ ) ;
249+ if ( isExperienceBundleMetadataAPIProgramaticallyEnabled . value === false ) {
250+ this . removeKeyFromRelatedObjectsToProcess ( Constants . ExpSites , objectsToProcess ) ;
251+ Logger . warn ( 'Since the api could not able enabled the experience sites would not be processed' ) ;
252+ }
253+
254+ Logger . logVerbose ( `Objects to process are ${ JSON . stringify ( objectsToProcess ) } ` ) ;
255+ }
256+ }
257+
258+ private removeKeyFromRelatedObjectsToProcess ( keyToRemove : string , relatedObjects : string [ ] ) : void {
259+ const index = relatedObjects . indexOf ( Constants . ExpSites ) ;
260+ if ( index > - 1 ) {
261+ relatedObjects . splice ( index , 1 ) ;
222262 }
223- return userActionMessage ;
224263 }
225264
226265 private async truncateObjects ( migrationObjects : MigrationTool [ ] , debugTimer : DebugTimer ) : Promise < MigratedObject [ ] > {
@@ -354,6 +393,23 @@ export default class Migrate extends OmniStudioBaseCommand {
354393 return consent ;
355394 }
356395
396+ private async getExpSiteMetadataEnableConsent ( ) : Promise < boolean > {
397+ let consent : boolean | null = null ;
398+
399+ while ( consent === null ) {
400+ try {
401+ consent = await Logger . confirm (
402+ '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'
403+ ) ;
404+ } catch ( error ) {
405+ Logger . log ( messages . getMessage ( 'invalidYesNoResponse' ) ) ;
406+ consent = null ;
407+ }
408+ }
409+
410+ return consent ;
411+ }
412+
357413 private mergeRecordAndUploadResults (
358414 migrationResults : MigrationResult ,
359415 migrationTool : MigrationTool
0 commit comments