22import { AnyJson } from '@salesforce/ts-types' ;
33import DRBundleMappings from '../mappings/DRBundle' ;
44import DRMapItemMappings from '../mappings/DRMapItem' ;
5- import { DebugTimer , QueryTools } from '../utils' ;
5+ import { DebugTimer , oldNew , QueryTools } from '../utils' ;
66import { NetUtils } from '../utils/net' ;
77import { BaseMigrationTool } from './base' ;
88import { MigrationResult , MigrationTool , ObjectMapping , TransformData , UploadRecordResult } from './interfaces' ;
99import { DataRaptorAssessmentInfo } from '../../src/utils' ;
1010
11- import { getAllFunctionMetadata , getReplacedString } from '../utils/formula/FormulaUtil' ;
11+ import {
12+ getAllFunctionMetadata ,
13+ getReplacedString ,
14+ populateRegexForFunctionMetadata ,
15+ } from '../utils/formula/FormulaUtil' ;
16+ import { StringVal } from '../utils/StringValue/stringval' ;
1217
1318export class DataRaptorMigrationTool extends BaseMigrationTool implements MigrationTool {
1419 static readonly DRBUNDLE_NAME = 'DRBundle__c' ;
@@ -183,10 +188,25 @@ export class DataRaptorMigrationTool extends BaseMigrationTool implements Migrat
183188 } ;
184189 }
185190
191+ private async getAllDRToItemsMap ( ) : Promise < Map < string , AnyJson [ ] > > {
192+ const drToItemsMap = new Map < string , AnyJson [ ] > ( ) ;
193+ const drItems = await this . getAllItems ( ) ;
194+ for ( const drItem of drItems ) {
195+ const drName = drItem [ 'Name' ] ;
196+ if ( drToItemsMap . has ( drName ) ) {
197+ drToItemsMap . get ( drName ) . push ( drItem ) ;
198+ } else {
199+ drToItemsMap . set ( drName , [ drItem ] ) ;
200+ }
201+ }
202+ return drToItemsMap ;
203+ }
204+
186205 public async assess ( ) : Promise < DataRaptorAssessmentInfo [ ] > {
187206 try {
188207 DebugTimer . getInstance ( ) . lap ( 'Query data raptors' ) ;
189208 const dataRaptors = await this . getAllDataRaptors ( ) ;
209+
190210 const dataRaptorAssessmentInfos = this . processDRComponents ( dataRaptors ) ;
191211 /* this.ux.log('dataRaptorAssessmentInfos');
192212 this.ux.log(dataRaptorAssessmentInfos.toString()); */
@@ -201,40 +221,73 @@ export class DataRaptorMigrationTool extends BaseMigrationTool implements Migrat
201221 const dataRaptorAssessmentInfos : DataRaptorAssessmentInfo [ ] = [ ] ;
202222 // Query all the functionMetadata with all required fields
203223 const functionDefinitionMetadata = await getAllFunctionMetadata ( this . namespace , this . connection ) ;
224+ populateRegexForFunctionMetadata ( functionDefinitionMetadata ) ;
225+ const existingDataRaptorNames = new Set < string > ( ) ;
226+ const dataRaptorItemsMap = await this . getAllDRToItemsMap ( ) ;
204227
205228 // Now process each OmniScript and its elements
206229 for ( const dataRaptor of dataRaptors ) {
230+ if ( dataRaptor [ this . namespacePrefix + 'Type__c' ] === 'Migration' ) continue ;
231+ const drName = dataRaptor [ 'Name' ] ;
207232 // Await here since processOSComponents is now async
208- this . ux . log ( dataRaptor [ 'Name' ] ) ;
209- this . ux . log ( dataRaptor [ this . namespacePrefix + 'Formula__c' ] ) ;
210- var customFunctionString = '' ;
211- if ( dataRaptor [ this . namespacePrefix + 'Formula__c' ] != null ) {
212- this . ux . log ( 'formula' ) ;
213- customFunctionString =
214- 'Original Formula :' + dataRaptor [ this . namespacePrefix + 'Formula__c' ] + '\n\n' + 'Replaced Formula :Formula' ;
215- try {
216- customFunctionString +=
217- 'Replaced Formula :Formula \n\n' +
218- getReplacedString (
219- this . namespacePrefix ,
220- dataRaptor [ this . namespacePrefix + 'Formula__c' ] ,
221- functionDefinitionMetadata
222- ) ;
223- } catch ( ex ) {
224- this . logger . error ( JSON . stringify ( ex ) ) ;
225- console . log (
226- "There was some problem while updating the formula syntax, please check the all the formula's syntax once : " +
227- dataRaptor [ this . namespacePrefix + 'Formula__c' ]
228- ) ;
229- }
233+ this . ux . log ( drName ) ;
234+ const warnings : string [ ] = [ ] ;
235+ const existingDRNameVal = new StringVal ( drName , 'name' ) ;
236+
237+ if ( ! existingDRNameVal . isNameCleaned ( ) ) {
238+ warnings . push (
239+ this . messages . getMessage ( 'changeMessage' , [
240+ existingDRNameVal . type ,
241+ existingDRNameVal . val ,
242+ existingDRNameVal . cleanName ( ) ,
243+ ] )
244+ ) ;
245+ }
246+ if ( existingDataRaptorNames . has ( existingDRNameVal . cleanName ( ) ) ) {
247+ warnings . push ( this . messages . getMessage ( 'duplicatedName' ) + ' ' + existingDRNameVal . cleanName ( ) ) ;
248+ } else {
249+ existingDataRaptorNames . add ( existingDRNameVal . cleanName ( ) ) ;
250+ }
251+ const apexDependencies = [ ] ;
252+ if ( dataRaptor [ this . namespacePrefix + 'CustomInputClass__c' ] ) {
253+ apexDependencies . push ( dataRaptor [ this . namespacePrefix + 'CustomInputClass__c' ] ) ;
254+ }
255+ if ( dataRaptor [ this . namespacePrefix + 'CustomOutputClass__c' ] ) {
256+ apexDependencies . push ( dataRaptor [ this . namespacePrefix + 'CustomOutputClass__c' ] ) ;
230257 }
231258
259+ const formulaChanges : oldNew [ ] = [ ] ;
260+ const drItems = dataRaptorItemsMap . get ( drName ) ;
261+ if ( drItems ) {
262+ for ( const drItem of drItems ) {
263+ // this.ux.log(dataRaptor[this.namespacePrefix + 'Formula__c']);
264+ const formula = drItem [ this . namespacePrefix + 'Formula__c' ] ;
265+ if ( formula ) {
266+ try {
267+ const newFormula = getReplacedString ( this . namespacePrefix , formula , functionDefinitionMetadata ) ;
268+ if ( newFormula !== formula ) {
269+ formulaChanges . push ( {
270+ old : formula ,
271+ new : newFormula ,
272+ } ) ;
273+ }
274+ } catch ( ex ) {
275+ this . logger . error ( JSON . stringify ( ex ) ) ;
276+ console . log (
277+ "There was some problem while updating the formula syntax, please check the all the formula's syntax once : " +
278+ formula
279+ ) ;
280+ }
281+ }
282+ }
283+ }
232284 const dataRaptorAssessmentInfo : DataRaptorAssessmentInfo = {
233- name : dataRaptor [ 'Name' ] ,
234- customFunction : customFunctionString ,
285+ name : existingDRNameVal . val ,
235286 id : dataRaptor [ 'Id' ] ,
287+ formulaChanges : formulaChanges ,
236288 infos : [ ] ,
237- warnings : [ ] ,
289+ apexDependencies : apexDependencies ,
290+ warnings : warnings ,
238291 } ;
239292 dataRaptorAssessmentInfos . push ( dataRaptorAssessmentInfo ) ;
240293 }
@@ -263,6 +316,20 @@ export class DataRaptorMigrationTool extends BaseMigrationTool implements Migrat
263316 ) ;
264317 }
265318
319+ /*
320+ private async getAllItemsForDataRaptorByName(drName: string): Promise<AnyJson[]> {
321+ const filters = new Map<string, any>();
322+ //Query all Elements
323+ return await QueryTools.queryWithFilter(
324+ this.connection,
325+ this.namespace,
326+ DataRaptorMigrationTool.DRMAPITEM_NAME,
327+ this.getDRMapItemFields(),
328+ filters.set('Name', drName)
329+ );
330+ }
331+ */
332+
266333 // Get All Items for one DataRaptor
267334 private async getItemsForDataRaptor (
268335 dataRaptorItems : AnyJson [ ] ,
0 commit comments