Completed
Push — master ( 979c33...1d1f77 )
by Daniel
01:08
created

module.exports.createOutputFileWithHeader   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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