|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import FolderViewMode from '../../models/FolderViewMode'
- import Folder from '../../models/Folder'
- import FolderService from './service'
- import Item from '../../models/Item'
- import {_} from '../../utilities/messages'
-
- export default class FolderPresenter {
- private readonly folderService = new FolderService()
-
- private async getCommonProps(folderId: string) {
- return Promise.all([
- this.folderService.getHierarchy(folderId),
- this.folderService.getChildren(folderId),
- this.folderService.getItems(folderId),
- ])
- }
-
- async getRootFolderViewState({ session, query, mode, }) {
- // TODO make this authentication checking more like a middleware.
- if (!session) {
- return {
- redirect: {
- url: new URL('/', process.env.BASE_URL)
- },
- }
- }
-
- const folderId = session.user.rootFolder.id
- const [hierarchy, children, items] = await this.getCommonProps(folderId)
- return {
- body: {
- data: {
- query,
- mode,
- items: items.map(Item.formatForView),
- children: children.map(c => Folder.formatForView(c)),
- hierarchy: hierarchy.map(h => Folder.formatForView(h)),
- },
- },
- }
- }
-
- async getDescendantFolderViewState({ session, query, mode, id, }) {
- // TODO make this authentication checking more like a middleware.
- if (!session) {
- return {
- redirect: {
- url: new URL('/', process.env.BASE_URL)
- },
- }
- }
-
- const folderId = id as string
- const [hierarchy, children, items] = await this.getCommonProps(folderId)
- return {
- body: {
- data: {
- query,
- mode,
- items: items.map(Item.formatForView),
- children: children.map(Folder.formatForView),
- hierarchy: hierarchy.map(Folder.formatForView),
- },
- },
- }
- }
-
- async getFolder({ id }) {
- try {
- const data = await this.folderService.get(id)
- // TODO compute owner of folder by getting the root folder
- if (data) {
- return {
- status: 200,
- title: _('FOLDER_FOUND_TITLE'),
- body: {
- description: _('FOLDER_FOUND_DESCRIPTION'),
- data,
- },
- }
- }
- return {
- status: 404,
- title: _('FOLDER_NOT_FOUND_TITLE'),
- body: {
- description: _('FOLDER_NOT_FOUND_DESCRIPTION'),
- },
- }
- } catch {
- return {
- status: 500,
- title: _('ERROR_RETRIEVING_FOLDER_TITLE'),
- body: {
- description: _('ERROR_RETRIEVING_FOLDER_DESCRIPTION'),
- },
- }
- }
- }
-
- async createFolder({ name, parentId, origin = null }) {
- try {
- const newFolder = await this.folderService.create(name, parentId)
- return {
- status: 201,
- title: _('FOLDER_CREATED_TITLE'),
- body: {
- description: _('FOLDER_CREATED_DESCRIPTION'),
- data: newFolder,
- },
- redirect: {
- url: new URL(`/my/folders/${newFolder.id}`, process.env.BASE_URL),
- },
- }
- } catch {
- return {
- status: 500,
- title: _('ERROR_CREATING_FOLDER_TITLE'),
- body: {
- description: _('ERROR_CREATING_FOLDER_DESCRIPTION'),
- },
- redirect: {
- url: new URL(origin),
- },
- }
- }
- }
-
- generateRedirectToCreateFolder({ sourceURL, }) {
- const url = sourceURL ? new URL(sourceURL) : new URL('/my/folders', process.env.BASE_URL)
- const search = Object.fromEntries(url.searchParams.entries())
-
- url.search = new URLSearchParams({
- ...search,
- 'mode': FolderViewMode.NEW_FOLDER,
- }).toString()
-
- return {
- redirect: {
- url
- }
- }
- }
- }
|