Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
126 changes: 120 additions & 6 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import prisma from '../config/prismaClient.js';
import {
deleteFromCloudinary,

Check warning on line 3 in src/controllers/user.controller.js

View workflow job for this annotation

GitHub Actions / eslint

'deleteFromCloudinary' is defined but never used

Check warning on line 3 in src/controllers/user.controller.js

View workflow job for this annotation

GitHub Actions / eslint

'deleteFromCloudinary' is defined but never used

Check warning on line 3 in src/controllers/user.controller.js

View workflow job for this annotation

GitHub Actions / eslint

'deleteFromCloudinary' is defined but never used
uploadToCloudinary,

Check warning on line 4 in src/controllers/user.controller.js

View workflow job for this annotation

GitHub Actions / eslint

'uploadToCloudinary' is defined but never used

Check warning on line 4 in src/controllers/user.controller.js

View workflow job for this annotation

GitHub Actions / eslint

'uploadToCloudinary' is defined but never used

Check warning on line 4 in src/controllers/user.controller.js

View workflow job for this annotation

GitHub Actions / eslint

'uploadToCloudinary' is defined but never used
} from '../utils/cloudinary.utils.js';
import { comparePassword, hashPassword } from '../utils/password.utils.js';
import {
updatePasswordValidation,
updateUserAccountValidation,
} from '../validations/user.validation.js';
/* eslint no-undef:off */
/**
* @desc Get all users with pagination
* @route GET /api/users/all?page=1
* @method GET
* @access Private (Admin only)
*/
export const getAllUsers = async (req, res, next) => {
try {
// Fetch all users from the database
const page = parseInt(req.query.page) || 1;
const limit = 10;
const skip = (page - 1) * limit;

// Get total number of users
const totalUsers = await prisma.user.count();

// Fetch users with pagination
const users = await prisma.user.findMany({
skip,
take: limit,
select: {
id: true,
email: true,
Expand All @@ -20,38 +39,107 @@

return res.status(200).json({
message: 'Users retrieved successfully',
currentPage: page,
totalPages: Math.ceil(totalUsers / limit),
totalUsers,
users,
});
} catch (error) {
next(error); // Ensure next is called with the error
next(error);
}
};

/**
* @desc Get full user profile by ID
* @route GET /api/users/:id
* @method GET
* @access Private (User or Admin)
*/
export const getUserById = async (req, res, next) => {
const { id } = req.params;

try {
// Ensure req.user is defined
const user = await prisma.user.findFirst({
where: { id },
select: {
id: true,
email: true,
username: true,
firstName: true,
lastName: true,
role: true,
profilePic: true,
phoneNumber: true,
jobTitle: true,
timezone: true,
bio: true,
preferences: true,
isActive: true,
isOwner: true,
createdAt: true,
updatedAt: true,
lastLogin: true, // Keep valid fields
// Removed invalid field `lastLogout`
department: {
select: {
id: true,
name: true,
},
},
organization: {
select: {
id: true,
name: true,
},
},
permissions: {
select: {
entityType: true,
entityId: true,
permissions: true,
},
},
teamMemberships: {
select: {
team: {
select: {
id: true,
name: true,
},
},
},
},
activityLogs: {
take: 5,
orderBy: { createdAt: 'desc' },
select: {
id: true,
action: true,
createdAt: true,
},
},
},
});

if (!user) {
return next(error); // Ensure next is called with the error
return res.status(404).json({ message: 'User not found' });
}

return res.status(200).json(user);
return res.status(200).json({
message: 'User retrieved successfully',
user,
});
} catch (error) {
next(error); // Ensure next is called with the error
next(error);
}
};

/**
* @desc Update user account
* @route PUT /api/users/:id
* @method PUT
* @access Private (User or Admin)
*/
export const updateUserAccount = async (req, res, next) => {
try {
const userId = req.params.id;
Expand Down Expand Up @@ -146,6 +234,12 @@
}
};

/**
* @desc Update user password
* @route PUT /api/users/update-password/:id
* @method PUT
* @access Private (User only)
*/
export const updateUserPassword = async (req, res, next) => {
try {
// Validate the request body
Expand Down Expand Up @@ -197,6 +291,12 @@
}
};

/**
* @desc Soft delete a user
* @route DELETE /api/users/:id
* @method DELETE
* @access Private (Admin only)
*/
export const softDeleteUser = async (req, res, next) => {
try {
const { id } = req.params;
Expand All @@ -222,6 +322,13 @@
next(error);
}
};

/**
* @desc Restore a soft-deleted user
* @route PATCH /api/users/restore/:id
* @method PATCH
* @access Private (Admin only)
*/
export const restoreUser = async (req, res, next) => {
try {
const { id } = req.params;
Expand All @@ -245,3 +352,10 @@
next(error);
}
};

/**
* @desc Upload a profile picture for a user
* @route POST /api/users/:id/profile-picture
* @method POST
* @access Private (User or Admin)
*/
Loading