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

215 lines
5.2 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,
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 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];
};
getBoardPath = (boardName: string): string[] => {
return [...this.getBoardDirPath(), boardName];
};
getTaskPath = (boardName: string, fileId: string): string[] => {
return [...this.getBoardPath(boardName), fileId];
};
2024-07-22 13:09:33 +02:00
getTaskContentPath = (
boardName: string,
fileId: string,
contentName
): string[] => {
return [...this.getTaskPath(boardName, fileId), contentName];
};
2024-07-22 11:49:05 +02:00
// handler
handleTaskFileContent = (
file: File,
fileContent: FileContent<string>
): void => {
if (
checkMatchesObjectStructure(fileContent, TaskFileContentReference) ==
false
)
return;
// TODO handle
};
// methods
createTask = (board: string, name: string): void => {
const file: File = FileModel.createFile();
const taskFileContent: TaskFileContent = TaskModel.createTaskFileContent(
board,
name
);
this.saveTask(file, taskFileContent);
};
saveTask = (file: File, newTaskFileContent: TaskFileContent): void => {
file.contentVersions.add(newTaskFileContent);
this.fileModel.addOrUpdateFile(file);
};
2024-07-22 12:39:26 +02:00
// locations
2024-07-22 13:09:33 +02:00
getTaskLocation = (file: File): TaskLocation | null => {
const locationPath: string[] = this.getTaskLocationPath(file.id);
const locationOrNull: TaskLocation | null =
this.storageModel.readStringifiable(locationPath, TaskLocationReference);
return locationOrNull;
};
storeTaskLocation = (file: File, taskLocation: TaskLocation): void => {
const locationPath: string[] = this.getTaskLocationPath(file.id);
this.storageModel.writeStringifiable(locationPath, taskLocation);
};
updateTaskLocation = (file: File, newLocation: TaskLocation): boolean => {
const previousLocation: TaskLocation | null = this.getTaskLocation(file);
if (previousLocation == null) return false;
const oldBoardName: string = previousLocation.board;
const sourcePath: string[] = this.getTaskPath(oldBoardName, file.id);
const newBoardName: string = newLocation.board;
const destinationPath: string[] = this.getTaskPath(newBoardName, file.id);
return this.storageModel.rename(sourcePath, destinationPath);
};
// storage
listBoardsNames = (): string[] => {
const boardDirPath: string[] = this.getBoardDirPath();
const boardNames: string[] = this.storageModel.list(boardDirPath);
return boardNames;
};
listFileIdsOfBoard = (boardName: string): string[] => {
const boardPath: string[] = this.getBoardPath(boardName);
const fileIds: string[] = this.storageModel.list(boardPath);
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 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
static createTaskFileContent = (
name: string,
board: string
): TaskFileContent => {
const fileContent: FileContent<"task"> =
FileModel.createFileContent("task");
return {
...fileContent,
name,
board,
};
};
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 02:05:27 +02:00
// types
2024-07-22 11:49:05 +02:00
export interface TaskFileContent extends FileContent<"task"> {
name: string;
2024-07-22 02:16:58 +02:00
board: 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
export interface TaskLocation extends ValidObject {
board: string;
}
// reference
2024-07-22 11:20:43 +02:00
export const TaskFileContentReference: TaskFileContent = {
dataVersion: DATA_VERSION,
id: "",
creationDate: "",
type: "task",
2024-07-22 11:49:05 +02:00
name: "",
2024-07-22 11:20:43 +02:00
board: "",
};
2024-07-22 12:22:52 +02:00
export const TaskLocationReference: TaskLocation = {
dataVersion: DATA_VERSION,
board: "",
};