* Start time tracking for a task
(task: TaskInfo)
| 1296 | * Start time tracking for a task |
| 1297 | */ |
| 1298 | async startTimeTracking(task: TaskInfo): Promise<TaskInfo> { |
| 1299 | const file = this.plugin.app.vault.getAbstractFileByPath(task.path); |
| 1300 | if (!(file instanceof TFile)) { |
| 1301 | throw new Error(`Cannot find task file: ${task.path}`); |
| 1302 | } |
| 1303 | |
| 1304 | // Check if already tracking |
| 1305 | const activeSession = this.plugin.getActiveTimeSession(task); |
| 1306 | if (activeSession) { |
| 1307 | throw new Error("Time tracking is already active for this task"); |
| 1308 | } |
| 1309 | |
| 1310 | // Step 1: Construct new state in memory |
| 1311 | const timeTrackingPlan = buildStartTimeTrackingPlan( |
| 1312 | task, |
| 1313 | getCurrentTimestamp(), |
| 1314 | new Date().toISOString() |
| 1315 | ); |
| 1316 | const { updatedTask, newEntry } = timeTrackingPlan; |
| 1317 | |
| 1318 | // Step 2: Persist to file |
| 1319 | await this.plugin.app.fileManager.processFrontMatter(file, (frontmatter) => { |
| 1320 | const timeEntriesField = this.plugin.fieldMapper.toUserField("timeEntries"); |
| 1321 | const dateModifiedField = this.plugin.fieldMapper.toUserField("dateModified"); |
| 1322 | applyStartTimeTrackingFrontmatterChange({ |
| 1323 | frontmatter, |
| 1324 | timeEntriesField, |
| 1325 | dateModifiedField, |
| 1326 | newEntry, |
| 1327 | dateModified: timeTrackingPlan.dateModified, |
| 1328 | }); |
| 1329 | }); |
| 1330 | |
| 1331 | // Step 3: Wait for fresh data and update cache |
| 1332 | try { |
| 1333 | // Wait for the metadata cache to have the updated time entries |
| 1334 | if (this.plugin.cacheManager.waitForFreshTaskData) { |
| 1335 | await this.plugin.cacheManager.waitForFreshTaskData(file); |
| 1336 | } |
| 1337 | this.plugin.cacheManager.updateTaskInfoInCache(task.path, updatedTask); |
| 1338 | } catch (cacheError) { |
| 1339 | tasknotesLogger.error("Error updating cache for time tracking start:", { |
| 1340 | category: "stale-data", |
| 1341 | operation: "updating-cache-time-tracking-start", |
| 1342 | error: cacheError, |
| 1343 | }); |
| 1344 | } |
| 1345 | |
| 1346 | // Step 4: Notify system of change |
| 1347 | this.plugin.emitter.trigger(EVENT_TASK_UPDATED, { |
| 1348 | path: task.path, |
| 1349 | originalTask: task, |
| 1350 | updatedTask: updatedTask, |
| 1351 | }); |
| 1352 | |
| 1353 | // Trigger webhook for time tracking start |
| 1354 | if (this.webhookNotifier) { |
| 1355 | try { |
no test coverage detected