-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathSenderProfile.js
268 lines (250 loc) · 9.16 KB
/
SenderProfile.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
'use strict';
import MetadataType from './MetadataType.js';
import cache from '../util/cache.js';
import { Util } from '../util/util.js';
import ReplaceCbReference from '../util/replaceContentBlockReference.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
*/
/**
* SenderProfile MetadataType
*
* @augments MetadataType
*/
class SenderProfile extends MetadataType {
/**
* Retrieves SOAP based metadata of metadata type into local filesystem. executes callback with retrieved metadata
*
* @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) {
/** @type {SoapRequestParams} */
let requestParams;
if (key) {
requestParams = {
filter: {
leftOperand: 'CustomerKey',
operator: 'equals',
rightOperand: key,
},
};
}
return super.retrieveSOAP(retrieveDir, requestParams, key);
}
/**
* Updates a single item
*
* @param {MetadataTypeItem} metadataItem a single item
* @returns {Promise} Promise
*/
static update(metadataItem) {
return super.updateSOAP(metadataItem);
}
/**
* Creates a single item
*
* @param {MetadataTypeItem} metadataItem a single item
* @returns {Promise} Promise
*/
static create(metadataItem) {
return super.createSOAP(metadataItem);
}
/**
* Delete a metadata item from the specified business unit
*
* @param {string} customerKey Identifier of data extension
* @returns {Promise.<boolean>} deletion success status
*/
static deleteByKey(customerKey) {
return super.deleteByKeySOAP(customerKey);
}
/**
* manages post retrieve steps
*
* @param {MetadataTypeItem} metadata a single item
* @returns {MetadataTypeItem} a single item
*/
static postRetrieveTasks(metadata) {
// makes or easier reading
if (metadata.Client) {
try {
metadata.createdBy = cache.searchForField(
'user',
metadata.Client.CreatedBy,
'AccountUserID',
'Name'
);
} catch (ex) {
Util.logger.verbose(
` - ${this.definition.type} ${metadata[this.definition.nameField]} (${
metadata[this.definition.keyField]
}): ${ex.message}.`
);
}
try {
metadata.modifiedBy = cache.searchForField(
'user',
metadata.Client.ModifiedBy,
'AccountUserID',
'Name'
);
} catch (ex) {
Util.logger.verbose(
` - ${this.definition.type} ${metadata[this.definition.nameField]} (${
metadata[this.definition.keyField]
}): ${ex.message}.`
);
}
delete metadata.Client;
}
return metadata;
}
/**
* prepares a single item for deployment
*
* @param {MetadataTypeItem} metadata a single query activity
* @returns {Promise.<MetadataTypeItem>} Promise
*/
static async preDeployTasks(metadata) {
// cleanup
delete metadata.createdBy;
delete metadata.modifiedBy;
if (
metadata.UseDefaultRMMRules &&
(metadata.AutoForwardToEmailAddress !== '' || metadata.AutoForwardToName !== '')
) {
Util.logger.warn(
` - ${this.definition.type} ${metadata[this.definition.nameField]} (${
metadata[this.definition.keyField]
}): AutoForwardToEmailAddress and AutoForwardToName will be ignored because UseDefaultRMMRules is set to true; setting UseDefaultRMMRules to false`
);
metadata.UseDefaultRMMRules = false;
}
if (!Util.OPTIONS.matchName) {
// #4 make sure the name is unique
const thisCache = cache.getCache()[this.definition.type];
const relevantNames = Object.keys(thisCache).map((key) => ({
type: null,
key: key,
name: thisCache[key][this.definition.nameField],
}));
// if the name is already in the folder for a different key, add a number to the end
metadata[this.definition.nameField] = this.findUniqueName(
metadata[this.definition.keyField],
metadata[this.definition.nameField],
relevantNames
);
}
return metadata;
}
/**
* Gets executed after deployment of metadata type
*
* @param {MetadataTypeMap} upsertResults metadata mapped by their keyField as returned by update/create
* @returns {Promise.<void>} -
*/
static async postDeployTasks(upsertResults) {
// re-retrieve all upserted items to ensure we have all fields (createdDate and modifiedDate are otherwise not present)
Util.logger.debug(
`Caching all ${this.definition.type} post-deploy to ensure we have all fields`
);
const typeCache = await this.retrieveForCache();
// update values in upsertResults with retrieved values before saving to disk
for (const key of Object.keys(upsertResults)) {
if (typeCache.metadata[key]) {
upsertResults[key] = typeCache.metadata[key];
}
}
}
/**
*
* @param {MetadataTypeItem} item single metadata item
* @param {string} [_] parameter not used
* @param {Set.<string>} [findAssetKeys] list of keys that were found referenced via ContentBlockByX; if set, method only gets keys and runs no updates
* @returns {Promise.<MetadataTypeItem>} key of the item that was updated
*/
static async replaceCbReference(item, _, findAssetKeys) {
const parentName = `${this.definition.type} ${item[this.definition.keyField]}`;
let changes = false;
let error;
// *** type specific logic ***
try {
item.FromName = ReplaceCbReference.replaceReference(
item.FromName,
parentName,
findAssetKeys
);
changes = true;
} catch (ex) {
if (ex.code !== 200) {
error = ex;
}
}
try {
item.FromAddress = ReplaceCbReference.replaceReference(
item.FromAddress,
parentName,
findAssetKeys
);
changes = true;
} catch (ex) {
if (ex.code !== 200) {
error = ex;
}
}
try {
item.AutoForwardToEmailAddress = ReplaceCbReference.replaceReference(
item.AutoForwardToEmailAddress,
parentName,
findAssetKeys
);
changes = true;
} catch (ex) {
if (ex.code !== 200) {
error = ex;
}
}
try {
item.AutoForwardToName = ReplaceCbReference.replaceReference(
item.AutoForwardToName,
parentName,
findAssetKeys
);
changes = true;
} catch (ex) {
if (ex.code !== 200) {
error = ex;
}
}
if (error) {
throw error;
}
if (!changes) {
const ex = new Error('No changes made to the code.');
// @ts-expect-error custom error object
ex.code = 200;
throw ex;
}
// *** finish ***
// replaceReference will throw an error if nothing was updated which will end execution here
// no error means we have a new item to deploy and need to update the item in our retrieve folder
return item;
}
}
// Assign definition & cache to static attributes
import MetadataTypeDefinitions from '../MetadataTypeDefinitions.js';
SenderProfile.definition = MetadataTypeDefinitions.senderProfile;
export default SenderProfile;