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

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

@ -1,8 +1,8 @@
import type {ColumnType, JSONColumnType} from "kysely"; import type {ColumnType} from "kysely";
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U> export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U> ? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>; : ColumnType<T, T | undefined, T>;
export type Int8 = ColumnType<string, bigint | number | string, bigint | number | string>; export type Int8 = ColumnType<string, bigint | number | string, bigint | number | string>;
@ -11,7 +11,7 @@ export type Json = ColumnType<JsonValue, string, string>;
export type JsonArray = JsonValue[]; export type JsonArray = JsonValue[];
export type JsonObject = { export type JsonObject = {
[K in string]?: JsonValue; [K in string]?: JsonValue;
}; };
export type JsonPrimitive = boolean | number | string | null; export type JsonPrimitive = boolean | number | string | null;
@ -21,50 +21,49 @@ export type JsonValue = JsonArray | JsonObject | JsonPrimitive;
export type Timestamp = ColumnType<Date, Date | string, Date | string>; export type Timestamp = ColumnType<Date, Date | string, Date | string>;
export interface Client { export interface Client {
archived: boolean; archived: boolean;
at: Timestamp; at: Timestamp;
id: number; id: number;
name: string; name: string;
server_deleted_at: Timestamp | null; server_deleted_at: Timestamp | null;
workspace_id: number; workspace_id: number;
} }
export interface Project { export interface Project {
active: boolean; active: boolean;
client_id: number | null; client_id: number | null;
id: Generated<number>; color: string;
name: string; id: Generated<number>;
raw_json: JSONColumnType<{ name: string;
color: string; raw_json: Json;
id: number; server_created_at: Timestamp;
name: string; server_deleted_at: Timestamp | null;
}>; server_updated_at: Timestamp;
toggl_id: Int8; toggl_id: Int8;
workspace_id: Int8; workspace_id: Int8;
} }
export interface TimeEntry { export interface TimeEntry {
description: string; description: string;
id: Generated<number>; id: Generated<number>;
project_id: Int8 | null; project_id: Int8 | null;
raw_json: JSONColumnType<{ raw_json: Json;
start: string; server_deleted_at: Timestamp | null;
end: string; server_updated_at: Timestamp;
seconds: number; start: Timestamp;
}>; stop: Timestamp;
start: Timestamp; tags: Generated<Json>;
stop: Timestamp; toggl_id: Int8;
toggl_id: Int8;
} }
export interface TogglPortalSeaqlMigrations { export interface TogglPortalSeaqlMigrations {
applied_at: Int8; applied_at: Int8;
version: string; version: string;
} }
export interface DB { export interface DB {
client: Client; client: Client;
project: Project; project: Project;
time_entry: TimeEntry; time_entry: TimeEntry;
toggl_portal_seaql_migrations: TogglPortalSeaqlMigrations; toggl_portal_seaql_migrations: TogglPortalSeaqlMigrations;
} }

View File

@ -52,12 +52,12 @@ export async function getData(config: OverviewConfig): Promise<Data> {
let projectIds = config.subjects.map((subject) => subject.projectId.toString()); let projectIds = config.subjects.map((subject) => subject.projectId.toString());
const projects = await db.selectFrom('project') const projects = await db.selectFrom('project')
.select('raw_json') .select(['toggl_id', 'name', 'color'])
.where('project.toggl_id', 'in', projectIds) .where('toggl_id', 'in', projectIds)
.execute(); .execute()
const timeEntries = await db.selectFrom('time_entry') const timeEntries = await db.selectFrom('time_entry')
.select(['project_id', 'raw_json']) .select(['project_id', 'start', 'stop'])
.where('project_id', 'in', projectIds) .where('project_id', 'in', projectIds)
.where('start', '>', dFns.parseISO(config.timePeriod.start)) .where('start', '>', dFns.parseISO(config.timePeriod.start))
.where('start', '<', dFns.parseISO(config.timePeriod.end)) .where('start', '<', dFns.parseISO(config.timePeriod.end))
@ -65,16 +65,16 @@ export async function getData(config: OverviewConfig): Promise<Data> {
return { return {
projects: projects.map((project) => ({ projects: projects.map((project) => ({
projectId: project.raw_json.id, projectId: parseInt(project.toggl_id),
name: project.raw_json.name, name: project.name,
color: project.raw_json.color, color: project.color,
})), })),
timeEntries: timeEntries.map((timeEntry) => ({ timeEntries: timeEntries.map((timeEntry) => ({
projectId: parseInt(timeEntry.project_id!), projectId: parseInt(timeEntry.project_id!),
start: timeEntry.raw_json.start, start: dFns.formatISO(timeEntry.start),
end: timeEntry.raw_json.end, end: dFns.formatISO(timeEntry.stop),
duration: timeEntry.raw_json.seconds, duration: dFns.differenceInSeconds(timeEntry.stop, timeEntry.start)
})), })),
} }
} }