@@ -5,10 +5,62 @@ import fetch from 'isomorphic-fetch';
5
5
import config from 'config' ;
6
6
import qs from 'qs' ;
7
7
import _ from 'lodash' ;
8
+ import { logger } from 'topcoder-react-lib' ;
8
9
import GrowsurfService from './growsurf' ;
10
+ import { sendEmailDirect } from './sendGrid' ;
9
11
10
12
const FormData = require ( 'form-data' ) ;
11
13
14
+ const JOB_FIELDS_RESPONSE = [
15
+ 'id' ,
16
+ 'slug' ,
17
+ 'country' ,
18
+ 'locality' ,
19
+ 'city' ,
20
+ 'name' ,
21
+ 'custom_fields' ,
22
+ 'enable_job_application_form' ,
23
+ 'created_on' ,
24
+ 'updated_on' ,
25
+ 'min_annual_salary' ,
26
+ 'salary_type' ,
27
+ 'max_annual_salary' ,
28
+ 'job_description_text' ,
29
+ ] ;
30
+ const CANDIDATE_FIELDS_RESPONSE = [
31
+ 'id' ,
32
+ 'slug' ,
33
+ 'first_name' ,
34
+ 'last_name' ,
35
+ 'email' ,
36
+ 'contact_number' ,
37
+ 'skill' ,
38
+ 'resume' ,
39
+ 'locality' ,
40
+ 'salary_expectation' ,
41
+ 'custom_fields' ,
42
+ ] ;
43
+
44
+ /**
45
+ * Send email to Kiril/Nick for debuging gig application errors
46
+ * @param {Object } error the error
47
+ */
48
+ function notifyKirilAndNick ( error ) {
49
+ logger . error ( error ) ;
50
+ sendEmailDirect ( {
51
+ personalizations : [
52
+ {
53
+ to :
[ { email :
'[email protected] ' } , { email :
'[email protected] ' } ] ,
54
+ subject : 'Gig application error alert' ,
55
+ } ,
56
+ ] ,
57
+ from :
{ email :
'[email protected] ' } ,
58
+ content : [ {
59
+ type : 'text/plain' , value : `The error occured as JSON string:\n\n ${ JSON . stringify ( error ) } ` ,
60
+ } ] ,
61
+ } ) ;
62
+ }
63
+
12
64
/**
13
65
* Auxiliary class that handles communication with recruitCRM
14
66
*/
@@ -44,13 +96,17 @@ export default class RecruitCRMService {
44
96
return this . getJobs ( req , res , next ) ;
45
97
}
46
98
if ( response . status >= 400 ) {
47
- return res . send ( {
99
+ const error = {
48
100
error : true ,
49
101
status : response . status ,
50
102
url : `${ this . private . baseUrl } /v1/jobs/search?${ qs . stringify ( req . query ) } ` ,
51
- } ) ;
103
+ errObj : await response . json ( ) ,
104
+ } ;
105
+ logger . error ( error ) ;
106
+ return res . send ( error ) ;
52
107
}
53
108
const data = await response . json ( ) ;
109
+ data . data = _ . map ( data . data , j => _ . pick ( j , JOB_FIELDS_RESPONSE ) ) ;
54
110
return res . send ( data ) ;
55
111
} catch ( err ) {
56
112
return next ( err ) ;
@@ -76,14 +132,17 @@ export default class RecruitCRMService {
76
132
return this . getJob ( req , res , next ) ;
77
133
}
78
134
if ( response . status >= 400 ) {
79
- return res . send ( {
135
+ const error = {
80
136
error : true ,
81
137
status : response . status ,
82
138
url : `${ this . private . baseUrl } /v1/jobs/${ req . params . id } ` ,
83
- } ) ;
139
+ errObj : await response . json ( ) ,
140
+ } ;
141
+ logger . error ( error ) ;
142
+ return res . send ( error ) ;
84
143
}
85
144
const data = await response . json ( ) ;
86
- return res . send ( data ) ;
145
+ return res . send ( _ . pick ( data , JOB_FIELDS_RESPONSE ) ) ;
87
146
} catch ( err ) {
88
147
return next ( err ) ;
89
148
}
@@ -108,11 +167,14 @@ export default class RecruitCRMService {
108
167
return this . getJobs ( req , res , next ) ;
109
168
}
110
169
if ( response . status >= 400 ) {
111
- return res . send ( {
170
+ const error = {
112
171
error : true ,
113
172
status : response . status ,
114
173
url : `${ this . private . baseUrl } /v1/jobs/search?${ qs . stringify ( req . query ) } ` ,
115
- } ) ;
174
+ errObj : await response . json ( ) ,
175
+ } ;
176
+ logger . error ( error ) ;
177
+ return res . send ( error ) ;
116
178
}
117
179
const data = await response . json ( ) ;
118
180
if ( data . current_page < data . last_page ) {
@@ -133,13 +195,17 @@ export default class RecruitCRMService {
133
195
const pageData = await pageDataRsp . json ( ) ;
134
196
data . data = _ . flatten ( data . data . concat ( pageData . data ) ) ;
135
197
}
136
- return res . send ( data . data ) ;
198
+ return res . send (
199
+ _ . map ( data . data , j => _ . pick ( j , JOB_FIELDS_RESPONSE ) ) ,
200
+ ) ;
137
201
} )
138
202
. catch ( e => res . send ( {
139
203
error : e ,
140
204
} ) ) ;
141
205
}
142
- return res . send ( data . data ) ;
206
+ return res . send (
207
+ _ . map ( data . data , j => _ . pick ( j , JOB_FIELDS_RESPONSE ) ) ,
208
+ ) ;
143
209
} catch ( err ) {
144
210
return next ( err ) ;
145
211
}
@@ -164,13 +230,17 @@ export default class RecruitCRMService {
164
230
return this . searchCandidates ( req , res , next ) ;
165
231
}
166
232
if ( response . status >= 400 ) {
167
- return res . send ( {
233
+ const error = {
168
234
error : true ,
169
235
status : response . status ,
170
236
url : `${ this . private . baseUrl } /v1/candidates/search?${ qs . stringify ( req . query ) } ` ,
171
- } ) ;
237
+ errObj : await response . json ( ) ,
238
+ } ;
239
+ logger . error ( error ) ;
240
+ return res . send ( error ) ;
172
241
}
173
242
const data = await response . json ( ) ;
243
+ data . data = _ . map ( data . data , j => _ . pick ( j , CANDIDATE_FIELDS_RESPONSE ) ) ;
174
244
return res . send ( data ) ;
175
245
} catch ( err ) {
176
246
return next ( err ) ;
@@ -215,6 +285,8 @@ export default class RecruitCRMService {
215
285
form . custom_fields . push ( {
216
286
field_id : 6 , value : `https://app.growsurf.com/dashboard/campaign/${ config . GROWSURF_CAMPAIGN_ID } /participant/${ growRes . id } ` ,
217
287
} ) ;
288
+ } else {
289
+ notifyKirilAndNick ( growRes ) ;
218
290
}
219
291
// clear the cookie
220
292
res . cookie ( config . GROWSURF_COOKIE , '' , {
@@ -231,12 +303,14 @@ export default class RecruitCRMService {
231
303
} ,
232
304
} ) ;
233
305
if ( candidateResponse . status >= 300 ) {
234
- return res . send ( {
306
+ const error = {
235
307
error : true ,
236
308
status : candidateResponse . status ,
237
309
url : `${ this . private . baseUrl } /v1/candidates/search?email=${ form . email } ` ,
238
310
errObj : await candidateResponse . json ( ) ,
239
- } ) ;
311
+ } ;
312
+ notifyKirilAndNick ( error ) ;
313
+ return res . send ( error ) ;
240
314
}
241
315
let candidateData = await candidateResponse . json ( ) ;
242
316
if ( candidateData . data ) {
@@ -265,13 +339,15 @@ export default class RecruitCRMService {
265
339
body : JSON . stringify ( form ) ,
266
340
} ) ;
267
341
if ( workCandidateResponse . status >= 300 ) {
268
- return res . send ( {
342
+ const error = {
269
343
error : true ,
270
344
status : workCandidateResponse . status ,
271
345
url : `${ this . private . baseUrl } /v1/candidates${ candidateSlug ? `/${ candidateSlug } ` : '' } ` ,
272
346
form,
273
347
errObj : await workCandidateResponse . json ( ) ,
274
- } ) ;
348
+ } ;
349
+ notifyKirilAndNick ( error ) ;
350
+ return res . send ( error ) ;
275
351
}
276
352
candidateData = await workCandidateResponse . json ( ) ;
277
353
// Attach resume to candidate if uploaded
@@ -286,7 +362,7 @@ export default class RecruitCRMService {
286
362
body : fileData ,
287
363
} ) ;
288
364
if ( fileCandidateResponse . status >= 300 ) {
289
- return res . send ( {
365
+ const error = {
290
366
error : true ,
291
367
status : fileCandidateResponse . status ,
292
368
url : `${ this . private . baseUrl } /v1/candidates/${ candidateData . slug } ` ,
@@ -295,7 +371,9 @@ export default class RecruitCRMService {
295
371
file,
296
372
formHeaders,
297
373
errObj : await fileCandidateResponse . json ( ) ,
298
- } ) ;
374
+ } ;
375
+ notifyKirilAndNick ( error ) ;
376
+ return res . send ( error ) ;
299
377
}
300
378
candidateData = await fileCandidateResponse . json ( ) ;
301
379
}
@@ -314,14 +392,16 @@ export default class RecruitCRMService {
314
392
success : true ,
315
393
} ) ;
316
394
}
317
- return res . send ( {
395
+ const error = {
318
396
error : true ,
319
397
status : applyResponse . status ,
320
398
url : `${ this . private . baseUrl } /v1/candidates/${ candidateData . slug } /assign?job_slug=${ id } ` ,
321
399
form,
322
400
candidateData,
323
401
errObj,
324
- } ) ;
402
+ } ;
403
+ notifyKirilAndNick ( error ) ;
404
+ return res . send ( error ) ;
325
405
}
326
406
// Set hired-stage
327
407
const hireStageResponse = await fetch ( `${ this . private . baseUrl } /v1/candidates/${ candidateData . slug } /hiring-stages/${ id } ` , {
@@ -337,13 +417,15 @@ export default class RecruitCRMService {
337
417
} ) ,
338
418
} ) ;
339
419
if ( hireStageResponse . status >= 300 ) {
340
- return res . send ( {
420
+ const error = {
341
421
error : true ,
342
422
status : hireStageResponse . status ,
343
423
url : `$${ this . private . baseUrl } /v1/candidates/${ candidateData . slug } /hiring-stages/${ id } ` ,
344
424
form,
345
425
errObj : await hireStageResponse . json ( ) ,
346
- } ) ;
426
+ } ;
427
+ notifyKirilAndNick ( error ) ;
428
+ return res . send ( error ) ;
347
429
}
348
430
// respond to API call
349
431
const data = await applyResponse . json ( ) ;
0 commit comments