Skip to content

Commit e0f37f4

Browse files
@W-19464589 - Data model skip logic for Use Case 2 (#409)
1 parent 1f6c692 commit e0f37f4

File tree

10 files changed

+412
-136
lines changed

10 files changed

+412
-136
lines changed

src/commands/omnistudio/migration/assess.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ProjectPathUtil } from '../../../utils/projectPathUtil';
2020
import { PreMigrate } from '../../../migration/premigrate';
2121
import { PostMigrate } from '../../../migration/postMigrate';
2222
import { CustomLabelsUtil } from '../../../utils/customLabels';
23+
import { initializeDataModelService } from '../../../utils/dataModelService';
2324
import { ValidatorService } from '../../../utils/validatorService';
2425

2526
Messages.importMessagesDirectory(__dirname);
@@ -79,8 +80,11 @@ export default class Assess extends OmniStudioBaseCommand {
7980
const apiVersion = conn.getApiVersion();
8081
const orgs: OmnistudioOrgDetails = await OrgUtils.getOrgDetails(conn);
8182

83+
// Initialize global data model service
84+
initializeDataModelService(orgs);
85+
8286
// Perform comprehensive validation using ValidatorService
83-
const validator = new ValidatorService(orgs, conn, messages);
87+
const validator = new ValidatorService(orgs, messages, conn);
8488
const isValidationPassed = await validator.validate();
8589

8690
if (!isValidationPassed) {

src/commands/omnistudio/migration/migrate.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ import { YES_SHORT, YES_LONG, NO_SHORT, NO_LONG } from '../../../utils/projectPa
3131
import { PostMigrate } from '../../../migration/postMigrate';
3232
import { PreMigrate } from '../../../migration/premigrate';
3333
import { GlobalAutoNumberMigrationTool } from '../../../migration/globalautonumber';
34-
import { ValidatorService } from '../../../utils/validatorService';
34+
import { initializeDataModelService } from '../../../utils/dataModelService';
3535
import { NameMappingRegistry } from '../../../migration/NameMappingRegistry';
36+
import { ValidatorService } from '../../../utils/validatorService';
3637

3738
// Initialize Messages with the current plugin directory
3839
Messages.importMessagesDirectory(__dirname);
@@ -92,10 +93,11 @@ export default class Migrate extends OmniStudioBaseCommand {
9293

9394
const orgs: OmnistudioOrgDetails = await OrgUtils.getOrgDetails(conn);
9495

95-
// Perform comprehensive validation using ValidatorService
96-
const validator = new ValidatorService(orgs, conn, messages);
97-
const isValidationPassed = await validator.validate();
96+
// Initialize global data model service
97+
initializeDataModelService(orgs);
9898

99+
const validator = new ValidatorService(orgs, messages, conn);
100+
const isValidationPassed = await validator.validate();
99101
if (!isValidationPassed) {
100102
return;
101103
}

src/migration/postMigrate.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,6 @@ export class PostMigrate extends BaseMigrationTool {
5151
return userActionMessage;
5252
}
5353

54-
/**
55-
* Checks if OmniStudio designers are already using the standard data model for the specific package.
56-
*/
57-
private async isStandardDesignerEnabled(namespaceToModify: string): Promise<boolean> {
58-
try {
59-
const query = `SELECT DeveloperName, Value FROM OmniInteractionConfig WHERE DeveloperName IN ('TheFirstInstalledOmniPackage', 'InstalledIndustryPackage')`;
60-
61-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
62-
const result = await this.connection.query(query);
63-
64-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
65-
if (result?.totalSize > 0) {
66-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
67-
const records = result.records as Array<{ DeveloperName: string; Value: string }>;
68-
69-
for (const record of records) {
70-
if (record.Value === namespaceToModify) {
71-
return true;
72-
}
73-
}
74-
return false;
75-
} else {
76-
return false;
77-
}
78-
} catch (error) {
79-
const errMsg = error instanceof Error ? error.message : String(error);
80-
Logger.error(this.messages.getMessage('errorCheckingStandardDesigner', [namespaceToModify, errMsg]));
81-
return false;
82-
}
83-
}
84-
8554
public async enableDesignersToUseStandardDataModelIfNeeded(
8655
namespaceToModify: string,
8756
userActionMessage: string[]
@@ -91,7 +60,7 @@ export class PostMigrate extends BaseMigrationTool {
9160

9261
// First check if standard designer is already enabled for this package
9362
Logger.logVerbose(this.messages.getMessage('checkingStandardDesignerStatus', [namespaceToModify]));
94-
const isAlreadyEnabled = await this.isStandardDesignerEnabled(namespaceToModify);
63+
const isAlreadyEnabled = await OrgPreferences.isStandardDesignerEnabled(this.connection, namespaceToModify);
9564

9665
if (isAlreadyEnabled) {
9766
Logger.logVerbose(this.messages.getMessage('standardDesignerAlreadyEnabled', [namespaceToModify]));

src/utils/constants/stringContants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export const Constants = {
2727
ApexComponentName: 'Apex Classes',
2828
CustomLabelComponentName: 'Custom Label',
2929
CustomLabelPluralName: 'Custom Labels',
30+
CustomDataModel: 'custom',
31+
StandardDataModel: 'standard',
3032

3133
// artifacts persistance folder names
3234
AssessmentReportsFolderName: 'assessment_reports',

src/utils/dataModelService.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { OmnistudioOrgDetails } from './orgUtils';
2+
import { Constants } from './constants/stringContants';
3+
4+
export class DataModelService {
5+
private readonly orgs: OmnistudioOrgDetails;
6+
7+
public constructor(orgs: OmnistudioOrgDetails) {
8+
this.orgs = orgs;
9+
}
10+
11+
public getDataModel(): string {
12+
const { omniStudioOrgPermissionEnabled } = this.orgs;
13+
if (!omniStudioOrgPermissionEnabled) {
14+
return Constants.CustomDataModel;
15+
}
16+
return Constants.StandardDataModel;
17+
}
18+
}
19+
20+
// Global instance and cached data model
21+
let globalDataModelService: DataModelService | null = null;
22+
let cachedDataModel: string | null = null;
23+
24+
// Initialize the global instance
25+
export function initializeDataModelService(orgs: OmnistudioOrgDetails): void {
26+
globalDataModelService = new DataModelService(orgs);
27+
cachedDataModel = null; // Reset cache when reinitializing
28+
}
29+
30+
// Get the global instance
31+
export function getDataModelService(): DataModelService {
32+
return globalDataModelService;
33+
}
34+
35+
// Convenience function to get data model info directly (with caching)
36+
export function getDataModelInfo(): string {
37+
if (cachedDataModel === null) {
38+
cachedDataModel = getDataModelService().getDataModel();
39+
}
40+
return cachedDataModel;
41+
}
42+
43+
// Convenience function to check if data model is standard
44+
export function isStandardDataModel(): boolean {
45+
const dataModel = getDataModelInfo();
46+
return dataModel === Constants.StandardDataModel;
47+
}
48+
49+
// Convenience function to check if data model is custom
50+
export function isCustomDataModel(): boolean {
51+
const dataModel = getDataModelInfo();
52+
return dataModel === Constants.CustomDataModel;
53+
}

src/utils/orgPreferences.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,44 @@ export class OrgPreferences {
154154
return false;
155155
}
156156
}
157+
158+
/**
159+
* Checks if OmniStudio designers are already using the standard data model for the specific package.
160+
*
161+
* @public
162+
* @static
163+
* @async
164+
* @param {Connection} connection - Salesforce connection instance
165+
* @param {string} namespaceToModify - The namespace to check for standard designer
166+
* @throws {Error} If checking the standard designer status fails
167+
* @returns {Promise<boolean>} True if standard designer is enabled, false otherwise
168+
*/
169+
public static async isStandardDesignerEnabled(connection: Connection, namespaceToModify: string): Promise<boolean> {
170+
try {
171+
const query = `SELECT DeveloperName, Value FROM OmniInteractionConfig
172+
WHERE DeveloperName IN ('TheFirstInstalledOmniPackage', 'InstalledIndustryPackage')`;
173+
174+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
175+
const result = await connection.query(query);
176+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
177+
if (result?.totalSize > 0) {
178+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
179+
const records = result.records as Array<{ DeveloperName: string; Value: string }>;
180+
181+
for (const record of records) {
182+
if (record.Value === namespaceToModify) {
183+
return true;
184+
}
185+
}
186+
return false;
187+
} else {
188+
return false;
189+
}
190+
} catch (error) {
191+
// TODO: What should be the default behavior if the query fails?
192+
const errMsg = error instanceof Error ? error.message : String(error);
193+
Logger.error(`Error checking standard designer for namespace ${namespaceToModify}: ${errMsg}`);
194+
return false;
195+
}
196+
}
157197
}

src/utils/orgUtils/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { Connection, Messages } from '@salesforce/core';
44
import { QueryTools } from '../query';
55
import { Logger } from '../logger';
6+
import { OrgPreferences } from '../orgPreferences';
67

78
// Load messages
89
const messages = Messages.loadMessages('@salesforce/plugin-omnistudio-migration-tool', 'migrate');
@@ -385,11 +386,8 @@ export class OrgUtils {
385386
try {
386387
return await connection.apex.get('/' + namespace + '/v1/orgPermission');
387388
} catch (e) {
388-
// Returning false as a fallback when the endpoint is not found.
389-
// As part of the 256 MVP, we don't want to block the migration just because the endpoint is missing.
390-
return !(e.errorCode === 'NOT_FOUND');
389+
// Any error in the apex mechanism will fallback to check the standard designer status
390+
return await OrgPreferences.isStandardDesignerEnabled(connection, namespace);
391391
}
392-
393-
return true;
394392
}
395393
}

src/utils/validatorService.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
import { Connection, Messages } from '@salesforce/core';
22
import { Logger } from '../utils/logger';
33
import { OmnistudioOrgDetails } from './orgUtils';
4+
import { isStandardDataModel } from './dataModelService';
45

56
export class ValidatorService {
6-
private readonly connection: Connection;
77
private readonly messages: Messages;
88
private readonly orgs: OmnistudioOrgDetails;
9-
10-
public constructor(orgs: OmnistudioOrgDetails, connection: Connection, messages: Messages) {
9+
private readonly connection: Connection;
10+
public constructor(orgs: OmnistudioOrgDetails, messages: Messages, connection: Connection) {
1111
this.orgs = orgs;
12-
this.connection = connection;
1312
this.messages = messages;
13+
this.connection = connection;
1414
}
1515

1616
public async validate(): Promise<boolean> {
17-
return (
18-
this.validateNamespace() &&
19-
this.validatePackageInstalled() &&
20-
this.validateOmniStudioOrgPermissionEnabled() &&
21-
(await this.validateOmniStudioLicenses())
22-
);
17+
const basicValidation = this.validateNamespace() && this.validatePackageInstalled();
18+
if (!basicValidation) {
19+
return false;
20+
}
21+
22+
// If data model is standard no need to check for the licences
23+
// TODO: Add metadata toggle validation
24+
const isStandard = isStandardDataModel();
25+
if (isStandard) {
26+
return true;
27+
}
28+
29+
// For custom data model, validate if licenses are valid
30+
const isLicensesValid = await this.validateOmniStudioLicenses();
31+
return isLicensesValid;
2332
}
2433

2534
public validatePackageInstalled(): boolean {
@@ -31,15 +40,6 @@ export class ValidatorService {
3140
return true;
3241
}
3342

34-
public validateOmniStudioOrgPermissionEnabled(): boolean {
35-
const { omniStudioOrgPermissionEnabled } = this.orgs;
36-
if (omniStudioOrgPermissionEnabled) {
37-
Logger.error(this.messages.getMessage('alreadyStandardModel'));
38-
return false;
39-
}
40-
return true;
41-
}
42-
4343
public validateNamespace(): boolean {
4444
const { hasValidNamespace } = this.orgs;
4545
if (!hasValidNamespace) {

0 commit comments

Comments
 (0)