Skip to content
Merged
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
170 changes: 97 additions & 73 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,6 @@
import prisma from '../config/prismaClient.js';

import { updateUserAccountValidation } from '../validations/user.validation.js';
/* eslint no-undef:off */
/**
* @swagger
* /api/users:
* get:
* summary: Retrieve all users
* tags: [Users]
* security:
* - bearerAuth: []
* responses:
* 200:
* description: List of users retrieved successfully
* content:
* application/json:
* schema:
* type: array
* items:
* type: object
* properties:
* id:
* type: string
* email:
* type: string
* firstName:
* type: string
* lastName:
* type: string
* role:
* type: string
* 401:
* description: Unauthorized
* 500:
* description: Server error
*/
export const getAllUsers = async (req, res, next) => {
try {
// Fetch all users from the database
Expand All @@ -56,47 +23,10 @@ export const getAllUsers = async (req, res, next) => {
}
};

/**
* @swagger
* /api/users/{id}:
* get:
* summary: Get user by ID
* tags: [Users]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* format: uuid
* description: User ID
* responses:
* 200:
* description: User details retrieved successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* id:
* type: string
* email:
* type: string
* firstName:
* type: string
* lastName:
* type: string
* role:
* type: string
* 404:
* description: User not found
* 500:
* description: Server error
*/
export const getUserById = async (req, res, next) => {
const { id } = req.params;
try {
// Fetch the user by ID from the database
// Ensure req.user is defined
const user = await prisma.user.findFirst({
where: { id },
select: {
Expand All @@ -109,11 +39,105 @@ export const getUserById = async (req, res, next) => {
});

if (!user) {
Copy link

Copilot AI Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'error' variable is not defined when no user is found, which can lead to a runtime error. Consider creating a new error instance with a descriptive message or returning a 404 response.

Suggested change
if (!user) {
if (!user) {
const error = new Error('User not found');
error.status = 404;

Copilot uses AI. Check for mistakes.
return res.status(404).json({ message: 'User not found' });
return next(error); // Ensure next is called with the error
}

return res.status(200).json(user);
} catch (error) {
next(error); // Ensure next is called with the error
}
};

export const updateUserAccount = async (req, res, next) => {
try {
const userId = req.params.id;
if (!userId) {
return res.status(400).json({ message: 'User ID is required' });
}

const { error, value } = updateUserAccountValidation(req.body);
if (error) {
return res.status(400).json({ message: error.details[0].message });
}

const user = await prisma.user.findFirst({
where: { id: userId },
});

if (!user) {
return res.status(404).json({ message: 'User not found' });
}

if (!user.isActive) {
return res.status(403).json({ message: 'Account is not active' });
}

const updateData = {};

// Update basic profile fields
if (value.firstName) {
updateData.firstName = value.firstName;
}
if (value.lastName) {
updateData.lastName = value.lastName;
}
if (value.phoneNumber) {
updateData.phoneNumber = encrypt(value.phoneNumber);
Copy link

Copilot AI Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'encrypt' function is used without being imported or defined in the file. Please import or otherwise define the 'encrypt' function to ensure correct encryption of phone number values.

Copilot uses AI. Check for mistakes.
}
if (value.jobTitle) {
updateData.jobTitle = value.jobTitle;
}
if (value.timezone) {
updateData.timezone = value.timezone;
}
if (value.bio) {
updateData.bio = value.bio;
}

// Admin-only fields
if (req.user.role === 'ADMIN') {
if (value.role) {
updateData.role = value.role;
}
if (value.departmentId) {
updateData.departmentId = value.departmentId;
}
if (value.organizationId) {
updateData.organizationId = value.organizationId;
}

// Set updatedBy if admin is modifying another user
if (req.user.id !== userId) {
updateData.updatedBy = req.user.id;
}
}

const updatedUser = await prisma.user.update({
where: { id: userId },
data: updateData,
select: {
id: true,
email: true,
firstName: true,
lastName: true,
role: true,
phoneNumber: true,
jobTitle: true,
timezone: true,
bio: true,
departmentId: true,
organizationId: true,
profilePic: true,
createdAt: true,
updatedAt: true,
},
});

res.status(200).json({
message: 'User account updated successfully',
user: updatedUser,
});
} catch (error) {
next(error);
}
};
Loading