Enhance task progress calculation and update logic
- Updated SQL migration to fix multilevel subtask progress calculation, ensuring accurate parent task updates based on subtasks. - Refactored backend functions to recursively recalculate task progress values, improving data integrity across task hierarchies. - Enhanced frontend components to refresh task progress values when tasks are updated, ensuring real-time synchronization. - Integrated logging for task progress updates to improve traceability and debugging.
This commit is contained in:
@@ -3,6 +3,7 @@ import db from "../../config/db";
|
||||
import { SocketEvents } from "../events";
|
||||
import { log, log_error, notifyProjectUpdates } from "../util";
|
||||
import { logProgressChange } from "../../services/activity-logs/activity-logs.service";
|
||||
import TasksControllerV2 from "../../controllers/tasks-controller-v2";
|
||||
|
||||
interface UpdateTaskProgressData {
|
||||
task_id: string;
|
||||
@@ -21,6 +22,9 @@ async function updateTaskAncestors(io: any, socket: Socket, projectId: string, t
|
||||
if (!taskId) return;
|
||||
|
||||
try {
|
||||
// Use the new controller method to update the task progress
|
||||
await TasksControllerV2.updateTaskProgress(taskId);
|
||||
|
||||
// Get the current task's progress ratio
|
||||
const progressRatio = await db.query(
|
||||
"SELECT get_task_complete_ratio($1) as ratio",
|
||||
@@ -156,8 +160,13 @@ export async function on_update_task_progress(io: any, socket: Socket, data: str
|
||||
|
||||
log(`Emitted progress update for task ${task_id} to project room ${projectId}`, null);
|
||||
|
||||
// Recursively update all ancestors in the task hierarchy
|
||||
await updateTaskAncestors(io, socket, projectId, parent_task_id);
|
||||
// If this task has a parent, use our controller to update all ancestors
|
||||
if (parent_task_id) {
|
||||
// Use the controller method to update the parent task's progress
|
||||
await TasksControllerV2.updateTaskProgress(parent_task_id);
|
||||
// Also use the existing method for socket notifications
|
||||
await updateTaskAncestors(io, socket, projectId, parent_task_id);
|
||||
}
|
||||
|
||||
// Notify that project updates are available
|
||||
notifyProjectUpdates(socket, task_id);
|
||||
|
||||
@@ -3,6 +3,7 @@ import db from "../../config/db";
|
||||
import { SocketEvents } from "../events";
|
||||
import { log, log_error, notifyProjectUpdates } from "../util";
|
||||
import { logWeightChange } from "../../services/activity-logs/activity-logs.service";
|
||||
import TasksControllerV2 from "../../controllers/tasks-controller-v2";
|
||||
|
||||
interface UpdateTaskWeightData {
|
||||
task_id: string;
|
||||
@@ -29,13 +30,8 @@ export async function on_update_task_weight(io: any, socket: Socket, data: strin
|
||||
const currentWeight = currentWeightResult.rows[0]?.weight;
|
||||
const projectId = currentWeightResult.rows[0]?.project_id;
|
||||
|
||||
// Update the task weight in the database
|
||||
await db.query(
|
||||
`UPDATE tasks
|
||||
SET weight = $1, updated_at = NOW()
|
||||
WHERE id = $2`,
|
||||
[weight, task_id]
|
||||
);
|
||||
// Update the task weight using our controller method
|
||||
await TasksControllerV2.updateTaskWeight(task_id, weight);
|
||||
|
||||
// Log the weight change using the activity logs service
|
||||
await logWeightChange({
|
||||
@@ -57,6 +53,10 @@ export async function on_update_task_weight(io: any, socket: Socket, data: strin
|
||||
|
||||
// If this is a subtask, update the parent task's progress
|
||||
if (parent_task_id) {
|
||||
// Use the controller to update the parent task progress
|
||||
await TasksControllerV2.updateTaskProgress(parent_task_id);
|
||||
|
||||
// Get the updated progress to emit to clients
|
||||
const progressRatio = await db.query(
|
||||
"SELECT get_task_complete_ratio($1) as ratio",
|
||||
[parent_task_id]
|
||||
@@ -70,6 +70,32 @@ export async function on_update_task_weight(io: any, socket: Socket, data: strin
|
||||
progress_value: progressRatio?.rows[0]?.ratio?.ratio || 0
|
||||
}
|
||||
);
|
||||
|
||||
// We also need to update any grandparent tasks
|
||||
const grandparentResult = await db.query(
|
||||
"SELECT parent_task_id FROM tasks WHERE id = $1",
|
||||
[parent_task_id]
|
||||
);
|
||||
|
||||
const grandparentId = grandparentResult.rows[0]?.parent_task_id;
|
||||
|
||||
if (grandparentId) {
|
||||
await TasksControllerV2.updateTaskProgress(grandparentId);
|
||||
|
||||
// Emit the grandparent's updated progress
|
||||
const grandparentProgressRatio = await db.query(
|
||||
"SELECT get_task_complete_ratio($1) as ratio",
|
||||
[grandparentId]
|
||||
);
|
||||
|
||||
socket.emit(
|
||||
SocketEvents.TASK_PROGRESS_UPDATED.toString(),
|
||||
{
|
||||
task_id: grandparentId,
|
||||
progress_value: grandparentProgressRatio?.rows[0]?.ratio?.ratio || 0
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Notify that project updates are available
|
||||
|
||||
Reference in New Issue
Block a user