-
Notifications
You must be signed in to change notification settings - Fork 36
/
ImportFile.js
488 lines (457 loc) · 19.8 KB
/
ImportFile.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
'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';
/**
* @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
*/
/**
* ImportFile MetadataType
*
* @augments MetadataType
*/
class ImportFile extends MetadataType {
/**
* Retrieves Metadata of Import File.
* Endpoint /automation/v1/imports/ return all Import Files with all details.
* Currently it is not needed to loop over Imports with endpoint /automation/v1/imports/{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
*/
static async retrieve(retrieveDir, _, __, key) {
let objectId = null;
if (key) {
// using '?$filter=customerKey%20eq%20' + encodeURIComponent(key) would also work but that just retrieves more data for no reason
objectId = await this._getObjectIdForSingleRetrieve(key);
if (!objectId) {
// avoid running the rest request below by returning early
Util.logger.info(
`Downloaded: ${this.definition.type} (0)${Util.getKeysString(key)}`
);
return { metadata: {}, type: this.definition.type };
}
}
Util.logger.debug(' - retrieving extended metadata for SMS imports');
const smsImportResults = await this.client.rest.getBulk(
'/legacy/v1/beta/mobile/imports/',
50
);
this.smsImports = {};
if (smsImportResults.totalResults > 0 && smsImportResults.entry.length > 0) {
Util.logger.info(`Caching dependent Metadata: dataExtension (source for SMS imports)`);
const sourceObject = {};
for (const item of smsImportResults.entry) {
// this api does not show the key but the name is assumed to be unique
this.smsImports[item.name] = item;
try {
if (!sourceObject[item.sourceObjectId]) {
sourceObject[item.sourceObjectId] = await this.client.rest.get(
'/legacy/v1/beta/object/' + item.sourceObjectId
);
}
item.sourceObjectKey = sourceObject[item.sourceObjectId].key;
} catch {
Util.logger.warn(
`endpoint /legacy/v1/beta/object/${item.sourceObjectId} does not exist`
);
}
}
}
return super.retrieveREST(
retrieveDir,
'/automation/v1/imports/' + (objectId || ''),
null,
key
);
}
/**
* Retrieves import definition metadata for caching
*
* @param {void | string[]} [_] parameter not used
* @param {void | string[]} [__] parameter not used
* @param {string} [key] customer key of single item to retrieve
* @returns {Promise.<MetadataTypeMapObj>} Promise
*/
static retrieveForCache(_, __, key) {
return this.retrieve(null, null, null, key);
}
/**
* Retrieve a specific Import 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
*/
static async retrieveAsTemplate(templateDir, name, templateVariables) {
// using '?$filter=name%20eq%20' + encodeURIComponent(name) would also work but that just retrieves more data for no reason
const cache = await this.retrieveForCache(null, null, 'name:' + name);
const metadataArr = Object.values(cache?.metadata);
if (Array.isArray(metadataArr) && metadataArr.length) {
// eq-operator returns a similar, not exact match and hence might return more than 1 entry
const metadata = metadataArr.find((item) => item.name === name);
if (!metadata) {
Util.logger.error(`No ${this.definition.typeName} found with name "${name}"`);
return;
}
const originalKey = metadata[this.definition.keyField];
const val = JSON.parse(
Util.replaceByObject(
JSON.stringify(this.postRetrieveTasks(metadata)),
templateVariables
)
);
// remove all fields listed in Definition for templating
this.keepTemplateFields(val);
await File.writeJSONToFile(
[templateDir, this.definition.type].join('/'),
originalKey + '.' + this.definition.type + '-meta',
JSON.parse(Util.replaceByObject(JSON.stringify(val), templateVariables))
);
Util.logger.info(`- templated ${this.definition.type}: ${name}`);
return { metadata: val, type: this.definition.type };
} else if (metadataArr) {
Util.logger.error(`No ${this.definition.typeName} found with name "${name}"`);
} else {
throw new Error(
`Encountered unknown error when retrieveing ${
this.definition.typeName
} "${name}": ${JSON.stringify(metadataArr)}`
);
}
}
/**
* helper to allow us to select single metadata entries via REST
*
* @private
* @param {string} key customer key
* @returns {Promise.<string>} objectId or enpty string
*/
static async _getObjectIdForSingleRetrieve(key) {
const response = await this.client.soap.retrieve('ImportDefinition', ['ObjectID'], {
filter: key.startsWith('name:')
? {
leftOperand: 'Name',
operator: 'equals',
rightOperand: key.slice(5),
}
: {
leftOperand: 'CustomerKey',
operator: 'equals',
rightOperand: key,
},
});
return response?.Results?.length ? response.Results[0].ObjectID : null;
}
/**
* Creates a single Import File
*
* @param {MetadataTypeItem} importFile a single Import File
* @returns {Promise} Promise
*/
static create(importFile) {
return super.createREST(importFile, '/automation/v1/imports/');
}
/**
* Updates a single Import File
*
* @param {MetadataTypeItem} importFile a single Import File
* @returns {Promise} Promise
*/
static update(importFile) {
return super.updateREST(
importFile,
'/automation/v1/imports/' + importFile.importDefinitionId
);
}
/**
* Deploys metadata
*
* @param {MetadataTypeMap} metadataMap 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(metadataMap, deployDir, retrieveDir) {
if (
Object.values(metadataMap).filter(
(item) => item.destination.c__destinationType === 'SMS'
).length > 0
) {
Util.logger.info(`Caching dependent Metadata: dataExtension (source for SMS imports)`);
const dataExtensionLegacyResult = await this.client.rest.getBulk(
'/legacy/v1/beta/object/',
500
);
this.dataExtensionsLegacy = {};
if (dataExtensionLegacyResult?.entry?.length) {
for (const item of dataExtensionLegacyResult.entry) {
this.dataExtensionsLegacy[item.key] = item;
}
}
}
return super.deploy(metadataMap, deployDir, retrieveDir);
}
/**
* prepares a import definition for deployment
*
* @param {MetadataTypeItem} metadata a single importDef
* @returns {Promise.<MetadataTypeItem>} Promise
*/
static async preDeployTasks(metadata) {
const fileLocation = cache.getByKey('fileLocation', metadata.source.r__fileLocation_name);
if (!fileLocation) {
throw new Error(`fileLocation ${metadata.r__fileLocation_name} not found in cache`);
}
metadata.fileTransferLocationId = fileLocation.id;
metadata.fileTransferLocationTypeId = fileLocation.locationTypeId;
delete metadata.source.r__fileLocation_name;
switch (metadata.destination.c__destinationType) {
case 'DataExtension': {
if (metadata.destination.r__dataExtension_key) {
metadata.destinationObjectId = cache.searchForField(
'dataExtension',
metadata.destination.r__dataExtension_key,
'CustomerKey',
'ObjectID'
);
delete metadata.destination.r__dataExtension_key;
} else {
throw new Error('Import Destination DataExtension not defined');
}
break;
}
case 'List': {
if (metadata.destination.r__list_PathName) {
metadata.destinationObjectId = cache.getListObjectId(
metadata.destination.r__list_PathName,
'ObjectID'
);
// destinationId is also needed for List types
metadata.destinationId = cache.getListObjectId(
metadata.destination.r__list_PathName,
'ID'
);
delete metadata.destination.r__list_PathName;
} else {
throw new Error('Import Destination List not defined');
}
break;
}
case 'SMS': {
if (metadata.destination.r__mobileKeyword_key) {
// code
const codeObj = cache.getByKey(
'mobileCode',
metadata.destination.r__mobileKeyword_key.split('.')[0]
);
if (!codeObj) {
throw new Error(
`mobileCode ${metadata.destination.r__mobileKeyword_key} not found in cache`
);
}
metadata.code = codeObj;
// keyword
const keywordObj = cache.getByKey(
'mobileKeyword',
metadata.destination.r__mobileKeyword_key
);
if (!keywordObj) {
throw new Error(
`mobileKeyword ${metadata.destination.r__mobileKeyword_key} not found in cache`
);
}
metadata.keyword = keywordObj;
} else {
Util.logger.error(
` - importFile ${metadata[this.definition.keyField]}: No code or keyword info found. Please re-download this from the source.`
);
}
// destination
metadata.destinationObjectId = '00000000-0000-0000-0000-000000000000';
metadata.destinationObjectType = 'MobileSubscription';
// source
if (this.dataExtensionsLegacy[metadata.source.r__dataExtension_key]) {
metadata.sourceObjectId =
this.dataExtensionsLegacy[metadata.source.r__dataExtension_key].id;
metadata.sourceObjectName =
this.dataExtensionsLegacy[
metadata.source.r__dataExtension_key
].dataExtensionName;
delete metadata.source.r__dataExtension_key;
}
Util.logger.debug(
` - importFile ${metadata[this.definition.keyField]}: Import Destination Type ${
metadata.destination.c__destinationType
} not fully supported. Deploy might fail.`
);
break;
}
default: {
Util.logger.debug(
` - importFile ${metadata[this.definition.keyField]}: Import Destination Type ${
metadata.destination.c__destinationType
} not fully supported. Deploy might fail.`
);
}
}
if (metadata.c__blankFileProcessing) {
// omit this if not set
metadata.blankFileProcessingType =
this.definition.blankFileProcessingTypeMapping[metadata.c__blankFileProcessing];
}
// When the destinationObjectTypeId is 584 it refers to Mobile Connect which is not supported as an Import Type
metadata.destinationObjectTypeId =
this.definition.destinationObjectTypeMapping[metadata.destination.c__destinationType];
metadata.subscriberImportTypeId =
this.definition.subscriberImportTypeMapping[metadata.c__subscriberImportType];
metadata.updateTypeId = this.definition.updateTypeMapping[metadata.c__dataAction];
delete metadata.destination;
delete metadata.source;
return metadata;
}
/**
* manages post retrieve steps
*
* @param {MetadataTypeItem} metadata a single item
* @returns {MetadataTypeItem} parsed metadata
*/
static postRetrieveTasks(metadata) {
metadata.destination = {};
metadata.source = {};
metadata.source.r__fileLocation_name = cache.searchForField(
'fileLocation',
metadata.fileTransferLocationId,
'id',
'name'
);
delete metadata.fileTransferLocationId;
// * When the destinationObjectTypeId is 584 it refers to Mobile Connect which is not supported as an Import Type
metadata.destination.c__destinationType = Util.inverseGet(
this.definition.destinationObjectTypeMapping,
metadata.destinationObjectTypeId
);
switch (metadata.destination.c__destinationType) {
case 'DataExtension': {
try {
metadata.destination.r__dataExtension_key = cache.searchForField(
'dataExtension',
metadata.destinationObjectId,
'ObjectID',
'CustomerKey'
);
delete metadata.destinationObjectId;
} catch (ex) {
Util.logger.warn(` - ImportFile ${metadata.customerKey}: ${ex.message}`);
}
break;
}
case 'List': {
try {
metadata.destination.r__list_PathName = cache.getListPathName(
metadata.destinationObjectId,
'ObjectID'
);
delete metadata.destinationObjectId;
} catch (ex) {
Util.logger.warn(` - ImportFile ${metadata.customerKey}: ${ex.message}`);
}
break;
}
case 'SMS': {
if (this.smsImports[metadata.name]) {
const smsImport = this.smsImports[metadata.name];
// code
try {
cache.searchForField('mobileCode', smsImport.code?.code, 'code', 'code');
} catch (ex) {
Util.logger.warn(` - ImportFile ${metadata.customerKey}: ${ex.message}`);
}
// keyword
try {
cache.searchForField(
'mobileKeyword',
smsImport.keyword?.keyword,
'c__codeKeyword',
'c__codeKeyword'
);
} catch (ex) {
Util.logger.warn(` - ImportFile ${metadata.customerKey}: ${ex.message}`);
}
// code + keyword
metadata.destination.r__mobileKeyword_key =
smsImport.code?.code + '.' + smsImport.keyword?.keyword;
// source dataExtension
if (smsImport.sourceObjectKey) {
metadata.source.r__dataExtension_key = smsImport.sourceObjectKey;
}
} else {
Util.logger.warn(
` - ImportFile ${metadata.customerKey}: Could not find mobile code and keyword nor source for this SMS import activity.`
);
}
// remove empty desitination
delete metadata.destinationObjectId;
break;
}
default: {
Util.logger.debug(
` - ImportFile ${metadata.customerKey}: Destination Type ${metadata.destinationObjectTypeId} not fully supported. Deploy might fail.`
);
}
}
delete metadata.destinationObjectTypeId;
if (metadata.blankFileProcessingType) {
// omit this if not set
metadata.c__blankFileProcessing = Util.inverseGet(
this.definition.blankFileProcessingTypeMapping,
metadata.blankFileProcessingType
);
delete metadata.blankFileProcessingType;
}
metadata.c__subscriberImportType = Util.inverseGet(
this.definition.subscriberImportTypeMapping,
metadata.subscriberImportTypeId
);
delete metadata.subscriberImportTypeId;
metadata.c__dataAction = Util.inverseGet(
this.definition.updateTypeMapping,
metadata.updateTypeId
);
delete metadata.updateTypeId;
return metadata;
}
/**
* Delete a metadata item from the specified business unit
*
* @param {string} customerKey Identifier of data extension
* @returns {Promise.<boolean>} deletion success flag
*/
static async deleteByKey(customerKey) {
// delete only works with the query's object id
const objectId = customerKey ? await this._getObjectIdForSingleRetrieve(customerKey) : null;
if (!objectId) {
Util.logger.error(` - import not found`);
return false;
}
return super.deleteByKeyREST('/automation/v1/imports/' + objectId, customerKey);
}
}
// Assign definition to static attributes
import MetadataTypeDefinitions from '../MetadataTypeDefinitions.js';
ImportFile.definition = MetadataTypeDefinitions.importFile;
export default ImportFile;