Skip to content

Gig referrals #5392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ workflows:
filters:
branches:
only:
- develop
- gig-referrals
# This is alternate dev env for parallel testing
- "build-test":
context : org-global
Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ ARG AUTH_SECRET
ARG COMMUNITY_APP_URL
ARG GSHEETS_API_KEY

# Gig work referrals
ARG SENDGRID_API_KEY
ARG GROWSURF_API_KEY
ARG GROWSURF_CAMPAIGN_ID

################################################################################
# Setting of environment variables in the Docker image.

Expand Down Expand Up @@ -121,6 +126,9 @@ ENV CONTENTFUL_EDU_CDN_API_KEY=$CONTENTFUL_EDU_CDN_API_KEY
ENV CONTENTFUL_EDU_PREVIEW_API_KEY=$CONTENTFUL_EDU_PREVIEW_API_KEY
ENV RECRUITCRM_API_KEY=$RECRUITCRM_API_KEY
ENV COMMUNITY_APP_URL=$COMMUNITY_APP_URL
ENV SENDGRID_API_KEY=$SENDGRID_API_KEY
ENV GROWSURF_API_KEY=$GROWSURF_API_KEY
ENV GROWSURF_CAMPAIGN_ID=$GROWSURF_CAMPAIGN_ID
ENV GSHEETS_API_KEY=$GSHEETS_API_KEY

################################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`Default render 1`] = `
id="textAreaInput"
onChange={[Function]}
placeholder=""
readOnly={false}
/>
</div>
`;
3 changes: 3 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ docker build -t $TAG \
--build-arg CONTENTFUL_COMCAST_CDN_API_KEY=$CONTENTFUL_COMCAST_CDN_API_KEY \
--build-arg CONTENTFUL_COMCAST_PREVIEW_API_KEY=$CONTENTFUL_COMCAST_PREVIEW_API_KEY \
--build-arg RECRUITCRM_API_KEY=$RECRUITCRM_API_KEY \
--build-arg SENDGRID_API_KEY=$SENDGRID_API_KEY \
--build-arg GROWSURF_API_KEY=$GROWSURF_API_KEY \
--build-arg GROWSURF_CAMPAIGN_ID=$GROWSURF_CAMPAIGN_ID \
--build-arg GSHEETS_API_KEY=$GSHEETS_API_KEY \
--build-arg COMMUNITY_APP_URL=$COMMUNITY_APP_URL .

Expand Down
3 changes: 3 additions & 0 deletions config/custom-environment-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ module.exports = {
},

RECRUITCRM_API_KEY: 'RECRUITCRM_API_KEY',
GROWSURF_API_KEY: 'GROWSURF_API_KEY',
SENDGRID_API_KEY: 'SENDGRID_API_KEY',
},
GROWSURF_CAMPAIGN_ID: 'GROWSURF_CAMPAIGN_ID',
AUTH_CONFIG: {
AUTH0_URL: 'TC_M2M_AUTH0_URL',
AUTH0_AUDIENCE: 'TC_M2M_AUDIENCE',
Expand Down
10 changes: 10 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ module.exports = {
},

RECRUITCRM_API_KEY: '',
GROWSURF_API_KEY: '',
SENDGRID_API_KEY: '',
},

GROWSURF_CAMPAIGN_ID: '',
GROWSURF_COOKIE: '_tc_gigs_ref',
GROWSURF_COOKIE_SETTINGS: {
secure: true,
domain: '',
expires: 7, // days
},

GSHEETS_API_KEY: 'AIzaSyBRdKySN5JNCb2H6ZxJdTTvp3cWU51jiOQ',
Expand Down
2 changes: 2 additions & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import mailChimpRouter from './routes/mailchimp';
import mockDocuSignFactory from './__mocks__/docu-sign-mock';
import recruitCRMRouter from './routes/recruitCRM';
import mmLeaderboardRouter from './routes/mmLeaderboard';
import growsurfRouter from './routes/growsurf';
import gSheetsRouter from './routes/gSheet';

/* Dome API for topcoder communities */
Expand Down Expand Up @@ -138,6 +139,7 @@ async function onExpressJsSetup(server) {
server.use('/api/mailchimp', mailChimpRouter);
server.use('/api/recruit', recruitCRMRouter);
server.use('/api/mml', mmLeaderboardRouter);
server.use('/api/growsurf', growsurfRouter);
server.use('/api/gsheets', gSheetsRouter);

// serve demo api
Expand Down
20 changes: 20 additions & 0 deletions src/server/routes/growsurf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* The routes related to Growsurf integration
*/

import express from 'express';
import GrowsurfService from '../services/growsurf';

const cors = require('cors');

const routes = express.Router();

// Enables CORS on those routes according config above
// ToDo configure CORS for set of our trusted domains
routes.use(cors());
routes.options('*', cors());

routes.get('/participants', (req, res) => new GrowsurfService().getParticipant(req, res).then(res.send.bind(res)));
routes.post('/participants', (req, res) => new GrowsurfService().getOrCreateParticipant(req, res).then(res.send.bind(res)));

export default routes;
3 changes: 3 additions & 0 deletions src/server/routes/mailchimp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import express from 'express';
import MailchimpService from '../services/mailchimp';
import { sendEmail } from '../services/sendGrid';

const routes = express.Router();
/* Sets Access-Control-Allow-Origin header to avoid CORS error.
Expand All @@ -28,4 +29,6 @@ routes.get('/campaign-folders', (req, res) => new MailchimpService().getCampaign

routes.get('/campaigns', (req, res) => new MailchimpService().getCampaigns(req).then(res.send.bind(res)));

routes.post('/email', (req, res) => sendEmail(req, res).then(res.send.bind(res)));

export default routes;
96 changes: 96 additions & 0 deletions src/server/services/growsurf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* Server-side functions necessary for effective integration with growsurf
*/
import fetch from 'isomorphic-fetch';
import config from 'config';

/**
* Auxiliary class that handles communication with growsurf
*/
export default class GrowsurfService {
/**
* Creates a new service instance.
* @param {String} baseUrl The base API endpoint.
*/
constructor(baseUrl = 'https://api.growsurf.com/v2') {
this.private = {
baseUrl,
apiKey: config.SECRET.GROWSURF_API_KEY,
authorization: `Bearer ${config.SECRET.GROWSURF_API_KEY}`,
};
}

/**
* Gets get participant by email or id.
* @return {Promise}
* @param {Object} req the request.
* @param {Object} res the response.
*/
async getParticipant(req, res) {
const { participantId } = req.query;
const response = await fetch(`${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${participantId}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: this.private.authorization,
},
});
if (response.status >= 300) {
res.status(response.status);
return {
error: await response.json(),
code: response.status,
url: `${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${participantId}`,
};
}
const data = await response.json();
return data;
}

/**
* Add participant
* @return {Promise}
* @param {Object} body the request payload.
*/
async addParticipant(body) {
const response = await fetch(`${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: this.private.authorization,
},
body,
});
if (response.status >= 300) {
return {
error: await response.json(),
code: response.status,
url: `${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant`,
body,
private: this.private, // to remove in final release
};
}
const data = await response.json();
return data;
}

/**
* Gets get participant by email or id
* if not exxists create it
* @return {Promise}
* @param {Object} req the request.
* @param {Object} res the response.
*/
async getOrCreateParticipant(req, res) {
const { body } = req;
const result = await this.addParticipant(JSON.stringify({
email: body.email,
firstName: body.firstName,
lastName: body.lastName,
}));
if (result.error) {
res.status(result.code);
}
return result;
}
}
39 changes: 34 additions & 5 deletions src/server/services/recruitCRM.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fetch from 'isomorphic-fetch';
import config from 'config';
import qs from 'qs';
import _ from 'lodash';
import GrowsurfService from './growsurf';

const FormData = require('form-data');

Expand Down Expand Up @@ -188,7 +189,35 @@ export default class RecruitCRMService {
const fileData = new FormData();
fileData.append('resume', file.buffer, file.originalname);
let candidateSlug;
let referralCookie = req.cookies[config.GROWSURF_COOKIE];
if (referralCookie) referralCookie = JSON.parse(referralCookie);
try {
// referral tracking via growsurf
if (referralCookie && referralCookie.gigId === id) {
const gs = new GrowsurfService();
const growRes = await gs.addParticipant(JSON.stringify({
email: form.email,
referredBy: referralCookie.referralId,
referralStatus: 'CREDIT_PENDING',
firstName: form.first_name,
lastName: form.last_name,
metadata: {
gigId: id,
},
}));
// If everything set in Growsurf
// add referral link to candidate profile in recruitCRM
if (!growRes.error) {
form.custom_fields.push({
field_id: 6, value: `https://app.growsurf.com/dashboard/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${growRes.id}`,
});
}
// clear the cookie
res.cookie(config.GROWSURF_COOKIE, '', {
maxAge: 0,
overwrite: true,
});
}
// Check if candidate exsits in the system?
const candidateResponse = await fetch(`${this.private.baseUrl}/v1/candidates/search?email=${form.email}`, {
method: 'GET',
Expand All @@ -197,7 +226,7 @@ export default class RecruitCRMService {
Authorization: this.private.authorization,
},
});
if (candidateResponse.status >= 400) {
if (candidateResponse.status >= 300) {
return res.send({
error: true,
status: candidateResponse.status,
Expand Down Expand Up @@ -231,7 +260,7 @@ export default class RecruitCRMService {
},
body: JSON.stringify(form),
});
if (workCandidateResponse.status >= 400) {
if (workCandidateResponse.status >= 300) {
return res.send({
error: true,
status: workCandidateResponse.status,
Expand All @@ -251,7 +280,7 @@ export default class RecruitCRMService {
},
body: fileData,
});
if (fileCandidateResponse.status >= 400) {
if (fileCandidateResponse.status >= 300) {
return res.send({
error: true,
status: fileCandidateResponse.status,
Expand All @@ -272,7 +301,7 @@ export default class RecruitCRMService {
Authorization: this.private.authorization,
},
});
if (applyResponse.status >= 400) {
if (applyResponse.status >= 300) {
const errObj = await applyResponse.json();
if (errObj.errorCode === 422 && errObj.errorMessage === 'Candidate is already assigned to this job') {
return res.send({
Expand Down Expand Up @@ -301,7 +330,7 @@ export default class RecruitCRMService {
status_id: '10',
}),
});
if (hireStageResponse.status >= 400) {
if (hireStageResponse.status >= 300) {
return res.send({
error: true,
status: hireStageResponse.status,
Expand Down
41 changes: 41 additions & 0 deletions src/server/services/sendGrid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Server-side functions necessary for sending emails via Sendgrid APIs
*/
import config from 'config';
import { logger } from 'topcoder-react-lib';
import fetch from 'isomorphic-fetch';

/**
* Sends emails via the Sendgrid API
* https://sendgrid.com/docs/api-reference/
* @param {Object} req the request
* @param {Object} res the response
*/
export const sendEmail = async (req, res) => {
try {
const msg = req.body;
const response = await fetch('https://api.sendgrid.com/v3/mail/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${config.SECRET.SENDGRID_API_KEY}`,
},
body: JSON.stringify(msg),
});
res.status(response.status);
return {};
} catch (error) {
logger.error(error);
const { message, code, response } = error;
res.status(code || 500);
if (error.response) {
const { headers, body } = response;
return {
message, headers, body,
};
}
return { message };
}
};

export default undefined;
4 changes: 4 additions & 0 deletions src/shared/components/GUIKit/Textarea/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function Textarea({
value,
onChange,
required,
readonly,
}) {
const [val, setVal] = useState(value);
const delayedOnChange = useRef(
Expand All @@ -25,6 +26,7 @@ function Textarea({
return (
<div className="textareaContainer" styleName="container">
<textarea
readOnly={readonly}
defaultValue={value}
placeholder={`${placeholder}${placeholder && required ? ' *' : ''}`}
id="textAreaInput"
Expand Down Expand Up @@ -54,6 +56,7 @@ Textarea.defaultProps = {
value: '',
onChange: () => {},
required: false,
readonly: false,
};

Textarea.propTypes = {
Expand All @@ -63,6 +66,7 @@ Textarea.propTypes = {
value: PT.string,
onChange: PT.func,
required: PT.bool,
readonly: PT.bool,
};

export default Textarea;
Loading