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,
|
2024-07-22 12:22:52 +02:00
|
|
|
ValidObject,
|
2024-07-22 11:49:05 +02:00
|
|
|
checkMatchesObjectStructure,
|
|
|
|
|
} from "../Utility/typeSafety";
|
2024-07-22 13:09:33 +02:00
|
|
|
import FileModel, {
|
|
|
|
|
File,
|
|
|
|
|
FileContent,
|
|
|
|
|
FileContentReference,
|
|
|
|
|
} 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 14:36:03 +02:00
|
|
|
import { Color } from "../../ViewModel/colors";
|
2024-07-22 13:09:33 +02:00
|
|
|
import StorageModel from "../Global/storageModel";
|
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.getModelDirectoryPath("taskModel");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getLocationDirPath = (): string[] => {
|
|
|
|
|
return [...this.getBasePath(), subDirectories.locations];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getTaskLocationPath = (fileId: string): string[] => {
|
|
|
|
|
return [...this.getLocationDirPath(), fileId];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getBoardDirPath = (): string[] => {
|
|
|
|
|
return [...this.getBasePath(), subDirectories.boards];
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 14:36:03 +02:00
|
|
|
getBoardPath = (boardId: string): string[] => {
|
|
|
|
|
return [...this.getBoardDirPath(), boardId];
|
2024-07-22 12:39:26 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 14:36:03 +02:00
|
|
|
getBoardInfoPath = (boardId: string): string[] => {
|
|
|
|
|
return [...this.getBoardPath(boardId), subDirectories.boardInfo];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getTaskDirPath = (boardId: string): string[] => {
|
|
|
|
|
return [...this.getBoardPath(boardId), subDirectories.boardTasks];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getTaskPath = (boardId: string, fileId: string): string[] => {
|
|
|
|
|
return [...this.getTaskDirPath(boardId), fileId];
|
2024-07-22 12:39:26 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 13:09:33 +02:00
|
|
|
getTaskContentPath = (
|
2024-07-22 14:36:03 +02:00
|
|
|
boardId: string,
|
2024-07-22 13:09:33 +02:00
|
|
|
fileId: string,
|
2024-07-22 14:36:03 +02:00
|
|
|
contentName: string
|
2024-07-22 13:09:33 +02:00
|
|
|
): string[] => {
|
2024-07-22 14:36:03 +02:00
|
|
|
return [...this.getTaskPath(boardId, fileId), contentName];
|
2024-07-22 13:09:33 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 11:49:05 +02:00
|
|
|
// handler
|
2024-07-22 14:36:03 +02:00
|
|
|
handleFileContent = (file: File, fileContent: FileContent<string>): void => {
|
2024-07-22 11:49:05 +02:00
|
|
|
if (
|
2024-07-22 14:36:03 +02:00
|
|
|
checkMatchesObjectStructure(fileContent, BoardInfoFileContentReference) ==
|
|
|
|
|
true
|
|
|
|
|
) {
|
|
|
|
|
// todo
|
|
|
|
|
} else if (
|
|
|
|
|
checkMatchesObjectStructure(fileContent, TaskFileContentReference) == true
|
|
|
|
|
) {
|
|
|
|
|
// todo
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// boards
|
|
|
|
|
createBoard = (name: string): void => {
|
2024-07-22 11:49:05 +02:00
|
|
|
const file: File = FileModel.createFile();
|
2024-07-22 14:36:03 +02:00
|
|
|
const boardInfoFileContent: BoardInfoFileContent =
|
|
|
|
|
TaskModel.createBoardInfoFileContent(name, Color.Standard);
|
|
|
|
|
this.fileModel.addOrUpdateFile(file, boardInfoFileContent);
|
2024-07-22 13:09:33 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 14:36:03 +02:00
|
|
|
listBoardIds = (): string[] => {
|
|
|
|
|
const boardDirPath: string[] = this.getBoardDirPath();
|
|
|
|
|
const boardNames: string[] = this.storageModel.list(boardDirPath);
|
|
|
|
|
return boardNames;
|
2024-07-22 13:09:33 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 14:36:03 +02:00
|
|
|
getBoardInfo = (boardId: string): BoardInfoFileContent | null => {
|
|
|
|
|
const boardInfoPath: string[] = this.getBoardInfoPath(boardId);
|
|
|
|
|
const boardInfoFileContentOrNull: BoardInfoFileContent | null =
|
|
|
|
|
this.storageModel.readStringifiable(
|
|
|
|
|
boardInfoPath,
|
|
|
|
|
BoardInfoFileContentReference
|
|
|
|
|
);
|
|
|
|
|
return boardInfoFileContentOrNull;
|
2024-07-22 13:09:33 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 14:36:03 +02:00
|
|
|
//tasks
|
|
|
|
|
createTask = (boardId: string, name: string): void => {
|
|
|
|
|
const file: File = FileModel.createFile();
|
|
|
|
|
const taskFileContent: TaskFileContent = TaskModel.createTaskFileContent(
|
|
|
|
|
boardId,
|
|
|
|
|
name
|
|
|
|
|
);
|
|
|
|
|
this.fileModel.addOrUpdateFile(file, taskFileContent);
|
2024-07-22 13:09:33 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 14:36:03 +02:00
|
|
|
listTaskIds = (boardName: string): string[] => {
|
|
|
|
|
const getTaskDirPath: string[] = this.getTaskDirPath(boardName);
|
|
|
|
|
const fileIds: string[] = this.storageModel.list(getTaskDirPath);
|
2024-07-22 13:09:33 +02:00
|
|
|
return fileIds;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getTaskVersionNames = (boardName: string, fileId: string): string[] => {
|
|
|
|
|
const fileDirectoryPath: string[] = this.getTaskPath(boardName, fileId);
|
|
|
|
|
const versionNames: string[] = this.storageModel.list(fileDirectoryPath);
|
|
|
|
|
return versionNames;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getTaskFileContent = (
|
|
|
|
|
boardName: string,
|
|
|
|
|
fileId: string,
|
|
|
|
|
versionName: string
|
|
|
|
|
): TaskFileContent | null => {
|
|
|
|
|
const fileContentPath: string[] = this.getTaskContentPath(
|
|
|
|
|
boardName,
|
|
|
|
|
fileId,
|
|
|
|
|
versionName
|
|
|
|
|
);
|
|
|
|
|
const taskFileContentOrNull: TaskFileContent | null =
|
|
|
|
|
this.storageModel.readStringifiable(
|
|
|
|
|
fileContentPath,
|
|
|
|
|
TaskFileContentReference
|
|
|
|
|
);
|
|
|
|
|
return taskFileContentOrNull;
|
|
|
|
|
};
|
2024-07-22 12:39:26 +02:00
|
|
|
|
2024-07-22 14:36:03 +02:00
|
|
|
moveTask = (taskFileId: string, oldBoardId: string, newBoardId: string): boolean => {
|
|
|
|
|
const sourcePath: string[] = this.getTaskPath(oldBoardId, taskFileId);
|
|
|
|
|
const destinationPath: string[] = this.getTaskPath(newBoardId, taskFileId);
|
|
|
|
|
return this.storageModel.rename(sourcePath, destinationPath);
|
|
|
|
|
}
|
|
|
|
|
|
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 14:36:03 +02:00
|
|
|
static createBoardInfoFileContent = (
|
|
|
|
|
name: string,
|
|
|
|
|
color: Color
|
|
|
|
|
): BoardInfoFileContent => {
|
|
|
|
|
const fileContent: FileContent<"board"> =
|
|
|
|
|
FileModel.createFileContent("board");
|
|
|
|
|
return {
|
|
|
|
|
...fileContent,
|
|
|
|
|
|
|
|
|
|
name,
|
|
|
|
|
color,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 11:49:05 +02:00
|
|
|
static createTaskFileContent = (
|
|
|
|
|
name: string,
|
2024-07-22 14:36:03 +02:00
|
|
|
boardId: string
|
2024-07-22 11:49:05 +02:00
|
|
|
): TaskFileContent => {
|
|
|
|
|
const fileContent: FileContent<"task"> =
|
|
|
|
|
FileModel.createFileContent("task");
|
|
|
|
|
return {
|
|
|
|
|
...fileContent,
|
|
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
|
locations: "locations",
|
|
|
|
|
boards: "boards",
|
2024-07-22 14:36:03 +02:00
|
|
|
boardInfo: "info",
|
|
|
|
|
boardTasks: "tasks",
|
2024-07-22 12:39:26 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 02:05:27 +02:00
|
|
|
// types
|
2024-07-22 14:36:03 +02:00
|
|
|
export interface BoardInfoFileContent extends FileContent<"board"> {
|
|
|
|
|
name: string;
|
|
|
|
|
color: Color;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-22 11:49:05 +02:00
|
|
|
export interface TaskFileContent extends FileContent<"task"> {
|
|
|
|
|
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 14:36:03 +02:00
|
|
|
export const BoardInfoFileContentReference: BoardInfoFileContent = {
|
2024-07-22 11:20:43 +02:00
|
|
|
dataVersion: DATA_VERSION,
|
|
|
|
|
|
|
|
|
|
id: "",
|
|
|
|
|
creationDate: "",
|
2024-07-22 14:36:03 +02:00
|
|
|
type: "board",
|
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 14:36:03 +02:00
|
|
|
export const TaskFileContentReference: TaskFileContent = {
|
2024-07-22 12:22:52 +02:00
|
|
|
dataVersion: DATA_VERSION,
|
|
|
|
|
|
2024-07-22 14:36:03 +02:00
|
|
|
id: "",
|
|
|
|
|
creationDate: "",
|
|
|
|
|
type: "task",
|
|
|
|
|
|
|
|
|
|
name: "",
|
|
|
|
|
boardId: "",
|
|
|
|
|
};
|