-
Notifications
You must be signed in to change notification settings - Fork 36
/
FileTransfer.js
195 lines (181 loc) · 7.08 KB
/
FileTransfer.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
'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';
/**
* FileTransfer MetadataType
*
* @augments MetadataType
*/
class FileTransfer extends MetadataType {
/**
* Retrieves Metadata of FileTransfer Activity.
* Endpoint /automation/v1/filetransfers/ returns all File Transfers
*
* @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) {
return super.retrieveREST(retrieveDir, '/automation/v1/filetransfers/', null, key);
}
/**
* Retrieves Metadata of FileTransfer Activity for caching
*
* @returns {Promise.<TYPE.MetadataTypeMapObj>} Promise
*/
static async retrieveForCache() {
return super.retrieveREST(null, '/automation/v1/filetransfers/');
}
/**
* Retrieve a specific File Transfer 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
*/
static async retrieveAsTemplate(templateDir, name, templateVariables) {
const res = await this.client.rest.get(
'/automation/v1/filetransfers/?$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/filetransfers/' + 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 File Transfer
*
* @param {TYPE.MetadataTypeItem} fileTransfer a single File Transfer
* @returns {Promise} Promise
*/
static create(fileTransfer) {
return super.createREST(fileTransfer, '/automation/v1/filetransfers/');
}
/**
* Updates a single File Transfer
*
* @param {TYPE.MetadataTypeItem} fileTransfer a single File Transfer
* @returns {Promise} Promise
*/
static update(fileTransfer) {
return super.updateREST(fileTransfer, '/automation/v1/filetransfers/' + fileTransfer.id);
}
/**
* prepares a fileTransfer for deployment
*
* @param {TYPE.MetadataTypeItem} metadata a single fileTransfer activity definition
* @returns {Promise} Promise
*/
static async preDeployTasks(metadata) {
if (metadata.r__fileLocation_name) {
metadata.fileTransferLocationId = cache.searchForField(
'fileLocation',
metadata.r__fileLocation_name,
'name',
'id'
);
delete metadata.r__fileLocation_name;
} else {
throw new Error(
'r__fileLocation_name not set. Please ensure the source is properly set up and re-retrieve it first.'
);
}
return metadata;
}
/**
* manages post retrieve steps
*
* @param {TYPE.MetadataTypeItem} metadata a single fileTransfer activity definition
* @returns {TYPE.MetadataTypeItem} parsed metadata
*/
static postRetrieveTasks(metadata) {
try {
metadata.r__fileLocation_name = cache.searchForField(
'fileLocation',
metadata.fileTransferLocationId,
'id',
'name'
);
delete metadata.fileTransferLocationId;
} catch (ex) {
Util.logger.warn(
` - FileTransfer '${metadata[this.definition.keyField]}': ${ex.message}`
);
}
return 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/filetransfers/' + filter);
const items = results?.items || [];
const found = items.find((item) =>
name ? item[this.definition.nameField] === name : item[this.definition.keyField] === key
);
return found?.id || 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(` - fileTransfer not found`);
return false;
}
return super.deleteByKeyREST('/automation/v1/filetransfers/' + objectId, customerKey);
}
}
// Assign definition to static attributes
import MetadataTypeDefinitions from '../MetadataTypeDefinitions.js';
FileTransfer.definition = MetadataTypeDefinitions.fileTransfer;
export default FileTransfer;