-
Notifications
You must be signed in to change notification settings - Fork 36
/
DataExtract.js
201 lines (189 loc) · 7.63 KB
/
DataExtract.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
'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
*/
/**
* DataExtract MetadataType
*
* @augments MetadataType
*/
class DataExtract extends MetadataType {
/**
* Retrieves Metadata of Data Extract Activity.
* Endpoint /automation/v1/dataextracts/ returns all Data Extracts
*
* @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) {
return super.retrieveREST(retrieveDir, '/automation/v1/dataextracts/', null, key);
}
/**
* Retrieves Metadata of Data Extract Activity for caching
*
* @returns {Promise.<MetadataTypeMapObj>} Promise of metadata
*/
static async retrieveForCache() {
return super.retrieveREST(null, '/automation/v1/dataextracts/');
}
/**
* Retrieve a specific dataExtract 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) {
const res = await this.client.rest.get(
'/automation/v1/dataextracts/?$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;
}
// get full definition
const extended = await this.client.rest.get(
'/automation/v1/dataextracts/' + metadata[this.definition.idField]
);
const originalKey = extended[this.definition.keyField];
const val = JSON.parse(
Util.replaceByObject(
JSON.stringify(this.postRetrieveTasks(extended)),
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)}`
);
}
}
/**
* Creates a single Data Extract
*
* @param {MetadataTypeItem} dataExtract a single Data Extract
* @returns {Promise} Promise
*/
static create(dataExtract) {
return super.createREST(dataExtract, '/automation/v1/dataextracts/');
}
/**
* Updates a single Data Extract
*
* @param {MetadataTypeItem} dataExtract a single Data Extract
* @returns {Promise} Promise
*/
static update(dataExtract) {
return super.updateREST(
dataExtract,
'/automation/v1/dataextracts/' + dataExtract.dataExtractDefinitionId
);
}
/**
* prepares a dataExtract for deployment
*
* @param {MetadataTypeItem} metadata a single dataExtract activity definition
* @returns {MetadataTypeItem} metadata object
*/
static preDeployTasks(metadata) {
metadata.dataExtractTypeId = cache.searchForField(
'dataExtractType',
metadata.r__dataExtractType_name,
'name',
'extractId'
);
delete metadata.r__dataExtractType_name;
return metadata;
}
/**
* manages post retrieve steps
*
* @param {MetadataTypeItem} metadata a single item
* @returns {MetadataTypeItem} metadata
*/
static postRetrieveTasks(metadata) {
try {
metadata.r__dataExtractType_name = cache.searchForField(
'dataExtractType',
metadata.dataExtractTypeId,
'extractId',
'name'
);
delete metadata.dataExtractTypeId;
} catch (ex) {
Util.logger.warn(
` - ${this.definition.type} ${metadata[this.definition.keyField]}: ${ex.message}`
);
}
return structuredClone(metadata);
}
/**
* 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 name = key.startsWith('name:') ? key.slice(5) : null;
const filter = name ? '?$filter=name%20eq%20' + encodeURIComponent(name) : '';
const results = await this.client.rest.get('/automation/v1/dataextracts/' + filter);
const items = results?.items || [];
const found = items.find((item) =>
name ? item[this.definition.nameField] === name : item[this.definition.keyField] === key
);
return found?.dataExtractDefinitionId || null;
}
/**
* 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(` - dataExtract not found`);
return false;
}
return super.deleteByKeyREST('/automation/v1/dataextracts/' + objectId, customerKey);
}
}
// Assign definition to static attributes
import MetadataTypeDefinitions from '../MetadataTypeDefinitions.js';
DataExtract.definition = MetadataTypeDefinitions.dataExtract;
export default DataExtract;