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

Commit 8f8b479

Browse files
feat(#4): push a daily reminder in case of viewings
1 parent 0606c60 commit 8f8b479

File tree

10 files changed

+180
-68
lines changed

10 files changed

+180
-68
lines changed

config/locales/de.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@
2121
"ITEMS": {
2222
"NEW_ITEMS": "Hey {{who}}, einige neue Inserate passen zu deinem Profil 🔍👍😃",
2323
"NEW_CANDIDATES": "Hey {{who}}, erweitert die Sichtbarkeit deiner Anzeige, um mehr Kandidaten zu erreichen 😉"
24+
},
25+
"REMINDER": {
26+
"TODAY_APPOINTMENT": "Hey {{who}}, cool du hast eine Besichtigung heute ⏰😃",
27+
"TODAY_APPOINTMENTS": "Hey {{who}}, cool du hast eine Besichtigungen heute ⏰😃"
2428
}
2529
}

config/locales/en.json

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
{
2-
"NOTIFICATIONS": {
3-
"ADVERTISE": {
4-
"TITLE": "Congrats!",
5-
"ONE_APPLICANT": "{{who}} has applied for an appointment 🎉"
6-
},
7-
"BROWSE": {
8-
"TITLE": "Hey!",
9-
"VIEWING_ACCEPTED": "{{who}} has accepted your application 🎉",
10-
"NEED_NEW_DATES": "Chat with {{who}} to schedule the viewing 🎉😃",
11-
"NEW_SUPERSTAR": "{{who}} has sent you a Superstar ⭐",
12-
"APPOINTMENT_RESCHEDULED": "{{who}} has rescheduled the appointment 📅"
13-
}
14-
},
15-
"CHAT_MESSAGES": {
16-
"NEW_CHAT_MESSAGE": "You've got a new message from {{who}} 💬"
17-
},
18-
"SUBSCRIPTIONS": {
19-
"ACTIVATED": "Awesome! You upgrade to FLUSTER PLUS is done, enjoy!"
20-
},
21-
"ITEMS": {
22-
"NEW_ITEMS": "Hey {{who}}, some new posts are matching your profile 🔍👍😃",
23-
"NEW_CANDIDATES": "Hey {{who}}, extend the visibility of your ad to reach more candidates 😉"
24-
}
2+
"NOTIFICATIONS": {
3+
"ADVERTISE": {
4+
"TITLE": "Congrats!",
5+
"ONE_APPLICANT": "{{who}} has applied for an appointment 🎉"
6+
},
7+
"BROWSE": {
8+
"TITLE": "Hey!",
9+
"VIEWING_ACCEPTED": "{{who}} has accepted your application 🎉",
10+
"NEED_NEW_DATES": "Chat with {{who}} to schedule the viewing 🎉😃",
11+
"NEW_SUPERSTAR": "{{who}} has sent you a Superstar ⭐",
12+
"APPOINTMENT_RESCHEDULED": "{{who}} has rescheduled the appointment 📅"
13+
}
14+
},
15+
"CHAT_MESSAGES": {
16+
"NEW_CHAT_MESSAGE": "You've got a new message from {{who}} 💬"
17+
},
18+
"SUBSCRIPTIONS": {
19+
"ACTIVATED": "Awesome! You upgrade to FLUSTER PLUS is done, enjoy!"
20+
},
21+
"ITEMS": {
22+
"NEW_ITEMS": "Hey {{who}}, some new posts are matching your profile 🔍👍😃",
23+
"NEW_CANDIDATES": "Hey {{who}}, extend the visibility of your ad to reach more candidates 😉"
24+
},
25+
"REMINDER": {
26+
"TODAY_APPOINTMENT": "Hey {{who}}, cool you have an upcoming viewing today ⏰😃",
27+
"TODAY_APPOINTMENTS": "Hey {{who}}, cool you have upcoming viewings today ⏰😃"
28+
}
2529
}

config/locales/fr.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@
2121
"ITEMS": {
2222
"NEW_ITEMS": "Hey {{who}}, des nouvelles annonces correspondent à ton profil 🔍👍😃",
2323
"NEW_CANDIDATES": "Hey {{who}}, étends la visibilité de ton annonce pour atteindre plus de candidats 😉"
24+
},
25+
"REMINDER": {
26+
"TODAY_APPOINTMENT": "Hey {{who}}, cool tu as une visite aujourd'hui ⏰😃",
27+
"TODAY_APPOINTMENTS": "Hey {{who}}, cool tu as des visites aujourd'hui ⏰😃"
2428
}
2529
}

config/locales/it.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@
2121
"ITEMS": {
2222
"NEW_ITEMS": "Ciao {{who}}, nuove camere e appartamenti corrispondoni alla tuo profilo 🔍👍😃",
2323
"NEW_CANDIDATES": "Hey {{who}}, espandi la visibilità del tuo annuncio per raggiungere più candidati 😉"
24+
},
25+
"REMINDER": {
26+
"TODAY_APPOINTMENT": "Hey {{who}}, cool hai una visita oggi ⏰😃",
27+
"TODAY_APPOINTMENTS": "Hey {{who}}, cool hai visite oggi ⏰😃"
2428
}
2529
}

cronjobs/cronjobs.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const pushNotifications = require('./push-notifications');
44
const pushChatMessages = require('./push-chat-messages');
55
const pushNewItems = require('./push-new-items');
66
const pushNewCandidates = require('./push-new-candidates');
7+
const pushAppointmentsReminder = require('./push-appointments-reminder');
78

89
const emailAdminNewItems = require('./email-admin-new-items');
910

@@ -51,3 +52,12 @@ const pushNewCandidatesJob = new cron.CronJob('0 0 19 * * 0,2,4', function() {
5152
true, /* Start the job right now */
5253
'Europe/Paris'
5354
);
55+
56+
const pushAppointmentsReminderJob = new cron.CronJob('0 56 7 * * *', function() {
57+
pushAppointmentsReminder.pushReminder();
58+
}, function () {
59+
/* This function is executed when the job stops */
60+
},
61+
true, /* Start the job right now */
62+
'Europe/Paris'
63+
);

cronjobs/helpers/push-candidates-helper.js

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
const Device = require('../../model/device');
2-
3-
const logger = require('log4js').getLogger('peterparker');
4-
51
const utils = require('../../controllers/utils/utils');
62

73
const constants = require('../../config/constants');
84

95
const CandidatesHelper = require('../../controllers/candidates/candidatesHelper');
106

11-
const pushSender = require('./push-sender');
12-
13-
const i18n = require("i18n");
14-
157
const Q = require('q');
168

179
const _ = require('underscore');
1810

1911
module.exports = {
20-
findCandidates: findCandidates,
21-
sendPushNotification: sendPushNotification
12+
findCandidates: findCandidates
2213
};
2314

2415
function findCandidates(item, ageMin, ageMax, lastLoginInDays) {
@@ -87,36 +78,3 @@ function findCandidates(item, ageMin, ageMax, lastLoginInDays) {
8778

8879
return deferred.promise;
8980
}
90-
91-
function sendPushNotification(user, labelKey) {
92-
Device.findDevice(user._id).then(function (device) {
93-
if (utils.isNotNull(device) && !utils.isStringEmpty(device.tokenId)) {
94-
processNofication(user, device, labelKey);
95-
}
96-
}, function (error) {
97-
logger.info('error', 'Error while looking for device informations.');
98-
});
99-
}
100-
101-
function processNofication(user, device, labelKey) {
102-
103-
if (utils.isNotNull(user.userParams) && utils.isNotNull(user.userParams.appSettings) && user.userParams.appSettings.pushNotifications) {
104-
105-
const msgText = getPushNotificationText(user, device, labelKey);
106-
107-
pushSender.pushNotification(msgText, device).then(function (data) {
108-
// Coolio all right here
109-
}, function (error) {
110-
logger.info('error', 'Error while pushing the notification to the client. ' + JSON.stringify(error));
111-
});
112-
} else {
113-
// Means user don't want to receive push notifications
114-
}
115-
}
116-
117-
function getPushNotificationText(user, device, labelKey) {
118-
119-
const language = !utils.isStringEmpty(device.language) ? device.language : 'en';
120-
121-
return i18n.__({phrase: labelKey, locale: language}, {who: user.facebook.firstName});
122-
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const Device = require('../../model/device');
2+
3+
const logger = require('log4js').getLogger('peterparker');
4+
5+
const pushSender = require('./push-sender');
6+
7+
const i18n = require("i18n");
8+
9+
const utils = require('../../controllers/utils/utils');
10+
11+
module.exports = {
12+
sendPushNotification: sendPushNotification
13+
};
14+
15+
function sendPushNotification(user, labelKey) {
16+
Device.findDevice(user._id).then(function (device) {
17+
if (utils.isNotNull(device) && !utils.isStringEmpty(device.tokenId)) {
18+
processNofication(user, device, labelKey);
19+
}
20+
}, function (error) {
21+
logger.info('error', 'Error while looking for device informations.');
22+
});
23+
}
24+
25+
function processNofication(user, device, labelKey) {
26+
27+
if (utils.isNotNull(user.userParams) && utils.isNotNull(user.userParams.appSettings) && user.userParams.appSettings.pushNotifications) {
28+
29+
const msgText = getPushNotificationText(user, device, labelKey);
30+
31+
pushSender.pushNotification(msgText, device).then(function (data) {
32+
// Coolio all right here
33+
}, function (error) {
34+
logger.info('error', 'Error while pushing the notification to the client. ' + JSON.stringify(error));
35+
});
36+
} else {
37+
// Means user don't want to receive push notifications
38+
}
39+
}
40+
41+
function getPushNotificationText(user, device, labelKey) {
42+
43+
const language = !utils.isStringEmpty(device.language) ? device.language : 'en';
44+
45+
return i18n.__({phrase: labelKey, locale: language}, {who: user.facebook.firstName});
46+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const mongoose = require('mongoose');
2+
3+
const Applicant = mongoose.model('Applicant');
4+
const User = mongoose.model('User');
5+
6+
const utils = require('../controllers/utils/utils');
7+
8+
const moment = require('moment');
9+
10+
const pushUsersHelper = require('./helpers/push-users-helper');
11+
12+
const Q = require('q');
13+
14+
const logger = require('log4js').getLogger('peterparker');
15+
16+
module.exports = {
17+
pushReminder: pushReminder
18+
};
19+
20+
function pushReminder() {
21+
22+
if (!constants.ARN_IOS_PROD) {
23+
return;
24+
}
25+
26+
const now = new Date();
27+
const begin = moment(now).startOf('day').toDate();
28+
const end = moment(now).endOf('day').toDate();
29+
30+
const query = {
31+
status: 'accepted',
32+
selected: {$gte: begin, $lt: end}
33+
};
34+
35+
Applicant.aggregate([{$match: query}, {$group: {_id: {user: "$user"}, count: {$sum: 1}}}], (err, applicants) => {
36+
if (err) {
37+
logger.info('error', 'Error while looking for applicants.');
38+
} else if (utils.isNotEmpty(applicants)) {
39+
40+
// Deep (sub) populate users
41+
Applicant.populate(applicants, {
42+
path: "_id.user",
43+
model: User,
44+
options: {lean: true},
45+
select: "_id userParams facebook.firstName"
46+
}).then(async (deepPopulatedApplicants) => {
47+
await pushNotifications(deepPopulatedApplicants);
48+
}, (err) => {
49+
logger.info('error', 'Error while populating the applicants.');
50+
});
51+
52+
}
53+
});
54+
}
55+
56+
function pushNotifications(deepPopulatedApplicants) {
57+
const deferred = Q.defer();
58+
59+
if (utils.isNotEmpty(deepPopulatedApplicants)) {
60+
61+
logger.info("Gonna try to send new items push notifications to " + deepPopulatedApplicants.length + " users.");
62+
63+
for (let i = 0, len = deepPopulatedApplicants.length; i < len; i++) {
64+
if (utils.isNotNull(deepPopulatedApplicants[i]._id) && utils.isNotNull(deepPopulatedApplicants[i]._id.user)) {
65+
66+
const labelKey = deepPopulatedApplicants[i].count > 1 ? 'REMINDER.TODAY_APPOINTMENTS' : 'REMINDER.TODAY_APPOINTMENT';
67+
68+
pushUsersHelper.sendPushNotification(deepPopulatedApplicants[i]._id.user, labelKey);
69+
}
70+
}
71+
72+
deferred.resolve();
73+
} else {
74+
deferred.resolve();
75+
}
76+
77+
return deferred.promise;
78+
}

cronjobs/push-new-candidates.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const constants = require('../config/constants');
1010

1111
const pushCandidatesHelper = require('./helpers/push-candidates-helper');
1212

13+
const pushUsersHelper = require('./helpers/push-users-helper');
14+
1315
const moment = require('moment');
1416
const Q = require('q');
1517

@@ -60,7 +62,7 @@ function pushNewCandidates() {
6062

6163
for (let i = 0, len = values.length; i < len; i++) {
6264
if (values[i].hasCandidates) {
63-
pushCandidatesHelper.sendPushNotification(values[i].item.user, 'ITEMS.NEW_CANDIDATES');
65+
pushUsersHelper.sendPushNotification(values[i].item.user, 'ITEMS.NEW_CANDIDATES');
6466
loggerCount++;
6567
}
6668
}

cronjobs/push-new-items.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const constants = require('../config/constants');
1010

1111
const pushCandidatesHelper = require('./helpers/push-candidates-helper');
1212

13+
const pushUsersHelper = require('./helpers/push-users-helper');
14+
1315
const moment = require('moment');
1416
const _ = require('underscore');
1517

@@ -62,7 +64,7 @@ function pushNewItems() {
6264
logger.info("Gonna try to send new items push notifications to " + uniqueUsers.length + " users.");
6365

6466
for (let i = 0, len = uniqueUsers.length; i < len; i++) {
65-
pushCandidatesHelper.sendPushNotification(uniqueUsers[i], 'ITEMS.NEW_ITEMS');
67+
pushUsersHelper.sendPushNotification(uniqueUsers[i], 'ITEMS.NEW_ITEMS');
6668
}
6769
}
6870
})

0 commit comments

Comments
 (0)