mizozobu 6 лет назад
Родитель
Сommit
315b97b7b5
2 измененных файлов с 38 добавлено и 31 удалено
  1. 37 11
      src/server/routes/apiv3/import.js
  2. 1 20
      src/server/service/import.js

+ 37 - 11
src/server/routes/apiv3/import.js

@@ -29,25 +29,51 @@ module.exports = (crowi) => {
   });
 
   /**
-   * defined overwrite option for each collection
+   * defined overwrite params for each collection
    * all imported documents are overwriten by this value
-   * may use async function
+   * each value can be any value or a function (_value, { _document, key, schema }) { return newValue }
    *
-   * @param {object} req
+   * @param {object} Model instance of mongoose model
+   * @param {object} req request object
    * @return {object} document to be persisted
    */
-  const overwriteParamsFn = {};
-  overwriteParamsFn.pages = (req) => {
-    return {
-      creator: ObjectId(req.user._id), // FIXME when importing users
-      lastUpdateUser: ObjectId(req.user._id), // FIXME when importing users
-    };
+  const overwriteParamsFn = async(Model, req) => {
+    const { collectionName } = Model.collection;
+
+    /* eslint-disable no-case-declarations */
+    switch (Model.collection.collectionName) {
+      case 'pages':
+        // TODO: use req.body to generate overwriteParams
+        return {
+          status: 'published', // FIXME when importing users and user groups
+          grant: 1, // FIXME when importing users and user groups
+          grantedUsers: [], // FIXME when importing users and user groups
+          grantedGroup: null, // FIXME when importing users and user groups
+          creator: ObjectId(req.user._id), // FIXME when importing users
+          lastUpdateUser: ObjectId(req.user._id), // FIXME when importing users
+          liker: [], // FIXME when importing users
+          seenUsers: [], // FIXME when importing users
+          commentCount: 0, // FIXME when importing comments
+          extended: {}, // FIXME when ?
+          pageIdOnHackmd: undefined, // FIXME when importing hackmd?
+          revisionHackmdSynced: undefined, // FIXME when importing hackmd?
+          hasDraftOnHackmd: undefined, // FIXME when importing hackmd?
+        };
+      // case 'revisoins':
+      //   return {};
+      // case 'users':
+      //   return {};
+      // ... add more cases
+      default:
+        throw new Error(`cannot find a model for collection name "${collectionName}"`);
+    }
+    /* eslint-enable no-case-declarations */
   };
 
   /**
    * @swagger
    *
-   *  /export/:collection:
+   *  /import/:collection:
    *    post:
    *      tags: [Import]
    *      description: import a collection from a zipped json
@@ -70,7 +96,7 @@ module.exports = (crowi) => {
       let overwriteParams;
       if (overwriteParamsFn[collection] != null) {
         // await in case overwriteParamsFn[collection] is a Promise
-        overwriteParams = await overwriteParamsFn[collection](req);
+        overwriteParams = await overwriteParamsFn(Model, req);
       }
 
       await importService.importFromZip(Model, zipFilePath, overwriteParams);

+ 1 - 20
src/server/service/import.js

@@ -19,21 +19,7 @@ class ImportService {
     this.initCollectionMap(crowi.models);
 
     // { pages: { _id: ..., path: ..., ...}, users: { _id: ..., username: ..., }, ... }
-    this.convertMap = {
-      pages: {
-        status: 'published', // FIXME when importing users and user groups
-        grant: 1, // FIXME when importing users and user groups
-        grantedUsers: [], // FIXME when importing users and user groups
-        grantedGroup: null, // FIXME when importing users and user groups
-        liker: [], // FIXME when importing users
-        seenUsers: [], // FIXME when importing users
-        commentCount: 0, // FIXME when importing comments
-        extended: {}, // FIXME when ?
-        pageIdOnHackmd: undefined, // FIXME when importing hackmd?
-        revisionHackmdSynced: undefined, // FIXME when importing hackmd?
-        hasDraftOnHackmd: undefined, // FIXME when importing hackmd?
-      },
-    };
+    this.convertMap = {};
     this.initConvertMap(crowi.models);
   }
 
@@ -59,16 +45,11 @@ class ImportService {
     // by default, original value is used for imported documents
     for (const model of Object.values(models)) {
       const { collectionName } = model.collection;
-      // temporary store for convert map
-      const _convertMap = this.convertMap[collectionName];
       this.convertMap[collectionName] = {};
 
       for (const key of Object.keys(model.schema.paths)) {
         this.convertMap[collectionName][key] = this.keepOriginal;
       }
-
-      // assign back the original convert map
-      Object.assign(this.convertMap[collectionName], _convertMap);
     }
   }