Skip to content

Commit

Permalink
Merge f8df6ea into 1c8f8ce
Browse files Browse the repository at this point in the history
  • Loading branch information
habaumugisha1 committed Sep 5, 2019
2 parents 1c8f8ce + f8df6ea commit 3a6001d
Show file tree
Hide file tree
Showing 19 changed files with 299 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
},
"rules": {
}
}
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "12.9.1"
- 12.9.1
cache:
directories:
- "node_modules"
Expand Down
42 changes: 13 additions & 29 deletions API/controllers/reviewSessionController.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
// import jwt from 'jsonwebtoken';
// import sessionMiddleware from '../middleware/sessionMiddleware';
import { sessions, reviews } from '../models/data';
import Responses from '../helpers/response';


export default class ReviewSessions {
// eslint-disable-next-line consistent-return
static createReview(req, res) {
const session = sessions.find((s) => s.sessionId === parseInt(req.params.sessionId, 10) && (req.userInfo.user.role === 'mentor'));
if (req.userInfo.role !== 'mentor')
// unauthorized status code
// eslint-disable-next-line brace-style
{
return res.status(401).json({
message: 'your are not allowed only mentor is allowed to create review',
});
return Responses.error(res, 'your are not allowed only mentor is allowed to create review');
}

if (!session) {
return res.status(404).json({
status: 404,
message: `session with ID ${req.params.sessionId} you passed is not found!`,
});
return Responses.error(res, `session with ID ${req.params.sessionId} you passed is not found!`);
}
if (session) {
const review = {
Expand All @@ -32,37 +30,23 @@ export default class ReviewSessions {

};
reviews.push(review);
res.status(200).json({
status: 200,
data: review,

});
return Responses.success(res, 200, review);
}
}

// eslint-disable-next-line consistent-return
static deleteReview(req, res) {
const session = sessions.find((r) => r.sessionId === parseInt(req.params.sessionId, 10) && (req.userInfo.user.role === 'admin'));
if (req.userInfo.role !== 'admin')
const session = sessions.find((r) => r.sessionId === parseInt(req.params.sessionId, 10) && (req.userInfo.role === 'admin'));
if (req.userInfo.role !== 'admin') {
// unauthorized status code
{
return res.status(401).json({
message: 'your are not allowed only admin is allowed to delete this review',
});
return Responses.error(res, 401, 'your are not allowed only admin is allowed to delete this review');
}

if (!session) {
return res.status(404).json({
status: 404,
message: `session with ID ${req.params.sessionId} you passed is not found`,
});
return Responses.error(res, 404, `session with ID ${req.params.sessionId} you passed is not found`);
}

if (session) sessions.splice(session, 1);
res.status(200).json({
status: 200,
data: {
message: `Review with id ${req.params.sessionId} successfully deleted!!`,
},
});
if (session) sessions.remove(session, 1);
return Responses.success(res, 200, `Review with id ${req.params.sessionId} successfully deleted!!`);
}
}
98 changes: 48 additions & 50 deletions API/controllers/sessionController.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,77 @@
import jwt from 'jsonwebtoken';
import { users, mentors, sessions } from '../models/data';

import { users, sessions } from '../models/data';
import Responses from '../helpers/response';

export default class Sessions {
// user should request a session to mentor
static requestSession(req, res) {
const session = {
sessionId: sessions.length + 1,
mentorId: req.body.mentorId,
menteeId: req.userInfo.user.id,
menteeId: req.userInfo.id,
questions: req.body.questions,
menteeEmail: req.userInfo.user.email,
menteeEmail: req.userInfo.email,
status: 'pending',
};
sessions.push(session);
res.status(200).json({
message: 'session requested successfully!',
data: session,
return Responses.success(res, 200, 'session requested successfully!', session);
// res.status(200).json({
// message: 'session requested successfully!',
// data: session,

});
// });
}

// mentor accepting menotorship request session
static acceptSession(req, res) {
const session = sessions.find((s) => s.sessionId === parseInt(req.params.sessionId, 10));

// eslint-disable-next-line consistent-return
static declineSession(req, res) {
// eslint-disable-next-line no-shadow
// eslint-disable-next-line max-len
// eslint-disable-next-line no-shadow
// eslint-disable-next-line max-len
const session = sessions.find((session) => session.sessionId === parseInt(req.params.sessionId, 10));
console.log(session);
if (!session) {
return res.status(404).json({

status: 404,
message: 'session with ID you type is not found',
});
return Responses.error(res, 404, `session with ID ${req.params.sessionId} you passed is not found!`);
}

res.status(200).json({
status: 200,
data: {
sessionId: req.params.sessionId,
mentorId: req.body.mentorId,
menteeId: req.userInfo.user.id,
questions: req.body.questions,
menteeEmail: req.userInfo.user.email,
status: 'Accepted',
},
});
const rejected = {
sessionId: req.params.sessionId,
mentorId: req.body.mentorId,
menteeId: req.userInfo.id,
questions: req.body.questions,
menteeEmail: req.userInfo.email,
status: 'rejected',
};
// eslint-disable-next-line no-console
sessions.splice(1, 1, rejected);
return Responses.success(res, 200, rejected);
}

// mentor should be able to decline session request mentorship
static declineSession(req, res) {
// mentor accepting menotorship request session
// eslint-disable-next-line consistent-return
static acceptSession(req, res) {
const session = sessions.find((s) => s.sessionId === parseInt(req.params.sessionId, 10));

if (!session) {
return res.status(404).json({
status: 404,
message: `session with ID ${req.params.sessionId}you passed is not found!`,
});
return Responses.error(res, 404, `session with ID ${req.params.sessionId} you type is not found`);
}

res.status(200).json({
status: 200,
data: {
sessionId: req.params.sessionId,
mentorId: req.body.mentorId,
menteeId: req.userInfo.user.id,
questions: req.body.questions,
menteeEmail: req.userInfo.user.email,
status: 'rejected',
},
});
const data = {
sessionId: req.params.sessionId,
mentorId: req.body.mentorId,
menteeId: req.userInfo.id,
questions: req.body.questions,
menteeEmail: req.userInfo.email,
status: 'Accepted',
};
sessions.splice(0, 1, data);
return Responses.success(res, 200, data);
}

// mentor should be able to decline session request mentorship
// eslint-disable-next-line consistent-return


// get sessions
static getSession(req, res) {
res.status(200).json({
sessions,
});
return Responses.success(res, 200, sessions);
}
}
118 changes: 50 additions & 68 deletions API/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,30 @@
/* eslint-disable no-unused-vars */
import Joi from 'joi';
import jwt from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
import { users } from '../models/data';
import schema from '../helpers/validation';
import Responses from '../helpers/response';


// import User from '../models/user'

export default class Users {
static homeView(req, res) {
res.status(200).json({
status: 200,
message: 'Welcome to Free Mentors',
});
return Responses.success(res, 200, 'Welcome to Free Mentors');
}


static userSignUp(req, res) {
const user = users.find((u) => u.email === req.body.email);
const user = users.find((user) => user.email === req.body.email);
// eslint-disable-next-line consistent-return
bcrypt.hash(req.body.password, 10, (err, hash) => {
if (err) {
return res.status(500).json({
error: err,
});
}
if (user) {
res.status(409).json({
message: 'Email already registed',
});
return Responses.success(res, 409, 'Email already registed');
// res.status(409).json({
// message: 'Email already registed',
// });
}
const schema = {
email: Joi.string().min(10).email().required(),
firstName: Joi.string().min(5).required(),
lastName: Joi.string().min(7).required(),
password: Joi.string().regex(/^[a-zA-Z0-9]{6,16}$/),
adress: Joi.string().required(),
biography: Joi.string().max(150).required(),
occupation: Joi.string().required(),
role: Joi.string(),
expertise: Joi.string().max(50).required(),
};
// // validation user input
const result = Joi.validate(req.body, schema);
if (result.error) {
res.status(400).send(result.error.details[0].message);
Expand All @@ -56,73 +42,69 @@ export default class Users {
expertise: req.body.expertise,
};

// console.log(user.req.body)
users.push(user);

jwt.sign({ user }, 'secretKey', (token) => {
res.status(201).json({
status: 201,
message: 'User created sucessful!',
data: {
token,
message: 'Good! user created sucessful!!!',
},

});
});
// }
// }
// }
// eslint-disable-next-line no-shadow
// eslint-disable-next-line no-use-before-define
const token = jwt.sign(user, 'privateKey', (token));
return Responses.success(res, 201, 'User created sucessful!', token);
}
});
}


static getusers(req, res) {
const user = users.filter((user) => user.role === 'mentee');
res.status(200).json({
status: 200,
data: user,
});
// });
return Responses.success(res, 200, user);
}

// });
// change user to mentor
// eslint-disable-next-line consistent-return
static specificuser(req, res) {
const user = users.find((user) => user.id === parseInt(req.params.id, 10) && (user.role === 'mentee'));
// eslint-disable-next-line no-use-before-define
const token = jwt.sign({ user }, 'privateKey', (token));
if (!user) {
return Responses.error(res, 404, `User with the given ID = ${req.params.id} not found!`);
}
return Responses.success(res, 200, token, 'User account changed to mentor');
}

status: 200,
message: 'User is successfully logged in',
data: {
token,
},
});
// user should be able to sign in
// eslint-disable-next-line consistent-return
static userlogin(req, res) {
const user = users.find((user) => user.email === req.body.email);
if (user) {
// eslint-disable-next-line consistent-return
bcrypt.compare(req.body.password, user.password, (err, result) => {
if (err) {
return Responses.error(res, 'password not match!');
}

if (result) {
// destructuring objectt
const { password, ...noA } = user;
// eslint-disable-next-line no-use-before-define
const token = jwt.sign(noA, 'privateKey', (token));
return Responses.success(res, 200, 'User is successfully logged in', token);
}
});
} else {
res.status(422).json({
message: 'You are not registed!',
});
return Responses.error(res, 422, 'You are not registed!');
}
}

static getMentors(req, res) {
const mentors = users.filter((user) => user.role === 'mentor');
res.status(200).json({
status: 200,
data: mentors,
});
return Responses.success(res, 200, 'mentors can help you', mentors);
}


static specificMentor(req, res) {
const mentor = users.find((m) => m.id === parseInt(req.params.id, 10) && (m.role === 'mentor'));
if (!mentor) {
return res.status(404).json({
status: 404,
message: `mentor with the given ID = ${req.params.id} not found!`,

});
return Responses.error(res, 404, `mentor with the given ID = ${req.params.id} not found!`);
}

return res.json({
data: mentor,
});
return Responses.success(res, 200, 'this is mentor you want.', mentor);
}
}
Loading

0 comments on commit 3a6001d

Please sign in to comment.