Created
October 1, 2018 17:12
-
-
Save dallashuggins/720b76bf03b6062b5176d60801126922 to your computer and use it in GitHub Desktop.
Recently built function to retrieve matches from Pocket Recruiter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rp = require('request-promise'); | |
const util = require('util'); | |
const _ = require('underscore'); | |
const Promise = require('bluebird'); | |
var moment = require('moment'); | |
const authenticate = async (credentials) => { | |
try { | |
let options = {}; | |
options.method = "POST"; | |
options.uri = `https://${credentials.prefix}.pocketrecruiter.com/api/authenticate`; | |
options.headers = {}; | |
options.headers['Content-Type'] = 'application/x-www-form-urlencoded'; | |
options.headers['Accept'] = 'application/json'; | |
options.form = {}; | |
options.form.username = credentials.username; | |
options.form.password = credentials.password; | |
options.qs = {}; | |
options.resolveWithFullResponse = true; | |
options.json = true; | |
let response = await rp(options); | |
if (response.statusCode === 200) { | |
let body = response.body; | |
return body; | |
} else if (response.body.message) { | |
throw new Error ({message: response.body.message}); | |
} else if (response.statusCode === 500) { | |
throw new Error ({message: 'Authentication failed.'}); | |
} else { | |
throw new Error ({message: 'Did not work.'}); | |
} | |
} | |
catch (e) { | |
throw new Error (e); | |
} | |
} | |
const getMatchesPR = async (credentials, job, page, authentication) => { | |
try { | |
let yesterday = moment().subtract(1, 'day'); | |
let date = moment(yesterday).unix(); | |
let options = {}; | |
options.method = "GET"; | |
options.uri = `https://${credentials.prefix}.pocketrecruiter.com/api/matches`; | |
options.headers = {}; | |
options.headers['Content-Type'] = 'application/json'; | |
options.headers['Accept'] = 'application/json'; | |
options.headers['x-access-token'] = authentication.token; | |
options.qs = {}; | |
options.qs.jobpostId = job.id; | |
options.qs.page = page; | |
options.qs.lastUpdated = date; | |
options.qs.status = 'SELECTED'; | |
options.resolveWithFullResponse = true; | |
options.json = true; | |
let response = await rp(options); | |
if (response.statusCode === 200) { | |
let array = await _.map(response.body.matches, async (match) => { | |
return match; | |
}); | |
return array; | |
} else if (response.body.message) { | |
throw new Error ({message: response.body.message}); | |
} else if (response.statusCode === 500) { | |
throw new Error ({message: 'Authentication failed.'}); | |
} else { | |
throw new Error ({message: 'Did not work.'}); | |
} | |
} | |
catch (e) { | |
console.log("Error getMatchesPR:", util.inspect(e, {showHidden: false, depth: null})); | |
throw new Error (e); | |
} | |
} | |
let recursive = async (credentials, job, authentication, matches = [], i = 1) => { | |
try { | |
let response = await getMatchesPR(credentials, job, i, authentication); | |
if (response.length === 0) { | |
return matches || []; | |
} else { | |
matches = await matches.concat(response); | |
i++; | |
return await recursive(credentials, job, authentication, matches, i); | |
} | |
} catch (e) { | |
console.log("getMatchesPR recursive error:", util.inspect(e, {showHidden: false, depth: null})); | |
throw new Error (e); | |
} | |
}; | |
async function main (credentials, jobArray) { | |
try { | |
let authentication = await authenticate(credentials); | |
let newArray = []; | |
await Promise.map(jobArray, async (job) => { | |
let final = {}; | |
final.id = job.id; | |
final.title = job.jobTitle; | |
final.description = job.requirements; | |
final.externalId = job.externalId; | |
let matches = await recursive(credentials, job, authentication); | |
let candidatesExisting = []; | |
let candidates = []; | |
await Promise.map(matches, (match) => { | |
if (match.candidate && match.candidate.notes && match.candidate.notes.length > 0) { | |
if (job.externalId && _.findWhere(match.candidate.notes, {note: job.externalId}) !== undefined && _.contains(candidatesExisting, match.candidate) == false) { | |
candidatesExisting.push(match.candidate); | |
} else if (job.externalId && _.findWhere(match.candidate.notes, {note: job.externalId}) == undefined && _.contains(candidates, match.candidate) == false) { | |
candidates.push(match.candidate); | |
} | |
} else if (job.externalId && _.contains(candidates, match.candidate) == false) { | |
candidates.push(match.candidate) | |
} | |
}); | |
final.candidates = candidates; | |
final.candidatesExisting = candidatesExisting; | |
newArray.push(final); | |
}); | |
return newArray; | |
} catch (e) { | |
console.log("getMatchesPR main function for matches error:", await util.inspect(e, {showHidden: false, depth: null})); | |
throw new Error(e); | |
} | |
} | |
module.exports = main; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment