Преглед изворни кода

Fix eslint error and throw an error if parent folder id is invalid

https://youtrack.weseek.co.jp/issue/GW-7833
- Fix unresolved import of Nullable module
- Fix get user id from currentUser
- Create custom error class of InvalidParentBookmarkFolder
- Update parent type in IBookmarkFolderDocument
- Throwing error when parentFolder object is null
Mudana-Grune пре 3 година
родитељ
комит
7fcae70716

+ 25 - 5
packages/app/src/server/models/bookmark-folder.ts

@@ -1,4 +1,5 @@
 import { Ref, IUser } from '@growi/core';
+import ExtensibleCustomError from 'extensible-custom-error';
 import {
   Types, Document, Model, Schema,
 } from 'mongoose';
@@ -9,10 +10,13 @@ import { getOrCreateModel } from '../util/mongoose-utils';
 
 const logger = loggerFactory('growi:models:bookmark-folder');
 
+export class InvalidParentBookmarkFolder extends ExtensibleCustomError {}
+
+
 export type IBookmarkFolderDocument = {
   name: string
   owner: Ref<IUser>
-  parent?: string
+  parent?: Ref<BookmarkFolderDocument>
 }
 export interface BookmarkFolderDocument extends Document {
   _id: Types.ObjectId
@@ -32,15 +36,31 @@ export interface BookmarkFolderModel extends Model<BookmarkFolderDocument>{
 const bookmarkFolderSchema = new Schema<BookmarkFolderDocument, BookmarkFolderModel>({
   name: { type: String },
   owner: { type: Schema.Types.ObjectId, ref: 'User', index: true },
-  parent: { type: Schema.Types.ObjectId, refPath: 'BookmarkFolder', required: false },
+  parent: { type: Schema.Types.ObjectId, ref: 'BookmarkFolder', required: false },
 });
 
 
 bookmarkFolderSchema.statics.createByParameters = async function(params: IBookmarkFolderDocument): Promise<BookmarkFolderDocument> {
   const { name, owner, parent } = params;
-  const parentFolder = await this.findById(parent);
-
-  const bookmarkFolder = await this.create({ name, owner, parent: parentFolder?._id || null }) as unknown as BookmarkFolderDocument;
+  let bookmarkFolder;
+  try {
+    if (parent === null) {
+      bookmarkFolder = await this.create({ name, owner, parent:  null }) as unknown as BookmarkFolderDocument;
+    }
+    else {
+      const parentFolder = await this.findById(parent);
+      if (!parentFolder) {
+        throw new InvalidParentBookmarkFolder("Parent folder doesn't exists");
+      }
+      bookmarkFolder = await this.create({ name, owner, parent:  parentFolder?._id }) as unknown as BookmarkFolderDocument;
+    }
+  }
+  catch (err) {
+    if (err instanceof InvalidParentBookmarkFolder) {
+      throw new InvalidParentBookmarkFolder(err);
+    }
+    return err;
+  }
   return bookmarkFolder;
 };
 

+ 3 - 2
packages/app/src/stores/bookmark.ts

@@ -1,7 +1,7 @@
+import { IUserHasId, Nullable } from '@growi/core';
 import { SWRResponse } from 'swr';
 import useSWRImmutable from 'swr/immutable';
 
-import { Nullable } from '~/interfaces/common';
 import { IPageHasId } from '~/interfaces/page';
 
 import { apiv3Get } from '../client/util/apiv3-client';
@@ -25,8 +25,9 @@ export const useSWRBookmarkInfo = (pageId: string | null | undefined): SWRRespon
 export const useSWRxCurrentUserBookmarks = (pageNum?: Nullable<number>): SWRResponse<IPageHasId[], Error> => {
   const { data: currentUser } = useCurrentUser();
   const currentPage = pageNum ?? 1;
+  const user = currentUser as IUserHasId;
   return useSWRImmutable(
-    currentUser != null ? `/bookmarks/${currentUser._id}` : null,
+    currentUser != null ? `/bookmarks/${user._id}` : null,
     endpoint => apiv3Get(endpoint, { page: currentPage }).then((response) => {
       const { paginationResult } = response.data;
       return paginationResult.docs.map((item) => {