-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathImportFile.js
299 lines (284 loc) · 11.6 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
'use strict';
import TYPE from '../../types/mcdev.d.js';
import MetadataType from './MetadataType.js';
import { Util } from '../util/util.js';
import File from '../util/file.js';
import cache from '../util/cache.js';
/**
* 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} [_] unused parameter
* @param {void} [__] unused parameter
* @param {string} [key] customer key of single item to retrieve
* @returns {Promise.<TYPE.MetadataTypeMapObj>} Promise
*/
static retrieve(retrieveDir, _, __, key) {
return super.retrieveREST(retrieveDir, '/automation/v1/imports/', null, key);
}
/**
* Retrieves import definition metadata for caching
*
* @returns {Promise.<TYPE.MetadataTypeMapObj>} Promise
*/
static async retrieveForCache() {
return super.retrieveREST(null, '/automation/v1/imports/');
}
/**
* 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 {TYPE.TemplateMap} templateVariables variables to be replaced in the metadata
* @returns {Promise.<TYPE.MetadataTypeItemObj>} Promise
*/
static async retrieveAsTemplate(templateDir, name, templateVariables) {
const res = await this.client.rest.get(
'/automation/v1/imports/?$filter=name%20eq%20' + encodeURIComponent(name)
);
if (Array.isArray(res?.items) && res?.items.length) {
// eq-operator returns a similar, not exact match and hence might return more than 1 entry
const metadata = res.items.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.parseMetadata(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 (res?.items) {
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(res)}`
);
}
}
/**
* manages post retrieve steps
*
* @param {TYPE.MetadataTypeItem} importDef a single importDef
* @returns {TYPE.MetadataTypeItem} metadata
*/
static postRetrieveTasks(importDef) {
const val = this.parseMetadata(importDef);
return val;
}
/**
* Creates a single Import File
*
* @param {TYPE.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 {TYPE.MetadataTypeItem} importFile a single Import File
* @returns {Promise} Promise
*/
static update(importFile) {
return super.updateREST(
importFile,
'/automation/v1/imports/' + importFile.importDefinitionId
);
}
/**
* prepares a import definition for deployment
*
* @param {TYPE.MetadataTypeItem} metadata a single importDef
* @returns {Promise} Promise
*/
static async preDeployTasks(metadata) {
metadata.fileTransferLocationId = cache.searchForField(
'fileLocation',
metadata.r__fileLocation_name,
'name',
'id'
);
delete metadata.r__fileLocation_name;
if (metadata.c__destinationType === 'DataExtension') {
if (metadata.r__dataExtension_CustomerKey) {
metadata.destinationObjectId = cache.searchForField(
'dataExtension',
metadata.r__dataExtension_CustomerKey,
'CustomerKey',
'ObjectID'
);
delete metadata.r__dataExtension_CustomerKey;
} else {
throw new Error('Import Destination DataExtension not defined');
}
} else if (metadata.c__destinationType === 'List') {
if (metadata.r__list_PathName) {
metadata.destinationObjectId = cache.getListObjectId(
metadata.r__list_PathName,
'ObjectID'
);
// destinationId is also needed for List types
metadata.destinationId = cache.getListObjectId(metadata.r__list_PathName, 'ID');
delete metadata.r__list_PathName;
} else {
throw new Error('Import Destination List not defined');
}
} else {
Util.logger.debug(
` - importFile ${metadata[this.definition.keyField]}: Import Destination Type ${
metadata.c__destinationType
} not fully supported. Deploy might fail.`
);
}
// When the destinationObjectTypeId is 584 it refers to Mobile Connect which is not supported as an Import Type
metadata.destinationObjectTypeId =
this.definition.destinationObjectTypeMapping[metadata.c__destinationType];
metadata.subscriberImportTypeId =
this.definition.subscriberImportTypeMapping[metadata.c__subscriberImportType];
metadata.updateTypeId = this.definition.updateTypeMapping[metadata.c__dataAction];
return metadata;
}
/**
* helper function to get a list of keys where notification email address should be updated
*
* @param {TYPE.MetadataTypeMap} metadataMap metadata mapped by their keyField
* @returns {string[]} list of keys
*/
static async getKeysToSetNotifications(metadataMap) {
const keysForDeploy = [];
if (Object.keys(metadataMap).length) {
Util.logger.info(
`Searching for ${this.definition.type} keys among downloaded items where notification email address should be updated:`
);
for (const item of Object.values(metadataMap)) {
if (item['notificationEmailAddress'] === Util.OPTIONS.completionEmail) {
Util.logger.verbose(
` ☇ skipping ${this.definition.type} ${
item[this.definition.keyField]
}: email address does not need to be updated`
);
} else {
keysForDeploy.push(item[this.definition.keyField]);
Util.logger.info(
` - added ${this.definition.type} to updateNotification queue: ${
item[this.definition.keyField]
}`
);
}
}
Util.logger.info(
`Found ${keysForDeploy.length} ${this.definition.type} keys to update email notification address`
);
}
return keysForDeploy;
}
/**
* parses retrieved Metadata before saving
*
* @param {TYPE.MetadataTypeItem} metadata a single import definition
* @returns {TYPE.MetadataTypeItem} parsed metadata definition
*/
static parseMetadata(metadata) {
metadata.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.c__destinationType = Util.inverseGet(
this.definition.destinationObjectTypeMapping,
metadata.destinationObjectTypeId
);
delete metadata.destinationObjectTypeId;
if (metadata.c__destinationType === 'DataExtension') {
try {
metadata.r__dataExtension_CustomerKey = cache.searchForField(
'dataExtension',
metadata.destinationObjectId,
'ObjectID',
'CustomerKey'
);
delete metadata.destinationObjectId;
} catch (ex) {
Util.logger.warn(` - ImportFile ${metadata.customerKey}: ${ex.message}`);
}
} else if (metadata.c__destinationType === 'List') {
try {
metadata.r__list_PathName = cache.getListPathName(
metadata.destinationObjectId,
'ObjectID'
);
delete metadata.destinationObjectId;
} catch (ex) {
Util.logger.warn(` - ImportFile ${metadata.customerKey}: ${ex.message}`);
}
}
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;
}
/**
* helper for {@link MetadataType.upsert}
*
* @param {TYPE.MetadataTypeMap} metadataMap list of metadata
* @param {string} metadataKey key of item we are looking at
* @param {boolean} hasError error flag from previous code
* @param {TYPE.MetadataTypeItemDiff[]} metadataToUpdate list of items to update
* @param {TYPE.MetadataTypeItem[]} metadataToCreate list of items to create
* @returns {'create' | 'update' | 'skip'} action to take
*/
static createOrUpdate(metadataMap, metadataKey, hasError, metadataToUpdate, metadataToCreate) {
if (Util.OPTIONS.clear) {
metadataMap[metadataKey].sendEmailNotification = false;
metadataMap[metadataKey].notificationEmailAddress = '';
} else if (Util.OPTIONS.completionEmail) {
metadataMap[metadataKey].sendEmailNotification = true;
metadataMap[metadataKey].notificationEmailAddress = Util.OPTIONS.completionEmail;
}
const createOrUpdateAction = super.createOrUpdate(
metadataMap,
metadataKey,
hasError,
metadataToUpdate,
metadataToCreate
);
return createOrUpdateAction;
}
}
// Assign definition to static attributes
import MetadataTypeDefinitions from '../MetadataTypeDefinitions.js';
ImportFile.definition = MetadataTypeDefinitions.importFile;
export default ImportFile;