"use strict";

var pgutil = require('./pgutil.js');
var report = require('./report.js');
var sfutil = require('./sfutil.js');
var s3 = require('./s3.js');

if (typeof(process.env.SFORCE_CLIENT_ID) === 'undefined') {
    require('dotenv').config();
}

pgutil.orgAccessList(function(results) {
    var orgs = results.rows;

    for (var i = 0; i < orgs.length; i++) {
        var access = orgs[i];
        console.log('get connection to org: '+access.orgid);
        var conn = sfutil.getSFConnection(access);

        s3.ensureBucket(access.orgid, function() {


            report.evalReport('00O61000003tGWS', access, conn, function (results) {
                console.log('back in workder.js, and we have tons of insights now: '+results.length);
                for (var r = 0; r < results.length; r++) {
                    console.log('result: '+results[r].Long_Name__c);
                }
                //sfutil.upsertInsights(insights);
            });

            // report.evalReportFolder('Current Actions', access, conn, function (result) {
            //      console.log('in worker.js we evalled a folder');
            //      console.log('back in workder.js, and we have tons of insights now: '+result.length);
            //
            //  });


        });
    }

});

// var up = {thing: 'test'};
// s3.uploadObject('00d61000000adm6eai', 'test', JSON.stringify(up), function(res) {
//     console.log(res);
// })

@@ -567,16 +567,18 @@
         
+//
 report.e
@@ -637,24 +637,114 @@
 (results) %7B%0A
+            report.evalReportFolder('Current Actions', access, conn, function (results) %7B%0A
             
@@ -995,18 +995,16 @@
         
-//
 sfutil.u
@@ -1021,106 +1021,21 @@
 hts(
-insights);%0A            %7D);%0A%0A            // report.evalReportFolder('Current Actions', access, conn
+conn, results
 , fu
@@ -1045,19 +1045,16 @@
 ion (res
-ult
 ) %7B%0A    
@@ -1053,34 +1053,34 @@
 ) %7B%0A            
-//
+  
       console.lo
@@ -1082,16 +1082,21 @@
 le.log('
+back 
 in worke
@@ -1100,32 +1100,18 @@
 rker
-.js we evalled a folder'
+ js: '+res
 );%0A 
@@ -1125,98 +1125,13 @@
     
-//
     
-  console.log('back in workder.js, and we have tons of insights now: '+result.length
+%7D
 );%0A 
@@ -1141,18 +1141,20 @@
         
-//
+%7D);%0A
 %0A       
@@ -1162,16 +1162,8 @@
     
-//  %7D);%0A
 %0A%0A  

call new upsert once we have insights