user pagination added
This commit is contained in:
parent
b4407f59a6
commit
8968b86a9f
@ -2,7 +2,7 @@ import { Request, Response } from 'express';
|
|||||||
import { Holiday, HolidayType } from '@models/Holiday';
|
import { Holiday, HolidayType } from '@models/Holiday';
|
||||||
import { holidayService } from '@services/holiday.service';
|
import { holidayService } from '@services/holiday.service';
|
||||||
import { sequelize } from '@config/database';
|
import { sequelize } from '@config/database';
|
||||||
import { QueryTypes } from 'sequelize';
|
import { QueryTypes, Op } from 'sequelize';
|
||||||
import logger from '@utils/logger';
|
import logger from '@utils/logger';
|
||||||
import { initializeHolidaysCache, clearWorkingHoursCache } from '@utils/tatTimeUtils';
|
import { initializeHolidaysCache, clearWorkingHoursCache } from '@utils/tatTimeUtils';
|
||||||
import { clearConfigCache } from '@services/configReader.service';
|
import { clearConfigCache } from '@services/configReader.service';
|
||||||
@ -520,32 +520,49 @@ export const updateUserRole = async (req: Request, res: Response): Promise<void>
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get All Users by Role
|
* Get All Users by Role (with pagination and filtering)
|
||||||
*
|
*
|
||||||
* Purpose: List all users with a specific role
|
* Purpose: List all users with optional role filtering and pagination
|
||||||
*
|
*
|
||||||
* Access: ADMIN only
|
* Access: ADMIN only
|
||||||
*
|
*
|
||||||
* Query: ?role=ADMIN | MANAGEMENT | USER
|
* Query:
|
||||||
|
* - ?role=ADMIN | MANAGEMENT | USER | ALL | ELEVATED (default: ELEVATED for ADMIN+MANAGEMENT only)
|
||||||
|
* - ?page=1 (default)
|
||||||
|
* - ?limit=10 (default)
|
||||||
*/
|
*/
|
||||||
export const getUsersByRole = async (req: Request, res: Response): Promise<void> => {
|
export const getUsersByRole = async (req: Request, res: Response): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { role } = req.query;
|
const { role, page = '1', limit = '10' } = req.query;
|
||||||
|
|
||||||
|
const pageNum = parseInt(page as string) || 1;
|
||||||
|
const limitNum = Math.min(parseInt(limit as string) || 10, 100); // Max 100 per page
|
||||||
|
const offset = (pageNum - 1) * limitNum;
|
||||||
|
|
||||||
const whereClause: any = { isActive: true };
|
const whereClause: any = { isActive: true };
|
||||||
|
|
||||||
if (role) {
|
// Handle role filtering
|
||||||
|
if (role && role !== 'ALL' && role !== 'ELEVATED') {
|
||||||
const validRoles: UserRole[] = ['USER', 'MANAGEMENT', 'ADMIN'];
|
const validRoles: UserRole[] = ['USER', 'MANAGEMENT', 'ADMIN'];
|
||||||
if (!validRoles.includes(role as UserRole)) {
|
if (!validRoles.includes(role as UserRole)) {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
error: 'Invalid role. Must be USER, MANAGEMENT, or ADMIN'
|
error: 'Invalid role. Must be USER, MANAGEMENT, ADMIN, ALL, or ELEVATED'
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
whereClause.role = role;
|
whereClause.role = role;
|
||||||
|
} else if (role === 'ELEVATED' || !role) {
|
||||||
|
// Default: Show only ADMIN and MANAGEMENT (elevated users)
|
||||||
|
whereClause.role = { [Op.in]: ['ADMIN', 'MANAGEMENT'] };
|
||||||
}
|
}
|
||||||
|
// If role === 'ALL', don't filter by role (show all users)
|
||||||
|
|
||||||
|
// Get total count for pagination
|
||||||
|
const totalUsers = await User.count({ where: whereClause });
|
||||||
|
const totalPages = Math.ceil(totalUsers / limitNum);
|
||||||
|
|
||||||
|
// Get paginated users
|
||||||
const users = await User.findAll({
|
const users = await User.findAll({
|
||||||
where: whereClause,
|
where: whereClause,
|
||||||
attributes: [
|
attributes: [
|
||||||
@ -565,23 +582,49 @@ export const getUsersByRole = async (req: Request, res: Response): Promise<void>
|
|||||||
order: [
|
order: [
|
||||||
['role', 'ASC'], // ADMIN first, then MANAGEMENT, then USER
|
['role', 'ASC'], // ADMIN first, then MANAGEMENT, then USER
|
||||||
['displayName', 'ASC']
|
['displayName', 'ASC']
|
||||||
]
|
],
|
||||||
|
limit: limitNum,
|
||||||
|
offset: offset
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get role summary (across all users, not just current page)
|
||||||
|
const roleStats = await sequelize.query(`
|
||||||
|
SELECT
|
||||||
|
role,
|
||||||
|
COUNT(*) as count
|
||||||
|
FROM users
|
||||||
|
WHERE is_active = true
|
||||||
|
GROUP BY role
|
||||||
|
ORDER BY
|
||||||
|
CASE role
|
||||||
|
WHEN 'ADMIN' THEN 1
|
||||||
|
WHEN 'MANAGEMENT' THEN 2
|
||||||
|
WHEN 'USER' THEN 3
|
||||||
|
END
|
||||||
|
`, {
|
||||||
|
type: QueryTypes.SELECT
|
||||||
});
|
});
|
||||||
|
|
||||||
// Group by role for summary
|
|
||||||
const summary = {
|
const summary = {
|
||||||
ADMIN: users.filter(u => u.role === 'ADMIN').length,
|
ADMIN: parseInt((roleStats.find((s: any) => s.role === 'ADMIN') as any)?.count || '0'),
|
||||||
MANAGEMENT: users.filter(u => u.role === 'MANAGEMENT').length,
|
MANAGEMENT: parseInt((roleStats.find((s: any) => s.role === 'MANAGEMENT') as any)?.count || '0'),
|
||||||
USER: users.filter(u => u.role === 'USER').length,
|
USER: parseInt((roleStats.find((s: any) => s.role === 'USER') as any)?.count || '0')
|
||||||
total: users.length
|
|
||||||
};
|
};
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
users: users,
|
users: users,
|
||||||
|
pagination: {
|
||||||
|
currentPage: pageNum,
|
||||||
|
totalPages: totalPages,
|
||||||
|
totalUsers: totalUsers,
|
||||||
|
limit: limitNum,
|
||||||
|
hasNextPage: pageNum < totalPages,
|
||||||
|
hasPrevPage: pageNum > 1
|
||||||
|
},
|
||||||
summary,
|
summary,
|
||||||
filter: role || 'all'
|
filter: role || 'ELEVATED'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user