diff --git a/package.json b/package.json index 1716df9..9ecca0c 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,10 @@ { "command": "harvest-vscode.logout", "title": "Harvest: Logout" + }, + { + "command": "harvest-vscode.stopTimer", + "title": "Harvest: Stop Timer" } ] }, diff --git a/src/Entities/CurrentTimeEntry.ts b/src/Entities/CurrentTimeEntry.ts new file mode 100644 index 0000000..401df63 --- /dev/null +++ b/src/Entities/CurrentTimeEntry.ts @@ -0,0 +1,38 @@ +let instance: CurrentTimeEntry | null + +interface CurrentTimeEntryProps { + id?: number, + taskName?: string, + projectName?: string, + notes?: string +} + +class CurrentTimeEntry { + public id?: number + public taskName?: string + public projectName?: string + public notes?: string + + constructor () { + if (!instance) instance = this + return instance + } + + set props (values: CurrentTimeEntryProps) { + if (values.id) this.id = values.id + if (values.taskName) this.taskName = values.taskName + if (values.projectName) this.projectName = values.projectName + if (values.notes) this.projectName = values.notes + } + + get props (): CurrentTimeEntryProps { + return { + id: this.id, + taskName: this.taskName, + projectName: this.projectName, + notes: this.notes + } + } +} + +export default CurrentTimeEntry diff --git a/src/UseCases/Commands/PunchTime.ts b/src/UseCases/Commands/PunchTime.ts index 6380a86..a56c3cd 100644 --- a/src/UseCases/Commands/PunchTime.ts +++ b/src/UseCases/Commands/PunchTime.ts @@ -1,4 +1,5 @@ import * as vscode from "vscode"; +import CurrentTimeEntry from "../../Entities/CurrentTimeEntry"; import TaskInterface from "../../Entities/Interfaces/TaskInterface"; import TimeEntryInterface from "../../Entities/Interfaces/TimeEntryInterface"; import Project from "../../Entities/Project"; @@ -52,11 +53,6 @@ function PunchTime (context: vscode.ExtensionContext): vscode.Disposable { return t.name === selectedTaskName }) - // console.log(selectedProject?.tasks[0]) - // console.log(selectedProject?.tasks[1]) - // console.log(selectedProject?.tasks[2]) - console.log(selectedTask) - const notes = await vscode.window.showInputBox({ ignoreFocusOut: true, placeHolder: 'Notes' @@ -74,11 +70,20 @@ function PunchTime (context: vscode.ExtensionContext): vscode.Disposable { notes: notes } - console.log(newTimeEntry) - const saveNewTimeEntryResponse = await saveNewTimeEntry(newTimeEntry) - console.log(saveNewTimeEntryResponse) + const newTimeEntryId = saveNewTimeEntryResponse.id + + const currentTimeEntry = new CurrentTimeEntry + currentTimeEntry.props = { + id: newTimeEntryId, + projectName: selectedProjectName, + taskName: selectedTaskName, + notes: notes + } + + await context.globalState.update('currentTaskId', newTimeEntryId) + vscode.window.showInformationMessage(`${selectedProjectName} \n ${selectedTaskName} \n ${notes}`) }) } diff --git a/src/UseCases/Commands/StopTimer.ts b/src/UseCases/Commands/StopTimer.ts new file mode 100644 index 0000000..77c0510 --- /dev/null +++ b/src/UseCases/Commands/StopTimer.ts @@ -0,0 +1,46 @@ +import * as vscode from "vscode" +import axios from 'axios' +import Harvest from "../../Entities/Harvest" + +function StopTimer (context: vscode.ExtensionContext): vscode.Disposable { + return vscode.commands.registerCommand('harvest-vscode.stopTimer', async () => { + + const accountId: string = context.globalState.get('accountId') || '' + const accessToken: string = context.globalState.get('accessToken') || '' + + if (!accountId || !accessToken) { + vscode.window.showWarningMessage('You are not authenticated with Harvest /n Run the "Harvest: Login Command" first.') + return + } + + const harvest = new Harvest({ + accountId: accountId, + accessToken: accessToken + }) + + const currentTaskId: string = context.globalState.get('currentTaskId') || '' + + if (!currentTaskId) { + vscode.window.showWarningMessage('There is no current task stored.') + return + } + + let timeEntryResponse: any + try { + timeEntryResponse = await axios.patch( + `https://api.harvestapp.com/v2/time_entries/${currentTaskId}/stop`, + {}, + { headers: harvest.headers } + ) + } catch (err) { + console.log(err) + vscode.window.showErrorMessage('Issue stoping timer with Harvest.') + return + } + + vscode.window.showInformationMessage('Harvest Timer Stopped') + await context.globalState.update('currentTaskId', '') + }) +} + +export default StopTimer diff --git a/src/extension.ts b/src/extension.ts index 9ce613e..3c31374 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode' import Logout from './UseCases/Commands/Logout' import PunchTime from './UseCases/Commands/PunchTime' import SetUserAuthentication from './UseCases/Commands/SetUserAuthentication' +import StopTimer from './UseCases/Commands/StopTimer' export function activate(context: vscode.ExtensionContext) { const statusbar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100) @@ -13,7 +14,8 @@ export function activate(context: vscode.ExtensionContext) { const commands: vscode.Disposable[] = [ SetUserAuthentication(context), Logout(context), - PunchTime(context) + PunchTime(context), + StopTimer(context) ] context.subscriptions.push(...commands)