Files
CT/src/Model/Files/taskModel.ts
T

259 lines
6.5 KiB
TypeScript
Raw Normal View History

2024-07-22 02:05:27 +02:00
// thile file is responsible for managing task files within chats.
2024-07-22 11:49:05 +02:00
import {
DATA_VERSION,
checkMatchesObjectStructure,
} from "../Utility/typeSafety";
2024-07-22 18:11:18 +02:00
import FileModel, { FileContent } 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-22 13:09:33 +02:00
import StorageModel from "../Global/storageModel";
import { v4 } from "uuid";
2024-07-22 02:16:58 +02:00
export default class TaskModel {
2024-07-22 13:09:33 +02:00
storageModel: StorageModel;
2024-07-22 02:16:58 +02:00
chatModel: ChatModel;
fileModel: FileModel;
2024-07-22 12:39:26 +02:00
// paths
getBasePath = (): string[] => {
return this.fileModel.getModelContainerPath("taskModel");
2024-07-22 12:39:26 +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)];
};
getBoardContainerPath = (): string[] => {
return [...this.getBasePath(), subDirectories.boards];
2024-07-22 12:39:26 +02:00
};
getBoardDirectoryPath = (boardId: string): string[] => {
return [...this.getBoardContainerPath(), boardId];
2024-07-22 14:36:03 +02:00
};
getTaskContainerPath = (boardId: string): string[] => {
return [...this.getBoardDirectoryPath(boardId), subDirectories.boardTasks];
2024-07-22 14:36:03 +02:00
};
2024-07-22 19:28:39 +02:00
getTaskReferencePath = (boardId: string, fileId: string): string[] => {
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) => {
this.storeBoard(boardInfoFileContent);
};
2024-07-22 19:28:39 +02:00
handleTask = (taskFileContent: TaskFileContent) => {
this.storeTask(taskFileContent);
};
2024-07-22 14:36:03 +02:00
// boards
createBoard = (name: string): void => {
2024-07-22 19:28:39 +02:00
const boardInfoFileContent: BoardInfoFileContent =
2024-07-22 18:11:18 +02:00
TaskModel.createBoardInfoFileContent(v4(), name, Color.Standard);
2024-07-22 19:28:39 +02:00
this.updateBoard(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-22 13:09:33 +02:00
};
2024-07-22 19:28:39 +02:00
storeBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
// store info
2024-07-22 18:11:18 +02:00
this.fileModel.addFileContent(boardInfoFileContent);
// add to list
const boardDirectoryPath: string[] = this.getBoardDirectoryPath(
2024-07-22 18:11:18 +02:00
boardInfoFileContent.fileId
);
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();
const boardNames: string[] = this.storageModel.list(boardContainerPath);
return boardNames;
};
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-22 18:11:18 +02:00
const taskFileContent: TaskFileContent = TaskModel.createTaskFileContent(
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-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 = (
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(
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 = (
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(
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-22 12:39:26 +02:00
export const subDirectories = {
boards: "boards",
2024-07-22 14:36:03 +02:00
boardTasks: "tasks",
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,
fileId: "string",
2024-07-22 18:11:18 +02:00
fileContentId: "",
2024-07-22 11:20:43 +02:00
creationDate: "",
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,
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
};