-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-guest-names.js
72 lines (60 loc) · 1.61 KB
/
check-guest-names.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
"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.queryStringParameters.hostId;
const guestName = event.queryStringParameters.guestName;
if(typeof hostId == "undefined" || hostId == "" || hostId == null || typeof guestName == "undefined" || guestName == "" || guestName == null)
return {
statusCode: "400",
body: JSON.stringify({
"message": "Bad Request",
})
};
connectDB();
try{
const hostUser = await User.findOne({ id: hostId }).populate("friends");
if (!hostUser)
return {
statusCode: "404",
body: JSON.stringify({
"message": "User Not Found",
})
};
const guestNames = hostUser.friends.map((friend) => friend.name);
if (guestNames.includes(guestName)) {
return {
statusCode: "409",
body: JSON.stringify({
"message": "Nickname Duplicated",
"name": guestName,
})
};
}
return {
statusCode: "200",
body: JSON.stringify({
"message": "Nickname Available",
"name": guestName,
})
};
} catch(err){
console.log(err);
return {
statusCode: "400",
body: JSON.stringify({
"message": "Bad Request",
})
};
}
};