fix current streak,

longest streak still broken
This commit is contained in:
Berkeley Martinez
2015-08-19 13:05:53 -07:00
parent 6a11b30bc2
commit 576315abdc

View File

@ -4,24 +4,33 @@ var _ = require('lodash'),
debug = require('debug')('freecc:cntr:userController');
const daysBetween = 1.5;
function calcCurrentStreak(cals) {
const revCals = cals.slice().reverse();
let streakBroken = false;
return revCals
const lastDayInStreak = revCals
.reduce((current, cal, index) => {
// if streak not borken and diff between this cal and the call after it
// is equal to zero
// moment.diff will return the days between rounded down
const before = revCals[index === 0 ? 0 : index - 1];
if (
!streakBroken &&
moment(revCals[index === 0 ? 0 : index - 1]).diff(cal, 'days') === 0
moment(before).diff(cal, 'days', true) < daysBetween
) {
return current + 1;
return index;
}
return 1;
}, 1);
return current;
}, 0);
const lastTimestamp = revCals[lastDayInStreak];
return Math.ceil(moment().diff(lastTimestamp, 'days', true));
}
// TODO(berks): calc longest streak
/*
function longestStreak(cals) {
}
*/
module.exports = function(app) {
var router = app.loopback.Router();
var User = app.models.User;
@ -129,9 +138,7 @@ module.exports = function(app) {
objOrNum :
objOrNum.timestamp;
})
.map(time => {
return moment(time).format('YYYY-MM-DD');
});
.sort();
user.currentStreak = calcCurrentStreak(cals);
@ -146,6 +153,9 @@ module.exports = function(app) {
objOrNum :
objOrNum.timestamp;
})
.filter((timestamp) => {
return !!timestamp;
})
.reduce((data, timeStamp) => {
data[(timeStamp / 1000)] = 1;
return data;