diff --git a/Server/index.js b/Server/index.js index 243e358..3a0b26e 100644 --- a/Server/index.js +++ b/Server/index.js @@ -1,8 +1,8 @@ const express = require("express"); const cors = require("cors"); +// const scheduleExpiryJobs = require("./services/expiry"); require("dotenv").config(); -const scheduleExpiryJobs = require("./services/expiry"); const saveCode = require("./routes/savecode"); const getCode = require("./routes/getcode"); @@ -34,5 +34,4 @@ app.get("*", (req, res) => { app.listen(port, () => { console.log(`app listening on port ${port}!`); - scheduleExpiryJobs(); }); diff --git a/Server/models/doc.js b/Server/models/doc.js index c8f5687..6358883 100644 --- a/Server/models/doc.js +++ b/Server/models/doc.js @@ -14,12 +14,14 @@ const doc = new mongoose.Schema({ required: true, unique: true, }, - expiryDate: { - type: String, - required: true, + expireAt: { + type: Date, + default: null } }); +doc.index({ expireAt: 1 }, { expireAfterSeconds: 0 }); + const Doc = mongoose.model("DelDog", doc); module.exports = Doc; diff --git a/Server/routes/savecode.js b/Server/routes/savecode.js index 0bb063b..bb1fbe9 100644 --- a/Server/routes/savecode.js +++ b/Server/routes/savecode.js @@ -17,14 +17,14 @@ router.post("/api/v1/saveCode", async (req, res) => { expiryDate === undefined ? (expiryDate = "72h") : (expiryDate = expiryDate); expiryDate = Date.now() + ms(expiryDate.toString()); - + if(!expiryDate) { return res.status(400).json({ error: "No expiry date provided" }); } console.log(new Date(expiryDate), expiryDate); - if (customUrl.length <= 4) { + if (customUrl && customUrl.length <= 4) { return res .status(400) .json({ error: "Custom url must be at least 5 characters long" }); @@ -34,24 +34,24 @@ router.post("/api/v1/saveCode", async (req, res) => { if (customUrl !== undefined) { let url = await Doc.findOne({ customUrl }).exec(); - if (url) + if (url) return res.status(400).json({ error: "Custom url already exists" }); } else { customUrl = await nanoid(5); } - + const doc = new Doc({ - language, - code, - customUrl, - expiryDate + code: code, + language: language, + customUrl: customUrl, + expireAt: expiryDate }); await doc.save(); - } - + } + catch (err) { console.log(err); } diff --git a/Server/services/expiry.js b/Server/services/expiry.js index cd701e5..9755648 100644 --- a/Server/services/expiry.js +++ b/Server/services/expiry.js @@ -18,4 +18,4 @@ const scheduleExpiryJobs = () => { }); }; -module.exports = scheduleExpiryJobs; +module.exports = scheduleExpiryJobs; \ No newline at end of file