@@ -8,104 +8,91 @@ import * as request from "request";
8
8
const DefaultHooksCollectionName = "_Hooks" ;
9
9
10
10
export class HooksController {
11
- _applicationId : string ;
12
- _collectionPrefix: string ;
11
+ _applicationId :string ;
12
+ _collectionPrefix:string ;
13
13
_collection ;
14
14
15
- constructor ( applicationId : string , collectionPrefix : string = '' ) {
15
+ constructor ( applicationId :string , collectionPrefix :string = '' ) {
16
16
this . _applicationId = applicationId ;
17
17
this . _collectionPrefix = collectionPrefix ;
18
18
}
19
-
20
- database ( ) {
21
- return DatabaseAdapter . getDatabaseConnection ( this . _applicationId , this . _collectionPrefix ) ;
19
+
20
+ load ( ) {
21
+ return this . _getHooks ( ) . then ( hooks => {
22
+ hooks = hooks || [ ] ;
23
+ hooks . forEach ( ( hook ) => {
24
+ this . addHookToTriggers ( hook ) ;
25
+ } ) ;
26
+ } ) ;
22
27
}
23
-
24
- collection ( ) {
28
+
29
+ getCollection ( ) {
25
30
if ( this . _collection ) {
26
31
return Promise . resolve ( this . _collection )
27
32
}
28
- return this . database ( ) . rawCollection ( DefaultHooksCollectionName ) . then ( ( collection ) => {
33
+
34
+ let database = DatabaseAdapter . getDatabaseConnection ( this . _applicationId , this . _collectionPrefix ) ;
35
+ return database . adaptiveCollection ( DefaultHooksCollectionName ) . then ( collection => {
29
36
this . _collection = collection ;
30
37
return collection ;
31
38
} ) ;
32
39
}
33
-
40
+
34
41
getFunction ( functionName ) {
35
- return this . getOne ( { functionName : functionName } )
42
+ return this . _getHooks ( { functionName : functionName } , 1 ) . then ( results => results [ 0 ] ) ;
36
43
}
37
-
44
+
38
45
getFunctions ( ) {
39
- return this . get ( { functionName : { $exists : true } } )
46
+ return this . _getHooks ( { functionName : { $exists : true } } ) ;
40
47
}
41
-
48
+
42
49
getTrigger ( className , triggerName ) {
43
- return this . getOne ( { className : className , triggerName : triggerName } )
50
+ return this . _getHooks ( { className : className , triggerName : triggerName } , 1 ) . then ( results => results [ 0 ] ) ;
44
51
}
45
-
52
+
46
53
getTriggers ( ) {
47
- return this . get ( { className : { $exists : true } , triggerName : { $exists : true } } )
54
+ return this . _getHooks ( { className : { $exists : true } , triggerName : { $exists : true } } ) ;
48
55
}
49
-
56
+
50
57
deleteFunction ( functionName ) {
51
58
triggers . removeFunction ( functionName , this . _applicationId ) ;
52
- return this . delete ( { functionName : functionName } ) ;
59
+ return this . _removeHooks ( { functionName : functionName } ) ;
53
60
}
54
-
61
+
55
62
deleteTrigger ( className , triggerName ) {
56
63
triggers . removeTrigger ( triggerName , className , this . _applicationId ) ;
57
- return this . delete ( { className : className , triggerName : triggerName } ) ;
58
- }
59
-
60
- delete ( query ) {
61
- return this . collection ( ) . then ( ( collection ) => {
62
- return collection . remove ( query )
63
- } ) . then ( ( res ) => {
64
- return { } ;
65
- } , 1 ) ;
64
+ return this . _removeHooks ( { className : className , triggerName : triggerName } ) ;
66
65
}
67
-
68
- getOne ( query ) {
69
- return this . collection ( )
70
- . then ( coll => coll . findOne ( query , { _id : 0 } ) )
71
- . then ( hook => {
72
- return hook ;
73
- } ) ;
66
+
67
+ _getHooks ( query , limit ) {
68
+ let options = limit ? { limit : limit } : undefined ;
69
+ return this . getCollection ( ) . then ( collection => collection . find ( query , options ) ) ;
74
70
}
75
-
76
- get ( query ) {
77
- return this . collection ( )
78
- . then ( coll => coll . find ( query , { _id : 0 } ) . toArray ( ) )
79
- . then ( hooks => {
80
- return hooks ;
71
+
72
+ _removeHooks ( query ) {
73
+ return this . getCollection ( ) . then ( collection => {
74
+ return collection . remove ( query ) ;
75
+ } ) . then ( ( ) => {
76
+ return { } ;
81
77
} ) ;
82
78
}
83
-
84
- getHooks ( ) {
85
- return this . collection ( )
86
- . then ( coll => coll . find ( { } , { _id : 0 } ) . toArray ( ) )
87
- . then ( hooks => {
88
- return hooks ;
89
- } , ( ) => ( [ ] ) )
90
- }
91
-
79
+
92
80
saveHook ( hook ) {
93
-
94
81
var query ;
95
82
if ( hook . functionName && hook . url ) {
96
- query = { functionName : hook . functionName }
83
+ query = { functionName : hook . functionName }
97
84
} else if ( hook . triggerName && hook . className && hook . url ) {
98
85
query = { className : hook . className , triggerName : hook . triggerName }
99
86
} else {
100
87
throw new Parse . Error ( 143 , "invalid hook declaration" ) ;
101
88
}
102
- return this . collection ( ) . then ( ( collection ) => {
103
- return collection . update ( query , hook , { upsert : true } )
104
- } ) . then ( function ( res ) {
105
- return hook ;
106
- } )
89
+ return this . getCollection ( )
90
+ . then ( collection => collection . upsertOne ( query , hook ) )
91
+ . then ( ( ) => {
92
+ return hook ;
93
+ } ) ;
107
94
}
108
-
95
+
109
96
addHookToTriggers ( hook ) {
110
97
var wrappedFunction = wrapToHTTPRequest ( hook ) ;
111
98
wrappedFunction . url = hook . url ;
@@ -114,13 +101,13 @@ export class HooksController {
114
101
} else {
115
102
triggers . addFunction ( hook . functionName , wrappedFunction , null , this . _applicationId ) ;
116
103
}
117
- }
118
-
104
+ }
105
+
119
106
addHook ( hook ) {
120
107
this . addHookToTriggers ( hook ) ;
121
108
return this . saveHook ( hook ) ;
122
109
}
123
-
110
+
124
111
createOrUpdateHook ( aHook ) {
125
112
var hook ;
126
113
if ( aHook && aHook . functionName && aHook . url ) {
@@ -132,69 +119,59 @@ export class HooksController {
132
119
hook . className = aHook . className ;
133
120
hook . url = aHook . url ;
134
121
hook . triggerName = aHook . triggerName ;
135
-
122
+
136
123
} else {
137
124
throw new Parse . Error ( 143 , "invalid hook declaration" ) ;
138
- }
139
-
125
+ }
126
+
140
127
return this . addHook ( hook ) ;
141
128
} ;
142
-
129
+
143
130
createHook ( aHook ) {
144
131
if ( aHook . functionName ) {
145
132
return this . getFunction ( aHook . functionName ) . then ( ( result ) => {
146
133
if ( result ) {
147
- throw new Parse . Error ( 143 , `function name: ${ aHook . functionName } already exits` ) ;
134
+ throw new Parse . Error ( 143 , `function name: ${ aHook . functionName } already exits` ) ;
148
135
} else {
149
136
return this . createOrUpdateHook ( aHook ) ;
150
137
}
151
138
} ) ;
152
139
} else if ( aHook . className && aHook . triggerName ) {
153
140
return this . getTrigger ( aHook . className , aHook . triggerName ) . then ( ( result ) => {
154
141
if ( result ) {
155
- throw new Parse . Error ( 143 , `class ${ aHook . className } already has trigger ${ aHook . triggerName } ` ) ;
142
+ throw new Parse . Error ( 143 , `class ${ aHook . className } already has trigger ${ aHook . triggerName } ` ) ;
156
143
}
157
144
return this . createOrUpdateHook ( aHook ) ;
158
145
} ) ;
159
146
}
160
-
147
+
161
148
throw new Parse . Error ( 143 , "invalid hook declaration" ) ;
162
149
} ;
163
-
150
+
164
151
updateHook ( aHook ) {
165
152
if ( aHook . functionName ) {
166
153
return this . getFunction ( aHook . functionName ) . then ( ( result ) => {
167
154
if ( result ) {
168
155
return this . createOrUpdateHook ( aHook ) ;
169
156
}
170
- throw new Parse . Error ( 143 , `no function named: ${ aHook . functionName } is defined` ) ;
157
+ throw new Parse . Error ( 143 , `no function named: ${ aHook . functionName } is defined` ) ;
171
158
} ) ;
172
159
} else if ( aHook . className && aHook . triggerName ) {
173
160
return this . getTrigger ( aHook . className , aHook . triggerName ) . then ( ( result ) => {
174
161
if ( result ) {
175
162
return this . createOrUpdateHook ( aHook ) ;
176
163
}
177
- throw new Parse . Error ( 143 , `class ${ aHook . className } does not exist` ) ;
164
+ throw new Parse . Error ( 143 , `class ${ aHook . className } does not exist` ) ;
178
165
} ) ;
179
166
}
180
167
throw new Parse . Error ( 143 , "invalid hook declaration" ) ;
181
168
} ;
182
-
183
- load ( ) {
184
- return this . getHooks ( ) . then ( ( hooks ) => {
185
- hooks = hooks || [ ] ;
186
- hooks . forEach ( ( hook ) => {
187
- this . addHookToTriggers ( hook ) ;
188
- } ) ;
189
- } ) ;
190
- }
191
-
192
169
}
193
170
194
171
function wrapToHTTPRequest ( hook ) {
195
- return function ( req , res ) {
196
- var jsonBody = { } ;
197
- for ( var i in req ) {
172
+ return ( req , res ) => {
173
+ let jsonBody = { } ;
174
+ for ( var i in req ) {
198
175
jsonBody [ i ] = req [ i ] ;
199
176
}
200
177
if ( req . object ) {
@@ -205,30 +182,31 @@ function wrapToHTTPRequest(hook) {
205
182
jsonBody . original = req . original . toJSON ( ) ;
206
183
jsonBody . original . className = req . original . className ;
207
184
}
208
- var jsonRequest = { } ;
209
- jsonRequest . headers = {
210
- 'Content-Type' : 'application/json'
211
- }
212
- jsonRequest . body = JSON . stringify ( jsonBody ) ;
213
-
214
- request . post ( hook . url , jsonRequest , function ( err , httpResponse , body ) {
185
+ let jsonRequest = {
186
+ headers : {
187
+ 'Content-Type' : 'application/json'
188
+ } ,
189
+ body : JSON . stringify ( jsonBody )
190
+ } ;
191
+
192
+ request . post ( hook . url , jsonRequest , function ( err , httpResponse , body ) {
215
193
var result ;
216
194
if ( body ) {
217
195
if ( typeof body == "string" ) {
218
196
try {
219
197
body = JSON . parse ( body ) ;
220
- } catch ( e ) {
221
- err = { error : "Malformed response" , code : - 1 } ;
198
+ } catch ( e ) {
199
+ err = { error : "Malformed response" , code : - 1 } ;
222
200
}
223
201
}
224
202
if ( ! err ) {
225
203
result = body . success ;
226
- err = body . error ;
204
+ err = body . error ;
227
205
}
228
206
}
229
207
if ( err ) {
230
208
return res . error ( err ) ;
231
- } else {
209
+ } else {
232
210
return res . success ( result ) ;
233
211
}
234
212
} ) ;
0 commit comments