Skip to content

Commit

Permalink
activating lecturer
Browse files Browse the repository at this point in the history
  • Loading branch information
ngecu committed Jan 27, 2024
1 parent e259eb0 commit 2306cb2
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 207 deletions.
12 changes: 12 additions & 0 deletions backend/controllers/studentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ export const deleteStudent = asyncHandler(async (req, res) => {
const student = await Student.findById(req.params.id);

if (student) {

// Find associated User and delete it
const associatedUser = await User.findOne({ email: student.email });
const associatedSchoolFees = await SchoolFees.findOne({student:student._id})

if (associatedUser) {
await associatedUser.remove();
}
if (associatedUser) {
await associatedSchoolFees.remove()
}

await student.remove();
res.json({ success: true, message: 'Student removed' });
} else {
Expand Down
32 changes: 31 additions & 1 deletion backend/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,42 @@ const updateProfile = asyncHandler(async (req, res) => {
}
});

// @desc Toggle user activation status
// @route PUT /api/users/:id/toggle-active
// @access Private/Admin
const toggleUserActivation = asyncHandler(async (req, res) => {
const userId = req.params.id;

const user = await User.findById(userId);

if (!user) {
res.status(404);
throw new Error('User not found');
}

user.isActive = !user.isActive; // Toggle isActive status

const updatedUser = await user.save();

res.status(200).json({
_id: updatedUser._id,
firstName: updatedUser.firstName,
secondName: updatedUser.secondName,
email: updatedUser.email,
isAdmin: updatedUser.isAdmin,
isActive: updatedUser.isActive,
userType: updatedUser.userType,
verified: updatedUser.verified,
});
});

export {
authUser,
registerUser,
sendRestPassword,
verifyResetPassword,
setNewPassword,
getAllUsers,
updateProfile
updateProfile,
toggleUserActivation
}
2 changes: 2 additions & 0 deletions backend/models/studentModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const studentSchema = mongoose.Schema(
}
);



studentSchema.methods.matchPassword = async function (enteredPassword) {
return await bcrypt.compare(enteredPassword, this.password);
};
Expand Down
2 changes: 1 addition & 1 deletion backend/routes/studentRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { admitStudent, deleteStudent, getAllStudents, studentsByCourse } from '.

student_router.route('/').post(protect, admin,admitStudent)
student_router.route('/').get(protect,getAllStudents)
student_router.route('/').delete(protect,deleteStudent)
student_router.route('/:id').delete(protect,deleteStudent)
student_router.route('/course/:id').get(protect,studentsByCourse)

export default student_router
5 changes: 3 additions & 2 deletions backend/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
authUser,
sendRestPassword,
getAllUsers,
updateProfile
updateProfile,
toggleUserActivation
} from '../controllers/userController.js'
import { protect, admin } from '../middleware/authMiddleware.js'

Expand All @@ -17,7 +18,7 @@ router.route('/').get(getAllUsers)
router.route('/reset-password').post(sendRestPassword)
router.route('/change-password/:id/:token').post(setNewPassword)
router.route('/update-profile').put(protect,updateProfile)

router.route('/:id/toggle-active').put(protect, admin, toggleUserActivation);



Expand Down
81 changes: 0 additions & 81 deletions frontend/cypress/e2e/login.cy.js

This file was deleted.

5 changes: 0 additions & 5 deletions frontend/cypress/e2e/spec.cy.js

This file was deleted.

5 changes: 0 additions & 5 deletions frontend/cypress/fixtures/example.json

This file was deleted.

25 changes: 0 additions & 25 deletions frontend/cypress/support/commands.js

This file was deleted.

20 changes: 0 additions & 20 deletions frontend/cypress/support/e2e.js

This file was deleted.

3 changes: 3 additions & 0 deletions frontend/src/actions/studentActions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
STUDENT_DETAILS_REQUEST,
STUDENT_DETAILS_SUCCESS,
STUDENT_DETAILS_FAIL,
STUDENT_DELETE_REQUEST,
STUDENT_DELETE_SUCCESS,
STUDENT_DELETE_FAIL,
} from '../constants/studentConstants';
import { useHistory } from 'react-router-dom';
const base_url = `http://localhost:5000/api/students`;
Expand Down
37 changes: 36 additions & 1 deletion frontend/src/actions/userActions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import {

USER_CHANGE_PASSWORD_REQUEST,
USER_CHANGE_PASSWORD_SUCCESS,
USER_CHANGE_PASSWORD_FAIL
USER_CHANGE_PASSWORD_FAIL,
USER_TOGGLE_ACTIVE_REQUEST,
USER_TOGGLE_ACTIVE_SUCCESS,
USER_TOGGLE_ACTIVE_FAIL,
} from '../constants/userConstants'
import { useHistory } from 'react-router-dom';

Expand Down Expand Up @@ -303,4 +306,36 @@ export const listUsers = () => async (dispatch, getState) => {
: error.message,
});
}
};

export const toggleUserActive = (userId) => async (dispatch, getState) => {
try {
dispatch({ type: USER_TOGGLE_ACTIVE_REQUEST });

const {
userLogin: { userInfo },
} = getState();

const config = {
headers: {
Authorization: `Bearer ${userInfo.token}`,
},
};


const { data } = await axios.put(`${base_url}/${userId}/toggle-active`, {}, config);

dispatch({
type: USER_TOGGLE_ACTIVE_SUCCESS,
payload: data,
});
} catch (error) {
dispatch({
type: USER_TOGGLE_ACTIVE_FAIL,
payload:
error.response && error.response.data.message
? error.response.data.message
: error.message,
});
}
};
6 changes: 5 additions & 1 deletion frontend/src/constants/userConstants.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ export const USER_RESET_PASSWORD_FAIL = 'USER_RESET_PASSWORD_FAIL'

export const USER_CHANGE_PASSWORD_REQUEST = 'USER_CHANGE_PASSWORD_REQUEST'
export const USER_CHANGE_PASSWORD_SUCCESS = 'USER_CHANGE_PASSWORD_SUCCESS'
export const USER_CHANGE_PASSWORD_FAIL = 'USER_CHANGE_PASSWORD_FAIL'
export const USER_CHANGE_PASSWORD_FAIL = 'USER_CHANGE_PASSWORD_FAIL'

export const USER_TOGGLE_ACTIVE_REQUEST = 'USER_TOGGLE_ACTIVE_REQUEST';
export const USER_TOGGLE_ACTIVE_SUCCESS = 'USER_TOGGLE_ACTIVE_SUCCESS';
export const USER_TOGGLE_ACTIVE_FAIL = 'USER_TOGGLE_ACTIVE_FAIL';
23 changes: 21 additions & 2 deletions frontend/src/reducers/userReducers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import {
USER_RESET_PASSWORD_FAIL,
USER_CHANGE_PASSWORD_REQUEST,
USER_CHANGE_PASSWORD_SUCCESS,
USER_CHANGE_PASSWORD_FAIL
USER_CHANGE_PASSWORD_FAIL,
USER_TOGGLE_ACTIVE_REQUEST,
USER_TOGGLE_ACTIVE_SUCCESS,
USER_TOGGLE_ACTIVE_FAIL,
} from '../constants/userConstants'

export const userLoginReducer = (state = {}, action) => {
Expand Down Expand Up @@ -163,4 +166,20 @@ export const userChangePasswordReducer = (state = {}, action) => {
default:
return state
}
}
}

export const userToggleActiveReducer = (state = {}, action) => {
switch (action.type) {
case USER_TOGGLE_ACTIVE_REQUEST:
return { loading: true };

case USER_TOGGLE_ACTIVE_SUCCESS:
return { loading: false, success: true, user: action.payload };

case USER_TOGGLE_ACTIVE_FAIL:
return { loading: false, error: action.payload };

default:
return state;
}
};
Loading

0 comments on commit 2306cb2

Please sign in to comment.