@@ -270,34 +270,61 @@ export default class RecruitCRMService {
270
270
fileData . append ( 'resume' , file . buffer , file . originalname ) ;
271
271
}
272
272
let candidateSlug ;
273
+ let isNewCandidate = true ;
274
+ let isReferred = false ;
273
275
let referralCookie = req . cookies [ config . GROWSURF_COOKIE ] ;
274
276
if ( referralCookie ) referralCookie = JSON . parse ( referralCookie ) ;
275
277
try {
276
278
// referral tracking via growsurf
277
279
if ( referralCookie ) {
278
280
const gs = new GrowsurfService ( ) ;
279
281
const tcHandle = _ . findIndex ( form . custom_fields , { field_id : 2 } ) ;
280
- const growRes = await gs . addParticipant ( JSON . stringify ( {
281
- email : form . email ,
282
- referredBy : referralCookie . referralId ,
283
- referralStatus : 'CREDIT_PENDING' ,
284
- firstName : form . first_name ,
285
- lastName : form . last_name ,
286
- metadata : {
287
- gigId : id ,
288
- tcHandle : form . custom_fields [ tcHandle ] . value ,
289
- } ,
290
- } ) ) ;
291
- // If everything set in Growsurf
292
- // add referral link to candidate profile in recruitCRM
293
- if ( ! growRes . error ) {
294
- form . custom_fields . push ( {
295
- field_id : 6 , value : `https://app.growsurf.com/dashboard/campaign/${ config . GROWSURF_CAMPAIGN_ID } /participant/${ growRes . id } ` ,
296
- } ) ;
282
+ // check if candidate exists in growsurf
283
+ const existRes = await gs . getParticipantByIdOREmail ( form . email ) ;
284
+ if ( existRes . id ) {
285
+ // candidate exists in growsurf
286
+ // update candidate to set referrer only if it is not set already
287
+ if ( ! existRes . referrer ) {
288
+ isReferred = true ;
289
+ const updateRes = await gs . updateParticipant ( form . email , JSON . stringify ( {
290
+ referredBy : referralCookie . referralId ,
291
+ referralStatus : 'CREDIT_PENDING' ,
292
+ metadata : {
293
+ gigID : id ,
294
+ } ,
295
+ } ) ) ;
296
+ // add referral link to candidate profile in recruitCRM
297
+ if ( ! updateRes . error ) {
298
+ form . custom_fields . push ( {
299
+ field_id : 6 , value : `https://app.growsurf.com/dashboard/campaign/${ config . GROWSURF_CAMPAIGN_ID } /participant/${ updateRes . id } ` ,
300
+ } ) ;
301
+ } else {
302
+ notifyKirilAndNick ( updateRes ) ;
303
+ }
304
+ }
297
305
} else {
298
- notifyKirilAndNick ( growRes ) ;
306
+ isReferred = true ;
307
+ const growRes = await gs . addParticipant ( JSON . stringify ( {
308
+ email : form . email ,
309
+ referredBy : referralCookie . referralId ,
310
+ referralStatus : 'CREDIT_PENDING' ,
311
+ firstName : form . first_name ,
312
+ lastName : form . last_name ,
313
+ metadata : {
314
+ gigId : id ,
315
+ tcHandle : form . custom_fields [ tcHandle ] . value ,
316
+ } ,
317
+ } ) ) ;
318
+ // add referral link to candidate profile in recruitCRM
319
+ if ( ! growRes . error ) {
320
+ form . custom_fields . push ( {
321
+ field_id : 6 , value : `https://app.growsurf.com/dashboard/campaign/${ config . GROWSURF_CAMPAIGN_ID } /participant/${ growRes . id } ` ,
322
+ } ) ;
323
+ } else {
324
+ notifyKirilAndNick ( growRes ) ;
325
+ }
299
326
}
300
- // clear the cookie
327
+ // finally, clear the cookie
301
328
res . cookie ( config . GROWSURF_COOKIE , '' , {
302
329
maxAge : 0 ,
303
330
overwrite : true ,
@@ -326,6 +353,7 @@ export default class RecruitCRMService {
326
353
// Candidate exists in recruitCRM
327
354
// We will update profile fields, otherwise we create new candidate below
328
355
// Check if candidate is placed in gig currently
356
+ isNewCandidate = false ;
329
357
const candStatusIndex = _ . findIndex (
330
358
candidateData . data [ 0 ] . custom_fields , { field_id : 12 } ,
331
359
) ;
@@ -450,6 +478,11 @@ export default class RecruitCRMService {
450
478
notifyKirilAndNick ( error ) ;
451
479
return res . send ( error ) ;
452
480
}
481
+ // For new candidates that apply via referral link
482
+ // aka triggered referral state step 1 - notify and etc.
483
+ if ( isNewCandidate && isReferred ) {
484
+ // console.log('isNewCandidate');
485
+ }
453
486
// respond to API call
454
487
const data = await applyResponse . json ( ) ;
455
488
return res . send ( data ) ;
0 commit comments