From 814f97c49df25e4a67e105541f97a7f8d4989c7f Mon Sep 17 00:00:00 2001 From: davidlang42 <37281992+davidlang42@users.noreply.github.com> Date: Mon, 12 Feb 2024 23:05:12 +1100 Subject: [PATCH] Untracked: Fix bug with deleted tasks (#42) --- app/Prerequisites.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/app/Prerequisites.js b/app/Prerequisites.js index e870f20..afabf2b 100644 --- a/app/Prerequisites.js +++ b/app/Prerequisites.js @@ -136,6 +136,7 @@ function processPrerequisiteState(boardId, state) { // create lookups var completedByName = {}; var unusedCompletedIdsByName = {}; + var deletedTaskIds = []; var duplicateNames = []; for (const id in state.taskStateById) { const taskState = state.taskStateById[id]; @@ -181,13 +182,16 @@ function processPrerequisiteState(boardId, state) { } state.nextDatePrerequisite = !nextDate ? null : formatDateTasks(nextDate); if (!taskState.due) { - // setTaskDueWithMessage() calls a task read, therefore only call if it might actually be useful + // getTaskOrAddToList() calls a task read, therefore only call if it might actually be useful if (duplicates.length > 0) { - processed.push(setTaskDueWithMessage(boardId, id, "Prerequisite tasks have duplicate names: " + duplicates.join(", "))); + var task = getTaskOrAddToList(boardId, id, deletedTaskIds); + if (task) processed.push(setTaskDueWithMessage(boardId, id, "Prerequisite tasks have duplicate names: " + duplicates.join(", "), task)); } else if (missing.length > 0) { - processed.push(setTaskDueWithMessage(boardId, id, "Could not find prerequisite tasks: " + missing.join(", "))); + var task = getTaskOrAddToList(boardId, id, deletedTaskIds); + if (task) processed.push(setTaskDueWithMessage(boardId, id, "Could not find prerequisite tasks: " + missing.join(", "), task)); } else if (ready) { - processed.push(setTaskDueWithMessage(boardId, id, "Prerequisite tasks complete: " + taskState.prerequisiteNames.join(", "))); + var task = getTaskOrAddToList(boardId, id, deletedTaskIds); + if (task) processed.push(setTaskDueWithMessage(boardId, id, "Prerequisite tasks complete: " + taskState.prerequisiteNames.join(", "), task)); } } } @@ -196,11 +200,23 @@ function processPrerequisiteState(boardId, state) { const id = unusedCompletedIdsByName[name]; delete state.taskStateById[id]; } + // remove deleted tasks + for (const id in deletedTaskIds) { + delete state.taskStateById[id]; + } return processed; } const MAX_NOTES_LENGTH = 8000; +function getTaskOrAddToList(boardId, taskId, listOfFailedTaskIds) { + try { + return Tasks.Tasks.get(boardId, taskId); + } catch (err) { + listOfFailedTaskIds.push(taskId); + } +} + function setTaskDueWithMessage(boardId, taskId, message, already_up_to_date_task) { var t = already_up_to_date_task ?? Tasks.Tasks.get(boardId, taskId); if (!t.due) {