Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Server dev (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sreesanjay authored Mar 9, 2024
2 parents b7cbbb1 + 5006fdd commit f6e4858
Show file tree
Hide file tree
Showing 16 changed files with 1,191 additions and 22 deletions.
2 changes: 1 addition & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const morgan = require('morgan')
const cookieParser = require('cookie-parser');
const { notFound, errorHandler } = require('./middlewares/errorMiddlewares')

const userRoute = require('./routes/userRoute.js')
const userRoute = require('./routes/indexRoute.js')
const adminRoute = require('./routes/adminRoute.js')
const ORIGIN = process.env.NODE_ENV === 'development' ? "http://localhost:4000" : 'https://thalia.vercel.app'
const corsConfig = {
Expand Down
118 changes: 118 additions & 0 deletions server/controller/adminUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const User = require('../models/userModel')
// const Profile = require('../models/profileModel')
// const Report = require('../models/reportsModel')

/**
* @desc request fetching all users
* @route GET /api/admin/users?search
* @access private
*/
const getUserList = async (req, res, next) => {
const search = req.query.search;
try {
const userList = await User.aggregate([
{
$match: {
role: 'USER'
}
},
{
$lookup: {
from: "profiles",
localField: '_id',
foreignField: "user_id",
as: "profile"
}
},
{
$unwind: {
path: '$profile'
}
},
{
$project: {
email: 1,
role: 1,
is_blocked: 1,
username: "$profile.username",
fullname: "$profile.fullname",
profile_img: "$profile.profile_img",
bio: "$profile.bio"
}
},
{
$lookup: {
from: "reports",
localField: '_id',
foreignField: "report_id",
as: "reports"
}
},
{
$match: {
$or: [
{ username: { $regex: new RegExp(search, 'i') } },
{ fullname: { $regex: new RegExp(search, 'i') } }
],
}
}
])
res.status(200).json({
success: true,
message: "users list fetched",
userList
})
} catch (error) {
next(error.message)
}
}

/**
* @desc request for blocking user
* @route GET /api/admin/users/block/:id
* @access private
*/

const blockUser = async (req, res, next) => {
try {
const block = await User.findOneAndUpdate({ _id: req.params.id }, { $set: { is_blocked: true } }, { new: true });
if (block.is_blocked) {
res.status(200).json({
success: true,
message: "user blocked"
})
} else {
throw new Error("Internal server error")
}
} catch (error) {
next(error.message)
}
}

/**
* @desc request for unblocking user
* @route GET /api/admin/users/unblock/:id
* @access private
*/

const unblockUser = async (req, res, next) => {
try {
const block = await User.findOneAndUpdate({ _id: req.params.id }, { $set: { is_blocked: false } }, { new: true });
if (!block.is_blocked) {
res.status(200).json({
success: true,
message: "user unblocked"
})
} else {
throw new Error("Internal server error")
}
} catch (error) {
next(error.message)
}
}

module.exports = {
getUserList,
blockUser,
unblockUser
}
Loading

0 comments on commit f6e4858

Please sign in to comment.