Skip to content

Commit 7ffd437

Browse files
committed
set billable flag from project
1 parent c70f470 commit 7ffd437

2 files changed

Lines changed: 40 additions & 42 deletions

File tree

src/toggl.js

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,46 @@ export class TogglGateway {
1111
return this.#request("get", "/workspaces");
1212
}
1313

14-
getProjects(workspaceId) {
15-
return this.#request("get", `/workspaces/${workspaceId}/projects`, {
14+
getProjects(workspace) {
15+
return this.#request("get", `/workspaces/${workspace.id}/projects`, {
1616
params: { active: true, per_page: 200 },
1717
});
1818
}
1919

20-
getTasks(workspaceId, projectId) {
20+
getTasks(project) {
2121
return this.#request(
2222
"get",
23-
`/workspaces/${workspaceId}/projects/${projectId}/tasks`,
23+
`/workspaces/${project.workspace_id}/projects/${project.id}/tasks`,
2424
);
2525
}
2626

27-
createTask(workspaceId, projectId, name) {
27+
createTask(project, name) {
2828
return this.#request(
2929
"post",
30-
`/workspaces/${workspaceId}/projects/${projectId}/tasks`,
30+
`/workspaces/${project.workspace_id}/projects/${project.id}/tasks`,
3131
{
3232
data: { name },
3333
},
3434
);
3535
}
3636

37-
startTimeEntry(workspaceId, projectId, taskId, description) {
38-
return this.#request("post", `/workspaces/${workspaceId}/time_entries`, {
39-
data: {
40-
description,
41-
start: new Date().toISOString(),
42-
duration: -1,
43-
workspace_id: workspaceId,
44-
project_id: projectId,
45-
task_id: taskId,
46-
created_with: "trektor",
37+
startTimeEntry(task, description) {
38+
return this.#request(
39+
"post",
40+
`/workspaces/${task.workspace_id}/time_entries`,
41+
{
42+
data: {
43+
description,
44+
start: new Date().toISOString(),
45+
duration: -1,
46+
workspace_id: task.workspace_id,
47+
project_id: task.project_id,
48+
task_id: task.id,
49+
billable: task.project_billable,
50+
created_with: "trektor",
51+
},
4752
},
48-
});
53+
);
4954
}
5055

5156
async #request(method, path, conf = {}) {
@@ -79,30 +84,15 @@ export class TogglService {
7984
this.#gateway = gateway;
8085
}
8186

82-
async addTask(tracking) {
83-
const workspaceId = await this.#getWorkspaceId();
84-
const projectId = await this.#getProjectId(workspaceId, tracking.project);
85-
86-
const togglTasks = await this.#gateway.getTasks(workspaceId, projectId);
87-
const togglTask =
88-
togglTasks.find((t) => t.name === tracking.task) ||
89-
(await this.#gateway.createTask(workspaceId, projectId, tracking.task));
90-
91-
return togglTask;
92-
}
93-
9487
async track(tracking) {
95-
const togglTask = await this.addTask(tracking);
88+
const workspace = await this.#getWorkspace();
89+
const project = await this.#getProject(workspace, tracking.project);
90+
const task = await this.#getOrCreateTask(project, tracking.task);
9691

97-
await this.#gateway.startTimeEntry(
98-
togglTask.workspace_id,
99-
togglTask.project_id,
100-
togglTask.id,
101-
tracking.description,
102-
);
92+
await this.#gateway.startTimeEntry(task, tracking.description);
10393
}
10494

105-
async #getWorkspaceId() {
95+
async #getWorkspace() {
10696
const workspaces = await this.#gateway.getWorkspaces();
10797

10898
if (workspaces.length === 0) {
@@ -114,11 +104,11 @@ export class TogglService {
114104
);
115105
}
116106

117-
return workspaces[0].id;
107+
return workspaces[0];
118108
}
119109

120-
async #getProjectId(workspaceId, projectName) {
121-
const allProjects = await this.#gateway.getProjects(workspaceId);
110+
async #getProject(workspace, projectName) {
111+
const allProjects = await this.#gateway.getProjects(workspace);
122112
const projects = allProjects.filter((project) =>
123113
project.name.endsWith(`(${projectName})`),
124114
);
@@ -132,6 +122,14 @@ export class TogglService {
132122
);
133123
}
134124

135-
return projects[0].id;
125+
return projects[0];
126+
}
127+
128+
async #getOrCreateTask(project, name) {
129+
const tasks = await this.#gateway.getTasks(project);
130+
return (
131+
tasks.find((t) => t.name == name) ||
132+
(await this.#gateway.createTask(project, name))
133+
);
136134
}
137135
}

src/trello.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ const track = async function (t) {
1919
if (token === null) {
2020
throw new Error("Toggl API Token not configured");
2121
}
22-
const togglService = new TogglService(new TogglGateway(token));
2322

2423
const tracking = await extractTrackingData(t);
2524

25+
const togglService = new TogglService(new TogglGateway(token));
2626
await togglService.track(tracking);
2727
await setTrackingData(t, tracking);
2828
t.alert({ message: `Started trekking #${tracking.task}` });

0 commit comments

Comments
 (0)