Skip to content

Commit

Permalink
Merge pull request #1 from sakshamchhimwal/issuesPage
Browse files Browse the repository at this point in the history
Issues page
  • Loading branch information
JayaSurya-27 authored Feb 29, 2024
2 parents 5d9ff7b + b7b5fe3 commit 98e5152
Show file tree
Hide file tree
Showing 13 changed files with 4,596 additions and 1,169 deletions.
13 changes: 13 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": "standard-with-typescript",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

**/tmp
4,695 changes: 3,679 additions & 1,016 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
"@types/bcrypt": "^5.0.0",
"@types/http-errors": "^2.0.1",
"@types/jsonwebtoken": "^9.0.2",
"@types/uuid": "^9.0.8",
"axios": "^1.5.0",
"cloudinary": "^1.41.3",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^16.3.1",
"express": "~4.16.1",
"express-rate-limit": "^7.1.5",
"firebase": "^10.3.1",
"firebase-admin": "^11.10.1",
"google-auth-library": "^9.0.0",
Expand All @@ -29,18 +32,25 @@
"multer": "^1.4.5-lts.1",
"node-cache": "^5.1.2",
"node-schedule": "^2.1.1",
"qs": "^6.11.2"
"qs": "^6.11.2",
"socket.io": "^4.7.4"
},
"devDependencies": {
"@types/cookie-parser": "^1.4.3",
"@types/cors": "^2.8.14",
"@types/express": "^4.17.17",
"@types/morgan": "^1.9.5",
"@types/multer": "^1.4.8",
"@types/multer": "^1.4.11",
"@types/node": "^20.5.6",
"@types/node-schedule": "^2.1.1",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"eslint": "^8.56.0",
"eslint-config-standard-with-typescript": "^43.0.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-promise": "^6.1.1",
"nodemon": "^3.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
"typescript": "^5.3.3"
}
}
44 changes: 30 additions & 14 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import cors from "cors";
import * as dotenv from "dotenv";
dotenv.config({ path: __dirname + "/.env" });
import express, { NextFunction, Request, Response } from "express";
import createHttpError from "http-errors";
import express, { Request, Response, NextFunction } from "express";
import logger from "morgan";
import path from "path";
dotenv.config({ path: __dirname + "/.env" });
// import { defaultRouter } from "./routes";
import cookieParser from "cookie-parser";
import { Authenticate } from "./middlewares/Authenticate";
import authRouter from "./routes/authRoutes";
import guestRouter from "./routes/guestRoutes";
import managerRoutes from "./routes/managerRoutes";
import cookieParser from "cookie-parser";
import userRouter from "./routes/userRoutes"
import { Authenticate } from "./middlewares/Authenticate";
import userRouter from "./routes/userRoutes";
// import { Authorize } from "./middlewares/Authorize";
import connectDB from "./config/connectDB";
// import notifications from "./models/notifications";
import User_Schema from "./models/user"; // Adjust the import path according to your project structure
import Analytics from "./models/analytics";



import schedule from 'node-schedule';
import { rateLimit } from "express-rate-limit";
import schedule from "node-schedule";
import backup from "./config/backupTimeSeriesData";

const job = schedule.scheduleJob('0 23 * * *', async function () {
const job = schedule.scheduleJob("0 23 * * *", async function () {
console.log("Backing UP");
await backup();
});
Expand All @@ -32,14 +31,24 @@ const job = schedule.scheduleJob('0 23 * * *', async function () {
// console.log(Date.now(), 'The answer to life, the universe, and everything!');
// });

const limiter = rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
limit: 50, // Limit each IP to 5 requests per `window` (here, per 15 minutes).
standardHeaders: "draft-7", // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
// store: ... , // Use an external store for consistency across multiple server instances.
});

import { createServer } from "http";
import { Server } from "socket.io";
import { startIOLoop } from "./services/sockets";

var app = express();

//connect to database
connectDB();


// app.use(limiter);
app.use(cors());
app.use(cookieParser());
app.use(logger("dev"));
Expand All @@ -49,8 +58,6 @@ app.use(express.urlencoded({ extended: false }));
//serve files of path /static
app.use("/api/static", express.static(path.join(__dirname, "..", "public")));



app.use("/api/auth", authRouter);
app.use("/api/guest", guestRouter);

Expand Down Expand Up @@ -86,6 +93,7 @@ app.get("/api/userstats", async (req: Request, res: Response) => {
res.status(500).send('Internal Server Error');
}
});

// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createHttpError(404));
Expand All @@ -102,9 +110,17 @@ app.use((err: any, req: Request, res: Response, next: NextFunction) => {
res.render("error");
});

app.listen(process.env.PORT, () => {
const server = app.listen(process.env.PORT, () => {
console.log(`Server is running on port ${process.env.PORT}`);
});

const io = new Server(server, {
cors: {
origin: "*",
},
});

startIOLoop(io);

//exporting app to be used in test
export default app;
// export default app;
94 changes: 94 additions & 0 deletions src/controllers/UserControllers/user.dashboard.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { type NextFunction, type Response } from 'express'
import UserModel from '../../models/user'
import createHttpError from 'http-errors'
import SuggestionModel from '../../models/suggestions'

export const getAllSuggestions = async (
req: any,
res: Response,
next: NextFunction
) => {
const loggedInUserData = req.user
try {
const currUser = await UserModel.findOne({
Email: loggedInUserData.email
})
if (!currUser) {
next(createHttpError(403, 'Unauthorized'))
return
}
if (currUser) {
const currUserMess = currUser.Eating_Mess
const currPage = req.query.page
const LIMIT = 10
const paginatedSuggestions = await SuggestionModel.find({
messId: currUserMess
})
.skip((currPage - 1) * LIMIT)
.limit(LIMIT)
.populate('userId', 'Username Image')
.populate('upvotes downvotes', 'Username')
.exec()
if (paginatedSuggestions.length > 0) {
return res.send({
suggestions: paginatedSuggestions,
hasNext: paginatedSuggestions.length === LIMIT
})
}
return res.status(204).send({ suggestions: [], hasNext: false })
}
} catch (err) {
console.log(err)
next(createHttpError(500, 'Internal Server Error'))
}
}

export const voteSuggestion = async (
req: any,
res: Response,
next: NextFunction
) => {
const loggedInUserData = req.user
try {
const currUser = await UserModel.findOne({
Email: loggedInUserData.email
})
if (!currUser) {
next(createHttpError(403, 'Unauthorized'))
return
}
const suggestionId = req.body.suggestionId
const updateType =
req.body.upvote === true
? {
$addToSet: { upvotes: currUser._id },
$pull: { downvotes: currUser._id }
}
: {
$pull: { upvotes: currUser._id },
$addToSet: { downvotes: currUser._id }
}
const newVote = await SuggestionModel.findOneAndUpdate(
{
_id: suggestionId
},
updateType,
{ new: true }
)
if (newVote !== null) {
return res.send({
_id: newVote._id,
message: 'Voted Successfully',
upvotes: newVote?.upvotes,
downvotes: newVote?.downvotes
})
} else {
return res.status(400).send({ message: 'Vote not casted' })
}
} catch (err) {
console.log(err)
next(createHttpError(500, 'Internal Server Error'))
}
}


Loading

0 comments on commit 98e5152

Please sign in to comment.