|
|
@@ -26,12 +26,12 @@ export interface BookmarkFolderDocument extends Document {
|
|
|
}
|
|
|
|
|
|
export interface BookmarkFolderModel extends Model<BookmarkFolderDocument>{
|
|
|
- createByParameters(params: IBookmarkFolder): BookmarkFolderDocument
|
|
|
- findFolderAndChildren(user: Types.ObjectId | string, parentId?: Types.ObjectId | string): BookmarkFolderItems[]
|
|
|
+ createByParameters(params: IBookmarkFolder): Promise<BookmarkFolderDocument>
|
|
|
+ findFolderAndChildren(user: Types.ObjectId | string, parentId?: Types.ObjectId | string): Promise<BookmarkFolderItems[] | null>
|
|
|
findChildFolderById(parentBookmarkFolder: Types.ObjectId | string): Promise<BookmarkFolderDocument[]>
|
|
|
- deleteFolderAndChildren(bookmarkFolderId: Types.ObjectId | string): {deletedCount: number}
|
|
|
- updateBookmarkFolder(bookmarkFolderId: string, name: string, parent: string): BookmarkFolderDocument | null
|
|
|
- insertOrUpdateBookmarkedPage(pageId: IPageHasId, userId: Types.ObjectId | string, folderId: string)
|
|
|
+ deleteFolderAndChildren(bookmarkFolderId: Types.ObjectId | string): Promise<{deletedCount: number}>
|
|
|
+ updateBookmarkFolder(bookmarkFolderId: string, name: string, parent: string): Promise<BookmarkFolderDocument>
|
|
|
+ insertOrUpdateBookmarkedPage(pageId: IPageHasId, userId: Types.ObjectId | string, folderId: string): Promise<BookmarkFolderDocument>
|
|
|
}
|
|
|
|
|
|
const bookmarkFolderSchema = new Schema<BookmarkFolderDocument, BookmarkFolderModel>({
|
|
|
@@ -57,10 +57,10 @@ bookmarkFolderSchema.virtual('children', {
|
|
|
|
|
|
bookmarkFolderSchema.statics.createByParameters = async function(params: IBookmarkFolder): Promise<BookmarkFolderDocument> {
|
|
|
const { name, owner, parent } = params;
|
|
|
- let bookmarkFolder;
|
|
|
+ let bookmarkFolder: BookmarkFolderDocument;
|
|
|
|
|
|
if (parent == null) {
|
|
|
- bookmarkFolder = await this.create({ name, owner }) as unknown as BookmarkFolderDocument;
|
|
|
+ bookmarkFolder = await this.create({ name, owner });
|
|
|
}
|
|
|
else {
|
|
|
// Check if parent folder id is valid and parent folder exists
|
|
|
@@ -73,7 +73,7 @@ bookmarkFolderSchema.statics.createByParameters = async function(params: IBookma
|
|
|
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 });
|
|
|
}
|
|
|
|
|
|
return bookmarkFolder;
|
|
|
@@ -84,8 +84,23 @@ bookmarkFolderSchema.statics.findFolderAndChildren = async function(
|
|
|
parentId?: Types.ObjectId | string,
|
|
|
): Promise<BookmarkFolderItems[]> {
|
|
|
|
|
|
- const parentFolder = await this.findById(parentId) as unknown as BookmarkFolderDocument;
|
|
|
- const bookmarkFolders = await this.find({ owner: userId, parent: parentFolder })
|
|
|
+ let parentFolder: BookmarkFolderDocument | null;
|
|
|
+ let query = {};
|
|
|
+ // Load child bookmark folders
|
|
|
+ if (parentId != null) {
|
|
|
+ parentFolder = await this.findById(parentId);
|
|
|
+ if (parentFolder != null) {
|
|
|
+ query = { owner: userId, parent: parentFolder };
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ throw new InvalidParentBookmarkFolderError('Parent folder not found');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Load initial / root bookmark folders
|
|
|
+ else {
|
|
|
+ query = { owner: userId, parent: null };
|
|
|
+ }
|
|
|
+ const bookmarkFolders: BookmarkFolderItems[] = await this.find(query)
|
|
|
.populate({ path: 'children' })
|
|
|
.populate({
|
|
|
path: 'bookmarks',
|
|
|
@@ -94,7 +109,7 @@ bookmarkFolderSchema.statics.findFolderAndChildren = async function(
|
|
|
path: 'page',
|
|
|
model: 'Page',
|
|
|
},
|
|
|
- }).exec() as unknown as BookmarkFolderItems[];
|
|
|
+ });
|
|
|
return bookmarkFolders;
|
|
|
};
|
|
|
|
|
|
@@ -128,19 +143,21 @@ bookmarkFolderSchema.statics.deleteFolderAndChildren = async function(bookmarkFo
|
|
|
};
|
|
|
|
|
|
bookmarkFolderSchema.statics.updateBookmarkFolder = async function(bookmarkFolderId: string, name: string, parent: string):
|
|
|
- Promise<BookmarkFolderDocument | null> {
|
|
|
-
|
|
|
+ Promise<BookmarkFolderDocument> {
|
|
|
const parentFolder = await this.findById(parent);
|
|
|
const updateFields = {
|
|
|
name, parent: parentFolder?._id || null,
|
|
|
};
|
|
|
const bookmarkFolder = await this.findByIdAndUpdate(bookmarkFolderId, { $set: updateFields }, { new: true });
|
|
|
+ if (bookmarkFolder == null) {
|
|
|
+ throw new Error('Update bookmark folder failed');
|
|
|
+ }
|
|
|
return bookmarkFolder;
|
|
|
|
|
|
};
|
|
|
|
|
|
bookmarkFolderSchema.statics.insertOrUpdateBookmarkedPage = async function(pageId: IPageHasId, userId: Types.ObjectId | string, folderId: string):
|
|
|
-Promise<BookmarkFolderDocument | null> {
|
|
|
+Promise<BookmarkFolderDocument> {
|
|
|
|
|
|
// Create bookmark or update existing
|
|
|
const bookmarkedPage = await Bookmark.findOneAndUpdate({ page: pageId, user: userId }, { page: pageId, user: userId }, { new: true, upsert: true });
|