'use strict'

let _ = require('underscore')
let mongoose = require('mongoose')
let winston = require('winston')

let Rat = require('../api/models/rat')
let Rescue = require('../api/models/rescue')

mongoose.connect('mongodb://localhost/fuelrats')

console.log('Zeroing out rat rescue counts...')

Rat.update({}, {
  $set: {
    failedAssistCount: 0,
    failedRescueCount: 0,
    successfulAssistCount: 0,
    successfulRescueCount: 0
  }
})
.then(function () {
  console.log(  'Loading rescues...')

  Rescue.find()
  .exec()
  .then(function (rescues) {
    let saves = []

    console.log('Updating rat rescue counts...')

    rescues.forEach(function (rescue, index) {
      console.log('Updating ' + rescue._id)

      let updates = {}

      if (rescue.firstLimpet) {
        if (rescue.successful) {
          updates[rescue.firstLimpet] = {
            $inc: {
              successfulRescueCount: 1
            }
          }
        } else {
          updates[rescue.firstLimpet] = {
            $inc: {
              failedRescueCount: 1
            }
          }
        }
      }

      if (rescue.rats.length) {
        rescue.rats.forEach(function (rat, index) {
          let update = {
            $inc: {}
          }

          if (rescue.successful) {
            updates[rescue.firstLimpet] = {
              $inc: {
                successfulAssistCount: 1
              }
            }
          } else {
            updates[rescue.firstLimpet] = {
              $inc: {
                failedAssistCount: 1
              }
            }
          }
        })
      }

      Object.keys(updates).forEach(function (id, index) {
        saves.push(Rat.findByIdAndUpdate(id, updates[id]))
      })
    })

    Promise.all(saves)
    .then(function () {
      console.log('Done')
      mongoose.disconnect()
    })
    .catch(function (error) {
      console.error(error)
      mongoose.disconnect()
    })
  })
  .catch(console.error)
})
.catch(console.error)

@@ -469,18 +469,16 @@
 ole.log(
-  
 'Loading
@@ -668,53 +668,8 @@
 ) %7B%0A
-      console.log('Updating ' + rescue._id)%0A%0A
     
@@ -684,18 +684,18 @@
 dates = 
-%7B%7D
+%5B%5D
 %0A%0A      
@@ -774,33 +774,76 @@
 ates
-%5Brescue.firstLimpet%5D =
+.push(%7B%0A            id: rescue.firstLimpet,%0A            update:
  %7B%0A
+  
     
@@ -850,32 +850,34 @@
         $inc: %7B%0A
+  
               su
@@ -891,32 +891,48 @@
 lRescueCount: 1%0A
+              %7D%0A
             %7D%0A  
@@ -932,32 +932,33 @@
    %7D%0A          %7D
+)
 %0A        %7D else 
@@ -980,33 +980,76 @@
 ates
-%5Brescue.firstLimpet%5D =
+.push(%7B%0A            id: rescue.firstLimpet,%0A            update:
  %7B%0A
+  
     
@@ -1056,32 +1056,34 @@
         $inc: %7B%0A
+  
               fa
@@ -1093,32 +1093,48 @@
 dRescueCount: 1%0A
+              %7D%0A
             %7D%0A  
@@ -1134,32 +1134,33 @@
    %7D%0A          %7D
+)
 %0A        %7D%0A     
@@ -1360,38 +1360,68 @@
  updates
-%5Brescue.firstLimpet%5D =
+.push(%7B%0A              id: rat,%0A              update:
  %7B%0A     
@@ -1425,32 +1425,34 @@
             
+ 
+ 
 $inc: %7B%0A
             
@@ -1431,32 +1431,34 @@
         $inc: %7B%0A
+  
                 
@@ -1474,32 +1474,50 @@
 lAssistCount: 1%0A
+                %7D%0A
               %7D%0A
@@ -1521,32 +1521,33 @@
  %7D%0A            %7D
+)
 %0A          %7D els
@@ -1573,30 +1573,60 @@
 ates
-%5Brescue.firstLimpet%5D =
+.push(%7B%0A              id: rat,%0A              update:
  %7B%0A 
@@ -1634,32 +1634,34 @@
             
+ 
+ 
 $inc: %7B%0A
             
@@ -1640,32 +1640,34 @@
         $inc: %7B%0A
+  
                 
@@ -1679,32 +1679,50 @@
 dAssistCount: 1%0A
+                %7D%0A
               %7D%0A
@@ -1726,32 +1726,33 @@
  %7D%0A            %7D
+)
 %0A          %7D%0A   
@@ -1778,28 +1778,15 @@
     
-Object.keys(
 updates
-)
 .for
@@ -1804,17 +1804,14 @@
 on (
-id, index
+update
 ) %7B%0A
@@ -1851,16 +1851,23 @@
 dUpdate(
+update.
 id, upda
@@ -1872,13 +1872,15 @@
 date
-s%5Bid%5D
+.update
 ))%0A 

Switch to array instead of update for better parsing