diff --git a/src/migration/flexcard.ts b/src/migration/flexcard.ts index a3693fb..eaeb0b8 100644 --- a/src/migration/flexcard.ts +++ b/src/migration/flexcard.ts @@ -1,7 +1,7 @@ /* eslint-disable */ import { AnyJson } from '@salesforce/ts-types'; import CardMappings from '../mappings/VlocityCard'; -import { DebugTimer, QueryTools } from '../utils'; +import { DebugTimer, QueryTools, SortDirection } from '../utils'; import { NetUtils } from '../utils/net'; import { BaseMigrationTool } from './base'; import { MigrationResult, MigrationTool, ObjectMapping, UploadRecordResult } from './interfaces'; @@ -92,20 +92,32 @@ export class CardMigrationTool extends BaseMigrationTool implements MigrationToo // Query all cards that are active private async getAllActiveCards(): Promise { DebugTimer.getInstance().lap('Query Vlocity Cards'); - // const filterStr: string = ` Where ${this.namespacePrefix}Active__c = true` const filters = new Map(); - if (!this.allVersions) { - filters.set(this.namespacePrefix + 'Active__c', true); - } filters.set(this.namespacePrefix + 'CardType__c', 'flex'); - return await QueryTools.queryWithFilter( - this.connection, - this.namespace, - CardMigrationTool.VLOCITYCARD_NAME, - this.getCardFields(), - filters - ); + if (this.allVersions) { + const sortFields = [ + { field: 'Name', direction: SortDirection.ASC }, + { field: this.namespacePrefix + 'Version__c', direction: SortDirection.ASC }, + ]; + return await QueryTools.queryWithFilterAndSort( + this.connection, + this.namespace, + CardMigrationTool.VLOCITYCARD_NAME, + this.getCardFields(), + filters, + sortFields + ); + } else { + filters.set(this.namespacePrefix + 'Active__c', true); + return await QueryTools.queryWithFilter( + this.connection, + this.namespace, + CardMigrationTool.VLOCITYCARD_NAME, + this.getCardFields(), + filters + ); + } } // Upload All the VlocityCard__c records to OmniUiCard diff --git a/src/migration/omniscript.ts b/src/migration/omniscript.ts index 25d30fd..490285e 100644 --- a/src/migration/omniscript.ts +++ b/src/migration/omniscript.ts @@ -375,9 +375,6 @@ export class OmniScriptMigrationTool extends BaseMigrationTool implements Migrat DebugTimer.getInstance().lap('Query OmniScripts'); this.logger.info('allVersions : ' + this.allVersions); const filters = new Map(); - if (!this.allVersions) { - filters.set(this.namespacePrefix + 'IsActive__c', true); - } if (this.exportType === OmniScriptExportType.IP) { filters.set(this.namespacePrefix + 'IsProcedure__c', true); @@ -385,13 +382,30 @@ export class OmniScriptMigrationTool extends BaseMigrationTool implements Migrat filters.set(this.namespacePrefix + 'IsProcedure__c', false); } - return await QueryTools.queryWithFilter( - this.connection, - this.namespace, - OmniScriptMigrationTool.OMNISCRIPT_NAME, - this.getOmniScriptFields(), - filters - ); + if (this.allVersions) { + const sortFields = [ + { field: this.namespacePrefix + 'Type__c', direction: SortDirection.ASC }, + { field: this.namespacePrefix + 'SubType__c', direction: SortDirection.ASC }, + { field: this.namespacePrefix + 'Version__c', direction: SortDirection.ASC }, + ]; + return await QueryTools.queryWithFilterAndSort( + this.connection, + this.namespace, + OmniScriptMigrationTool.OMNISCRIPT_NAME, + this.getOmniScriptFields(), + filters, + sortFields + ); + } else { + filters.set(this.namespacePrefix + 'IsActive__c', true); + return await QueryTools.queryWithFilter( + this.connection, + this.namespace, + OmniScriptMigrationTool.OMNISCRIPT_NAME, + this.getOmniScriptFields(), + filters + ); + } } // Get All Elements w.r.t OmniScript__c i.e Elements tagged to passed in IP/OS diff --git a/src/utils/query/index.ts b/src/utils/query/index.ts index 40ee91c..e769b9f 100644 --- a/src/utils/query/index.ts +++ b/src/utils/query/index.ts @@ -3,179 +3,233 @@ import { AnyJson } from '@salesforce/ts-types'; /* eslint-disable */ export class QueryTools { + public static buildCustomObjectQuery(namespace: string, name: string, fields: string[], filters?: Map) { + const queryFields = this.buildCustomObjectFields(namespace, ['Id', ...fields]); - public static buildCustomObjectQuery(namespace: string, name: string, fields: string[], filters?: Map) { - const queryFields = this.buildCustomObjectFields(namespace, ['Id', ...fields]); + let query = 'SELECT ' + queryFields.join(', ') + ' FROM ' + namespace + '__' + name; - let query = 'SELECT ' + queryFields.join(', ') + ' FROM ' + namespace + '__' + name; + const andFilters = []; + if (filters && filters.size > 0) { + for (let filter of filters.keys()) { + andFilters.push(`${filter} = ${QueryTools.getFilterValue(filters.get(filter))}`); + } - const andFilters = []; - if (filters && filters.size > 0) { - for (let filter of filters.keys()) { - andFilters.push(`${filter} = ${QueryTools.getFilterValue(filters.get(filter))}`); - } - - query += ' WHERE ' + andFilters.join(' AND '); - } - - return query; + query += ' WHERE ' + andFilters.join(' AND '); } - public static buildCustomObjectFields(namespace: string, fields: string[]): string[] { - const queryFields = []; - fields.forEach(field => { - if (field.indexOf('__') > -1) { - queryFields.push(namespace + '__' + field); - } else { - queryFields.push(field); - } + return query; + } + + public static buildCustomObjectFields(namespace: string, fields: string[]): string[] { + const queryFields = []; + fields.forEach((field) => { + if (field.indexOf('__') > -1) { + queryFields.push(namespace + '__' + field); + } else { + queryFields.push(field); + } + }); + + return queryFields; + } + + public static async queryAll( + connection: Connection, + namespace: string, + objectName: string, + fields: string[] + ): Promise { + let allrecords = []; + + const query = QueryTools.buildCustomObjectQuery(namespace, objectName, fields); + + // Execute the query + let results = await connection.query(query); + + if (results && results.totalSize > 0) { + allrecords = results.records; + + // Load more pages + while (results.nextRecordsUrl) { + results = await connection.queryMore(results.nextRecordsUrl); + results.records.forEach((row) => { + allrecords.push(row); }); - - return queryFields; + } } - public static async queryAll(connection: Connection, namespace: string, objectName: string, fields: string[]): Promise { - let allrecords = []; + return allrecords; + } - const query = QueryTools.buildCustomObjectQuery(namespace, objectName, fields); + public static async queryWithFilter( + connection: Connection, + namespace: string, + objectName: string, + fields: string[], + filters?: Map + ): Promise { + let allrecords = []; - // Execute the query - let results = await connection.query(query); + const query = QueryTools.buildCustomObjectQuery(namespace, objectName, fields, filters); - if (results && results.totalSize > 0) { - allrecords = results.records; + // Execute the query + let results = await connection.query(query); - // Load more pages - while (results.nextRecordsUrl) { - results = await connection.queryMore(results.nextRecordsUrl); - results.records.forEach(row => { - allrecords.push(row); - }) - } + if (results && results.totalSize > 0) { + allrecords = results.records; - } + // Load more pages + while (results.nextRecordsUrl) { + results = await connection.queryMore(results.nextRecordsUrl); + results.records.forEach((row) => { + allrecords.push(row); + }); + } + } - return allrecords; + return allrecords; + } + + public static async queryWithFilterAndSort( + connection: Connection, + namespace: string, + objectName: string, + fields: string[], + filters?: Map, + orderBy?: Array + ): Promise { + let allrecords = []; + + let query = QueryTools.buildCustomObjectQuery(namespace, objectName, fields, filters); + if (orderBy && orderBy.length > 0) { + const sortings = []; + for (let ob of orderBy) { + sortings.push(ob.field + ' ' + ob.direction); + } + query += ' ORDER BY ' + sortings.join(', '); } - public static async queryWithFilter(connection: Connection, namespace: string, objectName: string, fields: string[], filters?: Map): Promise { - let allrecords = []; + // Execute the query + let results = await connection.query(query); - const query = QueryTools.buildCustomObjectQuery(namespace, objectName, fields, filters); + if (results && results.totalSize > 0) { + allrecords = results.records; - // Execute the query - let results = await connection.query(query); + // Load more pages + while (results.nextRecordsUrl) { + results = await connection.queryMore(results.nextRecordsUrl); + results.records.forEach((row) => { + allrecords.push(row); + }); + } + } - if (results && results.totalSize > 0) { - allrecords = results.records; + return allrecords; + } + + public static async query( + connection: Connection, + objectName: string, + fields: string[], + filters?: Map, + orderBy?: Array + ) { + let query = 'SELECT ' + fields.join(', ') + ' FROM ' + objectName; + + const andFilters = []; + if (filters && filters.size > 0) { + for (let filter of filters.keys()) { + andFilters.push(`${filter} = ${QueryTools.getFilterValue(filters.get(filter))}`); + } + + query += ' WHERE ' + andFilters.join(' AND '); + } - // Load more pages - while (results.nextRecordsUrl) { - results = await connection.queryMore(results.nextRecordsUrl); - results.records.forEach(row => { - allrecords.push(row); - }) - } + if (orderBy && orderBy.length > 0) { + const sortings = []; + for (let ob of orderBy) { + sortings.push(ob.field + ' ' + ob.direction); + } + query += ' ORDER BY ' + sortings.join(', '); + } - } + // Execute the query + let results = await connection.query(query); - return allrecords; - } + let allrecords = []; + if (results && results.totalSize > 0) { + allrecords = results.records; - public static async query(connection: Connection, objectName: string, fields: string[], filters?: Map, orderBy?: Array) { - let query = 'SELECT ' + fields.join(', ') + ' FROM ' + objectName; - - const andFilters = []; - if (filters && filters.size > 0) { - for (let filter of filters.keys()) { - andFilters.push(`${filter} = ${QueryTools.getFilterValue(filters.get(filter))}`); - } - - query += ' WHERE ' + andFilters.join(' AND '); - } - - if (orderBy && orderBy.length > 0) { - const sortings = []; - for (let ob of orderBy) { - sortings.push(ob.field + ' ' + ob.direction); - } - query += ' ORDER BY ' + sortings.join(', '); - } - - // Execute the query - let results = await connection.query(query); - - let allrecords = []; - if (results && results.totalSize > 0) { - allrecords = results.records; - - // Load more pages - while (results.nextRecordsUrl) { - results = await connection.queryMore(results.nextRecordsUrl); - results.records.forEach(row => { - allrecords.push(row); - }); - } - } - - return allrecords; + // Load more pages + while (results.nextRecordsUrl) { + results = await connection.queryMore(results.nextRecordsUrl); + results.records.forEach((row) => { + allrecords.push(row); + }); + } } - public static async queryIds(connection: Connection, objectName: string, filters?: Map): Promise { - let allrecords = []; - const andFilters = []; - - let query = `SELECT ID FROM ${objectName}`; + return allrecords; + } - if (filters && filters.size > 0) { - for (let filter of filters.keys()) { - andFilters.push(`${filter} = ${QueryTools.getFilterValue(filters.get(filter))}`); - } + public static async queryIds( + connection: Connection, + objectName: string, + filters?: Map + ): Promise { + let allrecords = []; + const andFilters = []; - query += ' WHERE ' + andFilters.join(' AND '); - } + let query = `SELECT ID FROM ${objectName}`; - // Execute the query - let results = await connection.query(query); + if (filters && filters.size > 0) { + for (let filter of filters.keys()) { + andFilters.push(`${filter} = ${QueryTools.getFilterValue(filters.get(filter))}`); + } - if (results && results.totalSize > 0) { - allrecords = results.records; + query += ' WHERE ' + andFilters.join(' AND '); + } - // Load more pages - while (results.nextRecordsUrl) { - results = await connection.queryMore(results.nextRecordsUrl); - results.records.forEach(row => { - allrecords.push(row); - }) - } + // Execute the query + let results = await connection.query(query); - } + if (results && results.totalSize > 0) { + allrecords = results.records; - return allrecords.map(record => record['Id']); + // Load more pages + while (results.nextRecordsUrl) { + results = await connection.queryMore(results.nextRecordsUrl); + results.records.forEach((row) => { + allrecords.push(row); + }); + } } - private static getFilterValue(val: any): string { - switch (typeof val) { - case "bigint": - case "boolean": - case "number": - return `${val}`; - case "function": - return `'${val()}'`; - case "undefined": - return 'NULL'; - case "string": - default: - return `'${val}'`; - } + return allrecords.map((record) => record['Id']); + } + + private static getFilterValue(val: any): string { + switch (typeof val) { + case 'bigint': + case 'boolean': + case 'number': + return `${val}`; + case 'function': + return `'${val()}'`; + case 'undefined': + return 'NULL'; + case 'string': + default: + return `'${val}'`; } + } } export enum SortDirection { - ASC = 'asc', - DESC = 'desc' + ASC = 'asc', + DESC = 'desc', } export interface SortField { - field: string; - direction: SortDirection -} \ No newline at end of file + field: string; + direction: SortDirection; +}