var config = require('../config/app');
var _ = require('underscore');
var moment = require('moment');
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectId;
var db;
MongoClient.connect(config.mongoUrl, function(err, database) {
  db = database;
});

var JOB_ACTIVE = "Active";
var JOB_ERROR = "Error";
var JOB_COMPLETE = "Complete";

var command = {
  upload: function(req, res) {
    var uploadSuccesses = 0;

    if(req.body) {
      var recordType = req.body.type;
      startJob(recordType, startUpload);
      db.collection(recordType).createIndex({
        action_date: -1,
        library_code: 1
      }, {
        background: true
      });
    }
    res.end('');

    function startUpload(jobId) {
      var collection = db.collection(recordType);
      insert(req.body.rows);

      function insert(values) {
        if(values.length > 0) {
          var tmpValues = _.map(_.first(values, 1000), function(value) {
            value.job_id = jobId;
            value.action_date = moment(value.action_date, 'YYYY-MM-DD HH:mm:ss').toDate();
            return value;
          });
          collection.insertMany(tmpValues, function(err, result) {
            if(err) {
              console.log(err);
              finish(JOB_ERROR);
            }
            else {
              uploadSuccesses += result.insertedCount;
              updateJob(jobId, JOB_ACTIVE, uploadSuccesses);
              insert(_.rest(values, 1000));
            }
          });
        }
        else {
          finish(JOB_COMPLETE);
        }
      }

      function finish(state) {
        updateJob(jobId, state, uploadSuccesses);
      }
    }
  },
}

function startJob(type, callback) {
  db.collection('jobs').insertOne({
    type: type,
    start: new Date(),
    updated: new Date(),
    state: JOB_ACTIVE,
    processed: 0
  }, function(err, result) {
    if(err) {
      console.log(err);
    }
    else {
      callback(result.insertedId);
    }
  });
}

function updateJob(id, state, processed) {
  db.collection('jobs').updateOne({_id: id}, {$set: {
    updated: new Date(),
    state: state,
    processed: processed
  }});
}

module.exports = command;
@@ -364,16 +364,80 @@
 mplete%22;
+%0Avar CHUNK_SIZE = 1000;%0Avar DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss';
 %0A%0Avar co
@@ -650,25 +650,16 @@
 eIndex(%7B
-%0A        
 action_d
@@ -666,24 +666,16 @@
 ate: -1,
-%0A       
  library
@@ -686,28 +686,12 @@
 e: 1
-%0A      %7D, %7B%0A        
+%7D, %7B
 back
@@ -702,23 +702,16 @@
 nd: true
-%0A      
 %7D);%0A    
@@ -880,42 +880,8 @@
 ) %7B%0A
-        if(values.length %3E 0) %7B%0A  
     
@@ -922,20 +922,26 @@
 values, 
-1000
+CHUNK_SIZE
 ), funct
@@ -949,26 +949,24 @@
 on(value) %7B%0A
-  
           va
@@ -991,26 +991,24 @@
 ;%0A          
- 
- 
 value.action
@@ -1041,37 +1041,27 @@
 n_date, 
-'YYYY-MM-DD HH:mm:ss'
+DATE_FORMAT
 ).toDate
@@ -1064,18 +1064,16 @@
 Date();%0A
-  
         
@@ -1092,32 +1092,28 @@
 ue;%0A        
-  
 %7D);%0A
-  
         coll
@@ -1175,18 +1175,16 @@
         
-  
 if(err) 
@@ -1181,26 +1181,24 @@
   if(err) %7B%0A
-  
             
@@ -1231,26 +1231,51 @@
     
-  finish(JOB_ERROR
+updateJob(jobId, JOB_ERROR, uploadSuccesses
 );%0A 
@@ -1275,38 +1275,34 @@
 ses);%0A          
-  %7D%0A  
+%7D%0A
           else %7B
@@ -1314,18 +1314,16 @@
         
-  
 uploadSu
@@ -1355,18 +1355,16 @@
 dCount;%0A
-  
         
@@ -1426,24 +1426,18 @@
         
- 
- insert
+if
 (_.rest(
@@ -1448,44 +1448,77 @@
 es, 
-1000));%0A            %7D%0A          %7D);%0A
+CHUNK_SIZE)) %7B%0A              insert(_.rest(values, CHUNK_SIZE));%0A    
     
@@ -1515,32 +1515,36 @@
 ;%0A            %7D%0A
+    
         else %7B%0A 
@@ -1556,127 +1556,96 @@
     
-finish(JOB_COMPLETE);%0A        %7D%0A      %7D%0A%0A      function finish(state) %7B%0A        updateJob(jobId, state, uploadSuccesses
+    updateJob(jobId, JOB_COMPLETE, uploadSuccesses);%0A            %7D%0A          %7D%0A        %7D
 );%0A 

fix jobs staying in active state