|
@@ -1,38 +1,29 @@
|
|
|
-import { Ref, IUser, HasObjectId } from '@growi/core';
|
|
|
|
|
import { isValidObjectId } from '@growi/core/src/utils/objectid-utils';
|
|
import { isValidObjectId } from '@growi/core/src/utils/objectid-utils';
|
|
|
-import ExtensibleCustomError from 'extensible-custom-error';
|
|
|
|
|
import {
|
|
import {
|
|
|
Types, Document, Model, Schema,
|
|
Types, Document, Model, Schema,
|
|
|
} from 'mongoose';
|
|
} from 'mongoose';
|
|
|
|
|
|
|
|
|
|
+import { IBookmarkFolder } from '~/interfaces/bookmark-info';
|
|
|
|
|
+
|
|
|
|
|
|
|
|
import loggerFactory from '../../utils/logger';
|
|
import loggerFactory from '../../utils/logger';
|
|
|
import { getOrCreateModel } from '../util/mongoose-utils';
|
|
import { getOrCreateModel } from '../util/mongoose-utils';
|
|
|
|
|
|
|
|
|
|
+import { InvalidParentBookmarkFolderError } from './errors';
|
|
|
|
|
+
|
|
|
const logger = loggerFactory('growi:models:bookmark-folder');
|
|
const logger = loggerFactory('growi:models:bookmark-folder');
|
|
|
|
|
|
|
|
-export class InvalidParentBookmarkFolder extends ExtensibleCustomError {}
|
|
|
|
|
|
|
|
|
|
-export type BookmarkFolderItems = {
|
|
|
|
|
- bookmarkFolder: IBookmarkFolderDocument & HasObjectId
|
|
|
|
|
- children: BookmarkFolderItems[]
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-export type IBookmarkFolderDocument = {
|
|
|
|
|
- name: string
|
|
|
|
|
- owner: Ref<IUser>
|
|
|
|
|
- parent?: string
|
|
|
|
|
-}
|
|
|
|
|
export interface BookmarkFolderDocument extends Document {
|
|
export interface BookmarkFolderDocument extends Document {
|
|
|
_id: Types.ObjectId
|
|
_id: Types.ObjectId
|
|
|
name: string
|
|
name: string
|
|
|
owner: Types.ObjectId
|
|
owner: Types.ObjectId
|
|
|
- parent?: Types.ObjectId | null
|
|
|
|
|
|
|
+ parent?: [this]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export interface BookmarkFolderModel extends Model<BookmarkFolderDocument>{
|
|
export interface BookmarkFolderModel extends Model<BookmarkFolderDocument>{
|
|
|
- createByParameters(params: IBookmarkFolderDocument): IBookmarkFolderDocument
|
|
|
|
|
- findParentFolderByUserId(user: Types.ObjectId | string, parentId: Types.ObjectId | string | null): BookmarkFolderDocument[]
|
|
|
|
|
|
|
+ createByParameters(params: IBookmarkFolder): BookmarkFolderDocument
|
|
|
|
|
+ findParentFolderByUserId(user: Types.ObjectId | string): BookmarkFolderDocument[]
|
|
|
findChildFolderById(parentBookmarkFolder: Types.ObjectId | string): Promise<BookmarkFolderDocument[]>
|
|
findChildFolderById(parentBookmarkFolder: Types.ObjectId | string): Promise<BookmarkFolderDocument[]>
|
|
|
deleteFolderAndChildren(bookmarkFolderId: string): {deletedCount: number}
|
|
deleteFolderAndChildren(bookmarkFolderId: string): {deletedCount: number}
|
|
|
updateBookmarkFolder(bookmarkFolderId: string, name: string, parent: string): BookmarkFolderDocument | null
|
|
updateBookmarkFolder(bookmarkFolderId: string, name: string, parent: string): BookmarkFolderDocument | null
|
|
@@ -45,38 +36,38 @@ const bookmarkFolderSchema = new Schema<BookmarkFolderDocument, BookmarkFolderMo
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
-bookmarkFolderSchema.statics.createByParameters = async function(params: IBookmarkFolderDocument): Promise<BookmarkFolderDocument> {
|
|
|
|
|
|
|
+bookmarkFolderSchema.statics.createByParameters = async function(params: IBookmarkFolder): Promise<BookmarkFolderDocument> {
|
|
|
const { name, owner, parent } = params;
|
|
const { name, owner, parent } = params;
|
|
|
let bookmarkFolder;
|
|
let bookmarkFolder;
|
|
|
|
|
|
|
|
- if (parent === null) {
|
|
|
|
|
- bookmarkFolder = await this.create({ name, owner, parent: null }) as unknown as BookmarkFolderDocument;
|
|
|
|
|
|
|
+ if (parent == null) {
|
|
|
|
|
+ bookmarkFolder = await this.create({ name, owner }) as unknown as BookmarkFolderDocument;
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
// Check if parent folder id is valid and parent folder exists
|
|
// Check if parent folder id is valid and parent folder exists
|
|
|
- const isParentFolderIdValid = isValidObjectId(parent);
|
|
|
|
|
- const parentFolder = await this.findById(parent);
|
|
|
|
|
|
|
+ const isParentFolderIdValid = isValidObjectId(parent as string);
|
|
|
|
|
|
|
|
- if (!isParentFolderIdValid || parentFolder == null) {
|
|
|
|
|
- throw new InvalidParentBookmarkFolder("Parent folder id is invalid or parent folder doesn't exists");
|
|
|
|
|
|
|
+ if (!isParentFolderIdValid) {
|
|
|
|
|
+ throw new InvalidParentBookmarkFolderError('Parent folder id is invalid');
|
|
|
|
|
+ }
|
|
|
|
|
+ const parentFolder = await this.findById(parent);
|
|
|
|
|
+ if (parentFolder == null) {
|
|
|
|
|
+ throw new InvalidParentBookmarkFolderError('Parent folder not found');
|
|
|
}
|
|
}
|
|
|
- bookmarkFolder = await this.create({ name, owner, parent: parentFolder?._id }) as unknown as BookmarkFolderDocument;
|
|
|
|
|
|
|
+ bookmarkFolder = await this.create({ name, owner, parent: parentFolder._id }) as unknown as BookmarkFolderDocument;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return bookmarkFolder;
|
|
return bookmarkFolder;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-bookmarkFolderSchema.statics.findParentFolderByUserId = async function(
|
|
|
|
|
- userId: Types.ObjectId | string,
|
|
|
|
|
- parentId: Types.ObjectId | string | null,
|
|
|
|
|
-): Promise<BookmarkFolderDocument[]> {
|
|
|
|
|
- const bookmarks = this.find({ owner: userId, parent: parentId }) as unknown as BookmarkFolderDocument[];
|
|
|
|
|
|
|
+bookmarkFolderSchema.statics.findParentFolderByUserId = async function(userId: Types.ObjectId | string): Promise<BookmarkFolderDocument[]> {
|
|
|
|
|
+ const bookmarks = this.find({ owner: userId, parent: { $exists: false } }) as unknown as BookmarkFolderDocument[];
|
|
|
return bookmarks;
|
|
return bookmarks;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
bookmarkFolderSchema.statics.findChildFolderById = async function(parentFolderId: Types.ObjectId | string): Promise<BookmarkFolderDocument[]> {
|
|
bookmarkFolderSchema.statics.findChildFolderById = async function(parentFolderId: Types.ObjectId | string): Promise<BookmarkFolderDocument[]> {
|
|
|
const parentFolder = await this.findById(parentFolderId) as unknown as BookmarkFolderDocument;
|
|
const parentFolder = await this.findById(parentFolderId) as unknown as BookmarkFolderDocument;
|
|
|
- const childFolders = await this.find({ parent: parentFolder._id });
|
|
|
|
|
|
|
+ const childFolders = await this.find({ parent: parentFolder });
|
|
|
return childFolders;
|
|
return childFolders;
|
|
|
};
|
|
};
|
|
|
|
|
|