-
Notifications
You must be signed in to change notification settings - Fork 0
/
result-for-host-per-guest.js
69 lines (56 loc) · 1.43 KB
/
result-for-host-per-guest.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
"use strict"
const mongoose = require("mongoose");
const MONGO_URI = process.env.MONGO_URI;
const User = require("./models/user");
let connection = null;
const connectDB = () => {
if (connection && mongoose.connection.readyState === 1) return;
mongoose.connect(MONGO_URI, { }).then(
conn => {
connection = conn;
}
);
};
module.exports.handler = async (event, context) => {
const hostId = event.pathParameters.hostId;
const guestId = event.pathParameters.guestId;
const userId = event.requestContext.authorizer.lambda.userId;
if(hostId != userId)
return {
statusCode: "403",
body: JSON.stringify({
"message": "User Not Allowed",
})
};
connectDB();
try{
const hostUser = await User.findOne({ id: hostId }).populate("friends");
const guestResult = hostUser.friends.filter((item) => item._id == guestId);
if (guestResult.length == 0)
return {
statusCode: "400",
body: JSON.stringify({
"message": "Bad Request",
})
};
const guestData = {
...guestResult[0]._doc,
};
delete guestData._id;
return {
statusCode: "200",
body: JSON.stringify({
"message": "Guest Result",
"data": guestData
})
};
} catch (err) {
console.log(err);
return {
statusCode: "400",
body: JSON.stringify({
"message": "Bad Request",
})
};
}
};