Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.

Commit 4761671

Browse files
feat(#1): run push new items notifications hourly
1 parent 7fa7905 commit 4761671

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

cronjobs/cronjobs.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const pushNewItems = require('./push-new-items');
66

77
const emailAdminNewItems = require('./email-admin-new-items');
88

9-
const pushNotificationsJob = new cron.CronJob('0 */1 8-23 * * *', function() {
9+
const pushNotificationsJob = new cron.CronJob('0 */1 7-23 * * *', function() {
1010
pushNotifications.pushPendingNotifications();
1111
}, function () {
1212
/* This function is executed when the job stops */
@@ -15,7 +15,7 @@ const pushNotificationsJob = new cron.CronJob('0 */1 8-23 * * *', function() {
1515
'Europe/Paris'
1616
);
1717

18-
const pushChatMessagesJob = new cron.CronJob('0 */5 8-23 * * *', function() {
18+
const pushChatMessagesJob = new cron.CronJob('0 */5 7-23 * * *', function() {
1919
pushChatMessages.pushPendingChatMessages();
2020
}, function () {
2121
/* This function is executed when the job stops */
@@ -24,8 +24,7 @@ const pushChatMessagesJob = new cron.CronJob('0 */5 8-23 * * *', function() {
2424
'Europe/Paris'
2525
);
2626

27-
// Each day at 19:26
28-
const pushNewItemsJob = new cron.CronJob('00 26 19 * * *', function() {
27+
const pushNewItemsJob = new cron.CronJob('00 26 7-23 * * *', function() {
2928
pushNewItems.pushNewItems();
3029
}, function () {
3130
/* This function is executed when the job stops */
@@ -34,7 +33,7 @@ const pushNewItemsJob = new cron.CronJob('00 26 19 * * *', function() {
3433
'Europe/Paris'
3534
);
3635

37-
const emailAdminNewItemsJob = new cron.CronJob('0 */5 7-23 * * *', function() {
36+
const emailAdminNewItemsJob = new cron.CronJob('0 */2 7-23 * * *', function() {
3837
emailAdminNewItems.sendNewItems();
3938
}, function () {
4039
/* This function is executed when the job stops */

cronjobs/push-new-items.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
var mongoose = require('mongoose');
1+
const mongoose = require('mongoose');
22

3-
var Item = mongoose.model('Item');
4-
var CandidatesHelper = require('../controllers/candidates/candidatesHelper');
3+
const Item = mongoose.model('Item');
4+
const CandidatesHelper = require('../controllers/candidates/candidatesHelper');
55

6-
var Device = require('../model/device');
6+
const Device = require('../model/device');
77

8-
var logger = require('log4js').getLogger('peterparker');
8+
const logger = require('log4js').getLogger('peterparker');
99

10-
var utils = require('../controllers/utils/utils');
10+
const utils = require('../controllers/utils/utils');
1111

12-
var constants = require('../config/constants');
12+
const constants = require('../config/constants');
1313

14-
var pushSender = require('./push-sender');
14+
const pushSender = require('./push-sender');
1515

16-
var i18n = require("i18n");
16+
const i18n = require("i18n");
1717

18-
var moment = require('moment');
19-
var Q = require('q');
20-
var _ = require('underscore');
18+
const moment = require('moment');
19+
const Q = require('q');
20+
const _ = require('underscore');
2121

2222
module.exports = {
2323
pushNewItems: pushNewItems
@@ -29,28 +29,30 @@ function pushNewItems() {
2929
return;
3030
}
3131

32-
var now = new Date();
33-
var sinceYesterday = moment(now).add(-1, 'd').toDate();
32+
const now = new Date();
33+
const todayAt8am = moment().hours(8).minutes(0).seconds(0).toDate();
3434

35-
var query = {
35+
let sinceWhen = moment(now).isBefore(todayAt8am) ? moment(now).add(-8, 'h').toDate() : moment(now).add(-1, 'h').toDate();
36+
37+
const query = {
3638
status: 'published',
37-
createdAt: {$gte: sinceYesterday, $lte: now}
39+
createdAt: {$gt: sinceWhen, $lte: now}
3840
};
3941

4042
Item.find(query).lean().exec(function (err, items) {
4143
if (err) {
4244
logger.info('error', 'Error while looking for notifications.');
4345
} else {
4446
if (utils.isNotEmpty(items)) {
45-
var promises = new Array();
47+
const promises = new Array();
4648

47-
for (var i = 0, len = items.length; i < len; i++) {
49+
for (let i = 0, len = items.length; i < len; i++) {
4850
promises.push(targetUsersAndSendPushNotification(items[i]));
4951
}
5052

5153
Promise.all(promises).then(function (values) {
5254

53-
var uniqueUsers = _.map(_.groupBy(_.flatten(values), function (doc) {
55+
const uniqueUsers = _.map(_.groupBy(_.flatten(values), function (doc) {
5456
return doc._id;
5557
}), function (grouped) {
5658
return grouped[0];
@@ -60,7 +62,7 @@ function pushNewItems() {
6062

6163
logger.info("Gonna try to send new items push notifications to " + uniqueUsers.length + " users.");
6264

63-
for (var i = 0, len = uniqueUsers.length; i < len; i++) {
65+
for (let i = 0, len = uniqueUsers.length; i < len; i++) {
6466
sendPushNotification(uniqueUsers[i]);
6567
}
6668
}
@@ -71,11 +73,11 @@ function pushNewItems() {
7173
}
7274

7375
function targetUsersAndSendPushNotification(item) {
74-
var deferred = Q.defer();
76+
const deferred = Q.defer();
7577

76-
var candidatesHelper = new CandidatesHelper();
78+
const candidatesHelper = new CandidatesHelper();
7779

78-
var query = {
80+
let query = {
7981
longitude: item.address.location.coordinates[0],
8082
latitude: item.address.location.coordinates[1],
8183
type: item.attributes.type,
@@ -109,15 +111,15 @@ function targetUsersAndSendPushNotification(item) {
109111
query["availableend"] = '' + item.attributes.availability.end;
110112
}
111113

112-
var likes = _.map(item.likes, function (doc) {
114+
let likes = _.map(item.likes, function (doc) {
113115
return doc.user;
114116
});
115117

116-
var dislikes = _.map(item.dislikes, function (doc) {
118+
const dislikes = _.map(item.dislikes, function (doc) {
117119
return doc.user;
118120
});
119121

120-
var userIds = new Array();
122+
let userIds = new Array();
121123
userIds.push(item.user);
122124

123125
if (utils.isNotEmpty(likes)) {
@@ -151,7 +153,7 @@ function processNofication(user, device) {
151153

152154
if (utils.isNotNull(user.userParams) && utils.isNotNull(user.userParams.appSettings) && user.userParams.appSettings.pushNotifications) {
153155

154-
var msgText = getPushNotificationText(user, device);
156+
const msgText = getPushNotificationText(user, device);
155157

156158
pushSender.pushNotification(msgText, device).then(function (data) {
157159
// Coolio all right here
@@ -165,7 +167,7 @@ function processNofication(user, device) {
165167

166168
function getPushNotificationText(user, device) {
167169

168-
var language = !utils.isStringEmpty(device.language) ? device.language : 'en';
170+
const language = !utils.isStringEmpty(device.language) ? device.language : 'en';
169171

170172
return i18n.__({phrase: "ITEMS.NEW_ITEMS", locale: language}, {who: user.facebook.firstName});
171173
}

0 commit comments

Comments
 (0)