-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-files-for-greenhouse.js
212 lines (185 loc) · 7.29 KB
/
generate-files-for-greenhouse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const fs = require('fs');
const path = require('path');
const repl = require('repl');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const cliProgress = require('cli-progress');
const colors = require('ansi-colors');
const DATA_DIRECTORY = path.join(__dirname, 'data');
const EXPORTED_JSON_FILE = path.join(DATA_DIRECTORY, 'lever-export.json');
const CANDIDATE_CSV_FILE = path.join(DATA_DIRECTORY, 'candidates-for-greenhouse-import.csv');
const PROSPECT_CSV_FILE = path.join(DATA_DIRECTORY, 'prospects-for-greenhouse-import.csv');
const RESUMES_TEMP_DIRECTORY = path.join(DATA_DIRECTORY, 'delete_after_resumes_zipped');
const CANDIDATE_RESUMES_ZIP = path.join(DATA_DIRECTORY, 'candidate-resumes-for-greenhouse-import.zip');
const PROSPECT_RESUMES_ZIP = path.join(DATA_DIRECTORY, 'prospect-resumes-for-greenhouse-import.zip');
const ASSET_DIRECTORY = path.join(DATA_DIRECTORY, 'assetsByOpportunityId');
function mapStageTextToMilestone(stageText) {
switch(stageText) {
case 'Skills Assessment':
case 'Recruiter Phone Screen':
case 'Hiring Manager Phone Screen': {
return 'Assessment';
}
case 'On-site interview': {
return 'Face to Face';
}
case 'Offer': {
return 'Offer'
}
// All active opportunities at the very least have an application.
default: {
return 'Application'
}
}
}
// Convert all tabs to spaces, tabs is the reserved field separator
// Convert all newlines to double spaces, newline is reserved row separator
function sanitizeForCsv(str) {
return str.replace(/\t+/g, ' ').replace(/\n+/g, ' ');
}
function transcribeNote(note, potentialAuthors) {
const noteText = note.fields.map(f => f.value).join('\n');
const noteAuthor = potentialAuthors[note.user]?.name ?? 'Unknown author';
return `${noteText}\n --${noteAuthor}`;
}
async function zipResumesForOpportunities(opportunities, zipPath, progressTitle) {
await exec(`mkdir -p ${RESUMES_TEMP_DIRECTORY}`);
const fileMoves = opportunities.reduce((moves, o) => {
return moves.concat(o.resumes.map((r, resumeIndex) => ({
origin: path.join(ASSET_DIRECTORY, o.id, 'resumes', r.file.name),
destination: path.join(RESUMES_TEMP_DIRECTORY, `${o.contact.name}${resumeIndex > 0 ? ` ${resumeIndex}` : ''}${r.file.ext}`)
})));
}, []);
const progressBar = new cliProgress.SingleBar({
format: `${progressTitle} |` + colors.cyan('{bar}') + '| {percentage}% || {value}/{total} files moved || {duration_formatted}, ETA: {eta_formatted}',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});
progressBar.start(fileMoves.length);
while (fileMoves.length > 0) {
const { origin, destination } = fileMoves.shift();
await exec(`cp "${origin}" "${destination}"`);
progressBar.increment();
}
progressBar.stop();
await exec(`cd ${RESUMES_TEMP_DIRECTORY} && zip -r ${zipPath} .`);
console.log(`Aggregated resumes for active candidates in ${zipPath}`);
await exec(`rm -rf ${RESUMES_TEMP_DIRECTORY}`);
}
async function main() {
const opportunities = JSON.parse(fs.readFileSync(EXPORTED_JSON_FILE));
const knownDraftUsers = opportunities.reduce(
(knownDraftUsers, o) => {
o.followers.concat(o.owner ? [o.owner] : []).forEach((user) => {
knownDraftUsers[user.id] = user;
});
return knownDraftUsers;
},
{}
);
const { leads, active, archived } = opportunities.reduce(
({ leads, active, archived }, o) => {
if (o.archived) {
return { leads, active, archived: [ ...archived, o ] };
}
if (o.applications && o.applications.length > 0) {
return { leads, archived, active: [ ...active, o ] };
}
return { active, archived, leads: [ ...leads, o ] };
},
{ leads: [], active: [], archived: [] }
);
// CANDIDATES DATA GENERATION
const greenhouseCandidates = active.map((o) => ({
first: o.contact.name.trim().split(/\s+/)[0] || '(none specified)',
last: o.contact.name.trim().split(/\s+/).slice(1).join(' ').replace(/^\s+$/, '') || '(none specified)',
company: o.contact.headline || '(none specified)',
notes: o.notes.map(n => transcribeNote(n, knownDraftUsers)).join(', ') ?? '',
// All instances of multiple emails were observed to be duplicates of the
// first email. Greenhouse only accepts one email, so just eliminate
// duplicates.
email: o.contact.emails[0] ?? '',
phone: o.contact.phones.map(p => `${p.type}:${p.value}`).join(', ') ?? '',
socialMedia: o.links.join(', ') ?? '',
address: o.contact.location?.name ?? '',
source: o.sources.join(', ') ?? '',
job: o.applications[0].posting.text,
milestone: mapStageTextToMilestone(o.stage.text)
}));
const candidateCsvRows = greenhouseCandidates.map(c => {
return [
c.first,
c.last,
c.company,
'' /* title */,
c.notes,
c.email,
c.phone,
c.socialMedia,
'' /* website */,
c.address,
c.source,
'' /* who gets credit */,
c.job,
c.milestone
].map(field => {
return sanitizeForCsv(field);
}).join('\t');
});
const candidateCsvData = [
'First Name\tLast Name\tCompany\tTitle\tNotes\tEmail\tPhone\tSocial Media\tWebsite\tAddress\tSource\tWho gets credit\tJob\tMilestone',
...candidateCsvRows
].join('\n');
fs.writeFileSync(CANDIDATE_CSV_FILE, candidateCsvData);
console.log(`Wrote candidates to ${CANDIDATE_CSV_FILE}`);
// PROSPECTS DATA GENERATION
const greenhouseProspects = leads
.filter((o) => !!o.contact.name.trim())
.map((o) => ({
first: o.contact.name.trim().split(/\s+/)[0] || '(none specified)',
last: o.contact.name.trim().split(/\s+/).slice(1).join(' ').replace(/^\s+$/, '') || '(none specified)',
company: o.contact.headline || '(none specified)',
notes: o.notes.map(n => transcribeNote(n, knownDraftUsers)).join(', ') ?? '',
// All instances of multiple emails were observed to be duplicates of the
// first email. Greenhouse only accepts one email, so just eliminate
// duplicates.
email: o.contact.emails[0] ?? '',
phone: o.contact.phones.map(p => `${p.type}:${p.value}`).join(', ') ?? '',
socialMedia: o.links.join(', ') ?? '',
address: o.contact.location?.name ?? '',
source: o.sources.join(', ') ?? '',
}));
const prospectCsvRows = greenhouseProspects.map(p => {
return [
p.first,
p.last,
p.company,
'' /* title */,
p.notes,
p.email,
p.phone,
p.socialMedia,
'' /* website */,
p.address,
p.source,
'' /* who gets credit */,
'' /* job */,
'' /* department */,
'' /* pool */,
'' /* prospect stage */
].map(field => {
return sanitizeForCsv(field);
}).join('\t');
});
const prospectCsvData = [
'First Name\tLast Name\tCompany\tTitle\tNotes\tEmail\tPhone\tSocial Media\tWebsite\tAddress\tSource\tWho gets credit\tJob\tDepartment\tPool\tProspect Stage',
...prospectCsvRows
].join('\n');
fs.writeFileSync(PROSPECT_CSV_FILE, prospectCsvData);
console.log(`Wrote prospects to ${PROSPECT_CSV_FILE}`);
await zipResumesForOpportunities(active, CANDIDATE_RESUMES_ZIP, 'Candidate Resume Zip');
await zipResumesForOpportunities(leads, PROSPECT_RESUMES_ZIP, 'Prospect Resume Zip');
process.exit(0);
}
main();