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
+ module . exports = {
18
+ findCandidates : findCandidates ,
19
+ sendPushNotification : sendPushNotification
20
+ } ;
21
+
22
+ function findCandidates ( item , ageMin , ageMax ) {
23
+ const deferred = Q . defer ( ) ;
24
+
25
+ const candidatesHelper = new CandidatesHelper ( ) ;
26
+
27
+ let query = {
28
+ longitude : item . address . location . coordinates [ 0 ] ,
29
+ latitude : item . address . location . coordinates [ 1 ] ,
30
+ type : item . attributes . type ,
31
+ furnished : item . attributes . furnished ,
32
+ ageMin : ageMin ,
33
+ ageMax : ageMax ,
34
+ gender : item . userLimitations . gender
35
+ } ;
36
+
37
+ if ( utils . isNotNull ( item . attributes . rooms ) && item . attributes . rooms !== 0 ) {
38
+ query [ "rooms" ] = '' + item . attributes . rooms ;
39
+ }
40
+
41
+ if ( utils . isNotNull ( item . attributes . price . gross ) && item . attributes . price . gross !== 0 ) {
42
+ query [ "price" ] = '' + item . attributes . price . gross ;
43
+ }
44
+
45
+ if ( utils . isNotNull ( item . attributes . disabledFriendly ) ) {
46
+ query [ "disabledFriendly" ] = '' + item . attributes . disabledFriendly ;
47
+ }
48
+
49
+ if ( utils . isNotNull ( item . attributes . petsAllowed ) ) {
50
+ query [ "petsAllowed" ] = '' + item . attributes . petsAllowed ;
51
+ }
52
+
53
+ if ( utils . isNotNull ( item . attributes . availability . begin ) ) {
54
+ query [ "availablebegin" ] = '' + item . attributes . availability . begin ;
55
+ }
56
+
57
+ if ( utils . isNotNull ( item . attributes . availability . end ) ) {
58
+ query [ "availableend" ] = '' + item . attributes . availability . end ;
59
+ }
60
+
61
+ let likes = _ . map ( item . likes , function ( doc ) {
62
+ return doc . user ;
63
+ } ) ;
64
+
65
+ const dislikes = _ . map ( item . dislikes , function ( doc ) {
66
+ return doc . user ;
67
+ } ) ;
68
+
69
+ let userIds = new Array ( ) ;
70
+ userIds . push ( item . user ) ;
71
+
72
+ if ( utils . isNotEmpty ( likes ) ) {
73
+ userIds = userIds . concat ( likes ) ;
74
+ }
75
+
76
+ if ( utils . isNotEmpty ( dislikes ) ) {
77
+ userIds = userIds . concat ( dislikes ) ;
78
+ }
79
+
80
+ candidatesHelper . findCandidates ( userIds , 0 , constants . MAX_ITEM_USERS , query ) . then ( function ( users ) {
81
+ deferred . resolve ( users ) ;
82
+ } , function ( err ) {
83
+ deferred . reject ( new Error ( err ) ) ;
84
+ } ) ;
85
+
86
+ return deferred . promise ;
87
+ }
88
+
89
+ function sendPushNotification ( user , labelKey ) {
90
+ Device . findDevice ( user . _id ) . then ( function ( device ) {
91
+ if ( utils . isNotNull ( device ) && ! utils . isStringEmpty ( device . tokenId ) ) {
92
+ processNofication ( user , device , labelKey ) ;
93
+ }
94
+ } , function ( error ) {
95
+ logger . info ( 'error' , 'Error while looking for device informations.' ) ;
96
+ } ) ;
97
+ }
98
+
99
+ function processNofication ( user , device , labelKey ) {
100
+
101
+ if ( utils . isNotNull ( user . userParams ) && utils . isNotNull ( user . userParams . appSettings ) && user . userParams . appSettings . pushNotifications ) {
102
+
103
+ const msgText = getPushNotificationText ( user , device , labelKey ) ;
104
+
105
+ pushSender . pushNotification ( msgText , device ) . then ( function ( data ) {
106
+ // Coolio all right here
107
+ } , function ( error ) {
108
+ logger . info ( 'error' , 'Error while pushing the notification to the client. ' + JSON . stringify ( error ) ) ;
109
+ } ) ;
110
+ } else {
111
+ // Means user don't want to receive push notifications
112
+ }
113
+ }
114
+
115
+ function getPushNotificationText ( user , device , labelKey ) {
116
+
117
+ const language = ! utils . isStringEmpty ( device . language ) ? device . language : 'en' ;
118
+
119
+ return i18n . __ ( { phrase : labelKey , locale : language } , { who : user . facebook . firstName } ) ;
120
+ }
0 commit comments