-
Notifications
You must be signed in to change notification settings - Fork 36
/
Event.js
219 lines (207 loc) · 7.86 KB
/
Event.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
'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';
/**
* Event MetadataType
*
* @augments MetadataType
*/
class Event extends MetadataType {
/**
* Retrieves Metadata of Event Definition.
* Endpoint /interaction/v1/EventDefinitions return all Event Definitions with all details.
* Currently it is not needed to loop over Imports with endpoint /interaction/v1/EventDefinitions/{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 of metadata
*/
static retrieve(retrieveDir, _, __, key) {
Util.logBeta(this.definition.type);
return super.retrieveREST(
retrieveDir,
`/interaction/v1/EventDefinitions${
key ? '/key:' + encodeURIComponent(key) : ''
}?extras=all`,
null,
key
);
}
/**
* Retrieves event definition metadata for caching
*
* @returns {Promise.<TYPE.MetadataTypeMapObj>} Promise of metadata
*/
static retrieveForCache() {
return super.retrieveREST(null, '/interaction/v1/EventDefinitions/');
}
/**
* Retrieve a specific Event 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 of metadata
*/
static async retrieveAsTemplate(templateDir, name, templateVariables) {
Util.logBeta(this.definition.type);
const res = await this.client.rest.get(
'/interaction/v1/EventDefinitions?name=' + encodeURIComponent(name)
);
const event = res.items.filter((item) => item.name === name);
try {
if (!event || event.length === 0) {
throw new Error(`No Event Definitions Found with name "${name}"`);
} else if (event.length > 1) {
throw new Error(
`Multiple Event Definitions with name "${name}"` +
`please rename to be unique to avoid issues`
);
} else if (event?.length === 1) {
const originalKey = event[0][this.definition.keyField];
const eventDef = JSON.parse(
Util.replaceByObject(
JSON.stringify(this.parseMetadata(event[0])),
templateVariables
)
);
if (!eventDef.dataExtensionId) {
throw new Error(
`Event.parseMetadata:: ` +
`No Data Extension found for ` +
`event: ${eventDef.name}. ` +
`This cannot be templated`
);
}
// remove all fields listed in Definition for templating
this.keepTemplateFields(eventDef);
await File.writeJSONToFile(
[templateDir, this.definition.type].join('/'),
originalKey + '.' + this.definition.type + '-meta',
JSON.parse(Util.replaceByObject(JSON.stringify(eventDef), templateVariables))
);
Util.logger.info(`- templated ${this.definition.type}: ${name}`);
return { metadata: eventDef, type: this.definition.type };
} else {
throw new Error(
`Encountered unknown error when retrieveing ${
this.definition.typeName
} "${name}": ${JSON.stringify(res.body)}`
);
}
} catch (ex) {
Util.logger.error('Event.retrieveAsTemplate:: ' + ex);
return null;
}
}
/**
* manages post retrieve steps
*
* @param {TYPE.MetadataTypeItem} eventDef a single item of Event Definition
* @returns {TYPE.MetadataTypeItem} metadata
*/
static postRetrieveTasks(eventDef) {
const val = this.parseMetadata(eventDef);
this.keepRetrieveFields(val);
return val;
}
/**
* Delete a metadata item from the specified business unit
*
* @param {string} key Identifier of item
* @returns {Promise.<boolean>} deletion success status
*/
static deleteByKey(key) {
Util.logBeta(this.definition.type);
return super.deleteByKeyREST(
'/interaction/v1/eventDefinitions/key:' + encodeURIComponent(key),
key,
false
);
}
/**
* Deploys metadata - merely kept here to be able to print {@link Util.logBeta} once per deploy
*
* @param {TYPE.MetadataTypeMap} metadata 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.<TYPE.MetadataTypeMap>} Promise of keyField => metadata map
*/
static async deploy(metadata, deployDir, retrieveDir) {
Util.logBeta(this.definition.type);
return super.deploy(metadata, deployDir, retrieveDir);
}
/**
* Creates a single Event Definition
*
* @param {TYPE.MetadataTypeItem} metadata a single Event Definition
* @returns {Promise} Promise
*/
static create(metadata) {
return super.createREST(metadata, '/interaction/v1/EventDefinitions/');
}
/**
* Updates a single Event Definition (using PUT method since PATCH isn't supported)
*
* @param {TYPE.MetadataTypeItem} metadataEntry a single Event Definition
* @returns {Promise} Promise
*/
static async update(metadataEntry) {
return super.updateREST(
metadataEntry,
'/interaction/v1/EventDefinitions/' + metadataEntry.id,
'put'
);
}
/**
* prepares an event definition for deployment
*
* @param {TYPE.MetadataTypeItem} metadata a single eventDefinition
* @returns {TYPE.MetadataTypeItem} parsed version
*/
static preDeployTasks(metadata) {
metadata.dataExtensionId = cache.searchForField(
'dataExtension',
metadata.dataExtensionName,
'Name',
'ObjectID'
);
metadata.arguments.dataExtensionId = metadata.dataExtensionId;
return metadata;
}
/**
* parses retrieved Metadata before saving
*
* @param {TYPE.MetadataTypeItem} metadata a single event definition
* @returns {TYPE.MetadataTypeItem} parsed metadata
*/
static parseMetadata(metadata) {
try {
metadata.dataExtensionId = cache.searchForField(
'dataExtension',
metadata.dataExtensionId,
'ObjectID',
'CustomerKey'
);
metadata.arguments.dataExtensionId = metadata.dataExtensionId;
return JSON.parse(JSON.stringify(metadata));
} catch {
Util.metadataLogger(
'verbose',
this.definition.type,
'parseMetadata',
`No related Data Extension found for Event '${metadata.name}'. Consider deleting the event definition`
);
return metadata;
}
}
}
// Assign definition to static attributes
import MetadataTypeDefinitions from '../MetadataTypeDefinitions.js';
Event.definition = MetadataTypeDefinitions.event;
export default Event;