diff --git a/client/components/App.jsx b/client/components/App.jsx index a9e14030..fce23378 100644 --- a/client/components/App.jsx +++ b/client/components/App.jsx @@ -20,6 +20,11 @@ import EditContent from './content/EditContent'; import AdminFeed from './adminDashboard/AdminFeed'; import StudentList from "./students/StudentList" + +// componentDidMount = () => { + +// } + function App() { return ( <> diff --git a/client/components/actions/adminAction.js b/client/components/actions/adminAction.js index b3d2ae48..afa18c86 100644 --- a/client/components/actions/adminAction.js +++ b/client/components/actions/adminAction.js @@ -1,6 +1,7 @@ -export const adminloggedIn = adminCredentials => { +export const adminloggedIn = (adminCredentials) => { + console.log("in admin login action") return dispatch => { - fetch('http://localhost:3000/api/v1/admin/login', { + fetch('/api/v1/admin/login', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -9,17 +10,28 @@ export const adminloggedIn = adminCredentials => { }) .then(res => res.json()) .then(admin => { + console.log(admin) localStorage.setItem('token', admin.Token); - dispatch({ type: 'ADMIN_LOGIN_SUCCESSFUL', payload: admin }); + dispatch({ type: 'ADMIN_LOGIN_SUCCESSFULL', payload: admin }); }); }; }; +export const pendingApprovals = (dispatch) => { + return dispatch => { + fetch("http://localhost:3000/api/v1/admin/pending-approvals", { + method: "GET" + }) + .then(res => res.json()) + .then(pendingApprovals => { + dispatch({ type: "PENDING_APPROVALS", payload: pendingApprovals }) + }) + } +} - -export const adminLogout = () => { - console.log("logged out") +export const adminLogout = (dispatch) => { + console.log("inside adminLogout action") return dispatch => { - dispatch({type: "ADMIN_LOGOUT"}) + dispatch({ type: "ADMIN_LOGOUT" }) } } diff --git a/client/components/actions/contentAction.js b/client/components/actions/contentAction.js index 0aa99c26..bb1f4bcd 100644 --- a/client/components/actions/contentAction.js +++ b/client/components/actions/contentAction.js @@ -23,6 +23,7 @@ export const getContent = () => { }) .then(res => res.json()) .then(content => { + console.log(content, "in content") dispatch({type: 'GET_CONTENT', payload: content}) }) } diff --git a/client/components/actions/userAction.js b/client/components/actions/userAction.js index 1418b987..9896e630 100644 --- a/client/components/actions/userAction.js +++ b/client/components/actions/userAction.js @@ -1,46 +1,47 @@ -const userLoggedIn = userCredentials => { +const userLoggedIn = (dispatch, userCredentials) => { + console.log("in login action") return dispatch => { - fetch('/api/v1/users/login', { - method: 'POST', + fetch("/api/v1/users/login", { + method: "POST", headers: { - 'Content-Type': 'application/json', + "Content-Type": "application/json" }, - body: JSON.stringify(userCredentials), + body: JSON.stringify(userCredentials) }) .then(res => res.json()) .then(user => { - console.log(user, 'in user action'); - dispatch({ type: 'USER_LOGIN_SUCCESSFULL', payload: user }); + dispatch({ type: "USER_LOGIN_SUCCESSFULL", payload: user }); // localStorage.setItem("userToken", user.Token) // this.setState({email : user.user.email , username : user.user.username , isLoggedin: true }) }); }; }; - const userRegister = userCredentials => { return dispatch => { - fetch('/api/v1/users/register', { - method: 'POST', + fetch("/api/v1/users/register", { + method: "POST", body: JSON.stringify(userCredentials), headers: { - 'Content-Type': 'application/json', - }, + "Content-Type": "application/json" + } }) .then(res => res.json()) .then(user => { - dispatch({ type: 'USER_REGISTER_SUCCESSFULL', payload: user }); + dispatch({ type: "USER_REGISTER_SUCCESSFULL", payload: user }); }); }; }; -const userLogout = userCredentials => { + +const userLogout = (dispatch,userCredentials) => { + console.log("in user logout in actions") return dispatch => { dispatch({ - type : 'USER_LOGOUT', - payload : null - }) - } -} + type: "USER_LOGOUT", + payload: null + }); + }; +}; -module.exports= {userLoggedIn , userLogout ,userRegister}; \ No newline at end of file +module.exports = { userLoggedIn, userLogout, userRegister }; diff --git a/client/components/adminDashboard/AdminFeed.jsx b/client/components/adminDashboard/AdminFeed.jsx index da46ef4c..a07998d5 100644 --- a/client/components/adminDashboard/AdminFeed.jsx +++ b/client/components/adminDashboard/AdminFeed.jsx @@ -1,11 +1,13 @@ import React from "react"; import StudentSubmissionCard from "../students/studentDashboard/StudentSubmissionCard"; import AdminSidebar from "./AdminSidebar"; +import Header from "../header/Header" function AdminFeed(){ return ( <> +
diff --git a/client/components/adminDashboard/PendingApprovals.js b/client/components/adminDashboard/PendingApprovals.js index 9742a0ac..3edf826f 100644 --- a/client/components/adminDashboard/PendingApprovals.js +++ b/client/components/adminDashboard/PendingApprovals.js @@ -1,34 +1,27 @@ import React, { Component } from 'react'; import AdminSidebar from './AdminSidebar'; import PendingCard from './PendingCard'; +import { connect } from "react-redux" +import { pendingApprovals } from '../actions/adminAction' +import Header from "../header/Header" + class PendingApprovals extends Component { - constructor() { - super(); - this.state = { - pendingStudentList: null, - }; + constructor(props) { + super(props); + } componentDidMount() { - fetch('/api/v1/admin/pending-approvals', { - method: 'GET', - headers: { - // "Authorization": `Token ${localStorage.getItem('Admintoken')}`, - 'Content-Type': 'application/json', - }, - }) - .then(res => res.json()) - .then(data => this.setState({ pendingStudentList: data })); + this.props.pendingApprovals() } - render() { - const pendingStudentList = - this.state.pendingStudentList && this.state.pendingStudentList.users; - console.log(pendingStudentList); + render() { + const pendingStudentList = this.props.adminReducer.pendingApprovals.users return ( <> +
@@ -38,10 +31,16 @@ class PendingApprovals extends Component { Pending Approvals
- {pendingStudentList && + {/* {pendingStudentList && pendingStudentList.map(pendingStudent => ( - ))} + ))} */} + { + pendingStudentList && pendingStudentList.map(pendingStudent => { + return + }) + } + {/* */}
@@ -50,4 +49,11 @@ class PendingApprovals extends Component { } } -export default PendingApprovals; +const mapStateToProps = (state) => { + return state +} + +export default connect(mapStateToProps, { pendingApprovals })(PendingApprovals) + + +// export default PendingApprovals \ No newline at end of file diff --git a/client/components/adminDashboard/PendingCard.js b/client/components/adminDashboard/PendingCard.js index 38b9aee3..91b17060 100644 --- a/client/components/adminDashboard/PendingCard.js +++ b/client/components/adminDashboard/PendingCard.js @@ -5,12 +5,23 @@ class Pending extends Component { super(props); } - render() { - // console.log(this.props.pendingStudentData) - const { - username, email, isActive, isInCampus, isAdmin, createdAt, - } = this.props.pendingStudentData; + // handleApproveStudent = () => { + // console.log("inside handleApproveStudent") + // fetch(`api/v1/admin/approved/${this.props.pendingStudent._id}`) + // .then(res => res.json()) + // .then(approvedStudent => console.log(approvedstudent)) + // } + + // handleRejectStudent = () => { + // console.log("inside handleRejectStudent") + // fetch(`api/v1/admin/approved/${this.props.pendingStudent._id}`) + // .then(res => res.json()) + // .then(rejectedStudent => console.log(rejectedstudent)) + // } + render() { + const { username, email, createdAt } = this.props.pendingStudent + // console.log(this.props) return (
@@ -20,16 +31,14 @@ class Pending extends Component {
-Details: + Details: {email} - {isInCampus} - {/* {createdAt} */} + {createdAt}
-
- - + +
); diff --git a/client/components/auth/AdminLogin.js b/client/components/auth/AdminLogin.js index 556d6a5d..25cb4658 100644 --- a/client/components/auth/AdminLogin.js +++ b/client/components/auth/AdminLogin.js @@ -1,44 +1,49 @@ -import React, { Component } from 'react'; -import { connect } from 'react-redux'; -import store from '../store/store'; -import { adminloggedIn, adminLogout } from '../actions/adminAction'; +import React, { Component } from "react"; +import { connect } from "react-redux"; +import store from "../store/store"; +import { adminloggedIn, adminLogout } from "../actions/adminAction"; class AdminLogin extends Component { constructor(props) { super(props); this.state = { - email: '', - password: '', - admin: '', + email: "", + password: "", + admin: "" }; } handleChange = e => { this.setState({ - [e.target.name]: e.target.value, + [e.target.name]: e.target.value }); }; handleAdminLogin = e => { + // console.log("inside handleAdminLogin") e.preventDefault(); const adminCredentials = { email: this.state.email, - password: this.state.password, + password: this.state.password }; - this.props.adminloggedIn(adminCredentials); - store.subscribe(() => { - console.log(store.getState(), "in admin component"); - store.getState().adminReducer.adminData.Token - ? this.props.history.push('/admin/feed') - : this.setState({ - ...this.state, - admin: "Please Check Admin Credentials!" - - }); - }); + if (!adminCredentials.email || !adminCredentials.password) { + return alert("PLEASE CHECK INPUT FIELDS!"); + } else { + this.props.adminloggedIn(adminCredentials); + store.subscribe(() => { + store.getState().adminReducer.adminData.Token + ? this.props.history.push("/admin/feed") + : this.setState({ + ...this.state, + admin: store.getState().adminReducer.adminData.admin + }); + }); + } }; render() { // console.log(this.props) return ( + <> + {/*
*/}

{this.state.admin}

Admin-Login

@@ -63,15 +68,22 @@ class AdminLogin extends Component { />
-
+ ); } } const mapStateToProps = state => { return state; }; -export default connect(mapStateToProps, { adminloggedIn, adminLogout })(AdminLogin); +export default connect(mapStateToProps, { adminloggedIn, adminLogout })( + AdminLogin +); diff --git a/client/components/auth/LoginUser.js b/client/components/auth/LoginUser.js index 1621bd14..5120d5a2 100644 --- a/client/components/auth/LoginUser.js +++ b/client/components/auth/LoginUser.js @@ -1,26 +1,27 @@ -import React, { Component } from 'react'; -import { connect } from 'react-redux'; -import store from '../store/store'; -import { userLoggedIn } from '../actions/userAction'; -import Header from "../header/Header" +import React, { Component } from "react"; +import { connect } from "react-redux"; +import store from "../store/store"; +import { userLoggedIn } from "../actions/userAction"; +import Header from "../header/Header"; class LoginUser extends Component { constructor(props) { super(props); this.state = { - email: '', - password: '', - username: '', + email: "", + password: "", + username: "" }; } handleChange = e => { this.setState({ - [e.target.name]: e.target.value, + [e.target.name]: e.target.value }); }; handleSubmit = e => { + console.log("in handle submit") e.preventDefault(); this.postLoginData(); }; @@ -28,21 +29,25 @@ class LoginUser extends Component { postLoginData = e => { var userCredentials = { email: this.state.email, - password: this.state.password, - }; payload : { - isLoggedin : false + password: this.state.password + }; + if (!userCredentials.email || !userCredentials.password) { + return alert("Please Fill All Credentials!"); + } else { + this.props.userLoggedIn(userCredentials); + store.subscribe(() => { + store.getState().userReducer.userLoginData.Token + ? alert("user login sucessfull") + : this.setState({ + ...this.state, + user: store.getState().userReducer.userLoginData.user + }); + }); } - this.props.userLoggedIn(userCredentials); - store.subscribe(() => { - store.getState().userReducer.userLoginData.Token - ? alert('user login sucessfull') - : this.setState({ ...this.state, user: 'Invalid User!' }); - }); - this.props.history.push("/"); }; render() { - // console.log(this.props); + console.log(this.props,"props in render") return (
@@ -72,7 +77,11 @@ class LoginUser extends Component {
-
diff --git a/client/components/auth/RegisterUser.js b/client/components/auth/RegisterUser.js index 4f60c7e1..ce166850 100644 --- a/client/components/auth/RegisterUser.js +++ b/client/components/auth/RegisterUser.js @@ -1,9 +1,9 @@ import React, { Component } from "react"; -import {NavLink} from "react-router-dom"; +import { NavLink } from "react-router-dom"; import store from "../store/store"; import { userRegister } from "../actions/userAction"; import { connect } from "react-redux"; -import Header from '../header/Header'; +import Header from "../header/Header"; class RegisterUser extends Component { constructor() { @@ -18,7 +18,7 @@ class RegisterUser extends Component { handleChange = e => { this.setState({ [e.target.name]: e.target.value - }) + }); }; handleSubmit = event => { @@ -31,13 +31,17 @@ class RegisterUser extends Component { if ( !userCredentials.username || !userCredentials.email || - !userCredentials.password + !userCredentials.password || + userCredentials.password.length < 6 ) { - alert("check user details!"); + alert("Check userCredentials!"); } else { this.props.userRegister(userCredentials); store.subscribe(() => { - this.props.history.push("/login"); + console.log(store.getState(), "in user action"); + store.getState().userReducer.userRegisterData.User.email + ? this.props.history.push("/register/onboarding") + : alert(store.getState().userReducer.userRegisterData.User); }); } }; @@ -53,9 +57,10 @@ class RegisterUser extends Component { className="input" type="text" name="username" - placeholder="Enter username" + placeholder="Enter Username" onChange={this.handleChange} value={this.state.username} + required />

@@ -63,9 +68,11 @@ class RegisterUser extends Component { className="input" type="email" name="email" - placeholder="Enter email" + placeholder="Enter Email" onChange={this.handleChange} value={this.state.email} + required + pattern=".+@gmail\.com" />

@@ -73,16 +80,22 @@ class RegisterUser extends Component { className="input" type="password" name="password" - placeholder="Enter password" + placeholder="Enter Password Min 6! " onChange={this.handleChange} value={this.state.password} + required />

- Next + {/* + Next + */} + - + ); } } diff --git a/client/components/content/Content.js b/client/components/content/Content.js index b3924968..9ac59fef 100644 --- a/client/components/content/Content.js +++ b/client/components/content/Content.js @@ -1,5 +1,7 @@ import React, { Component } from "react"; import { Link } from "react-router-dom" +import { getContent } from "../actions/contentAction" +import { connect } from "react-redux"; class Content extends Component { @@ -16,6 +18,7 @@ class Content extends Component { console.log(this.props); // const {title, type, description,} = this.props.location.contentProps // console.log(this.props && this.props.location.contentProps.contentData, "inside content") + console.log(this.props.contentReducer) return ( <>
@@ -52,4 +55,10 @@ class Content extends Component { } } -export default Content; +const mapStateToProps = (state) => { + return state +} + +export default connect(mapStateToProps, { getContent })(Content) + +// export default Content \ No newline at end of file diff --git a/client/components/content/ContentCard.js b/client/components/content/ContentCard.js index 86958139..6230fe27 100644 --- a/client/components/content/ContentCard.js +++ b/client/components/content/ContentCard.js @@ -1,6 +1,8 @@ import React, { Component } from "react"; import { NavLink } from "react-router-dom"; + + class ContentCard extends Component { constructor(props) { super(props); @@ -19,7 +21,7 @@ class ContentCard extends Component {
{/* //TODO navlink should redirect to individual content piece by passing contentid from props */} - Read More >>> - + + */} + + >>> Read more ); } diff --git a/client/components/content/ContentList.js b/client/components/content/ContentList.js index fecdefcc..37a00398 100644 --- a/client/components/content/ContentList.js +++ b/client/components/content/ContentList.js @@ -1,32 +1,26 @@ import React, { Component } from "react"; import AdminSidebar from "../adminDashboard/AdminSidebar"; import ContentCard from "./ContentCard"; +import { connect } from "react-redux"; +import { getContent } from "../actions/contentAction" +import Header from "../header/Header" class ContentList extends Component { - constructor() { - super(); - this.state = { - contentList: "" - }; + constructor(props) { + super(props); } - handleChange = e => { - this.setState({ [e.target.name]: e.target.value }); - }; componentDidMount() { - fetch("http://localhost:3000/api/v1/content/list", { - headers: { - "Content-Type": "application/json" - } - }) - .then(res => res.json()) - .then(data => this.setState({ ...this.state, contentList: data })); + this.props.getContent() } render() { - console.log(this.props) + const contentList = this.props.contentReducer.content.contents + console.log(contentList) return ( + <> +
@@ -38,11 +32,24 @@ class ContentList extends Component { contentList.map(content => { return ; })} */} + + { + contentList && contentList.map(content => { + // console.log(content) + return + }) + }
+ ); } } -export default ContentList; + +const mapStateToProps = (state) => { + return state +} + +export default connect(mapStateToProps, { getContent })(ContentList) diff --git a/client/components/content/EditContent.js b/client/components/content/EditContent.js index a06a7383..e5602b86 100644 --- a/client/components/content/EditContent.js +++ b/client/components/content/EditContent.js @@ -1,6 +1,6 @@ import React, { Component } from "react" - class EditContent extends Component { + render() { return (
diff --git a/client/components/content/NewContentForm.js b/client/components/content/NewContentForm.js index 2217011a..b33e7af4 100644 --- a/client/components/content/NewContentForm.js +++ b/client/components/content/NewContentForm.js @@ -4,6 +4,7 @@ import { newContent } from '../actions/contentAction'; import { connect } from 'react-redux'; import AdminSidebar from '../adminDashboard/AdminSidebar'; + class NewContentForm extends Component { constructor() { super(); @@ -33,6 +34,7 @@ class NewContentForm extends Component { console.log(this.props) return ( <> +
diff --git a/client/components/header/Header.js b/client/components/header/Header.js index f38dd389..58966f1c 100644 --- a/client/components/header/Header.js +++ b/client/components/header/Header.js @@ -9,15 +9,19 @@ class Header extends React.Component { super(props); } - handleLogout = () => { + handleLogout = (e) => { + // console.log("inside handle logout") e.preventDefault() + this.props.userLogout() + this.props.adminLogout() } render() { const isUserLoggedIn = this.props.userReducer.isLoggedIn; const isAdminLoggedIn = this.props.adminReducer.isLoggedIn; - console.log(isUserLoggedIn, "user"); - console.log(isAdminLoggedIn, "admin") + // console.log(isUserLoggedIn, "user"); + // console.log(isAdminLoggedIn, "admin") + // console.log(this.props) return (
@@ -27,23 +31,15 @@ class Header extends React.Component {
- { - - isAdminLoggedIn ? - Logout + isUserLoggedIn || isAdminLoggedIn ? + Logout : - - } - - - -
); diff --git a/client/components/home/Home.js b/client/components/home/Home.js index 52ffbda6..0dd290ea 100644 --- a/client/components/home/Home.js +++ b/client/components/home/Home.js @@ -4,6 +4,8 @@ import { NavLink } from "react-router-dom"; class Home extends React.Component { render() { return ( + <> + {/*
*/}

An exciting new initiative to expose

@@ -23,6 +25,7 @@ class Home extends React.Component {
+ ); } } diff --git a/client/components/reducers/adminReducer.js b/client/components/reducers/adminReducer.js index 9a08fbd5..f543ee7a 100644 --- a/client/components/reducers/adminReducer.js +++ b/client/components/reducers/adminReducer.js @@ -1,10 +1,13 @@ const adminState = { adminData: '', - isLoggedIn: false + isLoggedIn: false, + pendingApprovals: "" }; + function adminReducer(state = adminState, action) { + console.log(action, "action in admin reducer") switch (action.type) { - case 'ADMIN_LOGIN_SUCCESSFUL': + case 'ADMIN_LOGIN_SUCCESSFULL': return { ...state, adminData: action.payload, @@ -13,7 +16,13 @@ function adminReducer(state = adminState, action) { case "ADMIN_LOGOUT": return { ...state, - isLoggedIn: false + isLoggedIn: false, + adminData: "" + } + case "PENDING_APPROVALS": + return { + ...state, + pendingApprovals: action.payload } default: return state; diff --git a/client/components/reducers/contentReducer.js b/client/components/reducers/contentReducer.js index 41414ef1..1a304df3 100644 --- a/client/components/reducers/contentReducer.js +++ b/client/components/reducers/contentReducer.js @@ -1,10 +1,13 @@ const contentState = { content: '', }; + + function contentReducer(state = contentState, action) { + console.log("in content action") switch (action.type) { case 'NEW_CONTENT_ADDED': - return { ...state, content: action.payload }; + return { ...state, content: action.payload } case 'GET_CONTENT': return {...state, content: action.payload} default: @@ -12,4 +15,5 @@ function contentReducer(state = contentState, action) { } } + export default contentReducer; diff --git a/client/components/reducers/userReducer.js b/client/components/reducers/userReducer.js index 67e61843..e17653dd 100644 --- a/client/components/reducers/userReducer.js +++ b/client/components/reducers/userReducer.js @@ -4,7 +4,9 @@ const userState = { isLoggedIn: false }; + function userReducer(state = userState, action) { + // console.log(action,"action in user reducer"); switch (action.type) { case 'USER_REGISTER_SUCCESSFULL': return { @@ -21,6 +23,7 @@ function userReducer(state = userState, action) { return { ...state, isLoggedIn: false, + userLoginData: "" }; default: return state; diff --git a/client/components/students/StudentList.js b/client/components/students/StudentList.js index bd44b321..41f33bf7 100644 --- a/client/components/students/StudentList.js +++ b/client/components/students/StudentList.js @@ -2,8 +2,8 @@ import React, { Component } from 'react'; import { connect } from 'react-redux'; import AdminSidebar from '../adminDashboard/AdminSidebar'; import { studentList } from '../actions/studentListAction'; - import StudentCard from './StudentCard'; +import Header from "../header/Header" class StudentList extends Component { constructor(props) { @@ -18,6 +18,8 @@ class StudentList extends Component { const studentList = this.props.studentListReducer.students.users return ( + <> +
@@ -33,6 +35,7 @@ class StudentList extends Component {
+ ); } } diff --git a/controllers/adminController.js b/controllers/adminController.js index ad6eae8f..92abcbdd 100644 --- a/controllers/adminController.js +++ b/controllers/adminController.js @@ -1,6 +1,6 @@ -var auth = require('../utils/auth'); -var User = require('../models/userSchema'); -var Admin = require('../models/adminSchema'); +var auth = require("../utils/auth"); +var User = require("../models/userSchema"); +var Admin = require("../models/adminSchema"); //TODO //redo functions @@ -8,14 +8,15 @@ var Admin = require('../models/adminSchema'); module.exports = { adminLogin: (req, res, next) => { var { email, password } = req.body; - if (email.length < 10 || password.length < 6) { - return res.status(401).json({ error: 'INVALID PASSWORD' }); + if (password.length < 6) { + return res.status(401).json({ admin: "INVALID PASSWORD" }); } Admin.findOne({ email }, (err, admin) => { if (err) return next(err); - if (!admin) return res.status(401).json({ admin: 'NOT ADMIN' }); + if (!admin) + return res.status(401).json({ admin: "NOT ADMIN PLEASE CHECK EMAIL!" }); if (!admin.confirmPassword(password)) - return res.json({ admin: 'Not Admin' }); + return res.json({ admin: "PASSWORD NOT CORRECT!" }); var token = auth.generateToken(email); res.status(200).json({ admin: admin, Token: token }); }); @@ -43,5 +44,5 @@ module.exports = { if (err) return next(err); res.json({ users: users }); }); - }, + } }; diff --git a/controllers/usersController.js b/controllers/usersController.js index 04f3ed9e..201aa644 100644 --- a/controllers/usersController.js +++ b/controllers/usersController.js @@ -1,19 +1,18 @@ -const User = require('../models/userSchema'); -const auth = require('../utils/auth'); +const User = require("../models/userSchema"); +const auth = require("../utils/auth"); // const mail = require('../utils/mailer'); module.exports = { registerUser: (req, res, next) => { const { username, email, password } = req.body; - if (email.length < 10 && password.length < 6) { - return res.status(401).json('INVALID USER'); + if (password.length < 6) { + return res.status(401).json({ error: "INVALID USER" }); } if (!username || !email || !password) { - return res.status(401).json({ error: 'INVALID USER' }); + return res.status(401).json({ User: "INVALID USER" }); } User.create(req.body, (err, UserCreated) => { - if (err) return next(err); - console.log(UserCreated); + if (err) return res.status(401).json({ User: "User Already Taken!" }); res.status(200).json({ User: UserCreated }); }); }, @@ -21,13 +20,13 @@ module.exports = { loginUser: (req, res, next) => { var { password, email } = req.body; if (!email || !password) { - return res.status(401).json({ error: 'INVALID USER' }); + return res.status(401).json({ error: "INVALID USER" }); } User.findOne({ email }, (err, user) => { - if (err) return next(err); - if (!user) return res.json({ user: 'User Not Found' }); + if (err) return res.status(401).json({ user: "Invalid User!" }); + if (!user) return res.json({ user: "User Not Found" }); if (!user.confirmPassword(password)) - return res.json({ user: 'Password Is Not Correct' }); + return res.json({ user: "Password Is Not Correct" }); var token = auth.generateToken(email); console.log(user); res.status(200).json({ user: user, Token: token }); @@ -39,19 +38,19 @@ module.exports = { .then(user => { if (!user) { return res.status(404).send({ - message: 'User not found with id ' + req.params.userId, + message: "User not found with id " + req.params.userId }); } res.json({ user }); }) .catch(err => { - if (err.kind === 'ObjectId') { + if (err.kind === "ObjectId") { return res.status(404).send({ - message: 'User not found with id ' + req.params.userId, + message: "User not found with id " + req.params.userId }); } return res.status(500).json({ - message: 'Error retrieving user with id ' + req.params.userId, + message: "Error retrieving user with id " + req.params.userId }); }); }, @@ -61,5 +60,5 @@ module.exports = { if (err) return next(err); res.status(200).json({ users: Users }); }); - }, + } }; diff --git a/models/userSchema.js b/models/userSchema.js index 093b29b6..cde2ebd6 100644 --- a/models/userSchema.js +++ b/models/userSchema.js @@ -13,7 +13,8 @@ const userSchema = new Schema( { username: { type: String, - required: true + required: true, + unique: true }, email: { type: String, diff --git a/routes/adminRouter.js b/routes/adminRouter.js index 59526437..80375374 100644 --- a/routes/adminRouter.js +++ b/routes/adminRouter.js @@ -12,6 +12,7 @@ router.put('/approved/:id', auth.verifyToken, admin.approveUser); router.delete('/remove/:id', auth.verifyToken, admin.removeUser); // Student Pending -router.get('/pending-approvals', auth.verifyToken, admin.pendingUsers); +router.get('/pending-approvals', admin.pendingUsers); + module.exports = router; diff --git a/utils/cron.js b/utils/cron.js index 99026f13..66a16b4e 100644 --- a/utils/cron.js +++ b/utils/cron.js @@ -1,96 +1,96 @@ -const cron = require('node-cron'); -const User = require('../models/userSchema'); -const Content = require('../models/contentSchema'); -// const Delivery = require('../controllers/deliveryController'); -const Delivery = require('../models/deliverySchema'); -const INDIVIDUAL = 'individual'; -const PAIR = 'pair'; -const GROUP = 'group'; -const contentList = []; +// const cron = require('node-cron'); +// const User = require('../models/userSchema'); +// const Content = require('../models/contentSchema'); +// // const Delivery = require('../controllers/deliveryController'); +// const Delivery = require('../models/deliverySchema'); +// const INDIVIDUAL = 'individual'; +// const PAIR = 'pair'; +// const GROUP = 'group'; +// const contentList = []; -Content.find({}).exec(function(err, contents) { - contents.forEach(content => contentList.push(content)); -}); -cron.schedule('* * * * *', function(req, res, next) { - // console.log(allUsers, 'allusers.userStatus'); - // require('./utils/allusers/users'); - // allUsers(); +// Content.find({}).exec(function(err, contents) { +// contents.forEach(content => contentList.push(content)); +// }); +// cron.schedule('* * * * *', function(req, res, next) { +// // console.log(allUsers, 'allusers.userStatus'); +// // require('./utils/allusers/users'); +// // allUsers(); - // find the type of email to be sent - // 1. Individual mail i) send content - // 2. pairMail -> i) makePairs ii) send content - // 3. groupMail -> i) makeGroups ii) send content +// // find the type of email to be sent +// // 1. Individual mail i) send content +// // 2. pairMail -> i) makePairs ii) send content +// // 3. groupMail -> i) makeGroups ii) send content - var mailType = determineDeliveryType(); +// var mailType = determineDeliveryType(); - switch (mailType) { - case INDIVIDUAL: - findNewContentPerStudentAndSendMail(); - case PAIR: - findNewContentPerStudentAndSendMail(); - case GROUP: - findNewContentPerStudentAndSendMail(); - } -}); +// switch (mailType) { +// case INDIVIDUAL: +// findNewContentPerStudentAndSendMail(); +// case PAIR: +// findNewContentPerStudentAndSendMail(); +// case GROUP: +// findNewContentPerStudentAndSendMail(); +// } +// }); -const determineDeliveryType = () => { - // var date = new Date(); - // determine day - // if its monday return -> 'individual' - // if its wednesday return -> 'pair' - // if its saturday return -> 'group' - return INDIVIDUAL; -}; +// const determineDeliveryType = () => { +// // var date = new Date(); +// // determine day +// // if its monday return -> 'individual' +// // if its wednesday return -> 'pair' +// // if its saturday return -> 'group' +// return INDIVIDUAL; +// }; -const findNewContentPerStudentAndSendMail = () => { - User.find({}).exec(function(err, users) { - users.forEach((user, i) => { - // console.log(user, 'cron'); - // console.log(contentList); - // find the content that has not been sent to this user. contentId - const sentContent = user.sentContent; - const contentNotSentList = contentList.reduce((acc, cv) => { - sentContent.includes(cv._id) ? acc : acc.push(cv); - return acc; - }, []); - const contentToSend = - contentNotSentList[ - Math.floor(Math.random() * contentNotSentList.length) - ]; +// const findNewContentPerStudentAndSendMail = () => { +// User.find({}).exec(function(err, users) { +// users.forEach((user, i) => { +// // console.log(user, 'cron'); +// // console.log(contentList); +// // find the content that has not been sent to this user. contentId +// const sentContent = user.sentContent; +// const contentNotSentList = contentList.reduce((acc, cv) => { +// sentContent.includes(cv._id) ? acc : acc.push(cv); +// return acc; +// }, []); +// const contentToSend = +// contentNotSentList[ +// Math.floor(Math.random() * contentNotSentList.length) +// ]; - // create a new delivery. +// // create a new delivery. - // const delivery = Delivery.newDelivery({ - // user: user._id, - // content: contentToSend._id, - // }); +// // const delivery = Delivery.newDelivery({ +// // user: user._id, +// // content: contentToSend._id, +// // }); - function deliveryId(req, res) { - const delivery = new Delivery({ - user: user._id, - content: contentToSend._id, - }); +// function deliveryId(req, res) { +// const delivery = new Delivery({ +// user: user._id, +// content: contentToSend._id, +// }); - delivery - .save() - .then(data => { - // console.log(data, "content data") - // const id = data._id; - // return { id }; +// delivery +// .save() +// .then(data => { +// // console.log(data, "content data") +// // const id = data._id; +// // return { id }; - res.json({ data }); - }) - .catch(err => { - console.log(err); - }); - } +// res.json({ data }); +// }) +// .catch(err => { +// console.log(err); +// }); +// } - console.log(data, 'inside cron'); - // sendMail(user._id, content._id, delivery._id) - }); - }); -}; +// console.log(data, 'inside cron'); +// // sendMail(user._id, content._id, delivery._id) +// }); +// }); +// }; -const sendMail = (userId, contentId, deliveryId) => { - // actually send the mail. -}; +// const sendMail = (userId, contentId, deliveryId) => { +// // actually send the mail. +// };