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

Merge pull request #4678 from weseek/fix/81697-mongoose-paginate-error

remove @types/mongoose-paginate-v2 to avoid typescript errors
Yuki Takei 4 лет назад
Родитель
Сommit
5c9639a15f

+ 0 - 1
packages/app/package.json

@@ -162,7 +162,6 @@
     "@handsontable/react": "=2.1.0",
     "@handsontable/react": "=2.1.0",
     "@types/compression": "^1.7.0",
     "@types/compression": "^1.7.0",
     "@types/express": "^4.17.11",
     "@types/express": "^4.17.11",
-    "@types/mongoose-paginate-v2": "1.3.9",
     "@types/multer": "^1.4.5",
     "@types/multer": "^1.4.5",
     "@types/react-dom": "^17.0.9",
     "@types/react-dom": "^17.0.9",
     "autoprefixer": "^9.0.0",
     "autoprefixer": "^9.0.0",

+ 2 - 3
packages/app/src/components/InAppNotification/InAppNotificationList.tsx

@@ -1,13 +1,12 @@
 import React, { FC } from 'react';
 import React, { FC } from 'react';
 
 
-import { PaginateResult } from 'mongoose';
 import { useTranslation } from 'react-i18next';
 import { useTranslation } from 'react-i18next';
-import { IInAppNotification } from '../../interfaces/in-app-notification';
+import { IInAppNotification, PaginateResult } from '../../interfaces/in-app-notification';
 import InAppNotificationElm from './InAppNotificationElm';
 import InAppNotificationElm from './InAppNotificationElm';
 
 
 
 
 type Props = {
 type Props = {
-  inAppNotificationData: PaginateResult<IInAppNotification> | undefined;
+  inAppNotificationData?: PaginateResult<IInAppNotification>;
 };
 };
 
 
 const InAppNotificationList: FC<Props> = (props: Props) => {
 const InAppNotificationList: FC<Props> = (props: Props) => {

+ 19 - 0
packages/app/src/interfaces/in-app-notification.ts

@@ -9,3 +9,22 @@ export interface IInAppNotification {
   actionUsers: any[] /* Need to set "User[]" as a type" */
   actionUsers: any[] /* Need to set "User[]" as a type" */
   createdAt: string
   createdAt: string
 }
 }
+
+/*
+* Note:
+* Need to use mongoose PaginateResult as a type after upgrading mongoose v6.0.0.
+* Until then, use the original "PaginateResult".
+*/
+export interface PaginateResult<T> {
+  docs: T[];
+  hasNextPage: boolean;
+  hasPrevPage: boolean;
+  limit: number;
+  nextPage: number | null;
+  offset: number;
+  page: number;
+  pagingCounter: number;
+  prevPage: number | null;
+  totalDocs: number;
+  totalPages: number;
+}

+ 1 - 1
packages/app/src/server/models/in-app-notification-settings.ts

@@ -6,7 +6,7 @@ import { IInAppNotificationSettings, subscribeRuleNames } from '../../interfaces
 export interface InAppNotificationSettingsDocument extends IInAppNotificationSettings, Document {}
 export interface InAppNotificationSettingsDocument extends IInAppNotificationSettings, Document {}
 export type InAppNotificationSettingsModel = Model<InAppNotificationSettingsDocument>
 export type InAppNotificationSettingsModel = Model<InAppNotificationSettingsDocument>
 
 
-const inAppNotificationSettingsSchema = new Schema<IInAppNotificationSettings>({
+const inAppNotificationSettingsSchema = new Schema<InAppNotificationSettingsDocument, InAppNotificationSettingsModel>({
   userId: { type: Schema.Types.ObjectId },
   userId: { type: Schema.Types.ObjectId },
   subscribeRules: [
   subscribeRules: [
     {
     {

+ 2 - 2
packages/app/src/server/models/in-app-notification.ts

@@ -1,5 +1,5 @@
 import {
 import {
-  Types, Document, PaginateModel, Schema, /* , Query */
+  Types, Document, Schema, Model,
 } from 'mongoose';
 } from 'mongoose';
 import mongoosePaginate from 'mongoose-paginate-v2';
 import mongoosePaginate from 'mongoose-paginate-v2';
 
 
@@ -24,7 +24,7 @@ export interface InAppNotificationDocument extends Document {
 }
 }
 
 
 
 
-export interface InAppNotificationModel extends PaginateModel<InAppNotificationDocument> {
+export interface InAppNotificationModel extends Model<InAppNotificationDocument> {
   findLatestInAppNotificationsByUser(user: Types.ObjectId, skip: number, offset: number)
   findLatestInAppNotificationsByUser(user: Types.ObjectId, skip: number, offset: number)
   getUnreadCountByUser(user: Types.ObjectId): Promise<number | undefined>
   getUnreadCountByUser(user: Types.ObjectId): Promise<number | undefined>
   open(user, id: Types.ObjectId): Promise<InAppNotificationDocument | null>
   open(user, id: Types.ObjectId): Promise<InAppNotificationDocument | null>

+ 5 - 2
packages/app/src/server/service/in-app-notification.ts

@@ -1,10 +1,11 @@
-import { Types, PaginateResult } from 'mongoose';
+import { Types } from 'mongoose';
 import { subDays } from 'date-fns';
 import { subDays } from 'date-fns';
 import Crowi from '../crowi';
 import Crowi from '../crowi';
 import {
 import {
   InAppNotification, STATUS_UNREAD, STATUS_UNOPENED, STATUS_OPENED,
   InAppNotification, STATUS_UNREAD, STATUS_UNOPENED, STATUS_OPENED,
   InAppNotificationDocument,
   InAppNotificationDocument,
 } from '~/server/models/in-app-notification';
 } from '~/server/models/in-app-notification';
+import { PaginateResult } from '../../interfaces/in-app-notification';
 
 
 import { ActivityDocument } from '~/server/models/activity';
 import { ActivityDocument } from '~/server/models/activity';
 import InAppNotificationSettings from '~/server/models/in-app-notification-settings';
 import InAppNotificationSettings from '~/server/models/in-app-notification-settings';
@@ -90,7 +91,9 @@ export default class InAppNotificationService {
     const { limit, offset } = queryOptions;
     const { limit, offset } = queryOptions;
 
 
     try {
     try {
-      const paginationResult = await InAppNotification.paginate(
+      // TODO: import @types/mongoose-paginate-v2 and use PaginateResult as a type after upgrading mongoose v6.0.0
+      // eslint-disable-next-line @typescript-eslint/no-explicit-any
+      const paginationResult = await (InAppNotification as any).paginate(
         { user: userId },
         { user: userId },
         {
         {
           sort: { createdAt: -1 },
           sort: { createdAt: -1 },

+ 1 - 3
packages/app/src/stores/in-app-notification.ts

@@ -1,9 +1,7 @@
 import useSWR, { SWRResponse } from 'swr';
 import useSWR, { SWRResponse } from 'swr';
 
 
-import { PaginateResult } from 'mongoose';
 import { apiv3Get } from '../client/util/apiv3-client';
 import { apiv3Get } from '../client/util/apiv3-client';
-import { IInAppNotification } from '../interfaces/in-app-notification';
-
+import { IInAppNotification, PaginateResult } from '../interfaces/in-app-notification';
 
 
 // eslint-disable-next-line @typescript-eslint/no-unused-vars
 // eslint-disable-next-line @typescript-eslint/no-unused-vars
 export const useSWRxInAppNotifications = <Data, Error>(
 export const useSWRxInAppNotifications = <Data, Error>(

+ 0 - 14
yarn.lock

@@ -3141,20 +3141,6 @@
     "@types/bson" "*"
     "@types/bson" "*"
     "@types/node" "*"
     "@types/node" "*"
 
 
-"@types/mongoose-paginate-v2@1.3.9":
-  version "1.3.9"
-  resolved "https://registry.yarnpkg.com/@types/mongoose-paginate-v2/-/mongoose-paginate-v2-1.3.9.tgz#a211bf0da49473e9e1f1a65d3aacbd5d5ff0408c"
-  integrity sha512-NHqTgOZRmi7gd/IkRJ2VXo88m0efKatLFrG63VEcAB98nO6nzbeRaXPUUgEFJ2Le6vleTE0WqvAuL0gO5IQF5A==
-  dependencies:
-    "@types/mongoose" "*"
-
-"@types/mongoose@*":
-  version "5.11.97"
-  resolved "https://registry.yarnpkg.com/@types/mongoose/-/mongoose-5.11.97.tgz#80b0357f3de6807eb597262f52e49c3e13ee14d8"
-  integrity sha512-cqwOVYT3qXyLiGw7ueU2kX9noE8DPGRY6z8eUxudhXY8NZ7DMKYAxyZkLSevGfhCX3dO/AoX5/SO9lAzfjon0Q==
-  dependencies:
-    mongoose "*"
-
 "@types/multer@^1.4.5":
 "@types/multer@^1.4.5":
   version "1.4.5"
   version "1.4.5"
   resolved "https://registry.yarnpkg.com/@types/multer/-/multer-1.4.5.tgz#db0557562307e9adb6661a9500c334cd7ddd0cd9"
   resolved "https://registry.yarnpkg.com/@types/multer/-/multer-1.4.5.tgz#db0557562307e9adb6661a9500c334cd7ddd0cd9"