-
Notifications
You must be signed in to change notification settings - Fork 5
/
applicationController.ts
329 lines (295 loc) · 9.23 KB
/
applicationController.ts
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import * as Koa from 'koa';
import { getRepository, Repository } from 'typeorm';
import {
AcceptedCommitteeType,
Application,
ApplicationStatus
} from '../../database/entities/Application';
import {
CommitteeChoice,
CommitteeType
} from '../../database/entities/CommitteeChoice';
// import { send } from "../utils/smtpClient";
import { getNotes, getComments } from './notesController';
import axios from 'axios';
/**
* Parsing is very specific to the kind of form we have right now.
* For exampl, I am getting 'freshman', 'sophomore', etc. by splitting
* the year field in the TypeForm. TODO: add typeform entries as a table
* and generalize the parsing.
* @param {request, response}
*/
const parseTypeFormWebHook = async ({ request, response }: Koa.Context) => {
// populate application
const application:
| {
form_response: { definition: { fields: any[] }; answers: any };
}
| any = request.body;
await parseApplicant(application.form_response);
response.body = 'Submission received!';
};
/**
* Parse existing applicants from TypeForm
* @param appicantObject
*/
const parseTypeFormResponses = async ({ response }: Koa.Context) => {
const formId = process.env.TYPEFORM_ID;
// get JSON from typeform
const url = `https://api.typeform.com/forms/${formId}/responses`;
const config = {
headers: { Authorization: `Bearer ${process.env.TYPEFORM_TOKEN}` }
};
let applicants: any[] = [];
await axios.get(url, config).then((res: { data: { items: any[] } }) => {
applicants = res.data.items;
});
for (const applicantObject of applicants) {
await parseApplicant(applicantObject);
}
response.body = 'Submission received!';
};
/**
* Parses an applicant submission's object from TypeForm API. TypeForm JSON format is generic
* for both WebHook and Reponses API - this function is used in both cases. Responses API is used to load
* responses before the start-up of the application.
* @param appicantObject
*/
const parseApplicant = async (applicantObject: { answers: any }) => {
// Repositories
const applicationRepo: Repository<Application> = getRepository(Application);
const committeeChoiceRepo: Repository<CommitteeChoice> = getRepository(
CommitteeChoice
);
const responses: any = {};
applicantObject.answers.forEach((answer: any) => {
// Map field reference name (set in TypeForm) to the response
if (answer.type === 'choice') {
responses[answer.field.ref] = answer.choice.label;
} else if (answer.type === 'choices') {
responses[answer.field.ref] = answer.choices.labels.map((c: string) =>
c.toLowerCase()
);
} else {
responses[answer.field.ref] = answer[answer.type];
}
});
const existsingNum = await applicationRepo.count({
email: responses.email
});
if (existsingNum === 0) {
const newApplication: Application = applicationRepo.create({
name: `${responses.firstName} ${responses.lastName}`,
email: responses.email,
year: responses.year.split(' ')[0].toLowerCase(),
director: responses.director,
status: ApplicationStatus.APPLIED,
essay1: responses.essay1,
essay2: responses.essay2,
essay3: responses.essay3,
resume_link: responses.resume_link,
commitments: responses.commitments,
attendedVH: responses.attendedVH,
feedback: responses.feedback,
github_link: responses.github_link,
linkedin_link: responses.linkedin_link,
social_link: responses.social_link,
design_link: responses.design_link,
source: responses.source,
committee_accepted: AcceptedCommitteeType.UNDECIDED
});
await applicationRepo.save(newApplication);
// Create committee relations for an applicant
responses.committees.forEach((committee: CommitteeType) => {
const committeeChoice: CommitteeChoice = committeeChoiceRepo.create({
committee: committee,
application: newApplication
});
committeeChoiceRepo.save(committeeChoice);
});
}
};
/**
* Display all applications for the grid on the main page.
* Resulting format of data in response:
* [{
* id: number,
* name: string,
* email: string,
* year: string,
* director: bool,
* status: string,
* resume_link: string,
* committee_accepted: string
* }]
* @param {response}
*/
const displayApplications = async ({ response }: Koa.Context) => {
// Application repository
const applicationRepo: Repository<Application> = getRepository(Application);
// Load applications with committees specified
const applications: Application[] = await applicationRepo.find({
select: [
'id',
'name',
'email',
'year',
'director',
'status',
'resume_link',
'committee_accepted'
],
relations: ['committees'],
order: {
id: 'ASC'
}
});
response.body = applications;
};
/**
* Helper method that gets application responses, populated by the TypeForm webhook.
* Return:
* {
* name: string,
* email: string,
* year: string,
* director: boolean,
* status: string,
* essay1: string,
* essay2: string,
* essay3: string,
* commitments: string,
* attendedVH: boolean,
* feedback: string,
* source: string
* resume_link: string | null,
* github_link: string | null,
* linkedin_link: string | null,
* social_link: string | null,
* design_link: string | null,
* committee_accepted: string | null,
* committees: {id: number, text: {committee: string}}[],
* }
* @param applicationId
*/
const getApplicationResponse = async (
applicationId: number
): Promise<Application | undefined> => {
const applicationRepo: Repository<Application> = getRepository(Application);
const application: Application | undefined = await applicationRepo.findOne(
applicationId,
{
relations: ['committees']
}
);
return application;
};
/**
* body: number, representing applicationId
* response:
* {
* application: {
* name: string,
* email: string,
* year: string,
* director: boolean,
* status: string,
* essay1: string,
* essay2: string,
* essay3: string,
* commitments: string,
* attendedVH: boolean,
* feedback: string,
* source: string
* resume_link: string | null,
* github_link: string | null,
* linkedin_link: string | null,
* social_link: string | null,
* design_link: string | null,
* committee_accepted: string | null,
* committees: string[],
* },
* notes:
* [{
* interviewer_name: string,
* reliability: number [1-7],
* interest: number [1-7],
* teamwork: number [1-7],
* overall: number [1-7],
* thoughts: string,
* responses: [{question: string, description: string, specificity: string, note: string}]
* }],
* comments: [{commenter_name: string, content: string}]
* }
*/
const getApplicantData = async ({ params, response }: Koa.Context) => {
const applicationId: number = (params.applicationId as unknown) as number;
response.body = {
application: await getApplicationResponse(applicationId),
notes: await getNotes(applicationId),
comments: await getComments(applicationId)
};
};
/**
* Send email based on status update.
*/
const sendEmail = async (email: string, status: string, committee?: string) => {
// let emailMessage: string;
// if (status === "accepted") {
// emailMessage = `Congratualations! You are accepted to ${committee} committee!`;
// } else if (status === "rejected") {
// emailMessage =
// "Unfortunately, we were not able to extend the position on VH boar to you.";
// } else if (status === "to_interview") {
// emailMessage = "Hey! Let's talk tomorrow 2pm CST.";
// } else {
// console.log(
// "Wrong status passed to sending emails. We have no emails for this case."
// );
// return;
// }
// send(email, "VandyHacks Board Decision", emailMessage);
};
/**
* ApplicationID and new status are sent in the body.
* body: {status: string, committee?: string}
*/
const updateStatus = async ({ params, request, response }: Koa.Context) => {
const body: { status: string; committee?: string } | any = request.body;
const applicationId = (params.applicationId as unknown) as number;
const newStatus: ApplicationStatus = body.status;
const committee: AcceptedCommitteeType | undefined = body.committee;
// Application repo
const applicationRepo: Repository<Application> = getRepository(Application);
// Get application to update
const application: Application | undefined = await applicationRepo.findOne(
applicationId,
{
select: ['id', 'status', 'email']
}
);
if (application) {
// Logic for emails
sendEmail(application.email as string, newStatus);
// Update status
application.status = newStatus;
// Update committees if accepted
if (newStatus === ApplicationStatus.ACCEPTED && committee) {
application.committee_accepted = committee;
}
await applicationRepo.save(application);
response.body = `Successfully updated ${application.email} with new status: ${newStatus}`;
} else {
response.status = 404;
response.body = {
message: 'Application is not found'
};
}
};
export {
parseTypeFormWebHook,
parseTypeFormResponses,
displayApplications,
updateStatus,
getApplicantData
};