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

removing skills from auth protected routes #50

Merged
merged 1 commit into from
Sep 11, 2023
Merged
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
4 changes: 2 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"singleQuote": true,
"tabWidth": 4
"singleQuote": true,
"tabWidth": 4
}
14 changes: 7 additions & 7 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ app.options('*', cors());
// app.use(helmet.contentSecurityPolicy({}));

const limiter = rateLimit({
max: 100,
windowMs: 60 * 60 * 1000,
message: 'Too many requests from this IP, please try again in an hour!'
max: 100,
windowMs: 60 * 60 * 1000,
message: 'Too many requests from this IP, please try again in an hour!'
});
app.use('/api', limiter);

Expand All @@ -57,20 +57,20 @@ app.use(xss());
// app.use(hpp());
//--------------------------------//
app.use('/api/v1/auth', authRouter);
app.use('/api/v1/skills', skillsRouter);
app.use(isLogin);
app.use('/api/v1/users', usersRouter);
app.use('/api/v1/admins', adminRouter);
app.use('/api/v1/skills', skillsRouter);
app.use('/api/v1/mentors', mentorsRouter);
app.use('/api/v1/courses', coursesRouter);
app.use('/api/v1/reviews', reviewsRouter);
app.use('/api/v1/friends', friendsRouter);
app.use('/api/v1/meetings', meetingsRouter);

app.all('*', (req, res, next) => {
res.status(res.locals.statusCode || 404).json({
message: 'Invalid route, please check URL'
});
res.status(res.locals.statusCode || 404).json({
message: 'Invalid route, please check URL'
});
});
//--------------------------------//
app.use(globalErrorHandler);
Expand Down
125 changes: 65 additions & 60 deletions src/controllers/meeting.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,89 @@ const Meeting = require('../models/meeting.model');
const AppError = require('../utils/appErrorsClass');
const catchAsyncError = require('../utils/catchAsyncErrors');
const {
standMentorsMeeting,
standUsersMeeting
standMentorsMeeting,
standUsersMeeting
} = require('../utils/ApiFeatures');
// ------------- User Operations ------------//
exports.getMyMeetings = catchAsyncError(async (req, res, next) => {
const userId = res.locals.userId;
const userId = res.locals.userId;

const meetingsQuery =
res.locals.userType === 'mentor'
? { mentor: userId, status: { $nin: ['not-selected', 'rejected'] } }
: { user: userId };
const populateOptions =
res.locals.userType === 'mentor'
? { path: 'user' }
: {
path: 'mentor',
populate: {
path: 'skill',
select: 'name'
}
};
const meetingsQuery =
res.locals.userType === 'mentor'
? { mentor: userId, status: { $nin: ['not-selected', 'rejected'] } }
: { user: userId };
const populateOptions =
res.locals.userType === 'mentor'
? { path: 'user' }
: {
path: 'mentor',
populate: {
path: 'skill',
select: 'name'
}
};

const meetings = await Meeting.find(meetingsQuery).populate(populateOptions);
const meetings = await Meeting.find(meetingsQuery).populate(
populateOptions
);

const selectedMeetings = meetings.map(meeting => {
if (res.locals.userType === 'mentor') {
return standMentorsMeeting(meeting);
} else {
return standUsersMeeting(meeting);
}
});
const selectedMeetings = meetings.map(meeting => {
if (res.locals.userType === 'mentor') {
return standMentorsMeeting(meeting);
} else {
return standUsersMeeting(meeting);
}
});

res.status(res.locals.statusCode || 200).json({
status: 'success',
results: selectedMeetings.length,
data: selectedMeetings
});
res.status(res.locals.statusCode || 200).json({
status: 'success',
results: selectedMeetings.length,
data: selectedMeetings
});
});

exports.updateMeeting = catchAsyncError(async (req, res, next) => {
const status = req.body.status == 'accepted' ? 'accepted' : 'rejected';
const meeting = await Meeting.findOneAndUpdate(
{ _id: req.params.id, status: 'pending' },
{ status },
{ new: true }
);
if (!meeting) return next(new AppError('No meeting found with that ID', 404));
const status = req.body.status == 'accepted' ? 'accepted' : 'rejected';
const meeting = await Meeting.findOneAndUpdate(
{ _id: req.params.id, status: 'pending' },
{ status },
{ new: true }
);
if (!meeting)
return next(new AppError('No meeting found with that ID', 404));

res.status(res.locals.statusCode || 200).json({
status: 'success',
data: meeting
});
res.status(res.locals.statusCode || 200).json({
status: 'success',
data: meeting
});
});

exports.createMeeting = catchAsyncError(async (req, res, next) => {
const userId = res.locals.userId;
const meeting = await Meeting.findByIdAndUpdate(req.params.id, {
user: userId,
status: 'pending'
});
const userId = res.locals.userId;
const meeting = await Meeting.findByIdAndUpdate(req.params.id, {
user: userId,
status: 'pending'
});

if (!meeting) return next(new AppError('No meeting found with that ID', 404));
if (!meeting)
return next(new AppError('No meeting found with that ID', 404));

res.status(res.locals.statusCode || 201).json({
status: 'success',
data: meeting
});
res.status(res.locals.statusCode || 201).json({
status: 'success',
data: meeting
});
});
// ---------- Basic CRUD Operations ----------//
exports.getMeeting = catchAsyncError(async (req, res, next) => {
const meeting = await Meeting.find({
mentor: req.params.id
});
if (!meeting) return next(new AppError('No meeting found with that ID', 404));
const meeting = await Meeting.find({
mentor: req.params.id
});
if (!meeting)
return next(new AppError('No meeting found with that ID', 404));

res.status(res.locals.statusCode || 200).json({
status: 'success',
length: meeting.length,
data: meeting
});
res.status(res.locals.statusCode || 200).json({
status: 'success',
length: meeting.length,
data: meeting
});
});
13 changes: 8 additions & 5 deletions src/routes/skill.routes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
const express = require('express');
const { isLogin } = require('../controllers/auth.controller');
const authController = require('../controllers/auth.controller');
const skillController = require('../controllers/skill.controller.js');
const app = require('../app');
//-----------------------------------------//
const router = express.Router();
//-------------------Router----------------//
router
.get('/', skillController.getAllSkills)
.get('/:id', skillController.getSkill);
.get('/', skillController.getAllSkills)
.get('/:id', skillController.getSkill);

app.use(isLogin);
router.use(authController.restrictTo('admin'));
router
.post('/', skillController.createSkill)
.patch('/:id', skillController.updateSkill)
.delete('/:id', skillController.deleteSkill);
.post('/', skillController.createSkill)
.patch('/:id', skillController.updateSkill)
.delete('/:id', skillController.deleteSkill);
//-------------------------------------------//
module.exports = router;