Просмотр исходного кода

Improve code

https://youtrack.weseek.co.jp/issue/GW-7833
- Move and rename IBookmarkFolderDocument to IBookmarkFolder
- Update Implementation of IBookmarkFolder
- Update parent references in BookmarkFolderDocument
- Update create bookmark folder condition
- Update statics method for find bookmark folder and child
Mudana-Grune 3 лет назад
Родитель
Сommit
6cb890620d

+ 6 - 0
packages/app/src/interfaces/bookmark-info.ts

@@ -17,3 +17,9 @@ type BookmarkedPage = {
 }
 
 export type MyBookmarkList = BookmarkedPage[]
+
+export interface IBookmarkFolder {
+  name: string
+  owner: Ref<IUser>
+  parent?: Ref<this>
+}

+ 19 - 20
packages/app/src/server/models/bookmark-folder.ts

@@ -1,9 +1,10 @@
-import { Ref, IUser } from '@growi/core';
 import { isValidObjectId } from '@growi/core/src/utils/objectid-utils';
 import {
   Types, Document, Model, Schema,
 } from 'mongoose';
 
+import { IBookmarkFolder } from '~/interfaces/bookmark-info';
+
 
 import loggerFactory from '../../utils/logger';
 import { getOrCreateModel } from '../util/mongoose-utils';
@@ -13,22 +14,17 @@ import { InvalidParentBookmarkFolderError } from './errors';
 const logger = loggerFactory('growi:models:bookmark-folder');
 
 
-export type IBookmarkFolderDocument = {
-  name: string
-  owner: Ref<IUser>
-  parent?: string
-}
 export interface BookmarkFolderDocument extends Document {
   _id: Types.ObjectId
   name: string
   owner: Types.ObjectId
-  parent?: Types.ObjectId | null
+  parent?: [this]
 }
 
 export interface BookmarkFolderModel extends Model<BookmarkFolderDocument>{
-  createByParameters(params: IBookmarkFolderDocument): IBookmarkFolderDocument
-  findParentFolderByUserId(user: Types.ObjectId | string): IBookmarkFolderDocument[]
-  findChildFolderById(parentBookmarkFolder: Types.ObjectId | string): Promise<IBookmarkFolderDocument[]>
+  createByParameters(params: IBookmarkFolder): BookmarkFolderDocument
+  findParentFolderByUserId(user: Types.ObjectId | string): BookmarkFolderDocument[]
+  findChildFolderById(parentBookmarkFolder: Types.ObjectId | string): Promise<BookmarkFolderDocument[]>
   deleteFolderAndChildren(bookmarkFolderId: string): {deletedCount: number}
   updateBookmarkFolder(bookmarkFolderId: string, name: string, parent: string): BookmarkFolderDocument | null
 }
@@ -40,35 +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;
   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 {
     // 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 (parentFolder != null && !isParentFolderIdValid) {
-      throw new InvalidParentBookmarkFolderError("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;
 };
 
 bookmarkFolderSchema.statics.findParentFolderByUserId = async function(userId: Types.ObjectId | string): Promise<BookmarkFolderDocument[]> {
-  const bookmarks = this.find({ owner: userId, parent: null }) as unknown as BookmarkFolderDocument[];
+  const bookmarks = this.find({ owner: userId, parent: { $exists: false } }) as unknown as BookmarkFolderDocument[];
   return bookmarks;
 };
 
 bookmarkFolderSchema.statics.findChildFolderById = async function(parentFolderId: Types.ObjectId | string): Promise<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;
 };