-
Notifications
You must be signed in to change notification settings - Fork 36
/
Verification.js
342 lines (329 loc) · 13.7 KB
/
Verification.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
'use strict';
import Automation from './Automation.js';
import MetadataType from './MetadataType.js';
import TYPE from '../../types/mcdev.d.js';
import { Util } from '../util/util.js';
import cache from '../util/cache.js';
import Cli from '../util/cli.js';
/**
* Verification MetadataType
*
* @augments MetadataType
*/
class Verification extends MetadataType {
/**
* Retrieves Metadata of Data Verification Activity.
*
* @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 of metadata
*/
static async retrieve(retrieveDir, _, __, key) {
let paramArr = [];
if (key?.startsWith('id:')) {
paramArr = [key.slice(3)];
} else if (key) {
paramArr = [key];
}
if (!paramArr.length) {
// there is no API endpoint to retrieve all dataVerification items, so we need to retrieve all automations and iterate over their activities
Util.logger.info(` - Caching dependent Metadata: automation`);
Automation.client = this.client;
Automation.buObject = this.buObject;
Automation.properties = this.properties;
Automation._skipNotificationRetrieve = true;
delete Automation._cachedMetadataMap;
const automationsMapObj = await Automation.retrieve();
delete Automation._skipNotificationRetrieve;
if (automationsMapObj?.metadata && Object.keys(automationsMapObj?.metadata).length) {
if (!key) {
// if we are not retrieving a single item, cache the automations for later use during retrieval of automations
Automation._cachedMetadataMap = automationsMapObj?.metadata;
}
// automations found, lets iterate over their activities to find the dataVerification items
const dataVerificationIds = [];
for (const automation of Object.values(automationsMapObj.metadata)) {
if (automation.steps) {
for (const step of automation.steps) {
for (const activity of step.activities) {
if (
activity.objectTypeId === 1000 &&
activity.activityObjectId &&
activity.activityObjectId !==
'00000000-0000-0000-0000-000000000000'
) {
dataVerificationIds.push(activity.activityObjectId);
}
}
}
}
}
if (dataVerificationIds.length) {
paramArr.push(...dataVerificationIds);
}
}
}
const results = {};
if (paramArr.length) {
const response = await this.retrieveRESTcollection(
paramArr.map((id) => ({ id, uri: '/automation/v1/dataverifications/' + id })),
undefined,
!key
);
if (response?.metadata) {
Object.assign(results, response.metadata);
}
}
if (retrieveDir) {
const savedMetadata = await this.saveResults(results, retrieveDir, null, null);
Util.logger.info(
`Downloaded: ${this.definition.type} (${Object.keys(savedMetadata).length})` +
Util.getKeysString(key)
);
}
return {
metadata: results,
type: this.definition.type,
};
}
/**
* helper for {@link this.retrieveRESTcollection}
*
* @param {Error} ex exception
* @param {string} id id or key of item
* @returns {null} -
*/
static handleRESTErrors(ex, id) {
if (ex.message === 'Not Found' || ex.message === 'Request failed with status code 400') {
// if the ID is too short, the system will throw the 400 error
Util.logger.debug(
` ☇ skipping ${this.definition.type} ${id}: ${ex.message} ${ex.code}`
);
} else {
// if we do get here, we should log the error and continue instead of failing to download all automations
Util.logger.error(
` ☇ skipping ${this.definition.type} ${id}: ${ex.message} ${ex.code}`
);
}
return null;
}
/**
* Retrieves Metadata of Data Extract Activity for caching
*
* @returns {Promise.<TYPE.MetadataTypeMapObj>} Promise of metadata
*/
static async retrieveForCache() {
return this.retrieve();
}
/**
* Creates a single Data Extract
*
* @param {TYPE.VerificationItem} metadata a single Data Extract
* @returns {Promise} Promise
*/
static create(metadata) {
return super.createREST(metadata, '/automation/v1/dataverifications/');
}
/**
* helper for {@link MetadataType.createREST}
*
* @param {TYPE.MetadataTypeItem} metadataEntry a single metadata Entry
* @param {object} apiResponse varies depending on the API call
* @param {TYPE.MetadataTypeItem} metadataEntryWithAllFields like metadataEntry but before non-creatable fields were stripped
* @returns {void}
*/
static async postCreateTasks(metadataEntry, apiResponse, metadataEntryWithAllFields) {
if (!apiResponse?.[this.definition.idField]) {
return;
}
Util.logger.warn(
` - ${this.definition.type} ${
metadataEntryWithAllFields?.[this.definition.idField]
}: new key ${
apiResponse?.[this.definition.idField]
} automatically assigned during creation`
);
metadataEntry[this.definition.idField] = apiResponse?.[this.definition.idField];
// map structure: cred/bu --> type --> old key --> new key
const buName = this.buObject.credential + '/' + this.buObject.businessUnit;
Automation.createdKeyMap ||= {};
Automation.createdKeyMap[buName] ||= {};
Automation.createdKeyMap[buName][this.definition.type] ||= {};
Automation.createdKeyMap[buName][this.definition.type][
metadataEntryWithAllFields[this.definition.idField]
] = metadataEntry[this.definition.idField];
}
/**
* Updates a single Data Extract
*
* @param {TYPE.VerificationItem} metadata a single Data Extract
* @returns {Promise} Promise
*/
static update(metadata) {
return super.updateREST(
metadata,
'/automation/v1/dataverifications/' + metadata.dataVerificationDefinitionId
);
}
/**
* prepares a verification for deployment
*
* @param {TYPE.VerificationItem} metadata a single verification activity definition
* @returns {TYPE.VerificationItem} metadata object
*/
static preDeployTasks(metadata) {
metadata.targetObjectId = cache.searchForField(
'dataExtension',
metadata.r__dataExtension_CustomerKey,
'CustomerKey',
'ObjectID'
);
delete metadata.r__dataExtension_CustomerKey;
return metadata;
}
/**
* parses retrieved Metadata before saving
*
* @param {TYPE.VerificationItem} metadata a single verification activity definition
* @returns {TYPE.VerificationItem} Array with one metadata object and one sql string
*/
static postRetrieveTasks(metadata) {
try {
metadata.r__dataExtension_CustomerKey = cache.searchForField(
'dataExtension',
metadata.targetObjectId,
'ObjectID',
'CustomerKey'
);
delete metadata.targetObjectId;
} catch (ex) {
Util.logger.warn(
` - ${this.definition.type} ${metadata[this.definition.keyField]}: ${ex.message}`
);
}
return metadata;
}
/**
* Delete a metadata item from the specified business unit
*
* @param {string} key Identifier of item
* @returns {Promise.<boolean>} deletion success status
*/
static deleteByKey(key) {
return super.deleteByKeyREST('/automation/v1/dataverifications/' + key, key);
}
/**
* helper function to get a list of keys where notification email addresses or notes should be updated
*
* @param {TYPE.MetadataTypeMap} metadataMap metadata mapped by their keyField
* @returns {string[]} list of keys
*/
static async getKeysToSetNotifications(metadataMap) {
const keysForDeploy = [];
let completionEmail = [];
if (Util.OPTIONS.completionEmail) {
completionEmail = Util.OPTIONS.completionEmail.split(',');
}
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)) {
const oldCompletionEmails = item['notificationEmailAddress'];
const oldCompletionNote = item['notificationEmailMessage'];
if (Util.OPTIONS.clear && (oldCompletionEmails != '' || oldCompletionNote != '')) {
keysForDeploy.push(item[this.definition.keyField]);
Util.logger.info(
` - added ${this.definition.type} to updateNotification queue: ${
item[this.definition.keyField]
}`
);
continue;
}
for (const email of completionEmail) {
if (oldCompletionEmails.includes(email) || !Util.emailValidator(email)) {
Util.logger.info(
` ☇ skipping ${email}- this email address is already in the notifications or is not a valid email`
);
Util.OPTIONS.completionEmail = completionEmail.slice(
completionEmail.indexOf(email),
1
);
}
}
if (oldCompletionNote == Util.OPTIONS.completionNote) {
Util.logger.verbose(
` ☇ skipping --completionNote, note does not need to be updated`
);
Util.OPTIONS.completionNote = undefined;
}
// if email address/-es were not set, ask for input
if (
Util.OPTIONS.completionNote &&
completionEmail.length < 1 &&
oldCompletionEmails.length < 1
) {
const emails = await Cli.updateNotificationEmails('completionEmail');
if (emails) {
Util.OPTIONS.completionEmail = emails.join(',');
} else {
Util.OPTIONS.completionNote = undefined;
Util.logger.info(
` ☇ skipping --completionNote' - the email address for Run completion was not set`
);
}
}
if (Util.OPTIONS.completionNote || completionEmail.length > 0) {
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;
}
/**
* 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].notificationEmailAddress = '';
metadataMap[metadataKey].notificationEmailMessage = '';
metadataMap[metadataKey].shouldEmailOnFailure = false;
}
if (Util.OPTIONS.completionEmail) {
metadataMap[metadataKey].notificationEmailAddress = Util.OPTIONS.completionEmail;
metadataMap[metadataKey].shouldEmailOnFailure = true;
}
if (Util.OPTIONS.completionNote) {
metadataMap[metadataKey].notificationEmailMessage = Util.OPTIONS.completionNote;
}
const createOrUpdateAction = super.createOrUpdate(
metadataMap,
metadataKey,
hasError,
metadataToUpdate,
metadataToCreate
);
return createOrUpdateAction;
}
}
// Assign definition to static attributes
import MetadataTypeDefinitions from '../MetadataTypeDefinitions.js';
Verification.definition = MetadataTypeDefinitions.verification;
export default Verification;