* Stop time tracking for a task
(task: TaskInfo)
| 1374 | * Stop time tracking for a task |
| 1375 | */ |
| 1376 | async stopTimeTracking(task: TaskInfo): Promise<TaskInfo> { |
| 1377 | const file = this.plugin.app.vault.getAbstractFileByPath(task.path); |
| 1378 | if (!(file instanceof TFile)) { |
| 1379 | throw new Error(`Cannot find task file: ${task.path}`); |
| 1380 | } |
| 1381 | |
| 1382 | const activeSession = this.plugin.getActiveTimeSession(task); |
| 1383 | if (!activeSession) { |
| 1384 | throw new Error("No active time tracking session for this task"); |
| 1385 | } |
| 1386 | |
| 1387 | // Step 1: Construct new state in memory |
| 1388 | const timeTrackingPlan = buildStopTimeTrackingPlan( |
| 1389 | task, |
| 1390 | activeSession, |
| 1391 | getCurrentTimestamp(), |
| 1392 | new Date().toISOString() |
| 1393 | ); |
| 1394 | const { updatedTask } = timeTrackingPlan; |
| 1395 | |
| 1396 | // Step 2: Persist to file |
| 1397 | await this.plugin.app.fileManager.processFrontMatter(file, (frontmatter) => { |
| 1398 | const timeEntriesField = this.plugin.fieldMapper.toUserField("timeEntries"); |
| 1399 | const dateModifiedField = this.plugin.fieldMapper.toUserField("dateModified"); |
| 1400 | applyStopTimeTrackingFrontmatterChange({ |
| 1401 | frontmatter, |
| 1402 | timeEntriesField, |
| 1403 | dateModifiedField, |
| 1404 | activeSession, |
| 1405 | stopTimestamp: timeTrackingPlan.stopTimestamp, |
| 1406 | dateModified: timeTrackingPlan.dateModified, |
| 1407 | }); |
| 1408 | }); |
| 1409 | |
| 1410 | // Step 3: Wait for fresh data and update cache |
| 1411 | try { |
| 1412 | // Wait for the metadata cache to have the updated time entries |
| 1413 | if (this.plugin.cacheManager.waitForFreshTaskData) { |
| 1414 | await this.plugin.cacheManager.waitForFreshTaskData(file); |
| 1415 | } |
| 1416 | this.plugin.cacheManager.updateTaskInfoInCache(task.path, updatedTask); |
| 1417 | } catch (cacheError) { |
| 1418 | tasknotesLogger.error("Error updating cache for time tracking stop:", { |
| 1419 | category: "stale-data", |
| 1420 | operation: "updating-cache-time-tracking-stop", |
| 1421 | error: cacheError, |
| 1422 | }); |
| 1423 | } |
| 1424 | |
| 1425 | // Step 4: Notify system of change |
| 1426 | this.plugin.emitter.trigger(EVENT_TASK_UPDATED, { |
| 1427 | path: task.path, |
| 1428 | originalTask: task, |
| 1429 | updatedTask: updatedTask, |
| 1430 | }); |
| 1431 | |
| 1432 | // Trigger webhook for time tracking stop |
| 1433 | if (this.webhookNotifier) { |
no test coverage detected