1
|
|
|
module.localFunctions = { |
2
|
|
|
decideBlackList: function (inEvaluatedValueForBlackList, inBlackListArray, inValueToEvaluate) { |
3
|
|
|
if (inEvaluatedValueForBlackList && (inBlackListArray.indexOf(inValueToEvaluate) === -1)) { |
4
|
|
|
return true; |
5
|
|
|
} |
6
|
|
|
return false; |
7
|
|
|
}, |
8
|
|
|
decideWhiteList: function (inEvaluatedValueForWhiteList, inWhiteListArray, inValueToEvaluate) { |
9
|
|
|
if (inEvaluatedValueForWhiteList && (inWhiteListArray.indexOf(inValueToEvaluate) > -1)) { |
10
|
|
|
return true; |
11
|
|
|
} |
12
|
|
|
return false; |
13
|
|
|
}, |
14
|
|
|
manageDateField: function (inCurrentList, crtIndex) { |
15
|
|
|
var crtResult = ''; |
16
|
|
|
if (inCurrentList[crtIndex] === null) { |
17
|
|
|
crtResult = 'null'; |
18
|
|
|
} else { |
19
|
|
|
crtResult = inCurrentList[crtIndex].replace('T', ' ').replace('Z', ''); |
20
|
|
|
} |
21
|
|
|
return crtResult; |
22
|
|
|
}, |
23
|
|
|
manageFieldsOfAllTypes: function (crtFieldAttributes, item) { |
24
|
|
|
if (crtFieldAttributes['Type'] === 'DateTime') { |
25
|
|
|
return module.localFunctions.manageDateField(item, crtFieldAttributes['Technical Name']); |
26
|
|
|
} |
27
|
|
|
var idFieldsType = ['Lookup', 'User']; |
28
|
|
|
if (idFieldsType.indexOf(crtFieldAttributes['Type']) > -1) { |
29
|
|
|
return item[crtFieldAttributes['Technical Name'] + 'Id']; |
30
|
|
|
} |
31
|
|
|
return item[crtFieldAttributes['Technical Name']]; |
32
|
|
|
} |
33
|
|
|
}; |
34
|
|
|
module.exports = { |
35
|
|
|
buildAuthenticationHeader: function (inAuthenticationArray) { |
36
|
|
|
var aReturn; |
37
|
|
|
switch (inAuthenticationArray.type) { |
38
|
|
|
case 'Addin': |
39
|
|
|
aReturn = inAuthenticationArray.credentials_Addin; |
40
|
|
|
break; |
41
|
|
|
case 'SAML': |
42
|
|
|
aReturn = inAuthenticationArray.credentials_SAML; |
43
|
|
|
break; |
44
|
|
|
default: |
45
|
|
|
aReturn = false; |
46
|
|
|
break; |
47
|
|
|
} |
48
|
|
|
return aReturn; |
49
|
|
|
}, |
50
|
|
|
buildCurrentListAttributeValues: function (inObjectListsConfiguredAttributes, inCurrentList) { |
51
|
|
|
var crtListAttributes = []; |
52
|
|
|
Object.keys(inObjectListsConfiguredAttributes).map(function (itemList) { |
53
|
|
|
if (itemList.substring(0, 4) === 'Date') { |
54
|
|
|
crtListAttributes[itemList] = module.localFunctions.manageDateField(inCurrentList, inObjectListsConfiguredAttributes[itemList]); |
55
|
|
|
} else { |
56
|
|
|
crtListAttributes[itemList] = inCurrentList[inObjectListsConfiguredAttributes[itemList]]; |
57
|
|
|
} |
58
|
|
|
}); |
59
|
|
|
return crtListAttributes; |
60
|
|
|
}, |
61
|
|
|
buildCurrentItemValues: function (fieldAttributes, item) { |
62
|
|
|
var crtRecord = []; |
63
|
|
|
var counterF = 0; |
64
|
|
|
Object.keys(fieldAttributes).map(function (itemF) { |
65
|
|
|
crtRecord[counterF] = module.localFunctions.manageFieldsOfAllTypes(fieldAttributes[itemF], item); |
66
|
|
|
counterF++; |
67
|
|
|
}); |
68
|
|
|
return crtRecord; |
69
|
|
|
}, |
70
|
|
|
buildCurrentRecordValues: function (inFieldsArray, crtRecordValues) { |
71
|
|
|
var crtRecordGM = []; |
72
|
|
|
var counterGM = 0; |
73
|
|
|
Object.keys(inFieldsArray).map(function (itemGM) { |
74
|
|
|
if (inFieldsArray[itemGM] === 'HtmlSchemaXml') { |
75
|
|
|
crtRecordGM[counterGM] = JSON.stringify(crtRecordValues[inFieldsArray[itemGM]]); |
76
|
|
|
} else { |
77
|
|
|
crtRecordGM[counterGM] = crtRecordValues[inFieldsArray[itemGM]]; |
78
|
|
|
} |
79
|
|
|
counterGM++; |
80
|
|
|
}); |
81
|
|
|
return crtRecordGM; |
82
|
|
|
}, |
83
|
|
|
buildRequestQuery: function (targetSharePointURL, arStandardLists, crtListName, queryType, inData) { |
84
|
|
|
var queryPrefix = ''; |
85
|
|
|
if (Object.keys(arStandardLists).indexOf(queryType) > -1) { |
86
|
|
|
queryPrefix = '_api/Web/' + arStandardLists[queryType]['APItrunk'] |
87
|
|
|
+ '/' + arStandardLists[queryType]['APIfunction'] + '(\'' |
88
|
|
|
+ crtListName + '\')/' + arStandardLists[queryType]['APIelement']; |
89
|
|
|
} else { |
90
|
|
|
queryPrefix = '_api/Web/' + queryType; |
91
|
|
|
} |
92
|
|
|
var headerOptions = inData.headers; |
93
|
|
|
headerOptions['Accept'] = 'application/json;odata=verbose'; |
94
|
|
|
return { |
95
|
|
|
url: targetSharePointURL + queryPrefix, |
96
|
|
|
headers: headerOptions, |
97
|
|
|
json: true |
98
|
|
|
}; |
99
|
|
|
}, |
100
|
|
|
createOutputFileWithHeader: function (inParameters, fs) { |
101
|
|
|
var writeStream = fs.createWriteStream(inParameters['filePath'] + inParameters['fileName'] + '.csv', {encoding: 'utf8'}); // initiate file stream |
102
|
|
|
writeStream.write(inParameters['fileHeader'] + '"\n'); // Headers content |
103
|
|
|
return writeStream; |
104
|
|
|
}, |
105
|
|
|
decideBlackListWhiteList: function (inDecisionValue, inBlackListArray, inWhiteListArray, inValueToEvaluate) { |
106
|
|
|
return (module.localFunctions.decideBlackList(inDecisionValue, inBlackListArray, inValueToEvaluate) || module.localFunctions.decideWhiteList(!inDecisionValue, inWhiteListArray, inValueToEvaluate)); |
107
|
|
|
}, |
108
|
|
|
internalQueryStructureArray: function (maxRecords) { |
109
|
|
|
return { |
110
|
|
|
'Fields': {'APItrunk': 'Lists', 'APIfunction': 'GetByTitle', 'APIelement': 'Fields'}, |
111
|
|
|
'GroupMembers': {'APItrunk': 'SiteGroups', 'APIfunction': 'GetById', 'APIelement': 'Users'}, |
112
|
|
|
'Items': {'APItrunk': 'Lists', 'APIfunction': 'GetByTitle', 'APIelement': 'Items' + '?$top=' + maxRecords}, |
113
|
|
|
'Views': {'APItrunk': 'Lists', 'APIfunction': 'GetByTitle', 'APIelement': 'Views'} |
114
|
|
|
}; |
115
|
|
|
}, |
116
|
|
|
manageRequestIntoCSVfile: function (inParameters, crtListParameters, responseListRecord, fieldAttributes, fs) { |
117
|
|
|
var writeStream = module.exports.createOutputFileWithHeader({'filePath': inParameters['filePath'], 'fileName': inParameters['fileName'], 'fileHeader': '"' + Object.keys(fieldAttributes).join('"' + inParameters['ListSeparator'] + '"') + (crtListParameters['Versioning Enabled'] ? '"' + inParameters['ListSeparator'] + '"Version' : '')}, fs); |
118
|
|
|
if (Object.keys(responseListRecord.d.results).length > 0) { |
119
|
|
|
responseListRecord.d.results.forEach(function (itemFieldValue) { |
120
|
|
|
writeStream.write('"' + module.exports.buildCurrentItemValues(fieldAttributes, itemFieldValue).join('"' + inParameters['ListSeparator'] + '"') + (crtListParameters['Versioning Enabled'] ? '"' + inParameters['ListSeparator'] + '"' + itemFieldValue.OData__UIVersionString : '') + '"\n'); // writing rows |
121
|
|
|
}); |
122
|
|
|
} |
123
|
|
|
writeStream.end(); |
124
|
|
|
} |
125
|
|
|
}; |
126
|
|
|
|