-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
287 lines (239 loc) · 7.98 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
require("dotenv").config();
const express = require("express");
const { MongoClient, ObjectId } = require("mongodb");
const app = express();
const port = process.env.PORT || 5000;
const cors = require("cors");
app.use(cors({ origin: "*" }));
app.use(express.json());
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.y9cyf.mongodb.net/?retryWrites=true&w=majority
`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const run = async () => {
try {
const db = client.db("blogPost");
const userCollection = db.collection("user");
const jobCollection = db.collection("job");
const messageCollection = db.collection("messages");
app.post("/user", async (req, res) => {
const user = req.body;
console.log(user);
const result = await userCollection.insertOne(user);
res.send(result);
});
app.get("/user/:email", async (req, res) => {
const email = req.params.email;
const result = await userCollection.findOne({ email });
if (result?.email) {
return res.send({ status: true, data: result });
}
res.send({ status: false });
});
app.patch("/apply", async (req, res) => {
const userId = req.body.userId;
const jobId = req.body.jobId;
const email = req.body.email;
const name = req.body.name;
const status = req.body.status;
const appliedDate = req.body.appliedDate;
const filter = { _id: ObjectId(jobId) };
const updateDoc = {
$push: {
applicants: {
id: ObjectId(userId),
email,
name,
status,
appliedDate,
},
},
};
const result = await jobCollection.updateOne(filter, updateDoc);
if (result.acknowledged) {
return res.send({ status: true, data: result });
}
res.send({ status: false });
});
app.patch("/query", async (req, res) => {
const userId = req.body.userId;
const jobId = req.body.jobId;
const email = req.body.email;
const question = req.body.question;
const filter = { _id: ObjectId(jobId) };
const updateDoc = {
$push: {
queries: {
id: ObjectId(userId),
email,
question: question,
reply: [],
},
},
};
const result = await jobCollection.updateOne(filter, updateDoc);
if (result?.acknowledged) {
return res.send({ status: true, data: result });
}
res.send({ status: false });
});
app.patch("/reply", async (req, res) => {
const userId = req.body.userId;
const reply = req.body.reply;
const filter = { "queries.id": ObjectId(userId) };
const updateDoc = {
$push: {
"queries.$[user].reply": reply,
},
};
const arrayFilter = {
arrayFilters: [{ "user.id": ObjectId(userId) }],
};
const result = await jobCollection.updateOne(
filter,
updateDoc,
arrayFilter
);
if (result.acknowledged) {
return res.send({ status: true, data: result });
}
res.send({ status: false });
});
app.get("/applied-jobs/:email", async (req, res) => {
const email = req.params.email;
const query = { applicants: { $elemMatch: { email: email } } };
const cursor = jobCollection.find(query).project({ applicants: 0 });
const result = await cursor.toArray();
res.send({ status: true, data: result });
});
app.get("/jobs", async (req, res) => {
const cursor = jobCollection.find({});
const result = await cursor.toArray();
res.send({ status: true, data: result });
});
app.get("/job/:id", async (req, res) => {
const id = req.params.id;
const result = await jobCollection.findOne({ _id: ObjectId(id) });
res.send({ status: true, data: result });
});
app.post("/job", async (req, res) => {
const job = req.body;
const result = await jobCollection.insertOne(job);
res.send({ status: true, data: result });
});
app.get("/posted-jobs/:id", async (req, res) => {
const id = req.params.id;
const cursor = jobCollection.find({
userId: id,
});
const result = await cursor.toArray();
res.send({ status: true, data: result });
});
app.patch("/close", async (req, res) => {
const jobId = req.body.jobId;
const filter = { _id: ObjectId(jobId) };
const updateDoc = {
$set: { jobStatus: "closed" },
};
const result = await jobCollection.updateOne(filter, updateDoc);
if (result.acknowledged) {
return res.send({ status: true, data: result });
}
res.send({ status: false });
});
app.get("/candidate/:id", async (req, res) => {
const id = req.params.id;
const result = await userCollection.findOne({ _id: ObjectId(id) });
res.send({ status: true, data: result });
});
app.patch("/approve", async (req, res) => {
const id = req.body._id;
const applicantsId = req.body.id;
const result = await jobCollection.updateOne(
{ _id: ObjectId(id), "applicants.id": ObjectId(applicantsId) },
{
$set: {
"applicants.$.status": "approved",
},
}
);
if (result.acknowledged) {
return res.send({ status: true, data: result });
}
res.send({ status: false });
});
app.get("/approved-jobs", async (req, res) => {
const status = req.query.status;
const email = req.query.email;
const query = {
applicants: { $elemMatch: { email: email, status: status } },
};
const cursor = jobCollection.find(query).project({ applicants: 0 });
const result = await cursor.toArray();
res.send({ status: true, data: result });
});
app.get("/searchByDate", async (req, res) => {
const appliedDate = req.query.appliedDate;
const email = req.query.email;
const query = {
applicants: { $elemMatch: { email: email, appliedDate: appliedDate } },
};
const cursor = jobCollection.find(query).project({ applicants: 0 });
const result = await cursor.toArray();
res.send({ status: true, data: result });
});
app.post("/create-message", async (req, res) => {
const members = req.body;
const prev = await messageCollection.findOne({
members: {
$all: [
{ $elemMatch: { email: members[0].email } },
{ $elemMatch: { email: members[1].email } },
],
},
});
if (prev) {
prev._id.toString();
return res.send({ status: true, data: prev });
}
const result = await messageCollection.insertOne({
members,
conversations: [],
});
const messageId = result?.insertedId?.toString();
res.send({
status: true,
data: { _id: messageId, members, conversations: [] },
});
});
app.patch("/send-message", async (req, res) => {
const { _id, message, author } = req.body;
const query = { _id: ObjectId(_id) };
const result = await messageCollection.updateOne(query, {
$push: { conversations: { message, author } },
});
res.send({ status: true, data: result });
});
app.get("/message/:email", async (req, res) => {
const email = req.params.email;
const query = { members: { $elemMatch: { email } } };
const data = await messageCollection.find(query).toArray();
res.send({ status: true, data });
});
app.get("/conversation/:id", async (req, res) => {
const query = { _id: ObjectId(req.params.id) };
const result = await messageCollection.findOne(query);
res.send({ status: true, data: result });
});
} finally {
}
};
run().catch((err) => console.log(err));
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.listen(port, () => {
console.log(`Example apps listening on port ${port}`);
});