diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..2457528 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "trailingComma": "es5", + "useTabs": true, + "semi": false, + "singleQuote": false, + "quoteProps": "preserve" +} \ No newline at end of file diff --git a/index.ts b/index.ts index 87a07f0..17b261d 100644 --- a/index.ts +++ b/index.ts @@ -1,63 +1,82 @@ -import mongoose from "mongoose"; -import express from "express"; -const app = express(); +import mongoose from "mongoose" +import express from "express" +const app = express() import * as dotenv from "dotenv" dotenv.config() import sernTime from "./schemas/sern-time.js" import bodyParser from "body-parser" -import rateLimit from 'express-rate-limit' -app.use(bodyParser.json()); -app.disable('x-powered-by'); - +import rateLimit from "express-rate-limit" +app.use(bodyParser.json()) +app.disable("x-powered-by") const limiter = rateLimit({ - windowMs: 1 * 60 * 1000, // 15 minutes - max: 10, // Limit each IP to 100 requests per `window` (here, per 15 minutes) - message: {"error": "You just got ratelimited."}, - standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers - legacyHeaders: false, // Disable the `X-RateLimit-*` headers + windowMs: 1 * 60 * 1000, + max: 50, + message: { error: "You just got ratelimited." }, + standardHeaders: true, + legacyHeaders: false, }) - -// Apply the rate limiting middleware to all requests app.use(limiter) -await mongoose.connect(`${process.env.MONGODB}`).then(() => { console.log("Connected to MongoDB!") }); +await mongoose.connect(`${process.env.MONGODB}`).then(() => { + console.log("Connected to MongoDB!") +}) -app.post('/sern/newTime', async (req, res, next) => { - if(req.body.name && req.body.timezone && (req.body.key === process.env.SERN_TIME)) { - sernTime.exists({ name: req.body.name }, function (err, doc) { - if (err) { - throw err +app.post("/sern/newTime", async (req, res, next) => { + if ( + req.body.name && + req.body.timezone && + req.body.key === process.env.SERN_TIME && + req.body.userid + ) { + sernTime.exists({ userid: req.body.userid }, function (err, doc) { + if (err) throw err + if (doc) { + res.status(400).json({ "error": "You already created a timezone!" }) } 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}) - saveToDB.save() - res.json({"ok": "kay done"}) - } + 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, + }) + saveToDB.save() + res.json({ "ok": "kay done" }) + } + } + }) } - }); + }) } else { - res.status(400).json({"error": "make sure you have name, timezone and key as a JSON post. You could also have your key wrong."}) + res.status(400).json({ + "error": + "make sure you have name, timezone and key as a JSON post. You could also have your key wrong.", + }) } }) -app.get('/sern/availableTime', async (req, res, next) => { - let get = await sernTime.find() as any +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) => { +app.get("/sern/getTime", async (req, res, next) => { if (req.query.name) { - let get = await sernTime.find({name: req.query.name}) as any + let get = (await sernTime.find({ name: req.query.name })) as any get = get.map((data) => data.timezone) res.send(get) } else { - res.json({"error": "Option name not provided."}) + res.json({ "error": "Option name not provided." }) } }) app.listen(7272, () => { console.log(`listening`) -}) \ No newline at end of file +}) diff --git a/schemas/sern-time.ts b/schemas/sern-time.ts index 4ce3a1c..3425dc3 100644 --- a/schemas/sern-time.ts +++ b/schemas/sern-time.ts @@ -2,6 +2,7 @@ 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 } }); const db = model('sern-timezones', schema) export default db; \ No newline at end of file