diff --git a/lib/app/helpers.js b/lib/app/helpers.js index e34bfc5dd5..2a908b3c0e 100644 --- a/lib/app/helpers.js +++ b/lib/app/helpers.js @@ -3,14 +3,6 @@ var moment; moment = require('moment'); -module.exports.daysBetween = function(a, b) { - var DAY; - DAY = 1000 * 60 * 60 * 24; - a = new Date((new Date(a)).toDateString()); - b = new Date((new Date(b)).toDateString()); - return Math.abs(Math.floor((a.getTime() - b.getTime()) / DAY)); -}; - module.exports.viewHelpers = function(view) { view.fn('taskClasses', function(type, completed, value, repeat) { var classes, dayMapping; diff --git a/lib/app/schema.js b/lib/app/schema.js index e0a00b36cb..0fe90b0dc2 100644 --- a/lib/app/schema.js +++ b/lib/app/schema.js @@ -1,8 +1,10 @@ // Generated by CoffeeScript 1.3.3 -var content, userSchema; +var content, moment, userSchema; content = require('./content'); +moment = require('moment'); + userSchema = { balance: 2, stats: { @@ -61,7 +63,7 @@ module.exports.updateSchema = function(model) { model.del(userPath); return; } - daysOld = require('./helpers').daysBetween(userObj.lastCron, new Date()); + daysOld = moment().sod().diff(moment(userObj.lastCron), 'days'); if (daysOld > 30) { sameTasks = _.filter(require('./content').defaultTasks, function(defaultTask) { var foundSame; diff --git a/lib/app/scoring.js b/lib/app/scoring.js index 3d77beaba5..30e1f4a956 100644 --- a/lib/app/scoring.js +++ b/lib/app/scoring.js @@ -191,15 +191,15 @@ score = function(spec) { cron = function() { var daysPassed, lastCron, today; - today = new Date(); - user.setNull('lastCron', today); - lastCron = user.get('lastCron'); - daysPassed = helpers.daysBetween(lastCron, today); + today = moment().sod(); + user.setNull('lastCron', today.toDate()); + lastCron = moment(user.get('lastCron')); + daysPassed = today.diff(lastCron, 'days'); if (daysPassed > 0) { - user.set('lastCron', today); - return _(daysPassed).times(function(n) { + user.set('lastCron', today.toDate()); + return _.times(daysPassed, function(n) { var tallyFor; - tallyFor = moment(lastCron).add('d', n); + tallyFor = lastCron.add('d', n); return tally(tallyFor); }); } diff --git a/src/app/helpers.coffee b/src/app/helpers.coffee index 69fc360544..8a8557ff13 100644 --- a/src/app/helpers.coffee +++ b/src/app/helpers.coffee @@ -1,13 +1,5 @@ moment = require('moment') -module.exports.daysBetween = (a, b) -> - #TODO replace this function with moment().diff()? - DAY = 1000 * 60 * 60 * 24 - # calculate as midnight - a = new Date( (new Date(a)).toDateString() ) - b = new Date( (new Date(b)).toDateString() ) - return Math.abs(Math.floor((a.getTime() - b.getTime()) / DAY)) - module.exports.viewHelpers = (view) -> view.fn 'taskClasses', (type, completed, value, repeat) -> #TODO figure out how to just pass in the task model, so i can access all these properties from one object diff --git a/src/app/schema.coffee b/src/app/schema.coffee index 0df6a9aad0..f2510a7992 100644 --- a/src/app/schema.coffee +++ b/src/app/schema.coffee @@ -1,4 +1,5 @@ content = require './content' +moment = require 'moment' userSchema = { balance: 2 @@ -40,7 +41,7 @@ module.exports.updateSchema = (model) -> return # Remove all users who haven't logged in for a month - daysOld = require('./helpers').daysBetween(userObj.lastCron, new Date()) + daysOld = moment().sod().diff(moment(userObj.lastCron), 'days') if daysOld > 30 # and who have mostly the default tasks sameTasks = _.filter require('./content').defaultTasks, (defaultTask) -> diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index 998fad40cd..33a25020af 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -166,14 +166,14 @@ score = (spec = {task:null, direction:null, cron:null}) -> return delta cron = -> - today = new Date() - user.setNull('lastCron', today) - lastCron = user.get('lastCron') - daysPassed = helpers.daysBetween(lastCron, today) + today = moment().sod() # start of day + user.setNull 'lastCron', today.toDate() + lastCron = moment(user.get('lastCron')) + daysPassed = today.diff(lastCron, 'days') if daysPassed > 0 - user.set('lastCron', today) # reset cron - _(daysPassed).times (n) -> - tallyFor = moment(lastCron).add('d',n) + user.set('lastCron', today.toDate()) # reset cron + _.times daysPassed, (n) -> + tallyFor = lastCron.add('d',n) tally(tallyFor) # At end of day, add value to all incomplete Daily & Todo tasks (further incentive)