/* eslint-disable no-console */ import Inject, { RegisterTransient } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import FileAccessService from "./FileAccessService.js"; @RegisterTransient export default class AtomicFolderService { @Inject private db: SocialMailContext; @Inject private fas: FileAccessService; async create(parentID: number, name: string) { const dateCreated = DateTime.now; const dateUpdated = dateCreated; // check access. // check write access... const existing = await this.fas.getFile({ parentID, name }, "read"); if (existing && existing.parentID === parentID && existing.isDeleted === false) { return existing; } // since it doesn't exist, we should verify if folder is writable await this.fas.getFile({ fileID: parentID }, "modify"); const folder = await this.db.appFiles.statements.selectOrInsert({ parentID, name, isDeleted: false, isFolder: true, fileSize: 0, contentType: "folder", dateCreated, dateUpdated }, { parentID, name }, 5); if (folder && folder.isDeleted) { folder.isDeleted = false; await this.db.appFiles.statements.update({ isDeleted : false}, { appFileID: folder.appFileID}); } if (!folder.path) { const parent = await this.db.appFiles.where({ parentID }, (x, p) => x.appFileID === p.parentID).first(); folder.path = `${parent.path}${folder.appFileID}/`; await this.db.appFiles.statements.update({ appFileID: folder.appFileID, path: folder.path }); } return folder; } }