From 1a14485c57b7db1d4a2094d503f49f98e5f75397 Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Mon, 4 Mar 2024 19:46:07 +0000 Subject: [PATCH] Update reads to toggl database --- src/data/db.d.ts | 75 +++++++++++++++++++++---------------------- src/data/fetchData.ts | 20 ++++++------ 2 files changed, 47 insertions(+), 48 deletions(-) diff --git a/src/data/db.d.ts b/src/data/db.d.ts index 96df047..28d8142 100644 --- a/src/data/db.d.ts +++ b/src/data/db.d.ts @@ -1,8 +1,8 @@ -import type {ColumnType, JSONColumnType} from "kysely"; +import type {ColumnType} from "kysely"; export type Generated = T extends ColumnType - ? ColumnType - : ColumnType; + ? ColumnType + : ColumnType; export type Int8 = ColumnType; @@ -11,7 +11,7 @@ export type Json = ColumnType; export type JsonArray = JsonValue[]; export type JsonObject = { - [K in string]?: JsonValue; + [K in string]?: JsonValue; }; export type JsonPrimitive = boolean | number | string | null; @@ -21,50 +21,49 @@ export type JsonValue = JsonArray | JsonObject | JsonPrimitive; export type Timestamp = ColumnType; export interface Client { - archived: boolean; - at: Timestamp; - id: number; - name: string; - server_deleted_at: Timestamp | null; - workspace_id: number; + archived: boolean; + at: Timestamp; + id: number; + name: string; + server_deleted_at: Timestamp | null; + workspace_id: number; } export interface Project { - active: boolean; - client_id: number | null; - id: Generated; - name: string; - raw_json: JSONColumnType<{ - color: string; - id: number; - name: string; - }>; - toggl_id: Int8; - workspace_id: Int8; + active: boolean; + client_id: number | null; + color: string; + id: Generated; + name: string; + raw_json: Json; + server_created_at: Timestamp; + server_deleted_at: Timestamp | null; + server_updated_at: Timestamp; + toggl_id: Int8; + workspace_id: Int8; } export interface TimeEntry { - description: string; - id: Generated; - project_id: Int8 | null; - raw_json: JSONColumnType<{ - start: string; - end: string; - seconds: number; - }>; - start: Timestamp; - stop: Timestamp; - toggl_id: Int8; + description: string; + id: Generated; + project_id: Int8 | null; + raw_json: Json; + server_deleted_at: Timestamp | null; + server_updated_at: Timestamp; + start: Timestamp; + stop: Timestamp; + tags: Generated; + toggl_id: Int8; } export interface TogglPortalSeaqlMigrations { - applied_at: Int8; - version: string; + applied_at: Int8; + version: string; } export interface DB { - client: Client; - project: Project; - time_entry: TimeEntry; - toggl_portal_seaql_migrations: TogglPortalSeaqlMigrations; + client: Client; + project: Project; + time_entry: TimeEntry; + toggl_portal_seaql_migrations: TogglPortalSeaqlMigrations; } diff --git a/src/data/fetchData.ts b/src/data/fetchData.ts index 212ad11..a2bce6e 100644 --- a/src/data/fetchData.ts +++ b/src/data/fetchData.ts @@ -52,12 +52,12 @@ export async function getData(config: OverviewConfig): Promise { let projectIds = config.subjects.map((subject) => subject.projectId.toString()); const projects = await db.selectFrom('project') - .select('raw_json') - .where('project.toggl_id', 'in', projectIds) - .execute(); + .select(['toggl_id', 'name', 'color']) + .where('toggl_id', 'in', projectIds) + .execute() const timeEntries = await db.selectFrom('time_entry') - .select(['project_id', 'raw_json']) + .select(['project_id', 'start', 'stop']) .where('project_id', 'in', projectIds) .where('start', '>', dFns.parseISO(config.timePeriod.start)) .where('start', '<', dFns.parseISO(config.timePeriod.end)) @@ -65,16 +65,16 @@ export async function getData(config: OverviewConfig): Promise { return { projects: projects.map((project) => ({ - projectId: project.raw_json.id, - name: project.raw_json.name, - color: project.raw_json.color, + projectId: parseInt(project.toggl_id), + name: project.name, + color: project.color, })), timeEntries: timeEntries.map((timeEntry) => ({ projectId: parseInt(timeEntry.project_id!), - start: timeEntry.raw_json.start, - end: timeEntry.raw_json.end, - duration: timeEntry.raw_json.seconds, + start: dFns.formatISO(timeEntry.start), + end: dFns.formatISO(timeEntry.stop), + duration: dFns.differenceInSeconds(timeEntry.stop, timeEntry.start) })), } }