|
|
@@ -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);
|