Переглянути джерело

impl removeAllInvalidRelations method to UserGroupRelations and PageGroupRelations

Yuki Takei 7 роки тому
батько
коміт
0c95e3e949
2 змінених файлів з 50 додано та 2 видалено
  1. 26 1
      lib/models/page-group-relation.js
  2. 24 1
      lib/models/user-group-relation.js

+ 26 - 1
lib/models/page-group-relation.js

@@ -45,6 +45,27 @@ class PageGroupRelation {
     return this._crowi;
     return this._crowi;
   }
   }
 
 
+  static init() {
+    this.removeAllInvalidRelations();
+  }
+
+  /**
+   * remove all invalid relations that has reference to unlinked document
+   */
+  static removeAllInvalidRelations() {
+    return this.findAllRelation()
+      .then(relations => {
+        // filter invalid documents
+        return relations.filter(relation => {
+          return relation.targetPage == null || relation.relatedGroup == null;
+        });
+      })
+      .then(invalidRelations => {
+        const ids = invalidRelations.map(relation => relation._id);
+        return this.deleteMany({ _id: { $in: ids }});
+      });
+  }
+
   /**
   /**
    * find all page and group relation
    * find all page and group relation
    *
    *
@@ -57,6 +78,7 @@ class PageGroupRelation {
     return this
     return this
       .find()
       .find()
       .populate('targetPage')
       .populate('targetPage')
+      .populate('relatedGroup')
       .exec();
       .exec();
   }
   }
 
 
@@ -229,10 +251,13 @@ class PageGroupRelation {
         }
         }
       });
       });
   }
   }
+
 }
 }
 
 
 module.exports = function(crowi) {
 module.exports = function(crowi) {
   PageGroupRelation.crowi = crowi;
   PageGroupRelation.crowi = crowi;
   schema.loadClass(PageGroupRelation);
   schema.loadClass(PageGroupRelation);
-  return mongoose.model('PageGroupRelation', schema);
+  const model = mongoose.model('PageGroupRelation', schema);
+  model.init();
+  return model;
 };
 };

+ 24 - 1
lib/models/user-group-relation.js

@@ -40,6 +40,27 @@ class UserGroupRelation {
     return this._crowi;
     return this._crowi;
   }
   }
 
 
+  static init() {
+    this.removeAllInvalidRelations();
+  }
+
+  /**
+   * remove all invalid relations that has reference to unlinked document
+   */
+  static removeAllInvalidRelations() {
+    return this.findAllRelation()
+      .then(relations => {
+        // filter invalid documents
+        return relations.filter(relation => {
+          return relation.relatedUser == null || relation.relatedGroup == null;
+        });
+      })
+      .then(invalidRelations => {
+        const ids = invalidRelations.map(relation => relation._id);
+        return this.deleteMany({ _id: { $in: ids }});
+      });
+  }
+
   /**
   /**
    * find all user and group relation
    * find all user and group relation
    *
    *
@@ -257,5 +278,7 @@ class UserGroupRelation {
 module.exports = function(crowi) {
 module.exports = function(crowi) {
   UserGroupRelation.crowi = crowi;
   UserGroupRelation.crowi = crowi;
   schema.loadClass(UserGroupRelation);
   schema.loadClass(UserGroupRelation);
-  return mongoose.model('UserGroupRelation', schema);
+  const model = mongoose.model('UserGroupRelation', schema);
+  model.init();
+  return model;
 };
 };