1
- const plugin = { } ,
2
- common = require ( '../../../api/utils/common.js' ) ,
3
- plugins = require ( '../../pluginManager.js' ) ,
4
- log = common . log ( 'assistant:api' ) ,
5
- fetch = require ( '../../../api/parts/data/fetch.js' ) ,
6
- async = require ( "async" ) ,
7
- assistant = require ( "./assistant.js" ) ,
8
- _ = require ( 'underscore' ) ;
9
-
10
- ( function ( plugin ) {
11
- plugins . register ( "/master" , function ( ob ) {
1
+ const exportedPlugin = { } ;
2
+ const common = require ( '../../../api/utils/common.js' ) ;
3
+ const plugins = require ( '../../pluginManager.js' ) ;
4
+ const log = common . log ( 'assistant:api' ) ;
5
+ const assistant = require ( "./assistant.js" ) ;
6
+ const _ = require ( 'underscore' ) ;
7
+
8
+ ( function ( ) {
9
+ plugins . register ( "/master" , function ( ) {
12
10
// Allow configs to load & scanner to find all jobs classes
13
11
setTimeout ( ( ) => {
14
12
require ( '../../../api/parts/jobs' ) . job ( 'assistant:generate' ) . replace ( ) . schedule ( "every " + assistant . JOB_SCHEDULE_INTERVAL + " minutes starting on the 0 min" ) ;
@@ -17,7 +15,6 @@ const plugin = {},
17
15
18
16
plugins . register ( "/o/assistant" , function ( ob ) {
19
17
const params = ob . params ;
20
- const paths = ob . paths ;
21
18
22
19
if ( ! params . qstring . api_key ) {
23
20
common . returnMessage ( params , 400 , 'Missing parameter "api_key"' ) ;
@@ -29,28 +26,25 @@ const plugin = {},
29
26
30
27
log . d ( 'Assistant plugin request: Get All Notifications' ) ;
31
28
const validate = ob . validateUserForMgmtReadAPI ;
32
- validate ( function ( params ) {
33
- const member = params . member ;
29
+ validate ( function ( paramsInValidate ) {
30
+ const member = paramsInValidate . member ;
34
31
35
32
if ( _ . isUndefined ( app_id ) || app_id === null ) {
36
33
//app id not provided, not app targeted
37
34
38
35
//for a single user return all of his notifications for all of his apps
39
36
assistant . getNotificationsForUser ( common . db , member , api_key , function ( err , results ) {
40
- common . returnOutput ( params , results ) ;
37
+ common . returnOutput ( paramsInValidate , results ) ;
41
38
} ) ;
42
39
}
43
40
else {
44
41
//app id provided, a single app targeted
45
42
46
43
//for a single user return all of his notifications for a specific app
47
44
assistant . getNotificationsForUserForSingleApp ( common . db , api_key , app_id , function ( err , singleAppNotifications ) {
48
- common . returnOutput ( params , [ singleAppNotifications ] ) ;
45
+ common . returnOutput ( paramsInValidate , [ singleAppNotifications ] ) ;
49
46
} ) ;
50
47
}
51
-
52
-
53
-
54
48
} , params ) ;
55
49
return true ;
56
50
} ) ;
@@ -66,30 +60,31 @@ const plugin = {},
66
60
67
61
log . d ( 'Assistant plugin request: /i/assistant' ) ;
68
62
const validate = ob . validateUserForMgmtReadAPI ;
69
- validate ( function ( params ) {
63
+ validate ( function ( paramsInValidate ) {
64
+ const api_key = paramsInValidate . qstring . api_key ;
70
65
71
- const member = params . member ;
72
- const api_key = params . qstring . api_key ;
66
+ let save_action ;
67
+ let notif ;
68
+ let save_val ;
73
69
74
70
const subAction = paths [ 3 ] ;
75
71
log . d ( 'Assistant plugin request: ' + subAction ) ;
76
72
switch ( subAction ) {
77
73
case 'global' :
78
74
case 'private' :
79
- if ( typeof params . qstring . save === "undefined" ) {
80
- common . returnMessage ( params , 400 , 'Missing parameter "save"' ) ;
75
+ if ( typeof paramsInValidate . qstring . save === "undefined" ) {
76
+ common . returnMessage ( paramsInValidate , 400 , 'Missing parameter "save"' ) ;
81
77
return false ;
82
78
}
83
79
84
- if ( typeof params . qstring . notif === "undefined" ) {
85
- common . returnMessage ( params , 400 , 'Missing parameter "notif"' ) ;
80
+ if ( typeof paramsInValidate . qstring . notif === "undefined" ) {
81
+ common . returnMessage ( paramsInValidate , 400 , 'Missing parameter "notif"' ) ;
86
82
return false ;
87
83
}
88
84
89
- const notif = params . qstring . notif ;
90
- const save_val = params . qstring . save ;
85
+ notif = paramsInValidate . qstring . notif ;
86
+ save_val = paramsInValidate . qstring . save ;
91
87
92
- let save_action ;
93
88
if ( save_val === "true" ) {
94
89
save_action = true ;
95
90
} //save
@@ -102,14 +97,14 @@ const plugin = {},
102
97
//this toggles the global save status
103
98
log . d ( 'Assistant plugin request: Change global notification status' ) ;
104
99
assistant . changeNotificationSavedStatus ( false , save_action , notif , api_key , common . db ) ;
105
- common . returnOutput ( params , "the global action was done " + save_action ) ;
100
+ common . returnOutput ( paramsInValidate , "the global action was done " + save_action ) ;
106
101
}
107
102
else if ( subAction === 'private' ) {
108
103
//this is called when changing the save status of a notification from the frontend
109
104
//this toggles the private save status
110
105
log . d ( 'Assistant plugin request: Change personal notification status' ) ;
111
106
assistant . changeNotificationSavedStatus ( true , save_action , notif , api_key , common . db ) ;
112
- common . returnOutput ( params , "the private action was done " + save_action ) ;
107
+ common . returnOutput ( paramsInValidate , "the private action was done " + save_action ) ;
113
108
}
114
109
break ;
115
110
case 'create_external' :
@@ -118,72 +113,71 @@ const plugin = {},
118
113
119
114
try {
120
115
//read provided fields
121
- const notifData = JSON . parse ( params . qstring . notif_data ) ;
122
- const pluginName = params . qstring . owner_name ;
123
- const notifType = params . qstring . notif_type ;
124
- const notifSubType = params . qstring . notif_subtype ;
125
- const i18nId = params . qstring . i18n_id ;
126
- const notifAppId = params . qstring . notif_app_id ;
127
- const notificationVersion = params . qstring . notif_version ;
128
- const targetUserApiKey = params . qstring . target_user_api_key ;
116
+ const notifData = JSON . parse ( paramsInValidate . qstring . notif_data ) ;
117
+ const pluginName = paramsInValidate . qstring . owner_name ;
118
+ const notifType = paramsInValidate . qstring . notif_type ;
119
+ const notifSubType = paramsInValidate . qstring . notif_subtype ;
120
+ const i18nId = paramsInValidate . qstring . i18n_id ;
121
+ const notifAppId = paramsInValidate . qstring . notif_app_id ;
122
+ const notificationVersion = paramsInValidate . qstring . notif_version ;
123
+ const targetUserApiKey = paramsInValidate . qstring . target_user_api_key ;
129
124
130
125
//check if they are set
131
126
if ( _ . isUndefined ( notifData ) ) {
132
- common . returnMessage ( params , 400 , 'Missing parameter "notif_data"' ) ;
127
+ common . returnMessage ( paramsInValidate , 400 , 'Missing parameter "notif_data"' ) ;
133
128
return false ;
134
129
}
135
130
136
131
if ( _ . isUndefined ( pluginName ) ) {
137
- common . returnMessage ( params , 400 , 'Missing parameter "owner_name"' ) ;
132
+ common . returnMessage ( paramsInValidate , 400 , 'Missing parameter "owner_name"' ) ;
138
133
return false ;
139
134
}
140
135
141
136
if ( _ . isUndefined ( notifType ) ) {
142
- common . returnMessage ( params , 400 , 'Missing parameter "notif_type"' ) ;
137
+ common . returnMessage ( paramsInValidate , 400 , 'Missing parameter "notif_type"' ) ;
143
138
return false ;
144
139
}
145
140
146
141
if ( _ . isUndefined ( notifSubType ) ) {
147
- common . returnMessage ( params , 400 , 'Missing parameter "notif_subtype"' ) ;
142
+ common . returnMessage ( paramsInValidate , 400 , 'Missing parameter "notif_subtype"' ) ;
148
143
return false ;
149
144
}
150
145
151
146
if ( _ . isUndefined ( i18nId ) ) {
152
- common . returnMessage ( params , 400 , 'Missing parameter "i18n_id"' ) ;
147
+ common . returnMessage ( paramsInValidate , 400 , 'Missing parameter "i18n_id"' ) ;
153
148
return false ;
154
149
}
155
150
156
151
if ( _ . isUndefined ( notifAppId ) ) {
157
- common . returnMessage ( params , 400 , 'Missing parameter "notif_app_id"' ) ;
152
+ common . returnMessage ( paramsInValidate , 400 , 'Missing parameter "notif_app_id"' ) ;
158
153
return false ;
159
154
}
160
155
161
156
if ( _ . isUndefined ( notificationVersion ) ) {
162
- common . returnMessage ( params , 400 , 'Missing parameter "notif_version"' ) ;
157
+ common . returnMessage ( paramsInValidate , 400 , 'Missing parameter "notif_version"' ) ;
163
158
return false ;
164
159
}
165
160
166
161
assistant . createNotificationExternal ( common . db , notifData , pluginName , notifType , notifSubType , i18nId , notifAppId , notificationVersion , targetUserApiKey , function ( succeeded , err ) {
167
162
if ( succeeded ) {
168
- common . returnOutput ( params , prepareMessage ( "Succeded in creating notification" , null , null ) ) ;
163
+ common . returnOutput ( paramsInValidate , prepareMessage ( "Succeded in creating notification" , null , null ) ) ;
169
164
}
170
165
else {
171
166
if ( _ . isUndefined ( err ) || err === null ) {
172
167
err = "N/A" ;
173
168
}
174
- common . returnMessage ( params , 500 , prepareMessage ( 'Failed to create notification' , err , null ) ) ;
169
+ common . returnMessage ( paramsInValidate , 500 , prepareMessage ( 'Failed to create notification' , err , null ) ) ;
175
170
}
176
171
} ) ;
177
172
}
178
173
catch ( ex ) {
179
- common . returnMessage ( params , 500 , prepareMessage ( 'Problem while trying to create notification' , ex , null ) ) ;
174
+ common . returnMessage ( paramsInValidate , 500 , prepareMessage ( 'Problem while trying to create notification' , ex , null ) ) ;
180
175
return false ;
181
176
}
182
177
break ;
183
178
default :
184
- common . returnMessage ( params , 400 , 'Invalid path' ) ;
179
+ common . returnMessage ( paramsInValidate , 400 , 'Invalid path' ) ;
185
180
return false ;
186
- break ;
187
181
}
188
182
189
183
log . d ( 'Assistant plugin request: 3' ) ;
@@ -195,11 +189,11 @@ const plugin = {},
195
189
const prepareMessage = function ( message , error , data ) {
196
190
let ret = { } ;
197
191
ret . message = message ;
198
- if ( ! _ . isUndefined ( error ) && error != null ) {
192
+ if ( ! _ . isUndefined ( error ) && error !== null ) {
199
193
ret . error = error ;
200
194
}
201
195
202
- if ( ! _ . isUndefined ( data ) && data != null ) {
196
+ if ( ! _ . isUndefined ( data ) && data !== null ) {
203
197
ret . data = data ;
204
198
}
205
199
@@ -304,6 +298,6 @@ const plugin = {},
304
298
common . db . collection ( db_name_config ) . remove ( { _id : common . db . ObjectID ( appId ) } , function ( ) { } ) ;
305
299
} ) ;
306
300
307
- } ( plugin ) ) ;
301
+ } ( exportedPlugin ) ) ;
308
302
309
- module . exports = plugin ;
303
+ module . exports = exportedPlugin ;
0 commit comments