From bc3e7f4d595694521be715d31ef4bdd45528d988 Mon Sep 17 00:00:00 2001 From: Luca Scalzotto Date: Wed, 22 May 2024 18:33:51 +0200 Subject: [PATCH] Fetch billability excluded tasks --- src/TimeChimpApi.ts | 10 ++++++++++ src/content/index.ts | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/TimeChimpApi.ts b/src/TimeChimpApi.ts index 8a42488..48d2c88 100644 --- a/src/TimeChimpApi.ts +++ b/src/TimeChimpApi.ts @@ -41,6 +41,10 @@ export class TimeChimpApi { public getCompany(): Promise { return this.doFetch('/api/company'); } + + public getTasks(): Promise { + return this.doFetch('/api/task'); + } } export interface User { @@ -64,3 +68,9 @@ export interface Company { export interface Theme { mainColor?: string; } + +export interface Task { + id: number; + name: string; + tagNames?: string[]; +} diff --git a/src/content/index.ts b/src/content/index.ts index 6596029..8069c81 100644 --- a/src/content/index.ts +++ b/src/content/index.ts @@ -14,10 +14,13 @@ setDefaultOptions({ firstWeekContainsDate: 4, }); +const BILLABILITY_EXCLUDE_TAG = 'billability-exclude'; + const api = new TimeChimpApi(); let currentDate = new Date(); let currentUser: User | undefined; +let billabilityExcludedTasks: number[] | undefined; settingsUpdateEvent.addListener(() => render()); @@ -35,10 +38,16 @@ chrome.runtime.onMessage.addListener(async (msg: Message) => { }); async function render(userName?: string) { + // Check if we have the current user info. if (!currentUser || (userName && userName !== currentUser.userName)) { currentUser = await getUser(userName); } + // Check if we have the excluded tasks. + if (!billabilityExcludedTasks) { + billabilityExcludedTasks = await getBillabilityExcludedTaskIds(); + } + await addBillabilityChart(currentDate, currentUser); } @@ -58,3 +67,13 @@ function getUser(userName?: string) { return api.getCurrentUser(); } } + +async function getBillabilityExcludedTaskIds(): Promise { + const tasks = (await api.getTasks()).filter( + (t) => t.tagNames?.includes(BILLABILITY_EXCLUDE_TAG), + ); + console.debug( + `Billability excluded tasks: ${tasks.map((t) => t.name).join(', ')}`, + ); + return tasks.map((t) => t.id); +}