161 lines
3.8 KiB
JavaScript
161 lines
3.8 KiB
JavaScript
const { prisma } = require('../../config/database');
|
|
|
|
exports.getAllUsers = async (req, res, next) => {
|
|
try {
|
|
const { page = 1, limit = 20, role, search, isActive } = req.query;
|
|
const skip = (page - 1) * limit;
|
|
|
|
const where = {};
|
|
if (role) where.role = role;
|
|
if (isActive !== undefined) where.isActive = isActive === 'true';
|
|
if (search) {
|
|
where.OR = [
|
|
{ email: { contains: search, mode: 'insensitive' } },
|
|
{ firstName: { contains: search, mode: 'insensitive' } },
|
|
{ lastName: { contains: search, mode: 'insensitive' } },
|
|
{ username: { contains: search, mode: 'insensitive' } },
|
|
];
|
|
}
|
|
|
|
const [users, total] = await Promise.all([
|
|
prisma.user.findMany({
|
|
where,
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
username: true,
|
|
firstName: true,
|
|
lastName: true,
|
|
role: true,
|
|
isVerified: true,
|
|
isActive: true,
|
|
createdAt: true,
|
|
lastLoginAt: true,
|
|
},
|
|
orderBy: { createdAt: 'desc' },
|
|
skip: parseInt(skip),
|
|
take: parseInt(limit),
|
|
}),
|
|
prisma.user.count({ where }),
|
|
]);
|
|
|
|
// res.json({
|
|
// success: true,
|
|
// data: { users, pagination: { page: +page, limit: +limit, total, pages: Math.ceil(total / limit) } },
|
|
// });
|
|
return res.status(200).json({
|
|
statusCode: 200,
|
|
status: true,
|
|
message: 'Users fetched successfully',
|
|
data: {
|
|
users,
|
|
pagination: {
|
|
page: +page,
|
|
limit: +limit,
|
|
total,
|
|
pages: Math.ceil(total / limit),
|
|
},
|
|
},
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
};
|
|
|
|
exports.updateUserStatus = async (req, res, next) => {
|
|
try {
|
|
const { isActive, role } = req.body;
|
|
const { id } = req.params;
|
|
|
|
const user = await prisma.user.findUnique({ where: { id } });
|
|
// if (!user) return res.status(404).json({ success: false, message: 'User not found' });
|
|
|
|
if (!user) {
|
|
return res.status(404).json({
|
|
statusCode: 404,
|
|
status: false,
|
|
message: 'User not found',
|
|
});
|
|
}
|
|
|
|
const updatedUser = await prisma.user.update({
|
|
where: { id },
|
|
data: {
|
|
...(isActive !== undefined && { isActive }),
|
|
...(role && { role }),
|
|
},
|
|
});
|
|
|
|
// res.json({ success: true, message: 'User updated successfully', data: updatedUser });
|
|
return res.status(200).json({
|
|
statusCode: 200,
|
|
status: true,
|
|
message: 'User updated successfully',
|
|
data: updatedUser,
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
};
|
|
|
|
exports.getUserById = async (req, res, next) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
// if (!id) {
|
|
// return res.status(400).json({
|
|
// success: false,
|
|
// message: "User ID is required",
|
|
// });
|
|
// }
|
|
|
|
if (!id) {
|
|
return res.status(400).json({
|
|
statusCode: 400,
|
|
status: false,
|
|
message: 'User ID is required',
|
|
});
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { id },
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
username: true,
|
|
firstName: true,
|
|
lastName: true,
|
|
role: true,
|
|
isVerified: true,
|
|
isActive: true,
|
|
createdAt: true,
|
|
lastLoginAt: true,
|
|
},
|
|
});
|
|
|
|
// if (!user) {
|
|
// return res.status(404).json({
|
|
// success: false,
|
|
// message: "User not found",
|
|
// });
|
|
// }
|
|
if (!user) {
|
|
return res.status(404).json({
|
|
statusCode: 404,
|
|
status: false,
|
|
message: 'User not found',
|
|
});
|
|
}
|
|
|
|
// res.json({ success: true, data: { user } });
|
|
return res.status(200).json({
|
|
statusCode: 200,
|
|
status: true,
|
|
message: 'User details fetched successfully',
|
|
data: { user },
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
};
|