Update reads to toggl database

This commit is contained in:
Joshua Coles 2024-03-04 19:46:07 +00:00
parent 50730a2b09
commit 1a14485c57
2 changed files with 47 additions and 48 deletions

21
src/data/db.d.ts vendored
View File

@ -1,4 +1,4 @@
import type {ColumnType, JSONColumnType} from "kysely";
import type {ColumnType} from "kysely";
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
@ -32,13 +32,13 @@ export interface Client {
export interface Project {
active: boolean;
client_id: number | null;
color: string;
id: Generated<number>;
name: string;
raw_json: JSONColumnType<{
color: string;
id: number;
name: string;
}>;
raw_json: Json;
server_created_at: Timestamp;
server_deleted_at: Timestamp | null;
server_updated_at: Timestamp;
toggl_id: Int8;
workspace_id: Int8;
}
@ -47,13 +47,12 @@ export interface TimeEntry {
description: string;
id: Generated<number>;
project_id: Int8 | null;
raw_json: JSONColumnType<{
start: string;
end: string;
seconds: number;
}>;
raw_json: Json;
server_deleted_at: Timestamp | null;
server_updated_at: Timestamp;
start: Timestamp;
stop: Timestamp;
tags: Generated<Json>;
toggl_id: Int8;
}

View File

@ -52,12 +52,12 @@ export async function getData(config: OverviewConfig): Promise<Data> {
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<Data> {
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)
})),
}
}