|
| 1 | +(() => { |
| 2 | + // lib/plugin.js |
| 3 | + var plugin = { |
| 4 | + constants: {}, |
| 5 | + // https://www.amplenote.com/help/developing_amplenote_plugins#noteOption |
| 6 | + taskOption: { |
| 7 | + "Bulk Reset Overdue Tasks": { |
| 8 | + run: async function(app, noteUUID) { |
| 9 | + try { |
| 10 | + const oneHourAgo = Math.floor(Date.now() / 1e3) - 3600; |
| 11 | + const taskDomains = await app.getTaskDomains(); |
| 12 | + if (!taskDomains || taskDomains.length === 0) { |
| 13 | + await app.alert("No task domains found."); |
| 14 | + return; |
| 15 | + } |
| 16 | + const overdueTasks = []; |
| 17 | + const tasksByDomain = {}; |
| 18 | + for (const taskDomain of taskDomains) { |
| 19 | + const domainTasks = []; |
| 20 | + for await (const task of app.getTaskDomainTasks(taskDomain.uuid)) { |
| 21 | + const taskDate = task.startAt || task.endAt; |
| 22 | + if (taskDate && taskDate < oneHourAgo) { |
| 23 | + overdueTasks.push(task); |
| 24 | + domainTasks.push(task); |
| 25 | + } |
| 26 | + } |
| 27 | + if (domainTasks.length > 0) { |
| 28 | + tasksByDomain[taskDomain.name] = domainTasks; |
| 29 | + } |
| 30 | + } |
| 31 | + if (overdueTasks.length === 0) { |
| 32 | + await app.alert("No overdue tasks found."); |
| 33 | + return; |
| 34 | + } |
| 35 | + const domainBreakdown = Object.entries(tasksByDomain).map(([domainName, tasks]) => { |
| 36 | + const taskList = tasks.map((task) => { |
| 37 | + const content = task.content || "Untitled task"; |
| 38 | + const truncated = content.length > 60 ? content.substring(0, 57) + "..." : content; |
| 39 | + return ` - ${truncated}`; |
| 40 | + }).join("\n"); |
| 41 | + return ` ${domainName} (${tasks.length}): |
| 42 | +${taskList}`; |
| 43 | + }).join("\n\n"); |
| 44 | + const confirmMessage = `Found ${overdueTasks.length} overdue task${overdueTasks.length > 1 ? "s" : ""}: |
| 45 | +
|
| 46 | +${domainBreakdown} |
| 47 | +
|
| 48 | +Remove from calendar?`; |
| 49 | + const confirmed = await app.alert(confirmMessage, { |
| 50 | + actions: [ |
| 51 | + { label: "Cancel", icon: "cancel" }, |
| 52 | + { label: "Remove from calendar", icon: "check" } |
| 53 | + ] |
| 54 | + }); |
| 55 | + if (confirmed === 1) { |
| 56 | + let successCount = 0; |
| 57 | + let failCount = 0; |
| 58 | + const errors = []; |
| 59 | + for (const task of overdueTasks) { |
| 60 | + try { |
| 61 | + const updates = {}; |
| 62 | + console.log(`Processing task ${task.uuid}:`); |
| 63 | + console.log(` startAt: ${task.startAt}`); |
| 64 | + console.log(` endAt: ${task.endAt}`); |
| 65 | + console.log(` hideUntil: ${task.hideUntil}`); |
| 66 | + if (task.startAt !== null && task.startAt !== void 0) { |
| 67 | + delete updates.startAt; |
| 68 | + } |
| 69 | + if (task.endAt !== null && task.endAt !== void 0) { |
| 70 | + delete updates.endAt; |
| 71 | + } |
| 72 | + if (task.hideUntil !== null && task.hideUntil !== void 0) { |
| 73 | + delete updates.hideUntil; |
| 74 | + } |
| 75 | + console.log(` Updates to apply:`, updates); |
| 76 | + if (Object.keys(updates).length > 0) { |
| 77 | + await app.updateTask(task.uuid, updates); |
| 78 | + console.log(` \u2713 Successfully updated task ${task.uuid}`); |
| 79 | + successCount++; |
| 80 | + } else { |
| 81 | + console.log( |
| 82 | + ` \u26A0 Task ${task.uuid} has no calendar dates to remove` |
| 83 | + ); |
| 84 | + successCount++; |
| 85 | + } |
| 86 | + } catch (error) { |
| 87 | + const errorMsg = `Task "${task.content || task.uuid}": ${error.message}`; |
| 88 | + console.error(`Failed to reset task ${task.uuid}:`, error); |
| 89 | + console.error(`Task data:`, JSON.stringify(task)); |
| 90 | + errors.push(errorMsg); |
| 91 | + failCount++; |
| 92 | + } |
| 93 | + } |
| 94 | + let resultMessage = `Successfully reset ${successCount} task${successCount !== 1 ? "s" : ""}.`; |
| 95 | + if (failCount > 0) { |
| 96 | + resultMessage += ` |
| 97 | +
|
| 98 | +${failCount} task${failCount !== 1 ? "s" : ""} failed: |
| 99 | +`; |
| 100 | + resultMessage += errors.map((e) => `\u2022 ${e}`).join("\n"); |
| 101 | + } |
| 102 | + await app.alert(resultMessage); |
| 103 | + } |
| 104 | + } catch (error) { |
| 105 | + console.error("Error in Bulk Reset Overdue Tasks:", error); |
| 106 | + await app.alert(`Error: ${error.message}`); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + }, |
| 111 | + insertText: {}, |
| 112 | + replaceText: {} |
| 113 | + }; |
| 114 | + var plugin_default = plugin; |
| 115 | +})(); |
0 commit comments