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
+ }
0 commit comments