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

spauth.then   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 0 Features 0
Metric Value
cc 2
c 11
b 0
f 0
nc 2
dl 0
loc 46
rs 8.9411
nop 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, false, config.SharePoint.Filters.Lists.NotHidden.BlackList, true, 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
                                        var crtRecordFieldWillBeExtracted = MyCustomFunctions.decideBlackListWhiteList(itemField.CanBeDeleted, true, config.SharePoint.Filters.Fields.CanBeDeleted.BlackList, false, config.SharePoint.Filters.Fields.CannotBeDeleted.WhiteList, itemField.InternalName); // check current Field against configured BlackList and WhiteList besides considering user defined Fields
33
                                        if (config.SharePoint.Filters.Lists.Hidden.WhiteList.indexOf(crtListParameters.Title) > -1) {  // for certain Lists all existing fields should be retrieved
34
                                            crtRecordFieldWillBeExtracted = true;
35
                                        }
36
                                        if (crtRecordFieldWillBeExtracted) {
37
                                            fieldAttributes[itemField.Title] = {'Technical Name': itemField.StaticName, 'Type': itemField.TypeAsString};
38
                                            wStreamListFields.write('"' + crtListParameters.Title + '"' + config.General.ListSeparator + '"' + MyCustomFunctions.buildCurrentRecordValues(config.SharePoint.MetaDataOutput.Fields, itemField).join('"' + config.General.ListSeparator + '"') + '"\n'); // fields of current List
39
                                        }
40
                                    });
41
                                    request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, MyCustomFunctions.internalQueryStructureArray(crtListParameters.Records), crtListParameters.Title, 'Items', data)).then(function (responseListRecord) { // Get the actual values from current list
42
                                        MyCustomFunctions.manageRequestIntoCSVfile({'filePath': config.General.PathForExtracts, 'fileName': crtListParameters.Title, 'ListSeparator': config.General.ListSeparator}, crtListParameters, responseListRecord, fieldAttributes, fs);
43
                                    });
44
                                }
45
                            });
46
                            request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, internalQueryStructureGeneric, crtListParameters.Title, 'Views', data)).then(function (responseViews) {
47
                                if (Object.keys(responseViews.d.results).length > 0) {
48
                                    responseViews.d.results.forEach(function (crtView) {
49
                                        wStreamListViews.write('"' + crtListParameters.Title + '"' + config.General.ListSeparator + '"' + MyCustomFunctions.buildCurrentRecordValues(config.SharePoint.MetaDataOutput.Views, crtView).join('"' + config.General.ListSeparator + '"') + '"\n'); // writing current Views record values
50
                                    });
51
                                }
52
                            });
53
                        }
54
                    });
55
                    wStreamList.end();
56
                }
57
            });
58
            request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, internalQueryStructureGeneric, '', 'SiteGroups', data)).then(function (responseSiteGroups) {
59
                if (Object.keys(responseSiteGroups.d.results).length > 0) {
60
                    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);
61
                    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);
62
                    responseSiteGroups.d.results.forEach(function (crtItemGroup) {
63
                        wStreamGroups.write('"' + MyCustomFunctions.buildCurrentRecordValues(config.SharePoint.MetaDataOutput.SiteGroups, crtItemGroup).join('"' + config.General.ListSeparator + '"') + '"\n'); // writing current record values
64
                        request.get(MyCustomFunctions.buildRequestQuery(targetSharePoint.URL, internalQueryStructureGeneric, crtItemGroup.Id, 'GroupMembers', data)).then(function (responseMembers) {
65
                            if (Object.keys(responseMembers.d.results).length > 0) {
66
                                responseMembers.d.results.forEach(function (crtItemGroupMember) {
67
                                    wStreamGroupMembers.write('"' + crtItemGroup.Title + '"' + config.General.ListSeparator + '"' + MyCustomFunctions.buildCurrentRecordValues(config.SharePoint.MetaDataOutput.SiteGroupMembers, crtItemGroupMember).join('"' + config.General.ListSeparator + '"') + '"\n'); // writing current record values
68
                                });
69
                            }
70
                        });
71
                    });
72
                    wStreamGroups.end();
73
                }
74
            });
75
        });
76