Yuki Takei 2 лет назад
Родитель
Сommit
645cefc2b9

+ 0 - 1
apps/app/src/interfaces/share-link.ts

@@ -10,7 +10,6 @@ export type IShareLink = {
   createdAt: Date,
   expiredAt?: Date,
   description: string,
-  isExpired: () => boolean,
 };
 
 export type IShareLinkHasId = IShareLink & HasObjectId;

+ 0 - 3
apps/app/src/server/middlewares/certify-shared-file/certify-shared-file.ts

@@ -1,10 +1,7 @@
-import type { IAttachment } from '@growi/core';
 import type { NextFunction, Request, Response } from 'express';
 
 import loggerFactory from '~/utils/logger';
 
-import { getModelSafely } from '../../util/mongoose-utils';
-
 import { retrieveValidShareLinkByReferer } from './retrieve-valid-share-link';
 import { validateAttachment } from './validate-attachment';
 import { validateReferer } from './validate-referer';

+ 25 - 18
apps/app/src/server/models/share-link.ts

@@ -1,16 +1,27 @@
-// disable no-return-await for model functions
-/* eslint-disable no-return-await */
+import mongoose, { Schema } from 'mongoose';
+import type {
+  Document, Model,
+} from 'mongoose';
+import mongoosePaginate from 'mongoose-paginate-v2';
+import uniqueValidator from 'mongoose-unique-validator';
 
-const mongoose = require('mongoose');
-const mongoosePaginate = require('mongoose-paginate-v2');
-const uniqueValidator = require('mongoose-unique-validator');
+import type { IShareLink } from '~/interfaces/share-link';
+
+import { getOrCreateModel } from '../util/mongoose-utils';
+
+
+export interface ShareLinkDocument extends IShareLink, Document {}
+
+export interface ShareLinkModel extends Model<ShareLinkDocument>{
+  isExpired: () => boolean,
+}
 
-const ObjectId = mongoose.Schema.Types.ObjectId;
 
 /*
  * define schema
  */
-const schema = new mongoose.Schema({
+const ObjectId = mongoose.Schema.Types.ObjectId;
+const schema = new Schema<ShareLinkDocument, ShareLinkModel>({
   relatedPage: {
     type: ObjectId,
     ref: 'Page',
@@ -25,15 +36,11 @@ const schema = new mongoose.Schema({
 schema.plugin(mongoosePaginate);
 schema.plugin(uniqueValidator);
 
-module.exports = function(crowi) {
-
-  schema.methods.isExpired = function() {
-    if (this.expiredAt == null) {
-      return false;
-    }
-    return this.expiredAt.getTime() < new Date().getTime();
-  };
-
-  const model = mongoose.model('ShareLink', schema);
-  return model;
+schema.methods.isExpired = function() {
+  if (this.expiredAt == null) {
+    return false;
+  }
+  return this.expiredAt.getTime() < new Date().getTime();
 };
+
+export default getOrCreateModel<ShareLinkDocument, ShareLinkModel>('ShareLink', schema);