-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
311 lines (284 loc) · 9.15 KB
/
index.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
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
/*------------------------------------
Express Config
--------------------------------------*/
const express = require("express");
const app = express();
/*------------------------------------
Firebase Config
--------------------------------------*/
const key = require("./key.json");
const { initializeApp, cert } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");
initializeApp({
credential: cert(key),
});
const db = getFirestore();
/*--------------------------------------------
External Functions
----------------------------------------------*/
const { addUser, authenticate, signout } = require("./src/user_service");
const errorHandler = require("./src/errorhandler");
const { requireAuth, checkUser } = require("./src/authMiddleware");
const { imageUpload } = require("./src/imageUpload");
const { imageDelete } = require("./src/imageDelete");
/*--------------------------------------
MiddleWares
----------------------------------------*/
const bodyParser = require("body-parser");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const PORT = process.env.PORT || 8080;
const Multer = require("multer");
const multer = Multer({
storage: Multer.memoryStorage(),
limits: {
fileSize: 2 * 1024 * 1024, //2mb
},
extended: true,
});
const pdfparse = require("pdf-parse");
const pdfMulter = Multer({ dest: "uploads/", storage: Multer.memoryStorage() });
//-----------paths---------------------------
const path = require("path");
const staticPath = path.join(__dirname, "/public");
const templatePath = path.join(__dirname, "/templates");
//--------use & sets-------------------------------
app.set("view engine", "hbs");
app.set("views", templatePath);
app.use(express.static(staticPath));
//app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(cors());
//--------------------jwt-------------------------------
const jwt = require("jsonwebtoken");
const maxAge = 3 * 24 * 60 * 60;
const createToken = (id) => {
return jwt.sign({ id }, "resume2hire", {
expiresIn: maxAge,
});
};
/*-------------------------------------------
Endpoints
--------------------------------------------*/
//checkuser
app.get("*", checkUser);
//*-----------------------Home---------------------------------
app.get("/", async (req, res) => {
res.sendFile(staticPath + "/home/index.html");
});
//----------------------Authentication----------------------------
//signup (get)
app.get("/signup", async (req, res) => {
res.sendFile(staticPath + "/signup.html");
});
//signup (post)- create account
app.post("/signup", async (req, res) => {
const { email, password } = req.body;
try {
const userResponse = await addUser(email, password);
const token = createToken(email);
res.cookie("jwt", token, { httpOnly: true, maxAge: maxAge * 1000 });
res.status(201).json(userResponse.user);
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//signin (get)
app.get("/signin", async (req, res) => {
res.sendFile(staticPath + "/signin.html");
});
//signin (post) - to account
app.post("/signin", async (req, res) => {
const { email, password } = req.body;
try {
const userResponse = await authenticate(email, password);
const token = createToken(userResponse.user.email);
res.cookie("jwt", token, { httpOnly: true, maxAge: maxAge * 1000 });
res.status(200).json(userResponse.user);
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//signout (get)
app.get("/signout", requireAuth, async (req, res) => {
await signout
.then(() => {
res.cookie("jwt", "", { maxAge: 1 });
res.status(200).send("OK");
})
.catch((error) => {
res.status(400).json({ error: errorHandler.handleErrors(error) });
});
});
//*-----------------------Resume Builder---------------------------------
app.get("/resumeBuilder", async (req, res) => {
res.sendFile(staticPath + "/resumeBuilder.html");
});
//----------------------Hiring ----------------------------
//hiring (get)
app.get("/hiring", requireAuth, async (req, res) => {
try {
const jobPostsRef = db.collection("JobPosts");
const jobPostedBy = jwt.verify(req.cookies.jwt, "resume2hire").id;
const response = await jobPostsRef
.where("jobPostedBy", "==", jobPostedBy)
.get();
let responseArray = [];
response.forEach((doc) => {
responseArray.push(doc.data());
});
res.render(templatePath + "/hiring.hbs", {
jobposts: responseArray.reverse(),
});
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//add job post (get)
app.get("/addJobPost", requireAuth, async (req, res) => {
res.sendFile(staticPath + "/addJobPost.html");
});
//image upload (post)
app.post(
"/uploadCompanyLogo",
requireAuth,
multer.single("companyLogo"),
imageUpload
);
//add job post (post)
app.post("/addJobPost", requireAuth, async (req, res) => {
try {
const jobPostId = `${
jwt.verify(req.cookies.jwt, "resume2hire").id
}_${Date.now()}`;
const jobPostJson = {
jobPostedBy: jwt.verify(req.cookies.jwt, "resume2hire").id,
jobPostId: jobPostId,
jobTitle: req.body.jobTitle,
jobId: req.body.jobId,
companyName: req.body.companyName,
companyLogoUrl: req.body.companyLogoUrl,
jobType: req.body.jobType,
jobFunction: req.body.jobFunction,
jobLocation: req.body.jobLocation,
jobMode: req.body.jobMode,
salary: req.body.salary,
experienceLevel: req.body.experienceLevel,
yearOfExperience: req.body.yearOfExperience,
skillsRequired: req.body.skillsRequired,
emailHR: req.body.emailHR,
hiringLink: req.body.hiringLink,
jobDescription: req.body.jobDescription,
};
const response = await db
.collection("JobPosts")
.doc(jobPostId)
.set(jobPostJson);
res.status(200).json(response);
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//edit job post (get)
app.get("/editJobPost/:id", requireAuth, async (req, res) => {
try {
const jobPostRef = db.collection("JobPosts").doc(req.params.id);
const response = await jobPostRef.get();
res.render(templatePath + "/editJobPost.hbs", response.data());
} catch (error) {
res.send("error: " + error);
}
});
//edit a job post(put)
app.post("/editJobPost/:id", requireAuth, async (req, res) => {
try {
const jobPostJson = {
jobTitle: req.body.jobTitle,
jobId: req.body.jobId,
companyName: req.body.companyName,
jobType: req.body.jobType,
jobLocation: req.body.jobLocation,
jobFunction: req.body.jobFunction,
experienceLevel: req.body.experienceLevel,
jobMode: req.body.jobMode,
salary: req.body.salary,
yearOfExperience: req.body.yearOfExperience,
skillsRequired: req.body.skillsRequired,
emailHR: req.body.emailHR,
hiringLink: req.body.hiringLink,
jobDescription: req.body.jobDescription,
};
const response = await db
.collection("JobPosts")
.doc(req.params.id)
.update(jobPostJson);
res.status(200).json(response);
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//delete a job post (delete)
app.post("/deleteJobPost/:id", requireAuth, async (req, res) => {
try {
imageDelete(req.body.imageUrl);
const response = await db
.collection("JobPosts")
.doc(req.params.id)
.delete();
res.status(200).json(response);
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//---------------------Jobs---------------------------------
// read all job posts
app.get("/readJobPost/all", async (req, res) => {
try {
const jobPostsRef = db.collection("JobPosts");
const response = await jobPostsRef.get();
let responseArray = [];
response.forEach((doc) => {
responseArray.push(doc.data());
});
res.render(templatePath + "/jobs.hbs", {
jobposts: responseArray.reverse(),
});
} catch (error) {
res.send("error: " + error);
}
});
// read job post by id
app.get("/readJobPost/:id", async (req, res) => {
try {
const jobPostRef = db.collection("JobPosts").doc(req.params.id);
const response = await jobPostRef.get();
res.render(templatePath + "/jobpost.hbs", response.data());
} catch (error) {
res.send("error: " + error);
}
});
//----------------------Screening-----------------------------
app.get("/screening", async (req, res) => {
res.sendFile(staticPath + "/screening.html");
});
/*---------------------------------
extract resume text
------------------------------------*/
app.post("/extractResumeText", pdfMulter.single("resume"), async (req, res) => {
if (!req.file) {
res.sendStatus(400);
res.end();
}
await pdfparse(req.file.buffer).then((result) => {
res.send(result.text);
});
});
/*---------------------------------
app listen
------------------------------------*/
app.listen(PORT, () => {
console.log(`Server is running on PORT http://localhost:${PORT}`);
});