main.js   A
last analyzed

Complexity

Total Complexity 22
Complexity/F 1.57

Size

Lines of Code 71
Function Count 14

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 0 Features 1
Metric Value
cc 0
wmc 22
c 10
b 0
f 1
nc 2
mnd 4
bc 21
fnc 14
dl 0
loc 71
rs 10
bpm 1.5
cpm 1.5713
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B spauth.then 0 62 1
1
var spauth = require('node-sp-auth');
2
var request = require('request-promise');
3
var config = require('./config.json');
4
var targetSharePoint = require('./targetSharePoint.json');
5
var MyCustomFunctions = require('./custom_functions.js');
6
var fs = require('fs');
7
8
spauth
9
        .getAuth(targetSharePoint.URL, MyCustomFunctions.buildAuthenticationHeader(targetSharePoint.authentication))
10
        .then(function (data) {
11
            var internalQueryStructureGeneric = MyCustomFunctions.internalQueryStructureArray(0);
12
            request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, internalQueryStructureGeneric, '', 'Lists', data)).then(function (responseList) {
13
                if (Object.keys(responseList.d.results).length > 0) {
14
                    var wStreamListViews = MyCustomFunctions.createOutputFileWithHeader({'filePath': config.General.PathForExtracts, 'fileName': config.General.MetaDataFileName.Views, 'fileHeader': '"List Name"' + config.General.ListSeparator + '"' + Object.keys(config.SharePoint.MetaDataOutput.Views).join('"' + config.General.ListSeparator + '"')}, fs);
15
                    var dataListLight = [];
16
                    var counter = 0;
17
                    responseList.d.results.forEach(function (itemList) {
18
                        dataListLight[counter] = MyCustomFunctions.buildCurrentListAttributeValues(config.SharePoint.MetaDataOutput.Lists, itemList);
19
                        counter++;
20
                    });
21
                    var wStreamList = MyCustomFunctions.createOutputFileWithHeader({'filePath': config.General.PathForExtracts, 'fileName': config.General.MetaDataFileName.Lists, 'fileHeader': '"' + Object.keys(dataListLight[0]).join('"' + config.General.ListSeparator + '"')}, fs);
22
                    var wStreamListFields = MyCustomFunctions.createOutputFileWithHeader({'filePath': config.General.PathForExtracts, 'fileName': config.General.MetaDataFileName.Fields, 'fileHeader': '"List"' + config.General.ListSeparator + '"' + Object.keys(config.SharePoint.MetaDataOutput.Fields).join('"' + config.General.ListSeparator + '"')}, fs);
23
                    dataListLight.forEach(function (crtListParameters) { // parse each List
24
                        if (MyCustomFunctions.decideBlackListWhiteList(!crtListParameters.Hidden, config.SharePoint.Filters.Lists.NotHidden.BlackList, config.SharePoint.Filters.Lists.Hidden.WhiteList, crtListParameters.Title)) { // check current List against configured BlackList and WhiteList besides considering user defined Lists
25
                            wStreamList.write('"' + Object.keys(crtListParameters).map(function (x) { // records detail of current List
26
                                return crtListParameters[x];
27
                            }).join('"' + config.General.ListSeparator + '"') + '"\n');
28
                            request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, internalQueryStructureGeneric, crtListParameters.Title, 'Fields', data)).then(function (responseField) { // Dynamically detect structure of the list, extracting the Field names and their text to display
29
                                if (Object.keys(responseField.d.results).length > 0) {
30
                                    var fieldAttributes = [];
31
                                    responseField.d.results.forEach(function (itemField) {
32
                                        if (MyCustomFunctions.decideBlackListWhiteList(itemField.CanBeDeleted, config.SharePoint.Filters.Fields.CanBeDeleted.BlackList, config.SharePoint.Filters.Fields.CannotBeDeleted.WhiteList, itemField.InternalName) || (config.SharePoint.Filters.Lists.Hidden.WhiteList.indexOf(crtListParameters.Title) > -1)) { // check current Field against configured BlackList and WhiteList besides considering user defined Fields || for certain Lists all existing fields should be retrieved
33
                                            fieldAttributes[itemField.Title] = {'Technical Name': itemField.StaticName, 'Type': itemField.TypeAsString};
34
                                            wStreamListFields.write('"' + crtListParameters.Title + '"' + config.General.ListSeparator + MyCustomFunctions.buildCurrentRecordValues(config.SharePoint.MetaDataOutput.Fields, itemField).join(config.General.ListSeparator) + '\n'); // fields of current List
35
                                        }
36
                                    });
37
                                    request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, MyCustomFunctions.internalQueryStructureArray(crtListParameters.Records), crtListParameters.Title, 'Items', data)).then(function (responseListRecord) { // Get the actual values from current list
38
                                        MyCustomFunctions.manageRequestIntoCSVfile({'filePath': config.General.PathForExtracts, 'fileName': crtListParameters.Title, 'ListSeparator': config.General.ListSeparator}, crtListParameters, responseListRecord, fieldAttributes, fs);
39
                                    });
40
                                }
41
                            });
42
                            request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, internalQueryStructureGeneric, crtListParameters.Title, 'Views', data)).then(function (responseViews) {
43
                                if (Object.keys(responseViews.d.results).length > 0) {
44
                                    responseViews.d.results.forEach(function (crtView) {
45
                                        wStreamListViews.write('"' + crtListParameters.Title + '"' + config.General.ListSeparator + MyCustomFunctions.buildCurrentRecordValues(config.SharePoint.MetaDataOutput.Views, crtView).join(config.General.ListSeparator) + '\n'); // writing current Views record values
46
                                    });
47
                                }
48
                            });
49
                        }
50
                    });
51
                    wStreamList.end();
52
                }
53
            });
54
            request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, internalQueryStructureGeneric, '', 'SiteGroups', data)).then(function (responseSiteGroups) {
55
                if (Object.keys(responseSiteGroups.d.results).length > 0) {
56
                    var wStreamGroups = MyCustomFunctions.createOutputFileWithHeader({'filePath': config.General.PathForExtracts, 'fileName': config.General.MetaDataFileName.SiteGroups, 'fileHeader': '"' + Object.keys(config.SharePoint.MetaDataOutput.SiteGroups).join('"' + config.General.ListSeparator + '"')}, fs);
57
                    var wStreamGroupMembers = MyCustomFunctions.createOutputFileWithHeader({'filePath': config.General.PathForExtracts, 'fileName': config.General.MetaDataFileName.SiteGroupMembers, 'fileHeader': '"Group"' + config.General.ListSeparator + '"' + Object.keys(config.SharePoint.MetaDataOutput.SiteGroupMembers).join('"' + config.General.ListSeparator + '"')}, fs);
58
                    responseSiteGroups.d.results.forEach(function (crtItemGroup) {
59
                        wStreamGroups.write(MyCustomFunctions.buildCurrentRecordValues(config.SharePoint.MetaDataOutput.SiteGroups, crtItemGroup).join(config.General.ListSeparator) + '\n'); // writing current record values
60
                        request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, internalQueryStructureGeneric, crtItemGroup.Id, 'GroupMembers', data)).then(function (responseMembers) {
61
                            if (Object.keys(responseMembers.d.results).length > 0) {
62
                                responseMembers.d.results.forEach(function (crtItemGroupMember) {
63
                                    wStreamGroupMembers.write('"' + crtItemGroup.Title + '"' + config.General.ListSeparator + MyCustomFunctions.buildCurrentRecordValues(config.SharePoint.MetaDataOutput.SiteGroupMembers, crtItemGroupMember).join(config.General.ListSeparator) + '\n'); // writing current record values
64
                                });
65
                            }
66
                        });
67
                    });
68
                    wStreamGroups.end();
69
                }
70
            });
71
        });
72