From c0e42e23a692ce5f0baf38a9d23fa610fe55e982 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 18 Feb 2025 18:42:26 +0200 Subject: [PATCH] feat(tasks): create backend model for task --- src/becca/becca_loader.ts | 7 +++- src/becca/entities/btask.ts | 58 ++++++++++++++++++++++++++++++ src/becca/entities/rows.ts | 9 +++++ src/becca/entity_constructor.ts | 4 ++- src/services/consistency_checks.ts | 2 +- 5 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/becca/entities/btask.ts diff --git a/src/becca/becca_loader.ts b/src/becca/becca_loader.ts index 8397b2dec..862e3e8be 100644 --- a/src/becca/becca_loader.ts +++ b/src/becca/becca_loader.ts @@ -11,9 +11,10 @@ import BOption from "./entities/boption.js"; import BEtapiToken from "./entities/betapi_token.js"; import cls from "../services/cls.js"; import entityConstructor from "../becca/entity_constructor.js"; -import type { AttributeRow, BranchRow, EtapiTokenRow, NoteRow, OptionRow } from "./entities/rows.js"; +import type { AttributeRow, BranchRow, EtapiTokenRow, NoteRow, OptionRow, TaskRow } from "./entities/rows.js"; import type AbstractBeccaEntity from "./entities/abstract_becca_entity.js"; import ws from "../services/ws.js"; +import BTask from "./entities/btask.js"; const beccaLoaded = new Promise(async (res, rej) => { const sqlInit = (await import("../services/sql_init.js")).default; @@ -63,6 +64,10 @@ function load() { for (const row of sql.getRows(`SELECT etapiTokenId, name, tokenHash, utcDateCreated, utcDateModified FROM etapi_tokens WHERE isDeleted = 0`)) { new BEtapiToken(row); } + + for (const row of sql.getRows(`SELECT taskId, parentNoteId, title, dueDate, isDone, isDeleted FROM tasks WHERE isDeleted = 0`)) { + new BTask(row); + } }); for (const noteId in becca.notes) { diff --git a/src/becca/entities/btask.ts b/src/becca/entities/btask.ts new file mode 100644 index 000000000..a0c8ce232 --- /dev/null +++ b/src/becca/entities/btask.ts @@ -0,0 +1,58 @@ +import AbstractBeccaEntity from "./abstract_becca_entity.js"; +import type BOption from "./boption.js"; +import type { TaskRow } from "./rows.js"; + +export default class BTask extends AbstractBeccaEntity { + + static get entityName() { + return "tasks"; + } + + static get primaryKeyName() { + return "taskId"; + } + + static get hashedProperties() { + return [ "taskId", "parentNoteId", "title", "dueDate", "isDone", "isDeleted" ]; + } + + taskId!: string; + parentNoteId!: string; + title!: string; + dueDate?: string; + isDone!: boolean; + private _isDeleted?: boolean; + + constructor(row?: TaskRow) { + super(); + + if (!row) { + return; + } + + this.updateFromRow(row); + } + + get isDeleted() { + return !!this._isDeleted; + } + + updateFromRow(row: TaskRow) { + this.taskId = row.taskId; + this.parentNoteId = row.parentNoteId; + this.title = row.title; + this.dueDate = row.dueDate; + this._isDeleted = !!row.isDeleted; + } + + getPojo() { + return { + taskId: this.taskId, + parentNoteId: this.parentNoteId, + title: this.title, + dueDate: this.dueDate, + isDeleted: this.isDeleted + }; + } + +} diff --git a/src/becca/entities/rows.ts b/src/becca/entities/rows.ts index 89fa36953..08a296c7a 100644 --- a/src/becca/entities/rows.ts +++ b/src/becca/entities/rows.ts @@ -136,3 +136,12 @@ export interface NoteRow { utcDateModified: string; content?: string | Buffer; } + +export interface TaskRow { + taskId: string; + parentNoteId: string; + title: string; + dueDate?: string; + isDone: boolean; + isDeleted: boolean; +} diff --git a/src/becca/entity_constructor.ts b/src/becca/entity_constructor.ts index 18f7a14c7..5e9d3e013 100644 --- a/src/becca/entity_constructor.ts +++ b/src/becca/entity_constructor.ts @@ -9,6 +9,7 @@ import BNote from "./entities/bnote.js"; import BOption from "./entities/boption.js"; import BRecentNote from "./entities/brecent_note.js"; import BRevision from "./entities/brevision.js"; +import BTask from "./entities/btask.js"; type EntityClass = new (row?: any) => AbstractBeccaEntity; @@ -21,7 +22,8 @@ const ENTITY_NAME_TO_ENTITY: Record & EntityClass> notes: BNote, options: BOption, recent_notes: BRecentNote, - revisions: BRevision + revisions: BRevision, + tasks: BTask }; function getEntityFromEntityName(entityName: keyof typeof ENTITY_NAME_TO_ENTITY) { diff --git a/src/services/consistency_checks.ts b/src/services/consistency_checks.ts index 8e7da9c9e..ae678f807 100644 --- a/src/services/consistency_checks.ts +++ b/src/services/consistency_checks.ts @@ -888,7 +888,7 @@ class ConsistencyChecks { return `${tableName}: ${count}`; } - const tables = ["notes", "revisions", "attachments", "branches", "attributes", "etapi_tokens", "blobs"]; + const tables = ["notes", "revisions", "attachments", "branches", "attributes", "etapi_tokens", "blobs", "tasks"]; log.info(`Table counts: ${tables.map((tableName) => getTableRowCount(tableName)).join(", ")}`); }