-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fix start-date only + ♻️ Big refactor modularization
- Loading branch information
Showing
4 changed files
with
157 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fetch from "node-fetch"; | ||
import { Buffer } from "buffer"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import os from "os"; | ||
|
||
const filePath = path.join(os.homedir(), ".toggl2tsc"); | ||
const token = fs.readFileSync(filePath, "utf8"); | ||
const base64Credentials = Buffer.from(`${token}:api_token`).toString("base64"); | ||
|
||
async function fetchFromToggl(url) { | ||
const response = await fetch(url, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Basic ${base64Credentials}`, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
console.error( | ||
`Failed to fetch data from Toggl API: ${response.statusText}`, | ||
); | ||
return null; | ||
} | ||
|
||
return await response.json(); | ||
} | ||
|
||
export async function fetchTimeEntries(start, end) { | ||
const url = `https://api.track.toggl.com/api/v9/me/time_entries?start_date=${start}&end_date=${end}`; | ||
return await fetchFromToggl(url); | ||
} | ||
|
||
export async function fetchWorkspaces() { | ||
const url = `https://api.track.toggl.com/api/v9/workspaces`; | ||
return await fetchFromToggl(url); | ||
} | ||
|
||
export async function fetchProjects(workspaceId) { | ||
const url = `https://api.track.toggl.com/api/v9/workspaces/${workspaceId}/projects`; | ||
return await fetchFromToggl(url); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import dayjs from "dayjs"; | ||
import utc from "dayjs/plugin/utc.js"; | ||
import timezone from "dayjs/plugin/timezone.js"; | ||
import isBetween from "dayjs/plugin/isBetween.js"; | ||
|
||
dayjs.extend(utc); | ||
dayjs.extend(timezone); | ||
dayjs.extend(isBetween); | ||
|
||
const currTz = "Europe/Brussels"; | ||
|
||
export function getCurrentDay() { | ||
return dayjs().tz(currTz).startOf("day").toISOString(); | ||
} | ||
|
||
export function getNextDay() { | ||
return dayjs().add(1, "days").tz(currTz).startOf("day").toISOString(); | ||
} | ||
|
||
export function getStartOfDay(date) { | ||
return dayjs(date).tz(currTz).startOf("day").toISOString(); | ||
} | ||
|
||
export function getEndOfDay(date) { | ||
return dayjs(date).tz(currTz).endOf("day").toISOString(); | ||
} | ||
|
||
export function getEndOfToday() { | ||
return dayjs().tz(currTz).endOf("day").toISOString(); | ||
} | ||
|
||
export function isBetweenDates(entry, start, end) { | ||
return dayjs(entry.start).isBetween(start, end); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import chalk from "chalk"; | ||
import createPrompt from "prompt-sync"; | ||
import { fetchWorkspaces } from "../api/toggl.js"; | ||
|
||
const prompt = createPrompt({}); | ||
|
||
export async function selectWorkspaceId() { | ||
const workspaces = await fetchWorkspaces(); | ||
|
||
if (!workspaces || workspaces.length === 0) { | ||
console.error("No workspaces found"); | ||
return null; | ||
} | ||
|
||
if (workspaces.length === 1) { | ||
console.log( | ||
chalk.green( | ||
`Automatically selecting the only workspace: ${chalk.blue(workspaces[0].name)}`, | ||
), | ||
); | ||
return workspaces[0].id; | ||
} | ||
|
||
console.log(chalk.green("Select a workspace ID:")); | ||
workspaces.forEach((workspace, index) => { | ||
console.log(chalk.blueBright(`${index + 1}. ${workspace.name}`)); | ||
}); | ||
|
||
const userInput = prompt( | ||
"Enter the number corresponding to the workspace ID: ", | ||
); | ||
const selectedIndex = parseInt(userInput); | ||
|
||
if ( | ||
isNaN(selectedIndex) || | ||
selectedIndex < 1 || | ||
selectedIndex > workspaces.length | ||
) { | ||
console.error("Invalid selection"); | ||
return null; | ||
} | ||
|
||
return workspaces[selectedIndex - 1].id; | ||
} |