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

Commit 73070cc

Browse files
merge(#3): push notifications about potential candidates
2 parents 020638e + eca555e commit 73070cc

File tree

14 files changed

+291
-136
lines changed

14 files changed

+291
-136
lines changed

config/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ define("DEFAULT_DISTANCE", 99);
1616

1717
// Discover roommates
1818
define("LIMIT_CANDIDATES", 40); // Find only roommates active in the last x days
19+
define("LIMIT_RECENT_CANDIDATES", 2);
1920
define("LIMIT_TARGETED_USERS", 60);
2021

2122
// Item

config/locales/de.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"ACTIVATED": "Genial! FLUSTER PLUS steht nun zu deiner Verfügung, viel Spass!"
2020
},
2121
"ITEMS": {
22-
"NEW_ITEMS": "Hoi {{who}}, einige neue Inserate passen zu deinem Profil 🔍👍😃"
22+
"NEW_ITEMS": "Hey {{who}}, einige neue Inserate passen zu deinem Profil 🔍👍😃",
23+
"NEW_CANDIDATES": "Hey {{who}}, erweitert die Sichtbarkeit deiner Anzeige, um mehr Kandidaten zu erreichen 😉"
2324
}
2425
}

config/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"ACTIVATED": "Awesome! You upgrade to FLUSTER PLUS is done, enjoy!"
2020
},
2121
"ITEMS": {
22-
"NEW_ITEMS": "Hey {{who}}, some new posts are matching your profile 🔍👍😃"
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 😉"
2324
}
2425
}

config/locales/fr.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"ACTIVATED": "Fantastique! Tu disposes dès à présent de FLUSTER PLUS!"
2020
},
2121
"ITEMS": {
22-
"NEW_ITEMS": "Hey {{who}}, des nouvelles annonces correspondent à ton profil 🔍👍😃"
22+
"NEW_ITEMS": "Hey {{who}}, des nouvelles annonces correspondent à ton profil 🔍👍😃",
23+
"NEW_CANDIDATES": "Hey {{who}}, étends la visibilité de ton annonce pour atteindre plus de candidats 😉"
2324
}
2425
}

config/locales/it.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"ACTIVATED": "Eccezionale! FLUSTER PLUS è ora disponibile a tua disposizione"
2020
},
2121
"ITEMS": {
22-
"NEW_ITEMS": "Ciao {{who}}, nuove camere e appartamenti corrispondoni alla tuo profilo 🔍👍😃"
22+
"NEW_ITEMS": "Ciao {{who}}, nuove camere e appartamenti corrispondoni alla tuo profilo 🔍👍😃",
23+
"NEW_CANDIDATES": "Hey {{who}}, espandi la visibilità del tuo annuncio per raggiungere più candidati 😉"
2324
}
2425
}

controllers/candidates/candidatesHelper.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ const utils = require('../utils/utils');
77

88
const queryUtils = require('../utils/query-utils');
99

10-
const constants = require('../../config/constants');
11-
1210
const Q = require('q');
1311

1412
function CandidatesHelper() {
1513
this.findCandidates = findCandidates;
1614
}
1715

18-
function findCandidates(userIds, page, limit, params) {
16+
function findCandidates(userIds, page, limit, params, lastLoginInDays) {
1917
let deferred = Q.defer();
2018

21-
const startLastLogin = moment(new Date()).startOf('day').add(-1 * constants.LIMIT_CANDIDATES, 'd').toDate();
19+
const startLastLogin = moment(new Date()).startOf('day').add(-1 * lastLoginInDays, 'd').toDate();
2220

2321
// Prepare the query
2422
let query = {

controllers/candidates/get-candidates.js

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

3-
var CandidatesHelper = require('./candidatesHelper');
3+
const CandidatesHelper = require('./candidatesHelper');
44

5-
var utils = require('../utils/utils');
5+
const utils = require('../utils/utils');
66

7-
var constants = require('../../config/constants');
7+
const constants = require('../../config/constants');
88

9-
module.exports.getCandidates = function (req, res, next) {
10-
var userId = req.params.userId || req.body.userId || req.query.userId || null;
9+
module.exports.getCandidates = (req, res, next) => {
10+
const userId = req.params.userId || req.body.userId || req.query.userId || null;
1111

12-
var userIds = new Array();
12+
const userIds = new Array();
1313
userIds.push(new mongoose.Types.ObjectId(userId));
1414

15-
var limit = constants.LIMIT_QUERY;
16-
var page = null;
15+
const limit = constants.LIMIT_QUERY;
16+
let page = null;
1717
if (!utils.isStringEmpty(req.query.pageIndex)) {
1818
page = parseInt(req.query.pageIndex) * limit;
1919
}
2020

21-
var candidatesHelper = new CandidatesHelper();
21+
const candidatesHelper = new CandidatesHelper();
2222

23-
candidatesHelper.findCandidates(userIds, page, limit, req.query).then(function (users) {
23+
candidatesHelper.findCandidates(userIds, page, limit, req.query, constants.LIMIT_CANDIDATES).then((users) => {
2424
res.format({
25-
json: function () {
25+
json: () => {
2626
res.json(users);
2727
}
2828
});
29-
}, function (err) {
29+
}, (err) => {
3030
res.status(500).json({
3131
error: "Get users error:" + err
3232
});

cronjobs/cronjobs.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const cron = require('cron');
33
const pushNotifications = require('./push-notifications');
44
const pushChatMessages = require('./push-chat-messages');
55
const pushNewItems = require('./push-new-items');
6+
const pushNewCandidates = require('./push-new-candidates');
67

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

@@ -24,7 +25,7 @@ const pushChatMessagesJob = new cron.CronJob('0 */5 7-23 * * *', function() {
2425
'Europe/Paris'
2526
);
2627

27-
const pushNewItemsJob = new cron.CronJob('00 26 7-23 * * *', function() {
28+
const pushNewItemsJob = new cron.CronJob('0 26 7-23 * * *', function() {
2829
pushNewItems.pushNewItems();
2930
}, function () {
3031
/* This function is executed when the job stops */
@@ -40,4 +41,13 @@ const emailAdminNewItemsJob = new cron.CronJob('0 */2 7-23 * * *', function() {
4041
},
4142
true, /* Start the job right now */
4243
'Europe/Paris'
43-
);
44+
);
45+
46+
const pushNewCandidatesJob = new cron.CronJob('0 0 19 * * 0,2,4', function() {
47+
pushNewCandidates.pushNewCandidates();
48+
}, function () {
49+
/* This function is executed when the job stops */
50+
},
51+
true, /* Start the job right now */
52+
'Europe/Paris'
53+
);
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
const Device = require('../../model/device');
2+
3+
const logger = require('log4js').getLogger('peterparker');
4+
5+
const utils = require('../../controllers/utils/utils');
6+
7+
const constants = require('../../config/constants');
8+
9+
const CandidatesHelper = require('../../controllers/candidates/candidatesHelper');
10+
11+
const pushSender = require('./push-sender');
12+
13+
const i18n = require("i18n");
14+
15+
const Q = require('q');
16+
17+
const _ = require('underscore');
18+
19+
module.exports = {
20+
findCandidates: findCandidates,
21+
sendPushNotification: sendPushNotification
22+
};
23+
24+
function findCandidates(item, ageMin, ageMax, lastLoginInDays) {
25+
const deferred = Q.defer();
26+
27+
const candidatesHelper = new CandidatesHelper();
28+
29+
let query = {
30+
longitude: item.address.location.coordinates[0],
31+
latitude: item.address.location.coordinates[1],
32+
type: item.attributes.type,
33+
furnished: item.attributes.furnished,
34+
ageMin: ageMin,
35+
ageMax: ageMax,
36+
gender: item.userLimitations.gender
37+
};
38+
39+
if (utils.isNotNull(item.attributes.rooms) && item.attributes.rooms !== 0) {
40+
query["rooms"] = '' + item.attributes.rooms;
41+
}
42+
43+
if (utils.isNotNull(item.attributes.price.gross) && item.attributes.price.gross !== 0) {
44+
query["price"] = '' + item.attributes.price.gross;
45+
}
46+
47+
if (utils.isNotNull(item.attributes.disabledFriendly)) {
48+
query["disabledFriendly"] = '' + item.attributes.disabledFriendly;
49+
}
50+
51+
if (utils.isNotNull(item.attributes.petsAllowed)) {
52+
query["petsAllowed"] = '' + item.attributes.petsAllowed;
53+
}
54+
55+
if (utils.isNotNull(item.attributes.availability.begin)) {
56+
query["availablebegin"] = '' + item.attributes.availability.begin;
57+
}
58+
59+
if (utils.isNotNull(item.attributes.availability.end)) {
60+
query["availableend"] = '' + item.attributes.availability.end;
61+
}
62+
63+
let likes = _.map(item.likes, function (doc) {
64+
return doc.user;
65+
});
66+
67+
const dislikes = _.map(item.dislikes, function (doc) {
68+
return doc.user;
69+
});
70+
71+
let userIds = new Array();
72+
userIds.push(item.user);
73+
74+
if (utils.isNotEmpty(likes)) {
75+
userIds = userIds.concat(likes);
76+
}
77+
78+
if (utils.isNotEmpty(dislikes)) {
79+
userIds = userIds.concat(dislikes);
80+
}
81+
82+
candidatesHelper.findCandidates(userIds, 0, constants.MAX_ITEM_USERS, query, lastLoginInDays).then((users) => {
83+
deferred.resolve(users);
84+
}, (err) => {
85+
deferred.reject(new Error(err));
86+
});
87+
88+
return deferred.promise;
89+
}
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+
}

cronjobs/push-sender.js renamed to cronjobs/helpers/push-sender.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var utils = require('../controllers/utils/utils');
2-
var constants = require('../config/constants');
1+
var utils = require('../../controllers/utils/utils');
2+
var constants = require('../../config/constants');
33

44
var Q = require('q');
55

0 commit comments

Comments
 (0)