Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added expireAt index to doc schema & removed services #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Server/index.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -34,5 +34,4 @@ app.get("*", (req, res) => {

app.listen(port, () => {
console.log(`app listening on port ${port}!`);
scheduleExpiryJobs();
});
8 changes: 5 additions & 3 deletions Server/models/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
20 changes: 10 additions & 10 deletions Server/routes/savecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion Server/services/expiry.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ const scheduleExpiryJobs = () => {
});
};

module.exports = scheduleExpiryJobs;
module.exports = scheduleExpiryJobs;