Completed
Push — master ( 7c9876...cb12e3 )
by Daniel
34s
created

custom_functions.js   B

Complexity

Total Complexity 37
Complexity/F 2.06

Size

Lines of Code 138
Function Count 18

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 2
Metric Value
cc 0
wmc 37
c 9
b 0
f 2
nc 64
mnd 2
bc 35
fnc 18
dl 0
loc 138
rs 8.6
bpm 1.9444
cpm 2.0555
noi 0

14 Functions

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