2024-07-24 00:12:45 +02:00
|
|
|
// thile file is responsible for managing boards within chats and tasks within boards.
|
2024-07-22 02:05:27 +02:00
|
|
|
|
2024-07-22 11:49:05 +02:00
|
|
|
import {
|
|
|
|
|
DATA_VERSION,
|
|
|
|
|
checkMatchesObjectStructure,
|
|
|
|
|
} from "../Utility/typeSafety";
|
2024-07-23 18:23:26 +02:00
|
|
|
import FileModel, { FileContent, FileModelSubPath } from "./fileModel";
|
2024-07-22 02:05:27 +02:00
|
|
|
|
2024-07-22 02:16:58 +02:00
|
|
|
import ChatModel from "../Chat/chatModel";
|
2024-07-22 19:52:08 +02:00
|
|
|
import { Color } from "../../colors";
|
2024-07-23 01:58:34 +02:00
|
|
|
import { HandlerManager } from "../Utility/utility";
|
2024-07-22 13:09:33 +02:00
|
|
|
import StorageModel from "../Global/storageModel";
|
2024-07-22 17:23:48 +02:00
|
|
|
import { v4 } from "uuid";
|
2024-07-22 02:16:58 +02:00
|
|
|
|
2024-07-24 00:12:45 +02:00
|
|
|
export default class BoardModel {
|
2024-07-22 13:09:33 +02:00
|
|
|
storageModel: StorageModel;
|
2024-07-22 02:16:58 +02:00
|
|
|
chatModel: ChatModel;
|
|
|
|
|
fileModel: FileModel;
|
|
|
|
|
|
2024-07-23 18:24:23 +02:00
|
|
|
// data
|
2024-07-23 01:58:34 +02:00
|
|
|
boardHandlerManager: HandlerManager<BoardInfoFileContent> =
|
|
|
|
|
new HandlerManager();
|
2024-07-23 01:40:30 +02:00
|
|
|
|
2024-07-22 12:39:26 +02:00
|
|
|
// paths
|
|
|
|
|
getBasePath = (): string[] => {
|
2024-07-23 18:23:26 +02:00
|
|
|
return this.fileModel.getModelContainerPath(FileModelSubPath.ModelTask);
|
2024-07-22 12:39:26 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-23 21:33:22 +02:00
|
|
|
getViewPath = (): string[] => {
|
|
|
|
|
return [...this.getBasePath(), FileModelSubPath.ModelView];
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
getBoardFilePath = (boardId: string): string[] => {
|
2024-07-22 18:09:58 +02:00
|
|
|
return [...this.fileModel.getFilePath(boardId)];
|
2024-07-22 12:39:26 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 19:28:39 +02:00
|
|
|
getTaskFilePath = (taskId: string): string[] => {
|
|
|
|
|
return [...this.fileModel.getFilePath(taskId)];
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
getBoardContainerPath = (): string[] => {
|
2024-07-23 18:23:26 +02:00
|
|
|
return [...this.getBasePath(), TaskModelSubPaths.Boards];
|
2024-07-22 12:39:26 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
getBoardDirectoryPath = (boardId: string): string[] => {
|
|
|
|
|
return [...this.getBoardContainerPath(), boardId];
|
2024-07-22 14:36:03 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
getTaskContainerPath = (boardId: string): string[] => {
|
2024-07-23 18:23:26 +02:00
|
|
|
return [
|
|
|
|
|
...this.getBoardDirectoryPath(boardId),
|
|
|
|
|
TaskModelSubPaths.BoardTasks,
|
|
|
|
|
];
|
2024-07-22 14:36:03 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 19:28:39 +02:00
|
|
|
getTaskReferencePath = (boardId: string, fileId: string): string[] => {
|
2024-07-22 17:23:48 +02:00
|
|
|
return [...this.getTaskContainerPath(boardId), fileId];
|
2024-07-22 12:39:26 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 23:33:57 +02:00
|
|
|
// handlers
|
2024-07-22 18:11:18 +02:00
|
|
|
handleFileContent = (fileContent: FileContent<string>): void => {
|
2024-07-22 11:49:05 +02:00
|
|
|
if (
|
2024-07-22 18:11:18 +02:00
|
|
|
checkMatchesObjectStructure(fileContent, BoardInfoFileContentReference) ==
|
2024-07-22 14:36:03 +02:00
|
|
|
true
|
|
|
|
|
) {
|
2024-07-22 19:28:39 +02:00
|
|
|
this.handleBoard(fileContent as BoardInfoFileContent);
|
2024-07-22 14:36:03 +02:00
|
|
|
} else if (
|
2024-07-22 18:11:18 +02:00
|
|
|
checkMatchesObjectStructure(fileContent, TaskFileContentReference) == true
|
2024-07-22 14:36:03 +02:00
|
|
|
) {
|
2024-07-22 18:11:18 +02:00
|
|
|
this.handleTask(fileContent as TaskFileContent);
|
2024-07-22 14:36:03 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 19:28:39 +02:00
|
|
|
handleBoard = (boardInfoFileContent: BoardInfoFileContent) => {
|
2024-07-23 01:40:30 +02:00
|
|
|
this.updateBoard(boardInfoFileContent);
|
2024-07-22 17:23:48 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 19:28:39 +02:00
|
|
|
handleTask = (taskFileContent: TaskFileContent) => {
|
|
|
|
|
this.storeTask(taskFileContent);
|
|
|
|
|
};
|
2024-07-22 17:23:48 +02:00
|
|
|
|
2024-07-22 14:36:03 +02:00
|
|
|
// boards
|
2024-07-23 00:32:29 +02:00
|
|
|
createBoard = (name: string): BoardInfoFileContent => {
|
2024-07-22 19:28:39 +02:00
|
|
|
const boardInfoFileContent: BoardInfoFileContent =
|
2024-07-24 00:12:45 +02:00
|
|
|
BoardModel.createBoardInfoFileContent(v4(), name, Color.Standard);
|
2024-07-22 19:28:39 +02:00
|
|
|
this.updateBoard(boardInfoFileContent);
|
2024-07-23 00:32:29 +02:00
|
|
|
return boardInfoFileContent;
|
2024-07-22 13:09:33 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 19:28:39 +02:00
|
|
|
updateBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
|
|
|
|
|
this.storeBoard(boardInfoFileContent);
|
|
|
|
|
this.chatModel.sendMessage("", boardInfoFileContent);
|
2024-07-23 01:58:34 +02:00
|
|
|
this.boardHandlerManager.trigger(boardInfoFileContent);
|
2024-07-22 13:09:33 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 19:28:39 +02:00
|
|
|
storeBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
|
2024-07-22 17:23:48 +02:00
|
|
|
// store info
|
2024-07-22 18:11:18 +02:00
|
|
|
this.fileModel.addFileContent(boardInfoFileContent);
|
2024-07-22 17:23:48 +02:00
|
|
|
|
|
|
|
|
// add to list
|
|
|
|
|
const boardDirectoryPath: string[] = this.getBoardDirectoryPath(
|
2024-07-22 18:11:18 +02:00
|
|
|
boardInfoFileContent.fileId
|
2024-07-22 17:23:48 +02:00
|
|
|
);
|
|
|
|
|
this.storageModel.write(boardDirectoryPath, "");
|
2024-07-22 13:09:33 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 18:09:58 +02:00
|
|
|
deleteBoard = (boardId: string): void => {
|
|
|
|
|
const boardFilePath: string[] = this.getBoardFilePath(boardId);
|
|
|
|
|
const boardDirectoryPath: string[] = this.getBoardDirectoryPath(boardId);
|
|
|
|
|
|
|
|
|
|
this.storageModel.removeRecursively(boardFilePath);
|
|
|
|
|
this.storageModel.removeRecursively(boardDirectoryPath);
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 19:28:39 +02:00
|
|
|
listBoardIds = (): string[] => {
|
|
|
|
|
const boardContainerPath: string[] = this.getBoardContainerPath();
|
2024-07-23 00:32:29 +02:00
|
|
|
const boardIds: string[] = this.storageModel.list(boardContainerPath);
|
|
|
|
|
return boardIds;
|
2024-07-22 19:28:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getBoardInfo = (fileId: string): BoardInfoFileContent | null => {
|
|
|
|
|
const boardInfoFileContentOrNull: BoardInfoFileContent | null =
|
|
|
|
|
this.fileModel.getLatestFileContent(
|
|
|
|
|
fileId,
|
|
|
|
|
BoardInfoFileContentReference
|
|
|
|
|
);
|
|
|
|
|
return boardInfoFileContentOrNull;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 14:36:03 +02:00
|
|
|
//tasks
|
|
|
|
|
createTask = (boardId: string, name: string): void => {
|
2024-07-24 00:12:45 +02:00
|
|
|
const taskFileContent: TaskFileContent = BoardModel.createTaskFileContent(
|
2024-07-22 17:23:48 +02:00
|
|
|
v4(),
|
2024-07-22 19:28:39 +02:00
|
|
|
name,
|
|
|
|
|
boardId
|
2024-07-22 14:36:03 +02:00
|
|
|
);
|
2024-07-22 19:28:39 +02:00
|
|
|
this.updateTask(taskFileContent);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updateTask = (taskFileContent: TaskFileContent): void => {
|
|
|
|
|
this.storeTask(taskFileContent);
|
|
|
|
|
this.chatModel.sendMessage("", taskFileContent);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
storeTask = (taskFileContent: TaskFileContent): void => {
|
|
|
|
|
// store info
|
2024-07-22 18:11:18 +02:00
|
|
|
this.fileModel.addFileContent(taskFileContent);
|
2024-07-22 19:28:39 +02:00
|
|
|
|
|
|
|
|
// add to board
|
|
|
|
|
const taskReferencePath: string[] = this.getTaskReferencePath(
|
|
|
|
|
taskFileContent.boardId,
|
|
|
|
|
taskFileContent.fileId
|
|
|
|
|
);
|
|
|
|
|
console.log("storing", taskReferencePath);
|
|
|
|
|
this.storageModel.write(taskReferencePath, "");
|
2024-07-22 13:09:33 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 19:28:39 +02:00
|
|
|
listTaskIds = (boardId: string): string[] => {
|
|
|
|
|
const taskContainerPath: string[] = this.getTaskContainerPath(boardId);
|
|
|
|
|
console.log("listing", taskContainerPath);
|
|
|
|
|
const fileIds: string[] = this.storageModel.list(taskContainerPath);
|
2024-07-22 13:09:33 +02:00
|
|
|
return fileIds;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 19:28:39 +02:00
|
|
|
deleteTask = (boardId: string, taskId: string): void => {
|
|
|
|
|
const taskFilePath: string[] = this.getTaskFilePath(taskId);
|
|
|
|
|
const taskReferencePath: string[] = this.getTaskReferencePath(
|
|
|
|
|
boardId,
|
|
|
|
|
taskId
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.storageModel.removeRecursively(taskFilePath);
|
|
|
|
|
this.storageModel.removeRecursively(taskReferencePath);
|
2024-07-22 15:07:31 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 02:16:58 +02:00
|
|
|
// init
|
2024-07-22 13:09:33 +02:00
|
|
|
constructor(
|
|
|
|
|
storageModel: StorageModel,
|
|
|
|
|
chatModel: ChatModel,
|
|
|
|
|
fileModel: FileModel
|
|
|
|
|
) {
|
2024-07-22 02:16:58 +02:00
|
|
|
this.chatModel = chatModel;
|
|
|
|
|
this.fileModel = fileModel;
|
2024-07-22 13:09:33 +02:00
|
|
|
this.storageModel = storageModel;
|
2024-07-23 01:40:30 +02:00
|
|
|
|
|
|
|
|
// handlers
|
2024-07-23 01:58:34 +02:00
|
|
|
fileModel.fileContentHandlerManager.addHandler(this.handleFileContent);
|
2024-07-22 02:16:58 +02:00
|
|
|
}
|
2024-07-22 11:49:05 +02:00
|
|
|
|
|
|
|
|
// utility
|
2024-07-22 18:11:18 +02:00
|
|
|
static createBoardInfoFileContent = (
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId: string,
|
2024-07-22 14:36:03 +02:00
|
|
|
name: string,
|
|
|
|
|
color: Color
|
2024-07-22 19:28:39 +02:00
|
|
|
): BoardInfoFileContent => {
|
2024-07-22 18:11:18 +02:00
|
|
|
const fileContent: FileContent<"board-info"> = FileModel.createFileContent(
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId,
|
|
|
|
|
"board-info"
|
|
|
|
|
);
|
2024-07-22 14:36:03 +02:00
|
|
|
return {
|
2024-07-22 18:11:18 +02:00
|
|
|
...fileContent,
|
2024-07-22 14:36:03 +02:00
|
|
|
|
|
|
|
|
name,
|
|
|
|
|
color,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
static createTaskFileContent = (
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId: string,
|
2024-07-22 11:49:05 +02:00
|
|
|
name: string,
|
2024-07-22 14:36:03 +02:00
|
|
|
boardId: string
|
2024-07-22 18:11:18 +02:00
|
|
|
): TaskFileContent => {
|
|
|
|
|
const fileContent: FileContent<"task"> = FileModel.createFileContent(
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId,
|
|
|
|
|
"task"
|
|
|
|
|
);
|
2024-07-22 11:49:05 +02:00
|
|
|
return {
|
2024-07-22 18:11:18 +02:00
|
|
|
...fileContent,
|
2024-07-22 11:49:05 +02:00
|
|
|
|
|
|
|
|
name,
|
2024-07-22 14:36:03 +02:00
|
|
|
boardId,
|
2024-07-22 11:49:05 +02:00
|
|
|
};
|
|
|
|
|
};
|
2024-07-22 02:16:58 +02:00
|
|
|
}
|
2024-07-22 02:05:27 +02:00
|
|
|
|
2024-07-23 18:23:26 +02:00
|
|
|
export enum TaskModelSubPaths {
|
|
|
|
|
Boards = "boards",
|
|
|
|
|
BoardTasks = "tasks",
|
2024-07-23 21:33:22 +02:00
|
|
|
}
|
2024-07-22 12:39:26 +02:00
|
|
|
|
2024-07-22 02:05:27 +02:00
|
|
|
// types
|
2024-07-22 19:28:39 +02:00
|
|
|
export interface BoardInfoFileContent extends FileContent<"board-info"> {
|
2024-07-22 14:36:03 +02:00
|
|
|
name: string;
|
|
|
|
|
color: Color;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
export interface TaskFileContent extends FileContent<"task"> {
|
2024-07-22 11:49:05 +02:00
|
|
|
name: string;
|
2024-07-22 14:36:03 +02:00
|
|
|
boardId: string;
|
2024-07-22 11:49:05 +02:00
|
|
|
|
2024-07-22 02:16:58 +02:00
|
|
|
category?: string;
|
|
|
|
|
status?: string;
|
|
|
|
|
|
|
|
|
|
description?: string;
|
|
|
|
|
|
|
|
|
|
priority?: number;
|
|
|
|
|
date?: string;
|
|
|
|
|
time?: string;
|
|
|
|
|
}
|
2024-07-22 11:20:43 +02:00
|
|
|
|
2024-07-22 12:22:52 +02:00
|
|
|
// reference
|
2024-07-22 19:28:39 +02:00
|
|
|
export const BoardInfoFileContentReference: BoardInfoFileContent = {
|
2024-07-22 11:20:43 +02:00
|
|
|
dataVersion: DATA_VERSION,
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId: "string",
|
2024-07-22 18:11:18 +02:00
|
|
|
fileContentId: "",
|
2024-07-22 11:20:43 +02:00
|
|
|
creationDate: "",
|
2024-07-22 17:23:48 +02:00
|
|
|
|
|
|
|
|
type: "board-info",
|
2024-07-22 11:49:05 +02:00
|
|
|
|
|
|
|
|
name: "",
|
2024-07-22 14:36:03 +02:00
|
|
|
color: "" as Color,
|
2024-07-22 11:20:43 +02:00
|
|
|
};
|
2024-07-22 12:22:52 +02:00
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
export const TaskFileContentReference: TaskFileContent = {
|
2024-07-22 12:22:52 +02:00
|
|
|
dataVersion: DATA_VERSION,
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId: "string",
|
2024-07-22 18:11:18 +02:00
|
|
|
fileContentId: "",
|
2024-07-22 14:36:03 +02:00
|
|
|
creationDate: "",
|
|
|
|
|
type: "task",
|
|
|
|
|
|
|
|
|
|
name: "",
|
|
|
|
|
boardId: "",
|
2024-07-22 15:07:31 +02:00
|
|
|
};
|