Skip to content

Commit 3756a4d

Browse files
feat: @W-19081579: Omnistudio License validation and additional fixes
Changes: - Moved the validation steps into seprate util - Added the test cases - Additional fixes based on local testing
1 parent c106db3 commit 3756a4d

File tree

7 files changed

+630
-21
lines changed

7 files changed

+630
-21
lines changed

messages/assess.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
"angularOSWarning": " Angular Omniscript will not be migrated, please convert this to LWC based Omniscript",
3434
"relatedObjectGA": "Please select the type of component to assess: 'apex' for Apex classes, 'lwc' for Lightning Web Components, 'flexipage' for FlexiPages, or 'apex,lwc,flexipage' if you want to include all types.",
3535
"apexLwc": "Please select the type of component to assess: 'apex' for Apex classes.",
36-
"invalidNamespace": "The namespace you have passed is not valid namespace, the valid namespace of your org is ",
36+
"unknownNamespace": "Org doesn't have Omnistudio namespace(s) configured",
3737
"noPackageInstalled": "No package installed on given org.",
3838
"alreadyStandardModel": "The org is already on standard data model.",
39+
"noOmniStudioLicenses": "OmniStudio licenses are not found in the org. OmniStudio licenses are required for migration assessment",
3940
"invalidRelatedObjectsOption": "Invalid option provided for -r: %s. Valid options are apex, lwc, expsites, flexipage.",
4041
"formulaSyntaxError": "There was some problem while updating the formula syntax, please check the all the formula's syntax once : %s",
4142
"errorDuringFlexCardAssessment": "Error during FlexCard assessment",

messages/migrate.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
"allVersionsDescription": "Migrate all versions of a component",
3434
"relatedObjectGA": "Please select the type of components to migrate: 'apex' for Apex classes, 'lwc' for Lightning Web Components, 'flexipage' for FlexiPages, 'expsites' for Experience Sites, or 'apex,lwc,flexipage,expsites' if you want to include all types.",
3535
"apexLwc": "Please select the type of components to migrate: 'apex' for Apex classes.",
36-
"invalidNamespace": "The namespace you have passed is not valid namespace, the valid namespace of your org is ",
36+
"unknownNamespace": "Org doesn't have Omnistudio namespace(s) configured",
3737
"noPackageInstalled": "No package installed on given org.",
3838
"alreadyStandardModel": "The org is already on standard data model.",
39+
"noOmniStudioLicenses": "OmniStudio licenses are not found in the org. OmniStudio licenses are required for migration",
3940
"invalidRelatedObjectsOption": "Invalid option provided for -r: %s. Valid options are apex, lwc, expsites, flexipage.",
4041
"userConsentMessage": "By proceeding further, you hereby consent to the use, accept changes to your custom code and the accompanying terms and conditions associated with the use of the OmniStudio Migration Tool. Do you want to proceed? [y/n]",
4142
"userDeclinedConsent": "User declined consent, will not process %s .",
@@ -268,4 +269,4 @@
268269
"customLabelAssessmentSummary": "Custom Label with same name and different value is already exist without namespace.",
269270
"generatedCustomLabelAssessmentReportPage": "Generated custom label assessment report page %s of %s with %s labels",
270271
"varDeclarationUpdated": "Variable initialization has been updated for target name"
271-
}
272+
}

src/commands/omnistudio/migration/assess.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { ProjectPathUtil } from '../../../utils/projectPathUtil';
1919
import { PreMigrate } from '../../../migration/premigrate';
2020
import { PostMigrate } from '../../../migration/postMigrate';
2121
import { CustomLabelsUtil } from '../../../utils/customLabels';
22+
import { ValidatorService } from '../../../utils/validatorService';
2223

2324
Messages.importMessagesDirectory(__dirname);
2425
const messages = Messages.loadMessages('@salesforce/plugin-omnistudio-migration-tool', 'assess');
@@ -77,16 +78,11 @@ export default class Assess extends OmniStudioBaseCommand {
7778
const apiVersion = conn.getApiVersion();
7879
const orgs: OmnistudioOrgDetails = await OrgUtils.getOrgDetails(conn);
7980

80-
if (!orgs.hasValidNamespace) {
81-
Logger.warn(messages.getMessage('invalidNamespace') + orgs.packageDetails.namespace);
82-
}
81+
// Perform comprehensive validation using ValidatorService
82+
const validator = new ValidatorService(orgs, conn, messages);
83+
const isValidationPassed = await validator.validate();
8384

84-
if (!orgs.packageDetails) {
85-
Logger.error(messages.getMessage('noPackageInstalled'));
86-
return;
87-
}
88-
if (orgs.omniStudioOrgPermissionEnabled) {
89-
Logger.error(messages.getMessage('alreadyStandardModel'));
85+
if (!isValidationPassed) {
9086
return;
9187
}
9288

src/commands/omnistudio/migration/migrate.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { YES_SHORT, YES_LONG, NO_SHORT, NO_LONG } from '../../../utils/projectPa
3030
import { PostMigrate } from '../../../migration/postMigrate';
3131
import { PreMigrate } from '../../../migration/premigrate';
3232
import { GlobalAutoNumberMigrationTool } from '../../../migration/globalautonumber';
33+
import { ValidatorService } from '../../../utils/validatorService';
3334
import { NameMappingRegistry } from '../../../migration/NameMappingRegistry';
3435

3536
// Initialize Messages with the current plugin directory
@@ -90,16 +91,11 @@ export default class Migrate extends OmniStudioBaseCommand {
9091

9192
const orgs: OmnistudioOrgDetails = await OrgUtils.getOrgDetails(conn);
9293

93-
if (!orgs.hasValidNamespace) {
94-
Logger.warn(messages.getMessage('invalidNamespace') + orgs.packageDetails.namespace);
95-
}
94+
// Perform comprehensive validation using ValidatorService
95+
const validator = new ValidatorService(orgs, conn, messages);
96+
const isValidationPassed = await validator.validate();
9697

97-
if (!orgs.packageDetails) {
98-
Logger.error(messages.getMessage('noPackageInstalled'));
99-
return;
100-
}
101-
if (orgs.omniStudioOrgPermissionEnabled) {
102-
Logger.error(messages.getMessage('alreadyStandardModel'));
98+
if (!isValidationPassed) {
10399
return;
104100
}
105101

src/utils/orgUtils/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,15 @@ export class OrgUtils {
350350
const pkg = installedOmniPackages[0];
351351
packageDetails.version = `${pkg.MajorVersion}.${pkg.MinorVersion}`;
352352
packageDetails.namespace = pkg.NamespacePrefix;
353+
} else {
354+
// return to validate the org information
355+
return {
356+
packageDetails: undefined,
357+
omniStudioOrgPermissionEnabled: false,
358+
orgDetails: orgDetails[0],
359+
dataModel: undefined,
360+
hasValidNamespace: false,
361+
};
353362
}
354363

355364
//Execute apex rest resource to get omnistudio org permission

src/utils/validatorService.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Connection, Messages } from '@salesforce/core';
2+
import { Logger } from '../utils/logger';
3+
import { OmnistudioOrgDetails } from './orgUtils';
4+
5+
export class ValidatorService {
6+
private readonly connection: Connection;
7+
private readonly messages: Messages;
8+
private readonly orgs: OmnistudioOrgDetails;
9+
10+
public constructor(orgs: OmnistudioOrgDetails, connection: Connection, messages: Messages) {
11+
this.orgs = orgs;
12+
this.connection = connection;
13+
this.messages = messages;
14+
}
15+
16+
public async validate(): Promise<boolean> {
17+
return (
18+
this.validateNamespace() &&
19+
this.validatePackageInstalled() &&
20+
this.validateOmniStudioOrgPermissionEnabled() &&
21+
(await this.validateOmniStudioLicenses())
22+
);
23+
}
24+
25+
public validatePackageInstalled(): boolean {
26+
const { packageDetails } = this.orgs;
27+
if (!packageDetails) {
28+
Logger.error(this.messages.getMessage('noPackageInstalled'));
29+
return false;
30+
}
31+
return true;
32+
}
33+
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+
43+
public validateNamespace(): boolean {
44+
const { hasValidNamespace } = this.orgs;
45+
if (!hasValidNamespace) {
46+
Logger.error(this.messages.getMessage('unknownNamespace'));
47+
return false;
48+
}
49+
return true;
50+
}
51+
52+
public async validateOmniStudioLicenses(): Promise<boolean> {
53+
try {
54+
const query =
55+
"SELECT count(DeveloperName) total FROM PermissionSetLicense WHERE PermissionSetLicenseKey LIKE 'OmniStudio%' AND Status = 'Active'";
56+
const result = await this.connection.query<{ total: string }>(query);
57+
58+
// Salesforce returns records as an array in result.records
59+
if (result?.records && result?.records?.length > 0) {
60+
// Since we only get one record with the total count, check if count > 0
61+
const totalCount = Number(result.records[0].total);
62+
if (totalCount > 0) {
63+
return true;
64+
}
65+
}
66+
67+
Logger.error(this.messages.getMessage('noOmniStudioLicenses'));
68+
return false;
69+
} catch (error) {
70+
if (error instanceof Error && error.message) {
71+
Logger.error(`Error validating OmniStudio licenses: ${error.message}`);
72+
} else {
73+
Logger.error('Error validating OmniStudio licenses: Unknown error');
74+
}
75+
return false;
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)