-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathAttributeSet.js
260 lines (241 loc) · 10.9 KB
/
AttributeSet.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
'use strict';
const TYPE = require('../../types/mcdev.d');
const MetadataType = require('./MetadataType');
const AttributeGroup = require('./AttributeGroup');
const Util = require('../util/util');
const cache = require('../util/cache');
/**
* AttributeSet MetadataType
*
* @augments MetadataType
*/
class AttributeSet extends MetadataType {
/**
* Retrieves Metadata of schema set Definitions.
*
* @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 async retrieve(retrieveDir, _, __, key) {
if (retrieveDir && !cache.getCache()?.attributeGroup) {
// ! attributeGroup and attributeSet both link to each other. caching attributeGroup here "manually", assuming that it's quicker than the other way round
Util.logger.info(' - Caching dependent Metadata: attributeGroup');
AttributeGroup.buObject = this.buObject;
AttributeGroup.client = this.client;
AttributeGroup.properties = this.properties;
const result = await AttributeGroup.retrieveForCache();
cache.setMetadata('attributeGroup', result.metadata);
}
return super.retrieveREST(
retrieveDir,
'/hub/v1/contacts/schema/setDefinitions',
null,
null,
key
);
}
/**
* Retrieves Metadata of schema set definitions for caching.
*
* @returns {Promise.<TYPE.MetadataTypeMapObj>} Promise
*/
static retrieveForCache() {
return super.retrieveREST(null, '/hub/v1/contacts/schema/setDefinitions');
}
/**
* Builds map of metadata entries mapped to their keyfields
*
* @param {object} body json of response body
* @param {string|number} [singleRetrieve] key of single item to filter by
* @returns {TYPE.MetadataTypeMap} keyField => metadata map
*/
static parseResponseBody(body, singleRetrieve) {
const metadataCache = super.parseResponseBody(body);
// make sure we add the entire list to cache before running postRetrieveTasks because of the self-references this type is using
// usually, the cache is only written into after all postRetrieveTasks have been run
cache.setMetadata(this.definition.type, metadataCache);
const metadataStructure = super.parseResponseBody(body, singleRetrieve);
return metadataStructure;
}
/**
* manages post retrieve steps
*
* @param {TYPE.MetadataTypeItem} metadata a single metadata
* @returns {TYPE.MetadataTypeItem} metadata
*/
static postRetrieveTasks(metadata) {
// folder
if (metadata.storageLogicalType === 'DataExtension') {
// attributeSet created for Group Connect do not have a folder
super.setFolderPath(metadata);
}
// source
switch (metadata.storageLogicalType) {
case 'ExactTargetSchema': // synced / shared DEs
case 'DataExtension': {
// local DEs
try {
metadata.r__dataExtension_CustomerKey = cache.searchForField(
'dataExtension',
metadata.storageReferenceID.value,
'ObjectID',
'CustomerKey'
);
// TODO: check if fields in metadata.sendAttributeStorageName exist in data extension --> error
// TODO: check if fields in data extension exist in metadata.sendAttributeStorageName --> warn
delete metadata.storageReferenceID;
delete metadata.storageName;
delete metadata.storageObjectInformation; // type ExactTargetSchema only
} catch (ex) {
Util.logger.warn(
` - ${this.definition.type} ${metadata[this.definition.keyField]}: ${
ex.message
}`
);
}
break;
}
case 'MobileAttributes': {
// TODO: implement
// "storageName": "_MobileAddress",
break;
}
case 'EnterpriseAttributes': {
// TODO: implement
// "storageName": "_EnterpriseAttribute",
break;
}
case 'PushAttributes': {
// TODO: implement
// "storageName": "_PushAddress",
break;
}
}
// relationships to attributeGroups & AttributeSet
if (Array.isArray(metadata.relationships)) {
for (const relationship of metadata.relationships) {
for (const type of ['left', 'right']) {
if (
relationship[type + 'Item']?.connectingID?.identifierType ===
'FullyQualifiedName'
) {
delete relationship[type + 'Item'].connectingID;
}
let relationshipObj = null;
switch (relationship[type + 'Item'].relationshipType) {
case 'AttributeGroup': {
try {
relationship[type + 'Item'].r__attributeGroup_definitionKey =
cache.searchForField(
'attributeGroup',
relationship[type + 'Item']?.identifier,
'definitionID',
'definitionKey'
);
delete relationship[type + 'Item']?.identifier;
} catch (ex) {
Util.logger.warn(
` - ${this.definition.type} ${
metadata[this.definition.keyField]
}: ${ex.message}`
);
}
// get relationship fieldnames
relationshipObj = {
valueDefinitions: this._getSystemValueDefinitions(),
};
break;
}
case 'AttributeSet': {
try {
relationship[type + 'Item'].r__attributeSet_definitionKey =
cache.searchForField(
'attributeSet',
relationship[type + 'Item']?.identifier,
'definitionID',
'definitionKey'
);
delete relationship[type + 'Item']?.identifier;
// get relationship fieldnames
// check if its a self-reference to metadata.valueDefinitions or if it's a reference to another attributeSet
relationshipObj =
relationship[type + 'Item'].r__attributeSet_definitionKey ===
metadata.definitionKey
? metadata
: cache.getByKey(
'attributeSet',
relationship[type + 'Item']
.r__attributeSet_definitionKey
);
} catch (ex) {
Util.logger.warn(
` - ${this.definition.type} ${
metadata[this.definition.keyField]
}: ${ex.message}`
);
}
break;
}
}
try {
// get relationship fieldnames
// resolve field values
for (const attr of relationship.relationshipAttributes) {
const id = attr[type + 'AttributeID'];
const valueDefinition = relationshipObj.valueDefinitions.find(
(item) => item.valueDefinitionID === id
);
if (valueDefinition) {
attr['c__' + type + 'FullyQualifiedName'] =
valueDefinition.fullyQualifiedName;
delete attr[type + 'AttributeID'];
delete attr[type + 'ConnectingID'];
} else {
throw new Error(
`Could not find ${type}AttributeID ${id} of relationship ${relationship.relationshipID}`
);
}
}
} catch (ex) {
Util.logger.warn(
` - ${this.definition.type} ${metadata[this.definition.nameField]} / ${
metadata[this.definition.keyField]
}: ${ex.message}`
);
}
}
}
}
// Member ID
delete metadata.customObjectOwnerMID;
// connectingID.identifierType seems to be always set to 'FullyQualifiedName' - to be sure we check it here and remove it if it's the case
if (metadata.connectingID?.identifierType === 'FullyQualifiedName') {
// remove useless field
delete metadata.connectingID;
}
return metadata;
}
/**
* helper for {@link AttributeSet.postRetrieveTasks}
*
* @returns {object[]} all system value definitions
*/
static _getSystemValueDefinitions() {
if (!this.systemValueDefinitions) {
this.systemValueDefinitions = Object.values(cache.getCache()['attributeSet'])
.flatMap((item) => {
if (item.isSystemDefined) {
return item.valueDefinitions;
}
})
.filter(Boolean);
}
return this.systemValueDefinitions;
}
}
// Assign definition to static attributes
AttributeSet.definition = require('../MetadataTypeDefinitions').attributeSet;
module.exports = AttributeSet;