feat: no need for name now, userid is required

This commit is contained in:
2022-10-30 17:51:10 +01:00
parent 54ab1ee8ca
commit 38bca54e9d
3 changed files with 35 additions and 50 deletions

View File

@@ -11,13 +11,12 @@ app.use(bodyParser.json())
app.disable("x-powered-by")
const limiter = rateLimit({
windowMs: 1 * 60 * 1000,
max: 50,
max: 30,
message: { error: "You just got ratelimited." },
standardHeaders: true,
legacyHeaders: false,
})
app.use(limiter)
const englishRegex = /^[A-Za-z0-9 ]*$/
await mongoose.connect(`${process.env.MONGODB}`).then(() => {
consolelogTime(`Connected to MongoDB!`)
@@ -25,7 +24,6 @@ await mongoose.connect(`${process.env.MONGODB}`).then(() => {
app.post("/sern/newTime", async (req, res, next) => {
if (
englishRegex.test(req.body.name) &&
req.body.timezone &&
req.body.key === process.env.SERN_TIME &&
req.body.userid
@@ -35,49 +33,47 @@ app.post("/sern/newTime", async (req, res, next) => {
if (doc) {
res.status(400).json({ "error": "You already created a timezone!" })
} else {
sernTime.exists({ name: req.body.name }, function (err, doc) {
if (err) {
throw err
} else {
if (doc) {
res
.status(400)
.json({ "error": "User already exists in the database." })
} else {
const saveToDB = new sernTime({
name: req.body.name,
timezone: req.body.timezone,
userid: req.body.userid,
})
saveToDB.save()
res.json({ "ok": "kay done" })
}
}
if (doc) {
res
.status(400)
.json({ "error": "User already exists in the database." })
} else {
const saveToDB = new sernTime({
timezone: req.body.timezone,
userid: req.body.userid,
})
saveToDB.save()
res.json({ "ok": "kay done" })
}
}
})
} else {
res.status(400).json({
"error": "make sure you have the right params.",
})
}
})
app.get("/sern/getTime", async (req, res, next) => {
if (req.query.userid) {
sernTime.exists({ userid: req.query.userid }, async function (err, doc) {
if (err) throw err
if (doc) {
const timezone = (await sernTime.findOne({ userid: req.query.userid }))?.timezone
res.json({"timezone": timezone})
} else {
res.status(400).json({
"error": "you don't exist in the database",
})
}
})
} else {
res.status(400).json({
"error": "make sure you have the right params and english characters.",
"error": "make sure you have the userid param",
})
}
})
app.get("/sern/availableTime", async (req, res, next) => {
let get = (await sernTime.find()) as any
get = get.map(data => data.name)
res.send(get)
})
app.get("/sern/getTime", async (req, res, next) => {
if (req.query.name) {
const get = await sernTime.findOne({ name: req.query.name })
res.send(get!.timezone)
} else {
res.json({ "error": "Option name not provided." })
}
})
app.listen(7272, () => {
consolelogTime(`listening`)
})

View File

@@ -1,6 +1,5 @@
import { model, Schema, Model } from "mongoose";
const schema = new Schema({
name: { type: String, required: true },
timezone: { type: String, required: true },
userid: { type: String, required: true }
});

View File

@@ -1,17 +1,7 @@
export async function consolelogTime(message: string) {
let unix_timestamp = Date.now()
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date = new Date(unix_timestamp * 1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
const convertedTime = hours + ':' + minutes.substring(-2) + ':' + seconds.substring(-2);
const unix = new Date()
return console.log(`[${convertedTime}] ${message}`);
return console.log(`[${unix.toLocaleTimeString()}] ${message}`);
}