refactor(task-sort-order): optimize access check and clean up code

- Improved the access check logic by incorporating team member validation in the SQL query, enhancing security and accuracy.
- Removed unnecessary whitespace for cleaner code formatting.
- Updated socket event emission for consistency in response structure.
This commit is contained in:
shancds
2025-07-01 16:57:07 +05:30
parent 63483e01c2
commit 0a92d38ccf
2 changed files with 15 additions and 17 deletions

View File

@@ -73,7 +73,7 @@ const onTaskSortOrderChange = async (io: Server, socket: Socket, data: ChangeReq
// PERFORMANCE OPTIMIZATION: Use cached dependency check if available // PERFORMANCE OPTIMIZATION: Use cached dependency check if available
const cacheKey = `${project_id}-${userId}-${team_id}`; const cacheKey = `${project_id}-${userId}-${team_id}`;
const cachedDependency = dependencyCache.get(cacheKey); const cachedDependency = dependencyCache.get(cacheKey);
let hasAccess = false; let hasAccess = false;
if (cachedDependency && (Date.now() - cachedDependency.timestamp) < CACHE_TTL) { if (cachedDependency && (Date.now() - cachedDependency.timestamp) < CACHE_TTL) {
hasAccess = cachedDependency.result; hasAccess = cachedDependency.result;
@@ -82,16 +82,16 @@ const onTaskSortOrderChange = async (io: Server, socket: Socket, data: ChangeReq
const dependencyResult = await dbPool.query(` const dependencyResult = await dbPool.query(`
SELECT EXISTS( SELECT EXISTS(
SELECT 1 FROM project_members pm SELECT 1 FROM project_members pm
INNER JOIN projects p ON p.id = pm.project_id INNER JOIN projects p ON p.id = pm.project_id
WHERE pm.project_id = $1 INNER JOIN team_members tm ON pm.team_member_id = tm.id
AND pm.user_id = $2 WHERE pm.project_id = $1
AND p.team_id = $3 AND tm.user_id = $2
AND pm.is_active = true AND p.team_id = $3
) as has_access ) as has_access
`, [project_id, userId, team_id]); `, [project_id, userId, team_id]);
hasAccess = dependencyResult.rows[0]?.has_access || false; hasAccess = dependencyResult.rows[0]?.has_access || false;
// Cache the result // Cache the result
dependencyCache.set(cacheKey, { result: hasAccess, timestamp: Date.now() }); dependencyCache.set(cacheKey, { result: hasAccess, timestamp: Date.now() });
} }
@@ -152,8 +152,8 @@ const onTaskSortOrderChange = async (io: Server, socket: Socket, data: ChangeReq
}); });
// Send success response // Send success response
socket.emit(SocketEvents.TASK_SORT_ORDER_CHANGE.toString(), { socket.emit(SocketEvents.TASK_SORT_ORDER_CHANGE.toString(), {
success: true, success: true,
task_id: task.id, task_id: task.id,
from_group, from_group,
to_group, to_group,
@@ -162,8 +162,8 @@ const onTaskSortOrderChange = async (io: Server, socket: Socket, data: ChangeReq
} catch (error) { } catch (error) {
log_error(error); log_error(error);
socket.emit(SocketEvents.TASK_SORT_ORDER_CHANGE.toString(), { socket.emit(SocketEvents.TASK_SORT_ORDER_CHANGE.toString(), {
error: "Internal server error" error: "Internal server error"
}); });
} }
}; };

View File

@@ -7,7 +7,6 @@ import {
DragStartEvent, DragStartEvent,
DragEndEvent, DragEndEvent,
DragOverEvent, DragOverEvent,
closestCorners,
KeyboardSensor, KeyboardSensor,
PointerSensor, PointerSensor,
useSensor, useSensor,
@@ -20,7 +19,6 @@ import {
import { import {
SortableContext, SortableContext,
horizontalListSortingStrategy, horizontalListSortingStrategy,
verticalListSortingStrategy,
} from '@dnd-kit/sortable'; } from '@dnd-kit/sortable';
import { RootState } from '@/app/store'; import { RootState } from '@/app/store';
import { import {
@@ -34,8 +32,6 @@ import {
fetchEnhancedKanbanLabels, fetchEnhancedKanbanLabels,
} from '@/features/enhanced-kanban/enhanced-kanban.slice'; } from '@/features/enhanced-kanban/enhanced-kanban.slice';
import EnhancedKanbanGroup from './EnhancedKanbanGroup'; import EnhancedKanbanGroup from './EnhancedKanbanGroup';
import EnhancedKanbanTaskCard from './EnhancedKanbanTaskCard';
import PerformanceMonitor from './PerformanceMonitor';
import './EnhancedKanbanBoard.css'; import './EnhancedKanbanBoard.css';
import { useSocket } from '@/socket/socketContext'; import { useSocket } from '@/socket/socketContext';
import { useAppSelector } from '@/hooks/useAppSelector'; import { useAppSelector } from '@/hooks/useAppSelector';
@@ -50,6 +46,7 @@ import ImprovedTaskFilters from '../task-management/improved-task-filters';
import { fetchStatusesCategories } from '@/features/taskAttributes/taskStatusSlice'; import { fetchStatusesCategories } from '@/features/taskAttributes/taskStatusSlice';
import { useFilterDataLoader } from '@/hooks/useFilterDataLoader'; import { useFilterDataLoader } from '@/hooks/useFilterDataLoader';
import { useTaskSocketHandlers } from '@/hooks/useTaskSocketHandlers'; import { useTaskSocketHandlers } from '@/hooks/useTaskSocketHandlers';
import { useAuthService } from '@/hooks/useAuth';
// Import the TaskListFilters component // Import the TaskListFilters component
const TaskListFilters = React.lazy(() => import('@/pages/projects/projectView/taskList/task-list-filters/task-list-filters')); const TaskListFilters = React.lazy(() => import('@/pages/projects/projectView/taskList/task-list-filters/task-list-filters'));
@@ -68,7 +65,8 @@ const EnhancedKanbanBoard: React.FC<EnhancedKanbanBoardProps> = ({ projectId, cl
performanceMetrics performanceMetrics
} = useSelector((state: RootState) => state.enhancedKanbanReducer); } = useSelector((state: RootState) => state.enhancedKanbanReducer);
const { socket } = useSocket(); const { socket } = useSocket();
const { teamId } = useAppSelector((state: RootState) => state.auth); const authService = useAuthService();
const teamId = authService.getCurrentSession()?.team_id;
const groupBy = useSelector((state: RootState) => state.enhancedKanbanReducer.groupBy); const groupBy = useSelector((state: RootState) => state.enhancedKanbanReducer.groupBy);
const project = useAppSelector((state: RootState) => state.projectReducer.project); const project = useAppSelector((state: RootState) => state.projectReducer.project);
const { statusCategories, status: existingStatuses } = useAppSelector((state) => state.taskStatusReducer); const { statusCategories, status: existingStatuses } = useAppSelector((state) => state.taskStatusReducer);