Completed
Push — master ( b15462...979c33 )
by Daniel
01:06
created

module.exports.manageRequestIntoCSVfile   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
nc 6
dl 0
loc 10
rs 9.4285
nop 5

1 Function

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