Completed
Push — master ( 8cba42...3886f4 )
by Daniel
53s
created

spauth.then   A

Complexity

Conditions 2
Paths 9

Size

Total Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
c 3
b 0
f 0
nc 9
dl 0
loc 73
rs 9.0675
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 myFunctions = new MyCustomFunctions();
7
var fs = require('fs');
8
9
spauth
10
        .getAuth(targetSharePoint.URL, {
11
            username: targetSharePoint.credentials.username,
12
            password: targetSharePoint.credentials.password
13
        })
14
        .then(function (data) {
15
            var fsOptions = {
16
                encoding: 'utf8'
17
            };
18
            var headerOptions = data.headers;
19
            headerOptions['Accept'] = 'application/json;odata=verbose';
20
            var ListNameArray = [];
21
            request.get(myFunctions.buildRequestQuery(targetSharePoint.URL, '', 'Lists', headerOptions)).then(function (response) {
22
                var dataObjectLists = response.d.results;
23
                if (Object.keys(dataObjectLists).length > 0) {
24
                    var dataListLight = [];
25
                    var counter = 0;
26
                    dataObjectLists.forEach(function (item) {
27
                        dataListLight[counter] = {
28
                            'Created': item.Created.replace('T', ' ').replace('Z', ''),
29
                            'Description': item.Description,
30
                            'EnableAttachments': item.EnableAttachments,
31
                            'EnableFolderCreation': item.EnableFolderCreation,
32
                            'EnableVersioning': item.EnableVersioning,
33
                            'Hidden': item.Hidden,
34
                            'Id': item.Id,
35
                            'IsPrivate': item.IsPrivate,
36
                            'ItemCount': item.ItemCount,
37
                            'LastItemDeletedDate': item.LastItemDeletedDate.replace('T', ' ').replace('Z', ''),
38
                            'LastItemModifiedDate': item.LastItemModifiedDate.replace('T', ' ').replace('Z', ''),
39
                            'LastItemUserModifiedDate': item.LastItemUserModifiedDate.replace('T', ' ').replace('Z', ''),
40
                            'MajorVersionLimit': item.MajorVersionLimit,
41
                            'NoCrawl': item.NoCrawl,
42
                            'ParserDisabled': item.ParserDisabled,
43
                            'Title': item.Title
44
                        };
45
                        ListNameArray[counter] = item.Title;
46
                        counter++;
47
                    });
48
                    // initiate MetaData for Lists
49
                    var wStreamList = fs.createWriteStream(config.General.PathForExtracts + config.General.MetaDataFileName.Lists + '.csv', fsOptions);
50
                    // headers of MetaData for Lists
51
                    wStreamList.write('"' + Object.keys(dataListLight[0]).join('"' + config.General.ListSeparator + '"') + '"\n');
52
                    // initiate MetaData for Fields
53
                    var wStreamListFields = fs.createWriteStream(config.General.PathForExtracts + config.General.MetaDataFileName.Fields + '.csv', fsOptions);
54
                    // headers of MetaData for Fields
55
                    wStreamListFields.write('"List"' + config.General.ListSeparator + '"' + Object.keys(config.SharePoint.MetaDataOutput.Fields).join('"' + config.General.ListSeparator + '"') + '"\n');
56
                    // parse each List
57
                    dataListLight.forEach(function (crtListParameters) {
58
                        // check current List against configured BlackList and WhiteList besides considering user defined Lists
59
                        if (myFunctions.decideBlackListWhiteList(crtListParameters.Hidden, false, config.SharePoint.Filters.Lists.NotHidden.BlackList, true, config.SharePoint.Filters.Lists.Hidden.WhiteList, crtListParameters.Title)) {
60
                            // records detail of current List
61
                            wStreamList.write('"' + Object.keys(crtListParameters).map(function (x) {
62
                                return crtListParameters[x];
63
                            }).join('"' + config.General.ListSeparator + '"') + '"\n');
64
                            // Dynamically detect structure of the list, extracting the Field names and their text to display
65
                            request.get(myFunctions.buildRequestQuery(targetSharePoint.URL, crtListParameters.Title, 'Fields', headerOptions)).then(function (response) {
66
                                var dataObject = response.d.results;
67
                                if (Object.keys(dataObject).length > 0) {
68
                                    var fieldAttributes = [];
69
                                    var counter = 0;
70
                                    dataObject.forEach(function (item) {
71
                                        var crtRecordFieldWillBeExtracted = myFunctions.decideBlackListWhiteList(item.CanBeDeleted, true, config.SharePoint.Filters.Fields.CanBeDeleted.BlackList, false, config.SharePoint.Filters.Fields.CannotBeDeleted.WhiteList, item.InternalName);
72
                                        // for certain Lists all existing fields should be retrieved
73
                                        if (config.SharePoint.Filters.Lists.Hidden.WhiteList.indexOf(crtListParameters.Title) > -1) {
74
                                            crtRecordFieldWillBeExtracted = true;
75
                                        }
76
                                        if (crtRecordFieldWillBeExtracted) {
77
                                            fieldAttributes[item.Title] = {
78
                                                'Technical Name': item.StaticName,
79
                                                'Type': item.TypeAsString
80
                                            };
81
                                            counter++;
82
                                            var crtListField = [];
83
                                            var counterF = 0
84
                                            Object.keys(config.SharePoint.MetaDataOutput.Fields).forEach(function (itemF) {
85
                                                crtListField[counterF] = item[config.SharePoint.MetaDataOutput.Fields[itemF]];
86
                                                counterF++;
87
                                            });
88
                                            wStreamListFields.write('"' + crtListParameters.Title + '"' + config.General.ListSeparator + '"' + crtListField.join('"' + config.General.ListSeparator + '"') + '"\n');
89
                                        }
90
                                    });
91
                                    // Get the actual values from current list
92
                                    request.get(myFunctions.buildRequestQuery(targetSharePoint.URL, crtListParameters.Title, 'Items', headerOptions)).then(function (response) {
93
                                        var wstream = fs.createWriteStream(config.General.PathForExtracts + crtListParameters.Title + '.csv', fsOptions);
94
                                        // writing headers for records within current list
95
                                        wstream.write('"' + Object.keys(fieldAttributes).join('"' + config.General.ListSeparator + '"') + (crtListParameters.EnableVersioning ? '"' + config.General.ListSeparator + '"Version' : '') + '"\n');
96
                                        var dataObjectValues = response.d.results;
97
                                        if (Object.keys(dataObjectValues).length > 0) {
98
                                            dataObjectValues.forEach(function (item) {
99
                                                var crtRecord = [];
100
                                                var counterF = 0
101
                                                Object.keys(fieldAttributes).map(function (itemF) {
102
                                                    switch (fieldAttributes[itemF]['Type']) {
103
                                                        case 'DateTime':
104
                                                            crtRecord[counterF] = item[fieldAttributes[itemF]['Technical Name']].replace('T', ' ').replace('Z', '');
105
                                                            break;
106
                                                        case 'Lookup':
107
                                                        case 'User':
108
                                                            crtRecord[counterF] = item[fieldAttributes[itemF]['Technical Name'] + 'Id'];
109
                                                            break;
110
                                                        default:
111
                                                            crtRecord[counterF] = item[fieldAttributes[itemF]['Technical Name']];
112
                                                            break;
113
                                                    }
114
                                                    counterF++;
115
                                                });
116
                                                // writing current record values
117
                                                wstream.write('"' + crtRecord.join('"' + config.General.ListSeparator + '"') + (crtListParameters.EnableVersioning ? '"' + config.General.ListSeparator + '"' + item.OData__UIVersionString : '') + '"\n');
118
                                            });
119
                                        }
120
                                        wstream.end(function () {
121
                                            if (config.General.Feedback.FileCompletion.OtherLists) {
122
                                                console.log(crtListParameters.Title + '.csv has been completed!\n' + (config.General.Feedback.ContentAsJSON.OtherLists ? JSON.stringify(dataObjectValues) : ''));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
123
                                            }
124
                                        });
125
                                    });
126
                                }
127
                            });
128
                        }
129
                    });
130
                    wStreamList.end(function () {
131
                        if (config.General.Feedback.FileCompletion.ListOfLists) {
132
                            console.log(config.General.MetaDataFileName.Lists + '.csv has been completed!\n' + (config.General.Feedback.ContentAsJSON.ListOfLists ? JSON.stringify(dataListLight) : ''));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
133
                        }
134
                    });
135
                }
136
            });
137
        });
138