2020-11-05 18:58:23 +02:00
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
import { Request, RequestMethod } from '../Api';
|
2020-11-05 18:58:23 +02:00
|
|
|
import defaultAction from '../utils/defaultAction';
|
|
|
|
import paginatedResults from '../utils/paginatedResults';
|
|
|
|
import BaseModel from '../../../BaseModel';
|
|
|
|
import requestFields from '../utils/requestFields';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Folder from '../../../models/Folder';
|
2024-01-08 13:58:11 +02:00
|
|
|
import { allForDisplay } from '../../../folders-screen-utils';
|
2020-11-11 13:52:47 +02:00
|
|
|
const { ErrorNotFound } = require('../utils/errors');
|
2020-11-05 18:58:23 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export default async function(request: Request, id: string = null, link: string = null) {
|
2024-03-02 16:25:27 +02:00
|
|
|
if (request.method === RequestMethod.GET && !id) {
|
2020-11-05 18:58:23 +02:00
|
|
|
if (request.query.as_tree) {
|
2024-03-02 16:25:27 +02:00
|
|
|
const folders = await allForDisplay({
|
|
|
|
fields: requestFields(request, BaseModel.TYPE_FOLDER),
|
|
|
|
includeDeleted: false,
|
|
|
|
});
|
2020-11-05 18:58:23 +02:00
|
|
|
const output = await Folder.allAsTree(folders);
|
|
|
|
return output;
|
|
|
|
} else {
|
2024-03-02 16:25:27 +02:00
|
|
|
return defaultAction(BaseModel.TYPE_FOLDER, request, id, link, null, { sql: 'deleted_time = 0' });
|
2020-11-05 18:58:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
if (request.method === RequestMethod.GET && id) {
|
2020-11-05 18:58:23 +02:00
|
|
|
if (link && link === 'notes') {
|
|
|
|
const folder = await Folder.load(id);
|
2020-11-11 13:52:47 +02:00
|
|
|
return paginatedResults(BaseModel.TYPE_NOTE, request, { sql: 'parent_id = ?', params: [folder.id] });
|
2020-11-05 18:58:23 +02:00
|
|
|
} else if (link) {
|
|
|
|
throw new ErrorNotFound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
if (request.method === RequestMethod.DELETE) {
|
2024-03-09 12:33:05 +02:00
|
|
|
await Folder.delete(id, { toTrash: request.query.permanent !== '1', sourceDescription: 'api/folders DELETE' });
|
2024-03-02 16:25:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-05 18:58:23 +02:00
|
|
|
return defaultAction(BaseModel.TYPE_FOLDER, request, id, link);
|
|
|
|
}
|