feat(database): optimize task sorting functions and introduce bulk update capability

- Added new SQL migration to fix window function errors in task sorting functions, replacing CTEs with direct updates for better performance.
- Introduced a bulk update function for task sort orders, allowing multiple updates in a single call to improve efficiency.
- Updated socket command to support bulk updates, enhancing the task sorting experience in the frontend.
- Simplified task update handling in the frontend to utilize the new bulk update feature, improving overall performance and user experience.
This commit is contained in:
chamikaJ
2025-07-11 14:46:07 +05:30
parent f2f12a2dfa
commit e4dfae9f1d
6 changed files with 385 additions and 107 deletions

View File

@@ -24,6 +24,14 @@ interface ChangeRequest {
priority: string;
};
team_id: string;
// New simplified approach
task_updates?: Array<{
task_id: string;
sort_order: number;
status_id?: string;
priority_id?: string;
phase_id?: string;
}>;
}
interface Config {
@@ -64,38 +72,72 @@ function updateUnmappedStatus(config: Config) {
export async function on_task_sort_order_change(_io: Server, socket: Socket, data: ChangeRequest) {
try {
const q = `SELECT handle_task_list_sort_order_change($1);`;
const config: Config = {
from_index: data.from_index,
to_index: data.to_index,
task_id: data.task.id,
from_group: data.from_group,
to_group: data.to_group,
project_id: data.project_id,
group_by: data.group_by,
to_last_index: Boolean(data.to_last_index)
};
if ((config.group_by === GroupBy.STATUS) && config.to_group) {
const canContinue = await TasksControllerV2.checkForCompletedDependencies(config.task_id, config?.to_group);
if (!canContinue) {
return socket.emit(SocketEvents.TASK_SORT_ORDER_CHANGE.toString(), {
completed_deps: canContinue
});
// New simplified approach - use bulk updates if provided
if (data.task_updates && data.task_updates.length > 0) {
// Check dependencies for status changes
if (data.group_by === GroupBy.STATUS && data.to_group) {
const canContinue = await TasksControllerV2.checkForCompletedDependencies(data.task.id, data.to_group);
if (!canContinue) {
return socket.emit(SocketEvents.TASK_SORT_ORDER_CHANGE.toString(), {
completed_deps: canContinue
});
}
}
notifyStatusChange(socket, config);
// Use the simple bulk update function
const q = `SELECT update_task_sort_orders_bulk($1);`;
await db.query(q, [JSON.stringify(data.task_updates)]);
await emitSortOrderChange(data, socket);
// Handle notifications and logging
if (data.group_by === GroupBy.STATUS && data.to_group) {
notifyStatusChange(socket, {
task_id: data.task.id,
to_group: data.to_group,
from_group: data.from_group,
from_index: data.from_index,
to_index: data.to_index,
project_id: data.project_id,
group_by: data.group_by,
to_last_index: data.to_last_index
});
}
} else {
// Fallback to old complex method
const q = `SELECT handle_task_list_sort_order_change($1);`;
const config: Config = {
from_index: data.from_index,
to_index: data.to_index,
task_id: data.task.id,
from_group: data.from_group,
to_group: data.to_group,
project_id: data.project_id,
group_by: data.group_by,
to_last_index: Boolean(data.to_last_index)
};
if ((config.group_by === GroupBy.STATUS) && config.to_group) {
const canContinue = await TasksControllerV2.checkForCompletedDependencies(config.task_id, config?.to_group);
if (!canContinue) {
return socket.emit(SocketEvents.TASK_SORT_ORDER_CHANGE.toString(), {
completed_deps: canContinue
});
}
notifyStatusChange(socket, config);
}
if (config.group_by === GroupBy.PHASE) {
updateUnmappedStatus(config);
}
await db.query(q, [JSON.stringify(config)]);
await emitSortOrderChange(data, socket);
}
if (config.group_by === GroupBy.PHASE) {
updateUnmappedStatus(config);
}
await db.query(q, [JSON.stringify(config)]);
await emitSortOrderChange(data, socket);
if (config.group_by === GroupBy.STATUS) {
// Common post-processing logic for both approaches
if (data.group_by === GroupBy.STATUS) {
const userId = getLoggedInUserIdFromSocket(socket);
const isAlreadyAssigned = await TasksControllerV2.checkUserAssignedToTask(data.task.id, userId as string, data.team_id);
@@ -104,7 +146,7 @@ export async function on_task_sort_order_change(_io: Server, socket: Socket, dat
}
}
if (config.group_by === GroupBy.PHASE) {
if (data.group_by === GroupBy.PHASE) {
void logPhaseChange({
task_id: data.task.id,
socket,
@@ -113,7 +155,7 @@ export async function on_task_sort_order_change(_io: Server, socket: Socket, dat
});
}
if (config.group_by === GroupBy.STATUS) {
if (data.group_by === GroupBy.STATUS) {
void logStatusChange({
task_id: data.task.id,
socket,
@@ -122,7 +164,7 @@ export async function on_task_sort_order_change(_io: Server, socket: Socket, dat
});
}
if (config.group_by === GroupBy.PRIORITY) {
if (data.group_by === GroupBy.PRIORITY) {
void logPriorityChange({
task_id: data.task.id,
socket,
@@ -131,7 +173,7 @@ export async function on_task_sort_order_change(_io: Server, socket: Socket, dat
});
}
void notifyProjectUpdates(socket, config.task_id);
void notifyProjectUpdates(socket, data.task.id);
return;
} catch (error) {
log_error(error);