-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeResource.js
242 lines (185 loc) · 6.86 KB
/
codeResource.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
<script runat='server'>
Platform.Load("core", "1.1")
Platform.Response.SetResponseHeader("Access-Control-Allow-Origin", "*");
Platform.Response.SetResponseHeader('Strict-Transport-Security', 'max-age=200');
Platform.Response.SetResponseHeader('X-XSS-Protection', '1; mode=block');
Platform.Response.SetResponseHeader('X-Frame-Options', 'Deny');
Platform.Response.SetResponseHeader('X-Content-Type-Options', 'nosniff');
Platform.Response.SetResponseHeader('Referrer-Policy', 'strict-origin-when-cross-origin');
Platform.Response.SetResponseHeader('Content-Security-Policy', "default-src 'self'");
var postData = Platform.Request.GetPostData();
var postJSON = Platform.Function.ParseJSON(postData);
var automationName = Platform.Request.GetQueryStringParameter('auto')
var route = Platform.Request.GetQueryStringParameter('route')
var prox = new Script.Util.WSProxy();
switch (route) {
case 'getLog':
var logName = postJSON.log;
var filter = postJSON.filter;
if (logName === null) {
response('Log Data Extension is required')
} else {
var logKey = getExternalKey(logName)
var data = wsRead(logKey, filter);
response(data)
}
break;
case 'getAutomationStatus':
var cols = ["Name","ProgramID","CustomerKey","Status"];
var filter = {
Property: "Name",
SimpleOperator: "equals",
Value: automationName
};
var result = prox.retrieve("Automation", cols, filter);
var res = result.Results[0].Status;
response(res)
break;
case 'getDataExtensionFields':
var dataExtensionName = postJSON.dataExtension;
var extKey = getExternalKey(dataExtensionName)
var res = getFieldNames(extKey)
response(res)
break;
case 'createDataExtension':
var dataExtensionName = postJSON.deName;
var fields = [
{
"Name": "timestamp",
"FieldType": "Date",
"DefaultValue": "GETDATE()",
"IsRequired": false
},
{
"Name": "action",
"FieldType": "Text",
"MaxLength": 250,
"IsRequired": false
},
{
"Name": "log",
"FieldType": "text",
"IsRequired": false
}
];
var config = {
"CustomerKey": String(Platform.Function.GUID()).toUpperCase(),
"Name": dataExtensionName,
"CategoryID": 0,
"Fields": fields
};
var result = prox.createItem("DataExtension", config);
response(result)
break;
case 'startAutomation':
var automationName = postJSON.automationName;
var automationIDConfig = {
Property: "Name",
SimpleOperator: "equals",
Value: automationName
}
var automationObjectIDReq = prox.retrieve("Automation", ["ProgramID"], automationIDConfig);
var automationObjectID = automationObjectIDReq.Results[0].ObjectID;
var opts = {};
var props = {
ObjectID: automationObjectID
};
var result = prox.performItem("Automation", props, "start", opts);
response(result)
break;
}
function getExternalKey(dataExtensionName){
var logDE = DataExtension.Retrieve({ Property: "Name", SimpleOperator: "equals", Value: dataExtensionName });
return logDE[0].CustomerKey;
}
function setLoggingThreshold(minutes){
var m = Number(minutes) || parseINT(number);
var d = new Date();
d.setMinutes(d.getMinutes() - m);
return d
}
function wsRead(logKey, filter) {
try {
var getCols = getFieldNames(logKey);
var cols = [];
for(var c = 0; c < getCols.length; c++){
cols.push(getCols[c].fieldName)
}
var logFilter;
if(filter.SimpleOperator === 'isNotNull'){
logFilter = {
Property: filter.Property,
SimpleOperator: filter.SimpleOperator,
Value: ''
}
} else {
logFilter = {
Property: filter.Property,
SimpleOperator: filter.SimpleOperator,
Value: setLoggingThreshold(filter.Value)
}
}
var desc = prox.retrieve("DataExtensionObject[" + logKey + "]", cols, logFilter);
var formatted = formatResult(desc.Results, "Properties")
return formatted
} catch (err) {
return err
}
}
/***********************************************
*
* function formatResult()
* Takes Objects that are Name/Value pairs {Name: "Key", Value: "Value"} and
* normalizes them to a standard JSON object {key: "value"}
*
* @param {arr} array to normalize
* @param {prop} property key of Array
*
* @output Array of normalized JSON object
*
***********************************************/
function formatResult(arr, prop) {
var results = [];
for (var i = 0; i < arr.length; i++) {
var result_list = arr[i][prop];
var obj = {};
for (k in result_list) {
var key = result_list[k].Name;
var val = result_list[k].Value
if (key.indexOf("_") != 0) obj[key] = val;
}
results.push(obj);
}
return results;
};
/***********************************************
*
* function getFieldNames()
* Gets all columns from a SOAP Object
*
* @param {deName} Name of DataExtension to get columns
*
* @output {array} array of fields
*
***********************************************/
function getFieldNames(logExtKey) {
var de = DataExtension.Init(logExtKey);
var fields = de.Fields.Retrieve();
//fields.sort(function(a, b) { return (a.Ordinal > b.Ordinal) ? 1 : -1 });
var out = [];
for (k in fields) {
out.push({
fieldName: fields[k].Name,
dataType: fields[k].FieldType,
isPrimaryKey: fields[k].IsPrimaryKey
});
}
return out;
} //End retrieveFieldNames
/************************
FORMAT API RESPONSES from Code Resource Pages
*************************/
function response(res) {
return Write(Stringify(res));
}
</script>