Skip to content

Commit 3f6602b

Browse files
fix: @W-18480479 : Code review comments
1 parent 25a2c3b commit 3f6602b

File tree

3 files changed

+109
-43
lines changed

3 files changed

+109
-43
lines changed

src/commands/omnistudio/migration/migrate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export default class Migrate extends OmniStudioBaseCommand {
208208
);
209209

210210
if (!migrateOnly) {
211-
await postMigrate.setDesignersToUseStandardDataModel(namespace, actionItems);
211+
await postMigrate.executeTasks(namespace, actionItems);
212212
}
213213
// From here also actionItems need to be collected
214214
await postMigrate.restoreExperienceAPIMetadataSettings(
@@ -437,7 +437,7 @@ export default class Migrate extends OmniStudioBaseCommand {
437437
allVersions
438438
),
439439
new CardMigrationTool(namespace, conn, this.logger, messages, this.ux, allVersions),
440-
new GlobalAutoNumberMigrationTool(namespace, conn, this.logger, messages, this.ux),
440+
// new GlobalAutoNumberMigrationTool(namespace, conn, this.logger, messages, this.ux),
441441
];
442442
} else {
443443
// For single component migration, the order doesn't matter as much

src/migration/postMigrate.ts

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ export class PostMigrate extends BaseMigrationTool {
4040
this.settingsPrefManager = new OmnistudioSettingsPrefManager(connection, messages);
4141
}
4242

43+
/**
44+
* Execute post migration tasks with dependency handling.
45+
*/
46+
public async executeTasks(namespaceToModify: string, userActionMessage: string[]): Promise<string[]> {
47+
const designerOk = await this.enableDesignersToUseStandardDataModelIfNeeded(namespaceToModify, userActionMessage);
48+
if (designerOk) {
49+
await this.enableStandardRuntimeIfNeeded(userActionMessage);
50+
}
51+
return userActionMessage;
52+
}
53+
4354
/**
4455
* Checks if OmniStudio designers are already using the standard data model for the specific package.
45-
*
46-
* This method queries the OmniInteractionConfig table to check for specific DeveloperName values
47-
* that indicate whether the standard data model is enabled for the package.
48-
*
49-
* @param namespaceToModify The namespace of the package being checked
50-
* @returns {Promise<boolean>} True if designers are already using standard data model, false otherwise
5156
*/
5257
private async isStandardDesignerEnabled(namespaceToModify: string): Promise<boolean> {
5358
try {
@@ -62,7 +67,6 @@ export class PostMigrate extends BaseMigrationTool {
6267
const records = result.records as Array<{ DeveloperName: string; Value: string }>;
6368

6469
for (const record of records) {
65-
// Check if the package matches the one we're migrating
6670
if (record.Value === namespaceToModify) {
6771
return true;
6872
}
@@ -74,15 +78,14 @@ export class PostMigrate extends BaseMigrationTool {
7478
} catch (error) {
7579
const errMsg = error instanceof Error ? error.message : String(error);
7680
Logger.error(this.messages.getMessage('errorCheckingStandardDesigner', [namespaceToModify, errMsg]));
77-
// Assume not enabled if query fails
7881
return false;
7982
}
8083
}
8184

82-
public async setDesignersToUseStandardDataModel(
85+
public async enableDesignersToUseStandardDataModelIfNeeded(
8386
namespaceToModify: string,
8487
userActionMessage: string[]
85-
): Promise<string[]> {
88+
): Promise<boolean> {
8689
try {
8790
Logger.logVerbose(this.messages.getMessage('settingDesignersToStandardModel'));
8891

@@ -92,11 +95,7 @@ export class PostMigrate extends BaseMigrationTool {
9295

9396
if (isAlreadyEnabled) {
9497
Logger.logVerbose(this.messages.getMessage('standardDesignerAlreadyEnabled', [namespaceToModify]));
95-
96-
// Even though designer is already enabled, we should still enable Standard Runtime
97-
await this.enableStandardRuntimeIfNeeded(userActionMessage);
98-
99-
return userActionMessage;
98+
return true;
10099
}
101100

102101
const apexCode = `
@@ -113,39 +112,27 @@ export class PostMigrate extends BaseMigrationTool {
113112
userActionMessage.push(this.messages.getMessage('manuallySwitchDesignerToStandardDataModel'));
114113

115114
// Do NOT attempt to enable standard runtime when setup fails
116-
return userActionMessage;
115+
return false;
117116
} else if (result?.success === true) {
118117
Logger.logVerbose(this.messages.getMessage('designersSetToStandardModel'));
119-
Logger.logVerbose(this.messages.getMessage('enableStandardRuntimeAfterDesigner'));
120-
121-
// Enable Standard OmniStudio Runtime after successful standard designer setup
122-
await this.enableStandardRuntimeIfNeeded(userActionMessage);
118+
return true;
123119
} else {
124120
// Handle unexpected result structure
125121
Logger.error('Received unexpected response from Apex execution - unable to determine success status');
126122
userActionMessage.push(this.messages.getMessage('manuallySwitchDesignerToStandardDataModel'));
127-
128-
// Do NOT attempt to enable standard runtime when result is unclear
129-
return userActionMessage;
123+
return false;
130124
}
131125
} catch (ex) {
132126
const errorDetails = ex instanceof Error ? ex.message : JSON.stringify(ex);
133-
Logger.error(this.messages.getMessage('exceptionSettingDesignersToStandardModel', [errorDetails]));
127+
Logger.error(this.messages.getMessage('exceptionSettingDesignersToStandardDataModel', [errorDetails]));
134128
Logger.logVerbose(this.messages.getMessage('skipStandardRuntimeDueToFailure'));
135129
userActionMessage.push(this.messages.getMessage('manuallySwitchDesignerToStandardDataModel'));
136-
137-
// Do NOT attempt to enable standard runtime when exception occurs
130+
return false;
138131
}
139-
return userActionMessage;
140132
}
141133

142134
/**
143135
* Enables Standard OmniStudio Runtime if it's currently disabled.
144-
*
145-
* This method is called after successfully setting designers to use standard data model.
146-
* It checks if Standard Runtime is already enabled and only enables it if disabled.
147-
*
148-
* @param userActionMessage Array to collect user action messages for manual intervention
149136
*/
150137
private async enableStandardRuntimeIfNeeded(userActionMessage: string[]): Promise<void> {
151138
try {

test/migration/postMigrate.test.ts

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe('PostMigrate', () => {
5353
tooling: {
5454
executeAnonymous: sandbox.stub(),
5555
},
56+
query: sandbox.stub(),
5657
} as unknown as Connection;
5758

5859
// Mock logger
@@ -92,6 +93,10 @@ describe('PostMigrate', () => {
9293
.withArgs('errorRevertingExperienceBundleMetadataAPI')
9394
.returns('Error reverting Experience Bundle Metadata API');
9495
getMessageStub.withArgs('errorDeployingComponents').returns('Error deploying components');
96+
// New messages referenced by implementation
97+
getMessageStub.withArgs('checkingStandardDesignerStatus', [testNamespace]).returns('Checking designer status');
98+
getMessageStub.withArgs('standardDesignerAlreadyEnabled', [testNamespace]).returns('Designer already enabled');
99+
getMessageStub.withArgs('skipStandardRuntimeDueToFailure').returns('Skip runtime due to failure');
95100

96101
// Mock Logger static methods
97102
sandbox.stub(Logger, 'logVerbose');
@@ -156,7 +161,7 @@ describe('PostMigrate', () => {
156161
expect(deployerStub.called).to.be.false;
157162
});
158163

159-
xit('should handle deployment errors gracefully', () => {
164+
it('should handle deployment errors gracefully', () => {
160165
// Arrange
161166
const error = new Error('Deployment failed');
162167
const deployerStub = sandbox.stub(Deployer.prototype, 'deploy').throws(error);
@@ -229,7 +234,10 @@ describe('PostMigrate', () => {
229234
const logVerboseStub = Logger.logVerbose as sinon.SinonStub;
230235

231236
// Act
232-
const result = await postMigrate.setDesignersToUseStandardDataModel(namespaceToModify, userActionMessage);
237+
const result = await postMigrate.enableDesignersToUseStandardDataModelIfNeeded(
238+
namespaceToModify,
239+
userActionMessage
240+
);
233241

234242
// Assert
235243
expect(anonymousApexRunnerStub.calledOnce).to.be.true;
@@ -239,7 +247,8 @@ describe('PostMigrate', () => {
239247
);
240248
expect(logVerboseStub.calledWith('Setting designers to standard model...')).to.be.true;
241249
expect(logVerboseStub.calledWith('Designers set to standard model')).to.be.true;
242-
expect(result).to.deep.equal(userActionMessage);
250+
expect(result).to.equal(true);
251+
expect(userActionMessage).to.deep.equal([]);
243252
});
244253

245254
it('should handle unsuccessful anonymous apex execution', async () => {
@@ -258,12 +267,16 @@ describe('PostMigrate', () => {
258267
const anonymousApexRunnerStub = sandbox.stub(AnonymousApexRunner, 'run').resolves(mockResult);
259268

260269
// Act
261-
const result = await postMigrate.setDesignersToUseStandardDataModel(namespaceToModify, userActionMessage);
270+
const result = await postMigrate.enableDesignersToUseStandardDataModelIfNeeded(
271+
namespaceToModify,
272+
userActionMessage
273+
);
262274

263275
// Assert
264276
expect(anonymousApexRunnerStub.calledOnce).to.be.true;
265277
expect(logErrorStub.called).to.be.true;
266-
expect(result).to.include('Please manually switch designer to standard data model');
278+
expect(result).to.equal(false);
279+
expect(userActionMessage).to.include('Please manually switch designer to standard data model');
267280
});
268281

269282
it('should handle exceptions during execution', async () => {
@@ -274,12 +287,78 @@ describe('PostMigrate', () => {
274287
const anonymousApexRunnerStub = sandbox.stub(AnonymousApexRunner, 'run').rejects(error);
275288

276289
// Act
277-
const result = await postMigrate.setDesignersToUseStandardDataModel(namespaceToModify, userActionMessage);
290+
const result = await postMigrate.enableDesignersToUseStandardDataModelIfNeeded(
291+
namespaceToModify,
292+
userActionMessage
293+
);
278294

279295
// Assert
280296
expect(anonymousApexRunnerStub.calledOnce).to.be.true;
281297
expect(logErrorStub.called).to.be.true;
282-
expect(result).to.include('Please manually switch designer to standard data model');
298+
expect(result).to.equal(false);
299+
expect(userActionMessage).to.include('Please manually switch designer to standard data model');
300+
});
301+
302+
it('should return true when standard designer already enabled and skip Apex', async () => {
303+
// Arrange
304+
const namespaceToModify = 'test_namespace';
305+
const userActionMessage: string[] = [];
306+
// Stub SOQL query to indicate designer already enabled for this namespace
307+
(connection.query as unknown as sinon.SinonStub).resolves({
308+
totalSize: 2,
309+
records: [
310+
{ DeveloperName: 'TheFirstInstalledOmniPackage', Value: namespaceToModify },
311+
{ DeveloperName: 'InstalledIndustryPackage', Value: 'other' },
312+
],
313+
});
314+
const anonymousApexRunnerStub = sandbox.stub(AnonymousApexRunner, 'run');
315+
316+
// Act
317+
const result = await (postMigrate as any).enableDesignersToUseStandardDataModelIfNeeded(
318+
namespaceToModify,
319+
userActionMessage
320+
);
321+
322+
// Assert
323+
expect(result).to.equal(true);
324+
expect(anonymousApexRunnerStub.called).to.be.false;
325+
expect(userActionMessage).to.deep.equal([]);
326+
});
327+
});
328+
329+
describe('executeTasks', () => {
330+
it('should enable runtime when designer step succeeds', async () => {
331+
// Arrange
332+
const enableDesignerStub = sandbox
333+
.stub(postMigrate as any, 'enableDesignersToUseStandardDataModelIfNeeded')
334+
.resolves(true);
335+
const enableRuntimeSpy = sandbox.stub(postMigrate as any, 'enableStandardRuntimeIfNeeded').resolves();
336+
const actionItems: string[] = [];
337+
338+
// Act
339+
const res = await (postMigrate as any).executeTasks(testNamespace, actionItems);
340+
341+
// Assert
342+
expect(enableDesignerStub.calledOnce).to.be.true;
343+
expect(enableRuntimeSpy.calledOnce).to.be.true;
344+
expect(res).to.equal(actionItems);
345+
});
346+
347+
it('should not enable runtime when designer step fails', async () => {
348+
// Arrange
349+
const enableDesignerStub = sandbox
350+
.stub(postMigrate as any, 'enableDesignersToUseStandardDataModelIfNeeded')
351+
.resolves(false);
352+
const enableRuntimeSpy = sandbox.stub(postMigrate as any, 'enableStandardRuntimeIfNeeded').resolves();
353+
const actionItems: string[] = [];
354+
355+
// Act
356+
const res = await (postMigrate as any).executeTasks(testNamespace, actionItems);
357+
358+
// Assert
359+
expect(enableDesignerStub.calledOnce).to.be.true;
360+
expect(enableRuntimeSpy.called).to.be.false;
361+
expect(res).to.equal(actionItems);
283362
});
284363
});
285364

@@ -465,7 +544,7 @@ describe('PostMigrate', () => {
465544

466545
// Act
467546
postMigrate.deploy();
468-
await postMigrate.setDesignersToUseStandardDataModel('test_namespace', []);
547+
await (postMigrate as any).enableDesignersToUseStandardDataModelIfNeeded('test_namespace', []);
469548
await postMigrate.restoreExperienceAPIMetadataSettings({ value: true }, []);
470549

471550
// Assert
@@ -500,7 +579,7 @@ describe('PostMigrate', () => {
500579

501580
// Act
502581
postMigrateNoDeploy.deploy();
503-
await postMigrateNoDeploy.setDesignersToUseStandardDataModel('test_namespace', []);
582+
await (postMigrateNoDeploy as any).enableDesignersToUseStandardDataModelIfNeeded('test_namespace', []);
504583

505584
// Assert
506585
expect(deployerStub.called).to.be.false;

0 commit comments

Comments
 (0)