From f4cf906127a1f12ade2dbf42e7f5f891e2049518 Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Mon, 28 Nov 2016 21:19:53 -0600 Subject: [PATCH 1/4] Added remove when previous login incentive notifications exist --- test/api/v3/unit/libs/cron.test.js | 14 +++++++++++++- website/server/libs/cron.js | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/test/api/v3/unit/libs/cron.test.js b/test/api/v3/unit/libs/cron.test.js index 3a8843e283..73b90050b1 100644 --- a/test/api/v3/unit/libs/cron.test.js +++ b/test/api/v3/unit/libs/cron.test.js @@ -6,7 +6,7 @@ import requireAgain from 'require-again'; import { recoverCron, cron } from '../../../../../website/server/libs/cron'; import { model as User } from '../../../../../website/server/models/user'; import * as Tasks from '../../../../../website/server/models/task'; -import { clone } from 'lodash'; +import { clone, filter } from 'lodash'; import common from '../../../../../website/common'; import analytics from '../../../../../website/server/libs/analyticsService'; @@ -780,6 +780,18 @@ describe('cron', () => { expect(user.notifications[0].type).to.eql('LOGIN_INCENTIVE'); }); + it('replaces previous notifications', () => { + cron({user, tasksByType, daysMissed, analytics}); + cron({user, tasksByType, daysMissed, analytics}); + cron({user, tasksByType, daysMissed, analytics}); + + let filteredNotifications = filter(user.notifications, function filterNotifications (notification) { + return notification.type === 'LOGIN_INCENTIVE'; + }); + + expect(filteredNotifications.length).to.equal(1); + }); + it('increments loginIncentives by 1 even if days are skipped in between', () => { daysMissed = 3; cron({user, tasksByType, daysMissed, analytics}); diff --git a/website/server/libs/cron.js b/website/server/libs/cron.js index 8cacc68323..b34c332c11 100644 --- a/website/server/libs/cron.js +++ b/website/server/libs/cron.js @@ -130,6 +130,14 @@ function trackCronAnalytics (analytics, user, _progress, options) { function awardLoginIncentives (user) { if (user.loginIncentives > 50) return; + + //Remove old noitification if it exists + user.notifications + .toObject() + .find((notif, index) => { + if (notif.type === 'LOGIN_INCENTIVE') user.notifications.splice(index, 1); + }); + let notificationData = {}; notificationData.message = i18n.t('checkinEarned', user.preferences.language); From 1d2482f8bc288f1a31f82c91f0dd4e864fafaa7a Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Mon, 28 Nov 2016 21:24:25 -0600 Subject: [PATCH 2/4] Fixed linting issues --- test/api/v3/unit/libs/cron.test.js | 2 +- website/server/libs/cron.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/api/v3/unit/libs/cron.test.js b/test/api/v3/unit/libs/cron.test.js index 73b90050b1..d65409d727 100644 --- a/test/api/v3/unit/libs/cron.test.js +++ b/test/api/v3/unit/libs/cron.test.js @@ -786,7 +786,7 @@ describe('cron', () => { cron({user, tasksByType, daysMissed, analytics}); let filteredNotifications = filter(user.notifications, function filterNotifications (notification) { - return notification.type === 'LOGIN_INCENTIVE'; + return notification.type === 'LOGIN_INCENTIVE'; }); expect(filteredNotifications.length).to.equal(1); diff --git a/website/server/libs/cron.js b/website/server/libs/cron.js index b34c332c11..eb5f2f7d34 100644 --- a/website/server/libs/cron.js +++ b/website/server/libs/cron.js @@ -131,7 +131,7 @@ function trackCronAnalytics (analytics, user, _progress, options) { function awardLoginIncentives (user) { if (user.loginIncentives > 50) return; - //Remove old noitification if it exists + // Remove old noitification if it exists user.notifications .toObject() .find((notif, index) => { From 8582a67308a13bcf6709be8b38108e591439d8bd Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Tue, 29 Nov 2016 08:53:36 -0600 Subject: [PATCH 3/4] Fixed broken tests and style changes --- test/api/v3/unit/libs/cron.test.js | 14 ++++++-------- website/server/libs/cron.js | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/test/api/v3/unit/libs/cron.test.js b/test/api/v3/unit/libs/cron.test.js index d65409d727..75d31f5f4a 100644 --- a/test/api/v3/unit/libs/cron.test.js +++ b/test/api/v3/unit/libs/cron.test.js @@ -704,14 +704,14 @@ describe('cron', () => { cron({user, tasksByType, daysMissed, analytics}); - expect(user.notifications.length - notifsBefore2).to.equal(1); - expect(user.notifications[1].type).to.not.equal('CRON'); - expect(user.notifications[2].type).to.equal('CRON'); - expect(user.notifications[2].data).to.eql({ + expect(user.notifications.length - notifsBefore2).to.equal(0); + expect(user.notifications[0].type).to.not.equal('CRON'); + expect(user.notifications[1].type).to.equal('CRON'); + expect(user.notifications[1].data).to.eql({ hp: user.stats.hp - hpBefore2 - (hpBefore2 - hpBefore1), mp: user.stats.mp - mpBefore2 - (mpBefore2 - mpBefore1), }); - expect(user.notifications[1].type).to.not.equal('CRON'); + expect(user.notifications[0].type).to.not.equal('CRON'); }); }); @@ -785,9 +785,7 @@ describe('cron', () => { cron({user, tasksByType, daysMissed, analytics}); cron({user, tasksByType, daysMissed, analytics}); - let filteredNotifications = filter(user.notifications, function filterNotifications (notification) { - return notification.type === 'LOGIN_INCENTIVE'; - }); + let filteredNotifications = user.notifications.filter(n => n.type === 'LOGIN_INCENTIVE'); expect(filteredNotifications.length).to.equal(1); }); diff --git a/website/server/libs/cron.js b/website/server/libs/cron.js index eb5f2f7d34..367356922b 100644 --- a/website/server/libs/cron.js +++ b/website/server/libs/cron.js @@ -131,7 +131,7 @@ function trackCronAnalytics (analytics, user, _progress, options) { function awardLoginIncentives (user) { if (user.loginIncentives > 50) return; - // Remove old noitification if it exists + // Remove old notifications if they exists user.notifications .toObject() .find((notif, index) => { From bab41647f5807aeeafb7b958b75c923e55af6fee Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Tue, 29 Nov 2016 09:18:07 -0600 Subject: [PATCH 4/4] Fixed lint issues --- test/api/v3/unit/libs/cron.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/api/v3/unit/libs/cron.test.js b/test/api/v3/unit/libs/cron.test.js index 75d31f5f4a..5c26fe798b 100644 --- a/test/api/v3/unit/libs/cron.test.js +++ b/test/api/v3/unit/libs/cron.test.js @@ -6,7 +6,7 @@ import requireAgain from 'require-again'; import { recoverCron, cron } from '../../../../../website/server/libs/cron'; import { model as User } from '../../../../../website/server/models/user'; import * as Tasks from '../../../../../website/server/models/task'; -import { clone, filter } from 'lodash'; +import { clone } from 'lodash'; import common from '../../../../../website/common'; import analytics from '../../../../../website/server/libs/analyticsService';