Skip to content

Commit 54fa282

Browse files
committed
gev referrals v1
1 parent e1a82bd commit 54fa282

File tree

12 files changed

+495
-154
lines changed

12 files changed

+495
-154
lines changed

src/server/services/growsurf.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ export default class GrowsurfService {
2020
};
2121
}
2222

23+
/**
24+
* Gets get participant.
25+
* @return {Promise}
26+
* @param {String} idOrEmail growsurf id or email
27+
*/
28+
async getParticipantByIdOREmail(idOrEmail) {
29+
const response = await fetch(`${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${idOrEmail}`, {
30+
method: 'GET',
31+
headers: {
32+
'Content-Type': 'application/json',
33+
Authorization: this.private.authorization,
34+
},
35+
});
36+
if (response.status >= 300) {
37+
return {
38+
error: await response.json(),
39+
code: response.status,
40+
url: `${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${idOrEmail}`,
41+
};
42+
}
43+
const data = await response.json();
44+
return data;
45+
}
46+
2347
/**
2448
* Gets get participant by email or id.
2549
* @return {Promise}
@@ -67,7 +91,6 @@ export default class GrowsurfService {
6791
code: response.status,
6892
url: `${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant`,
6993
body,
70-
private: this.private, // to remove in final release
7194
};
7295
}
7396
const data = await response.json();
@@ -96,4 +119,31 @@ export default class GrowsurfService {
96119
}
97120
return result;
98121
}
122+
123+
/**
124+
* Update participant in growSurf
125+
* @param {string} idOrEmail id or email
126+
* @param {string} body payload
127+
* @returns {Promise}
128+
*/
129+
async updateParticipant(idOrEmail, body) {
130+
const response = await fetch(`${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${idOrEmail}`, {
131+
method: 'POST',
132+
headers: {
133+
'Content-Type': 'application/json',
134+
Authorization: this.private.authorization,
135+
},
136+
body,
137+
});
138+
if (response.status >= 300) {
139+
return {
140+
error: await response.json(),
141+
code: response.status,
142+
url: `${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${idOrEmail}`,
143+
body,
144+
};
145+
}
146+
const data = await response.json();
147+
return data;
148+
}
99149
}

src/server/services/recruitCRM.js

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -270,34 +270,61 @@ export default class RecruitCRMService {
270270
fileData.append('resume', file.buffer, file.originalname);
271271
}
272272
let candidateSlug;
273+
let isNewCandidate = true;
274+
let isReferred = false;
273275
let referralCookie = req.cookies[config.GROWSURF_COOKIE];
274276
if (referralCookie) referralCookie = JSON.parse(referralCookie);
275277
try {
276278
// referral tracking via growsurf
277279
if (referralCookie) {
278280
const gs = new GrowsurfService();
279281
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+
}
297305
} 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+
}
299326
}
300-
// clear the cookie
327+
// finally, clear the cookie
301328
res.cookie(config.GROWSURF_COOKIE, '', {
302329
maxAge: 0,
303330
overwrite: true,
@@ -326,6 +353,7 @@ export default class RecruitCRMService {
326353
// Candidate exists in recruitCRM
327354
// We will update profile fields, otherwise we create new candidate below
328355
// Check if candidate is placed in gig currently
356+
isNewCandidate = false;
329357
const candStatusIndex = _.findIndex(
330358
candidateData.data[0].custom_fields, { field_id: 12 },
331359
);
@@ -450,6 +478,11 @@ export default class RecruitCRMService {
450478
notifyKirilAndNick(error);
451479
return res.send(error);
452480
}
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+
}
453486
// respond to API call
454487
const data = await applyResponse.json();
455488
return res.send(data);

src/shared/actions/growSurf.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Actions related to growsurf (gig referrals)
3+
*/
4+
/* global fetch */
5+
import { redux, config } from 'topcoder-react-utils';
6+
7+
const PROXY_ENDPOINT = `${config.URL.COMMUNITY_APP}/api`;
8+
9+
/**
10+
* Fetch init
11+
*/
12+
function getReferralIdInit() {
13+
return {
14+
loading: true,
15+
};
16+
}
17+
18+
/**
19+
* Get referral id for the logged user
20+
* if this member does not exist in growsurf it creates it in the system
21+
*/
22+
async function getReferralIdDone(profile) {
23+
if (profile.email) {
24+
const res = await fetch(`${PROXY_ENDPOINT}/growsurf/participants?participantId=${profile.email}`, {
25+
method: 'POST',
26+
body: JSON.stringify({
27+
email: profile.email,
28+
firstName: profile.firstName,
29+
lastName: profile.lastName,
30+
tcHandle: profile.handle,
31+
}),
32+
headers: {
33+
'Content-Type': 'application/json',
34+
},
35+
});
36+
if (res.status >= 300) {
37+
return {
38+
error: true,
39+
details: await res.json(),
40+
};
41+
}
42+
const data = await res.json();
43+
return {
44+
data,
45+
};
46+
}
47+
// no referral id without email
48+
return {
49+
error: true,
50+
details: 'No profile email found',
51+
};
52+
}
53+
54+
export default redux.createActions({
55+
GROWSURF: {
56+
GET_REFERRALID_INIT: getReferralIdInit,
57+
GET_REFERRALID_DONE: getReferralIdDone,
58+
},
59+
});

0 commit comments

Comments
 (0)