-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathEvent.js
1086 lines (1024 loc) · 47.6 KB
/
Event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
import MetadataType from './MetadataType.js';
import { Util } from '../util/util.js';
import File from '../util/file.js';
import cache from '../util/cache.js';
import deepEqual from 'deep-equal';
import pLimit from 'p-limit';
/**
* @typedef {import('../../types/mcdev.d.js').BuObject} BuObject
* @typedef {import('../../types/mcdev.d.js').CodeExtract} CodeExtract
* @typedef {import('../../types/mcdev.d.js').CodeExtractItem} CodeExtractItem
* @typedef {import('../../types/mcdev.d.js').MetadataTypeItem} MetadataTypeItem
* @typedef {import('../../types/mcdev.d.js').MetadataTypeItemDiff} MetadataTypeItemDiff
* @typedef {import('../../types/mcdev.d.js').MetadataTypeItemObj} MetadataTypeItemObj
* @typedef {import('../../types/mcdev.d.js').MetadataTypeMap} MetadataTypeMap
* @typedef {import('../../types/mcdev.d.js').MetadataTypeMapObj} MetadataTypeMapObj
* @typedef {import('../../types/mcdev.d.js').SoapRequestParams} SoapRequestParams
* @typedef {import('../../types/mcdev.d.js').TemplateMap} TemplateMap
*
* @typedef {import('../../types/mcdev.d.js').ReferenceObject} ReferenceObject
* @typedef {import('../../types/mcdev.d.js').SfObjectField} SfObjectField
* @typedef {import('../../types/mcdev.d.js').configurationArguments} configurationArguments
* @typedef {import('../../types/mcdev.d.js').Conditions} Conditions
*/
/**
* Event MetadataType
*
* @augments MetadataType
*/
class Event extends MetadataType {
static reCacheDataExtensions = [];
/**
* Retrieves Metadata of Event Definition.
* Endpoint /interaction/v1/eventDefinitions return all Event Definitions with all details.
* Currently it is not needed to loop over Imports with endpoint /interaction/v1/eventDefinitions/{id}
*
* @param {string} retrieveDir Directory where retrieved metadata directory will be saved
* @param {void | string[]} [_] unused parameter
* @param {void | string[]} [__] unused parameter
* @param {string} [key] customer key of single item to retrieve
* @returns {Promise.<MetadataTypeMapObj>} Promise of metadata
*/
static async retrieve(retrieveDir, _, __, key) {
Util.logBeta(this.definition.type);
try {
return await super.retrieveREST(
retrieveDir,
`/interaction/v1/eventDefinitions${
key ? '/key:' + encodeURIComponent(key) : ''
}?extras=all`,
null,
key
);
} catch (ex) {
// if the event does not exist, the API returns the error "Request failed with status code 400 (ERR_BAD_REQUEST)" which would otherwise bring execution to a hold
if (key && ex.code === 'ERR_BAD_REQUEST') {
Util.logger.info(
`Downloaded: ${this.definition.type} (0)${Util.getKeysString(key)}`
);
} else {
throw ex;
}
}
return;
}
/**
* Retrieves event definition metadata for caching
*
* @returns {Promise.<MetadataTypeMapObj>} Promise of metadata
*/
static retrieveForCache() {
return this.retrieve(null);
}
/**
* Retrieve a specific Event Definition by Name
*
* @param {string} templateDir Directory where retrieved metadata directory will be saved
* @param {string} name name of the metadata file
* @param {TemplateMap} templateVariables variables to be replaced in the metadata
* @returns {Promise.<MetadataTypeItemObj>} Promise of metadata
*/
static async retrieveAsTemplate(templateDir, name, templateVariables) {
Util.logBeta(this.definition.type);
const res = await this.client.rest.get(
'/interaction/v1/eventDefinitions?name=' + encodeURIComponent(name)
);
const event = res.items.filter((item) => item.name === name);
try {
if (!event || event.length === 0) {
throw new Error(`No Event Definitions Found with name "${name}"`);
} else if (event.length > 1) {
throw new Error(
`Multiple Event Definitions with name "${name}"` +
`please rename to be unique to avoid issues`
);
} else if (event?.length === 1) {
const originalKey = event[0][this.definition.keyField];
const metadataItemTemplated = Util.replaceByObject(
await this.postRetrieveTasks(event[0]),
templateVariables
);
if (!metadataItemTemplated.r__dataExtension_key) {
throw new Error(
`Event.postRetrieveTasks:: No Data Extension found for ${this.definition.type}: ${metadataItemTemplated.name}. This cannot be templated.`
);
}
// remove all fields listed in Definition for templating
this.keepTemplateFields(metadataItemTemplated);
await File.writeJSONToFile(
[templateDir, this.definition.type].join('/'),
originalKey + '.' + this.definition.type + '-meta',
metadataItemTemplated
);
Util.logger.info(` - templated ${this.definition.type}: ${name}`);
return { metadata: metadataItemTemplated, type: this.definition.type };
} else {
throw new Error(
`Encountered unknown error when retrieveing ${
this.definition.typeName
} "${name}": ${JSON.stringify(res.body)}`
);
}
} catch (ex) {
Util.logger.error('Event.retrieveAsTemplate:: ' + ex);
return null;
}
}
/**
* Delete a metadata item from the specified business unit
*
* @param {string} key Identifier of item
* @returns {Promise.<boolean>} deletion success status
*/
static deleteByKey(key) {
Util.logBeta(this.definition.type);
return super.deleteByKeyREST(
'/interaction/v1/eventDefinitions/key:' + encodeURIComponent(key),
key,
false
);
}
/**
* Deploys metadata - merely kept here to be able to print {@link Util.logBeta} once per deploy
*
* @param {MetadataTypeMap} metadata metadata mapped by their keyField
* @param {string} deployDir directory where deploy metadata are saved
* @param {string} retrieveDir directory where metadata after deploy should be saved
* @returns {Promise.<MetadataTypeMap>} Promise of keyField => metadata map
*/
static async deploy(metadata, deployDir, retrieveDir) {
Util.logBeta(this.definition.type);
return super.deploy(metadata, deployDir, retrieveDir);
}
/**
* Creates a single Event Definition
*
* @param {MetadataTypeItem} metadata a single Event Definition
* @returns {Promise} Promise
*/
static create(metadata) {
return super.createREST(metadata, '/interaction/v1/eventDefinitions/');
}
/**
* Updates a single Event Definition (using PUT method since PATCH isn't supported)
*
* @param {MetadataTypeItem} metadataEntry a single Event Definition
* @returns {Promise} Promise
*/
static async update(metadataEntry) {
return super.updateREST(
metadataEntry,
'/interaction/v1/eventDefinitions/key:' +
encodeURIComponent(metadataEntry[this.definition.keyField]),
'put'
);
}
/**
* prepares an event definition for deployment
*
* @param {MetadataTypeItem} metadata a single eventDefinition
* @returns {Promise.<MetadataTypeItem>} parsed version
*/
static async preDeployTasks(metadata) {
// Note: lots has to be done in createOrUpdate based on what action is required
metadata.arguments ||= {};
metadata.arguments.eventDefinitionKey = metadata.eventDefinitionKey;
// standard values
metadata.isVisibleInPicker ||= false;
if (metadata.isVisibleInPicker && !metadata.sourceApplicationExtensionId) {
Util.logger.warn(
` - ${this.definition.type} ${metadata[this.definition.keyField]}: isVisibleInPicker is true but sourceApplicationExtensionId is missing. Setting isVisibleInPicker to false.`
);
metadata.isVisibleInPicker = false;
}
metadata.isPlatformObject ||= false;
metadata.mode ||= 'Production';
switch (metadata.type) {
case 'APIEvent': {
metadata.entrySourceGroupConfigUrl ||=
'jb:///data/entry/api-event/entrysourcegroupconfig.json';
metadata.iconUrl ||= '/images/icon_journeyBuilder-event-api-blue.svg';
break;
}
case 'SalesforceObjectTriggerV2': {
metadata.iconUrl ||= '/jbint-events/events/SalesforceData/images/SF-Event-Icon.svg';
break;
}
}
await this.preDeployTasks_SalesforceEntryEvents(
metadata.type,
metadata.configurationArguments
);
return metadata;
}
/**
* helper for {@link MetadataType.upsert}
*
* @param {MetadataTypeMap} metadataMap list of metadata
* @param {string} metadataKey key of item we are looking at
* @param {boolean} hasError error flag from previous code
* @param {MetadataTypeItemDiff[]} metadataToUpdate list of items to update
* @param {MetadataTypeItem[]} metadataToCreate list of items to create
* @returns {Promise.<'create'|'update'|'skip'>} action to take
*/
static async createOrUpdate(
metadataMap,
metadataKey,
hasError,
metadataToUpdate,
metadataToCreate
) {
const createOrUpdateAction = await super.createOrUpdate(
metadataMap,
metadataKey,
hasError,
metadataToUpdate,
metadataToCreate
);
const metadataItem = metadataMap[metadataKey];
if (createOrUpdateAction === 'update') {
if (metadataItem.r__dataExtension_key) {
metadataItem.dataExtensionId = cache.searchForField(
'dataExtension',
metadataItem.r__dataExtension_key,
'CustomerKey',
'ObjectID'
);
metadataItem.dataExtensionName = cache.searchForField(
'dataExtension',
metadataItem.r__dataExtension_key,
'CustomerKey',
'Name'
);
metadataItem.arguments.dataExtensionId = metadataItem.dataExtensionId;
if (metadataItem.schema) {
metadataItem.schema.id = metadataItem.dataExtensionId;
metadataItem.schema.name = metadataItem.dataExtensionName;
}
}
if (metadataItem.schema?.fields?.length) {
const normalizedKey = File.reverseFilterIllegalFilenames(
metadataMap[metadataKey][this.definition.keyField]
);
const cachedVersion = cache.getByKey(this.definition.type, normalizedKey);
if (cachedVersion?.schema?.fields?.length) {
const cacheClone = structuredClone(cachedVersion);
cacheClone.schema.fields = cacheClone.schema.fields.map((field) => {
delete field.isDevicePreference;
return field;
});
if (!deepEqual(metadataItem?.schema?.fields, cacheClone?.schema?.fields)) {
Util.logger.warn(
` - ${this.definition.type} ${metadataItem[this.definition.keyField]}: schema fields differ from server version. Resetting as this will not be reflected on dataExtension.`
);
metadataItem.schema.fields = cacheClone.schema.fields;
}
}
}
} else if (createOrUpdateAction === 'create') {
try {
if (metadataItem.r__dataExtension_key) {
metadataItem.dataExtensionId = cache.searchForField(
'dataExtension',
metadataItem.r__dataExtension_key,
'CustomerKey',
'ObjectID'
);
metadataItem.dataExtensionName = cache.searchForField(
'dataExtension',
metadataItem.r__dataExtension_key,
'CustomerKey',
'Name'
);
if (metadataItem.schema) {
delete metadataItem.schema;
Util.logger.info(
` - ${this.definition.type} ${metadataItem[this.definition.keyField]}: dataExtension ${metadataItem.r__dataExtension_key} found, ignoring schema-section in ${this.definition.type} json`
);
}
}
} catch {
// no action
}
if (metadataItem.schema) {
if (metadataItem.r__dataExtension_key) {
metadataItem.schema.name = metadataItem.r__dataExtension_key;
}
Util.logger.warn(
`Data Extension ${metadataItem.schema.name || metadataItem[this.definition.keyField]} not found on BU. Creating it automatically based on schema-definition.`
);
// we want the event api to create the DE for us based on the schema
this.reCacheDataExtensions.push({
eventKey: metadataItem[this.definition.keyField],
deKey: metadataItem.schema.name || metadataItem[this.definition.keyField],
});
}
}
return createOrUpdateAction;
}
/**
* Gets executed after deployment of metadata type
*
* @param {MetadataTypeMap} upsertResults metadata mapped by their keyField as returned by update/create
* @param {MetadataTypeMap} originalMetadata metadata to be updated (contains additioanl fields)
* @param {{created: number, updated: number}} createdUpdated counter representing successful creates/updates
* @returns {Promise.<void>} -
*/
static async postDeployTasks(upsertResults, originalMetadata, createdUpdated) {
// CREATE ONLY - if dataExtensions were auto-
if (this.reCacheDataExtensions.length && createdUpdated.created > 0) {
Util.logger.warn(' - Re-caching dependent Metadata: dataExtension');
const deRetrieve = await DataExtension.retrieveForCache();
cache.setMetadata('dataExtension', deRetrieve.metadata);
const reDownloadDeKeys = [];
// try to update key & name of the auto-generated dataExtension
for (const { eventKey, deKey } of this.reCacheDataExtensions) {
if (!upsertResults[eventKey]) {
continue;
}
const eventItem = upsertResults[eventKey];
const newDeKey = cache.searchForField(
'dataExtension',
eventItem.dataExtensionId,
'ObjectID',
'CustomerKey'
);
// get dataExtension from cache which conveniently already has the ObjectID set
const deObj = cache.getByKey('dataExtension', newDeKey);
const oldName = deObj[DataExtension.definition.nameField];
// prepare a clone of the DE to update name & key to match the event
const clone = structuredClone(deObj);
clone[DataExtension.definition.keyField] = deKey;
clone[DataExtension.definition.nameField] = deKey;
try {
// update DE on server
await DataExtension.update(clone, true);
Util.logger.info(
` - changed dataExtension ${newDeKey} (${oldName}) key/name to ${deKey}`
);
// update cache
deObj[DataExtension.definition.keyField] = deKey;
deObj[DataExtension.definition.nameField] = deKey;
reDownloadDeKeys.push(deObj[DataExtension.definition.keyField]);
} catch {
// fallback, set DE key to value of DE name
const clone = structuredClone(deObj);
clone[DataExtension.definition.keyField] = oldName;
try {
// update DE on server
await DataExtension.update(clone, true);
Util.logger.info(
` - changed dataExtension ${newDeKey} (${oldName}) key to ${oldName}`
);
// update cache
deObj[DataExtension.definition.keyField] =
deObj[DataExtension.definition.nameField];
reDownloadDeKeys.push(deObj[DataExtension.definition.keyField]);
} catch {
Util.logger.debug(
` - failed to change dataExtension ${newDeKey} (${oldName}) key/name`
);
}
}
}
this.reCacheDataExtensions.length = 0;
// ensure we have downloaded auto-created DEs
if (reDownloadDeKeys.length) {
const retriever = new Retriever(this.properties, this.buObject);
await retriever.retrieve(['dataExtension'], reDownloadDeKeys);
}
}
}
/**
* parses retrieved Metadata before saving
*
* @param {MetadataTypeItem} metadata a single event definition
* @returns {Promise.<MetadataTypeItem>} parsed metadata
*/
static async postRetrieveTasks(metadata) {
try {
metadata.createdBy = cache.searchForField(
'user',
metadata.createdBy,
'AccountUserID',
'Name'
);
} catch (ex) {
Util.logger.verbose(
` - ${this.definition.type} ${metadata[this.definition.nameField]} (${
metadata[this.definition.keyField]
}): ${ex.message}.`
);
}
try {
metadata.modifiedBy = cache.searchForField(
'user',
metadata.modifiedBy,
'AccountUserID',
'Name'
);
} catch (ex) {
Util.logger.verbose(
` - ${this.definition.type} ${metadata[this.definition.nameField]} (${
metadata[this.definition.keyField]
}): ${ex.message}.`
);
}
try {
metadata.r__dataExtension_key = cache.searchForField(
'dataExtension',
metadata.dataExtensionId,
'ObjectID',
'CustomerKey'
);
delete metadata.dataExtensionId;
delete metadata.dataExtensionName;
delete metadata.arguments.dataExtensionId;
if (metadata.schema) {
delete metadata.schema.id;
}
} catch (ex) {
Util.logger.verbose(
` - ${this.definition.type} ${metadata[this.definition.nameField]} (${
metadata[this.definition.keyField]
}): ${ex.message}.`
);
}
if (!metadata.isPlatformObject) {
delete metadata.isPlatformObject;
}
if (metadata.mode === 'Production') {
delete metadata.mode;
}
try {
await this.postRetrieveTasks_SalesforceEntryEvents(
metadata.type,
metadata.configurationArguments,
metadata.eventDefinitionKey,
metadata.publishedInteractionCount >= 1
);
} catch (ex) {
Util.logger.warn(
` - ${this.definition.type} '${metadata[this.definition.nameField]}' (${metadata[this.definition.keyField]}): ${ex.message}`
);
}
return metadata;
}
static sfObjects = {
/** @type {string[]} */
workflowObjects: null,
/** @type {Object.<string, ReferenceObject[]>} object-name > object data */
referencedObjects: {},
/** @type {Object.<string, Object.<string, SfObjectField>>} object-name > field-name > field data */
objectFields: {},
/** @type {Object.<string, Promise.<any>>} */
loadingFields: {},
/** @type {Object.<string, Promise.<any>>} */
loadingRelatedObjects: {},
/** @type {Promise.<any>} */
loadingWorkflowObjects: null,
};
/**
* helper for {@link checkSalesforceEntryEvents} that retrieves information about SF object fields
*
* @param {string} objectAPIName salesforce object api name
*/
static async getSalesforceObjects(objectAPIName) {
if (!objectAPIName) {
return;
}
// 1 get all available Salesforce objects
// similar response to /jbint/getWorkflowObjects
if (!this.sfObjects.workflowObjects) {
if (!this.sfObjects.loadingWorkflowObjects) {
this.sfObjects.loadingWorkflowObjects = this._getWorkflowObjects();
}
await this.sfObjects.loadingWorkflowObjects;
}
// 2 get objects related to the selected object
// same response as /jbint/getRelatedObjects?type=<objectAPIName>
if (!this.sfObjects.referencedObjects?.[objectAPIName]) {
if (!this.sfObjects.loadingRelatedObjects[objectAPIName]) {
this.sfObjects.loadingRelatedObjects[objectAPIName] =
this._getRelatedSfObjects(objectAPIName);
}
await this.sfObjects.loadingRelatedObjects[objectAPIName];
// 3 get fields
const rateLimit = pLimit(20);
const uniqueSfObjectNames = this.sfObjects.referencedObjects[objectAPIName]
? [
...new Set(
Object.values(this.sfObjects.referencedObjects[objectAPIName])
.map((el) => el.referenceObjectName)
.sort()
),
]
: [];
await Promise.all(
uniqueSfObjectNames.map((objectAPIName) =>
rateLimit(async () => {
this.sfObjects.loadingFields[objectAPIName] ||=
this._getSalesforceObjectFields(objectAPIName);
return this.sfObjects.loadingFields[objectAPIName];
})
)
);
// 4 create Common fields
const contactLeadName = 'Contacts and Leads';
if (
this.sfObjects.objectFields['Contact'] &&
this.sfObjects.objectFields['Lead'] &&
!this.sfObjects.workflowObjects.includes(contactLeadName)
) {
Util.logger.info(
Util.getGrayMsg(' - Constructing Common / Contacts and Leads object')
);
// add fake entry to workflowObjects to allow testing for this easily
this.sfObjects.workflowObjects.push(contactLeadName);
// construct fields object for it
this.sfObjects.objectFields[contactLeadName] = {};
const contactFieldNames = Object.keys(this.sfObjects.objectFields['Contact']);
const leadFieldNames = Object.keys(this.sfObjects.objectFields['Lead']);
for (const fieldName of contactFieldNames.filter((item) =>
leadFieldNames.includes(item)
)) {
// copy the value from contact - while thats not perfectly correct it will hopefully be sufficient for what we need to check
this.sfObjects.objectFields[contactLeadName][fieldName] = structuredClone(
this.sfObjects.objectFields['Contact'][fieldName]
);
this.sfObjects.objectFields[contactLeadName][fieldName].objectname = 'Common';
// do not delete fields from Contact or Lead because it depends on the environment where we have to look for those
}
// create duplicate to also reference this via "Common"
this.sfObjects.objectFields['Common'] =
this.sfObjects.objectFields[contactLeadName];
}
}
return;
}
/**
* helper that allows skipping to run this again in multi-key retrieval
*/
static async _getWorkflowObjects() {
Util.logger.info(Util.getGrayMsg(' - Caching Salesforce Objects'));
const workflowObjectsResponse = await this.client.rest.get(
`/data/v1/integration/member/salesforce/workflowobjects`
);
this.sfObjects.workflowObjects = workflowObjectsResponse
? workflowObjectsResponse.map((o) => o.apiname)
: [];
}
/**
* helper that allows skipping to run this again in multi-key retrieval
*
* @param {string} objectAPIName SF entry object of the current event
*/
static async _getRelatedSfObjects(objectAPIName) {
Util.logger.info(
Util.getGrayMsg(' - Caching Related Salesforce Objects for ' + objectAPIName)
);
try {
const referenceObjectsResponse = await this.client.rest.get(
`/data/v1/integration/member/salesforce/object/${objectAPIName}/referenceobjects`
);
// add itself first so that we get the fields for objectAPIName as well
const selfReference = {
referenceObjectName: objectAPIName,
relationshipName: objectAPIName,
};
this.sfObjects.referencedObjects[objectAPIName] = referenceObjectsResponse
? [selfReference, ...referenceObjectsResponse]
: [selfReference];
if (
referenceObjectsResponse.some((el) => el.referenceObjectName === 'Lead') &&
referenceObjectsResponse.some((el) => el.referenceObjectName === 'Contact')
) {
// add fake object "Common" to referenced objects for testing
this.sfObjects.referencedObjects[objectAPIName].push({
displayname: 'Common',
relationshipIdName: 'Id',
relationshipName: 'Common',
isPolymorphic: false,
referenceObjectName: 'Common',
});
}
} catch (ex) {
if (ex.code === 'ERR_BAD_RESPONSE') {
throw new Error(
`Could not find Salesforce entry object ${objectAPIName} on connected org.`
);
}
}
}
/**
* helper that allows skipping to run this again in multi-key retrieval
*
* @param {string} objectAPIName SF object for which to get the fields
*/
static async _getSalesforceObjectFields(objectAPIName) {
if (this.sfObjects.objectFields[objectAPIName] || objectAPIName === 'Common') {
return;
}
Util.logger.verbose(
Util.getGrayMsg(' - Caching Fields for Salesforce Object ' + objectAPIName)
);
const referenceObjectsFieldsResponse = await this.client.rest.get(
`/legacy/v1/beta/integration/member/salesforce/object/${objectAPIName}`
);
if (referenceObjectsFieldsResponse?.sfobjectfields?.length) {
Util.logger.debug(
`Found ${referenceObjectsFieldsResponse?.sfobjectfields?.length} fields for Salesforce Object ${objectAPIName}`
);
this.sfObjects.objectFields[objectAPIName] = {};
// !add default fields that are somehow not always returned by this legacy beta API
for (const field of this.defaultSalesforceFields) {
// @ts-expect-error hack to work around shortcomings of legacy beta API
this.sfObjects.objectFields[objectAPIName][field] = {
label: field,
name: field,
};
}
// add fields returned by API
for (const field of referenceObjectsFieldsResponse.sfobjectfields) {
this.sfObjects.objectFields[objectAPIName][field.name] = field;
}
} else {
Util.logger.warn(
`Could not cache fields for Salesforce Object '${objectAPIName}'. This is likely caused by insufficient access of your MC-Connect integration user. Please check assigned permission sets / the profile.`
);
}
return;
}
static defaultSalesforceFields = [
'Id',
'Name',
'FirstName',
'LastName',
'Phone',
'CreatedById',
'CreatedDate',
'IsDeleted',
'LastModifiedById',
'LastModifiedDate',
'SystemModstamp',
];
/**
*
* @param {configurationArguments} ca trigger[0].configurationArguments
* @param {boolean} isPublished if the current item is published it means we do not need to do contact vs common checks
*/
static checkSalesforceEntryEvents(ca, isPublished) {
// 1 check eventDataConfig
const edcObjects = ca.eventDataConfig.objects.sort((a, b) =>
a.dePrefix.localeCompare(b.dePrefix)
);
const errors = [];
const dePrefixFields = {};
const dePrefixRelationshipMap = {};
const dePrefixReferenceObjectMap = {};
// SFMC only uses "Common" to aggreagate Contacts and Leads if that was actively selected in the entry event. Also, already published journeys/events continue to work even if fields would later be changed, leading to a shift from or to the "common" fake-object.
const checkCommon =
ca.whoToInject === 'Contact ID/Lead ID (Contacts and Leads)' && !isPublished;
for (const object of edcObjects) {
// create secondary object to quickly check eventDataSummary against
dePrefixFields[object.dePrefix] = object.fields;
// if the current object is the entry object then relationshipName and referenceObject are set to empty strings because it's not "referencing" a "relationship" but just listing its own fields
dePrefixRelationshipMap[object.dePrefix] =
object.relationshipName === ''
? object.dePrefix.split(':')[0]
: object.relationshipName;
dePrefixReferenceObjectMap[object.dePrefix] =
object.referenceObject === ''
? object.dePrefix.split(':')[0]
: object.referenceObject;
// 1.1 check if fields in eventDataConfig exist in Salesforce
// if it has no value this is the entry-source object itself
const referencedObject =
object.referenceObject === '' ? ca.objectAPIName : object.referenceObject;
// sort list of fields alphabetically
object.fields.sort();
// check if object was cached earlier
if (!this.sfObjects.workflowObjects.includes(referencedObject)) {
errors.push(`Salesforce object ${referencedObject} not found on connected org.`);
} else if (
!this.sfObjects.objectFields[referencedObject] ||
!Object.keys(this.sfObjects.objectFields[referencedObject]).length
) {
// check if we found fields for the object
errors.push(
`Fields for Salesforce object ${referencedObject} could not be checked. Fields selected in entry event: ` +
object.fields.join(', ')
);
} else {
// check if the fields selected in the eventDefinition are actually available
for (const fieldName of object.fields) {
if (
checkCommon &&
(referencedObject === 'Contact' || referencedObject === 'Lead') &&
this.sfObjects.objectFields['Common'][fieldName]
) {
errors.push(
`Salesforce object field ${referencedObject}.${fieldName} needs to be referenced as Common.${fieldName}`
);
} else if (!this.sfObjects.objectFields[referencedObject][fieldName]) {
// TODO reactivate after switch to new API
// errors.push(
// `Salesforce object field ${referencedObject}.${fieldName} not available on connected org.`
// );
}
// 1.2 check if all fields in eventDataConfig are listed in the eventDataSummary
if (!ca.eventDataSummary.includes(object.dePrefix + fieldName)) {
// TODO instead, remove in postRetrieve and re-add in preDeploy
errors.push(
`Field ${object.dePrefix + fieldName} is listed under eventDataConfig${object.referenceObject ? ` for referenceObject ` + object.referenceObject : ''} but not in eventDataSummary`
);
}
}
}
}
// 2 compare eventDataConfig with eventDataSummary
// check if all fields in eventDataSummary are listed in the eventDataConfig
for (let fieldName of ca.eventDataSummary) {
// TODO instead, remove in postRetrieve and re-add in preDeploy
const fieldPath = fieldName.split(':');
fieldName = fieldPath.pop();
const dePrefix = fieldPath.join(':') + ':';
if (!dePrefixFields[dePrefix]) {
errors.push(
`Field ${dePrefix + fieldName} is listed under eventDataSummary but object ${dePrefix} was not found in eventDataConfig`
);
} else if (!dePrefixFields[dePrefix]?.includes(fieldName)) {
errors.push(
`Field ${dePrefix + fieldName} is listed under eventDataSummary but not in eventDataConfig`
);
}
}
// 3 check contactKey
// check against referencedObjects
const referencedContactObj = this.sfObjects.referencedObjects[ca.objectAPIName].find(
(el) =>
el.relationshipName ===
(ca.contactKey.relationshipName == ''
? ca.contactKey.referenceObjectName
: ca.contactKey.relationshipName)
);
if (referencedContactObj) {
if (
ca.contactKey.isPolymorphic &&
referencedContactObj.isPolymorphic !== ca.contactKey.isPolymorphic
) {
errors.push(
`configurationArguments.contactKey states an incorrect isPolimorphic value. Should be ${referencedContactObj.isPolymorphic}`
);
}
if (referencedContactObj.referenceObjectName !== ca.contactKey.referenceObjectName) {
errors.push(
`configurationArguments.contactKey states an incorrect referenceObjectName value. Should be ${referencedContactObj.referenceObjectName}`
);
}
// * if contactKey uses "Common" then there is no fieldName attribute but instead relationshipIdName needs to be checked
if (
checkCommon &&
ca.contactKey.referenceObjectName === 'Contact' &&
this.sfObjects.objectFields['Common'][
ca.contactKey.fieldName || ca.contactKey.relationshipIdName
]
) {
errors.push(
`configurationArguments.contactKey should be referencing Common instead of Contact`
);
} else if (
!this.sfObjects.objectFields[ca.contactKey.referenceObjectName]?.[
ca.contactKey.fieldName || ca.contactKey.relationshipIdName
]
) {
errors.push(
`configurationArguments.contactKey states the invalid fieldName '${ca.contactKey.fieldName || ca.contactKey.relationshipIdName}' value that does not exist on ${ca.contactKey.referenceObjectName}`
);
}
} else {
errors.push(
`configurationArguments.contactKey references ${
ca.contactKey.relationshipName == ''
? ca.contactKey.referenceObjectName
: ca.contactKey.relationshipName
} which is not found in related salesforce objects`
);
}
// 4 check passThroughArgument
const dePrefixCommon = ca.objectAPIName + ':Common';
for (const key of Object.keys(ca.passThroughArgument.fields)) {
const fieldPath = ca.passThroughArgument.fields[key].split(':');
const fieldName = fieldPath.pop();
const dePrefix = fieldPath.join(':') + ':';
// it seems these fields do NOT need to be in the eventDataConfig
const relationshipName = dePrefixRelationshipMap[dePrefix];
const referenceObject = dePrefixReferenceObjectMap[dePrefix];
if (!this.sfObjects.objectFields[referenceObject]?.[fieldName]) {
errors.push(
`Field ${dePrefix + fieldName} is listed under passThroughArgument.fields.${key} but is not available on connected org.`
);
} else if (
checkCommon &&
(relationshipName === 'Contact' || relationshipName === 'Lead') &&
this.sfObjects.objectFields['Common']?.[fieldName]
) {
errors.push(
`Field ${dePrefix + fieldName} is listed under passThroughArgument.fields.${key} but needs to be referenced as ${dePrefixCommon}.${fieldName}`
);
}
}
// 5.a check primaryObjectFilterCriteria
this.checkSfFilterFieldsExist(
ca.primaryObjectFilterCriteria.conditions,
errors,
'primaryObjectFilterCriteria'
);
// 5.b check relatedObjectFilterCriteria
this.checkSfFilterFieldsExist(
ca.relatedObjectFilterCriteria.conditions,
errors,
'relatedObjectFilterCriteria'
);
// 6.a remove primaryObjectFilterSummary (and auto-generate it again in preDeploy from primaryObjectFilterCriteria)
// TODO
// 6.b remove relatedObjectFilterSummary (and auto-generate it again in preDeploy from relatedObjectFilterCriteria)
// TODO
// 7 remove eventDataSummary (and auto-generate it again in preDeploy from eventDataConfig)
// TODO
// 8 remove evaluationCriteriaSummary (and auto-generate it again in preDeploy from salesforceTriggerCriteria)
// TODO
// throw error if problems were found
if (errors?.length) {
// add a line break
errors.unshift(``);
throw new Error(errors.join('\n - ')); // eslint-disable-line unicorn/error-message
}
}
/**
*
* @param {object[]} conditions -
* @param {string[]} errors list of errors
* @param {'primaryObjectFilterCriteria'|'relatedObjectFilterCriteria'} context used to improve error logs
*/
static checkSfFilterFieldsExist(conditions, errors, context) {
for (const condition of conditions) {
if (
condition.fieldName & condition.referenceObjectName &&
!this.sfObjects.objectFields[condition.referenceObjectName]?.[condition.fieldName]
) {
errors.push(
`Field ${condition.referenceObjectName}.${condition.fieldName} is listed under ${context} but is not available on connected org.`
);
} else if (condition.conditions) {
this.checkSfFilterFieldsExist(condition.conditions, errors, context);
}
}
}
static requiredConfigurationArguments = [
'applicationExtensionKey',
'contactKey',
'contactPersonType',
'eventDataConfig',
'objectAPIName',
'passThroughArgument',
'primaryObjectFilterCriteria',
'relatedObjectFilterCriteria',
'salesforceTriggerCriteria',
'version',
'whoToInject',
];
/**
*
* @param {string} triggerType e.g. SalesforceObjectTriggerV2, APIEvent, ...
* @param {configurationArguments} ca trigger[0].configurationArguments
* @param {string} key of event / journey
* @param {boolean} isPublished if the current item is published it means we do not need to do contact vs common checks
* @param {string} [type] optionally provide metadatype for error on missing configurationArguments attributes
* @returns {Promise.<void>} -
*/
static async postRetrieveTasks_SalesforceEntryEvents(triggerType, ca, key, isPublished, type) {
if (triggerType !== 'SalesforceObjectTriggerV2' || !ca) {
return;
}
// normalize payload because these fields are sometimes set as strings and sometimes as objects
// @ts-expect-error journeys SOMETIMES spell it "Api" and this script aims to auto-correct that
if (ca.objectApiName) {
// on event only the uppercase version is used. lets make sure we normalize that here.
// @ts-expect-error journeys SOMETIMES spell it "Api" and this script aims to auto-correct that
ca.objectAPIName = ca.objectApiName;
// @ts-expect-error journeys SOMETIMES spell it "Api" and this script aims to auto-correct that
delete ca.objectApiName;
}
// check if everything important is there or else MC Connect cannot publish this AND other journeys
for (const attribute of this.requiredConfigurationArguments) {
if (!ca[attribute]) {
Util.logger.error(
` - ${type || this.definition.type} ${key}: required field configurationArguments.${attribute} not set`
);
}
}
// normalize payload because these fields are sometimes set as strings and sometimes as objects
ca.contactKey =
'string' === typeof ca.contactKey ? JSON.parse(ca.contactKey) : ca.contactKey;
ca.eventDataConfig =
'string' === typeof ca.eventDataConfig
? JSON.parse(ca.eventDataConfig)
: ca.eventDataConfig;
ca.eventDataSummary =
'string' === typeof ca.eventDataSummary
? // @ts-expect-error transforming this from API-string-format to from mcdev-format
ca.eventDataSummary.split('; ').filter(Boolean).sort()
: ca.eventDataSummary;
ca.passThroughArgument =
'string' === typeof ca.passThroughArgument
? JSON.parse(ca.passThroughArgument)
: ca.passThroughArgument;
ca.primaryObjectFilterCriteria =
'string' === typeof ca.primaryObjectFilterCriteria
? JSON.parse(ca.primaryObjectFilterCriteria)
: ca.primaryObjectFilterCriteria;
ca.relatedObjectFilterCriteria =
'string' === typeof ca.relatedObjectFilterCriteria
? JSON.parse(ca.relatedObjectFilterCriteria)
: ca.relatedObjectFilterCriteria;
// get info about salesforce objects if not already cached
// * SF product team actually sometimes sets this field with uppercase "API" and sometimes with "Api"... Great job, guys!