|
|
@@ -33,91 +33,90 @@ schema.plugin(mongoosePaginate);
|
|
|
|
|
|
const PAGE_ITEMS = 10;
|
|
|
|
|
|
-export default (crowi) => {
|
|
|
- schema.statics.findUserGroupsWithPagination = function(opts) {
|
|
|
- const query = { parent: null };
|
|
|
- const options = Object.assign({}, opts);
|
|
|
- if (options.page == null) {
|
|
|
- options.page = 1;
|
|
|
- }
|
|
|
- if (options.limit == null) {
|
|
|
- options.limit = PAGE_ITEMS;
|
|
|
- }
|
|
|
-
|
|
|
- return this.paginate(query, options)
|
|
|
- .catch((err) => {
|
|
|
- debug('Error on pagination:', err);
|
|
|
- });
|
|
|
- };
|
|
|
+schema.statics.findUserGroupsWithPagination = function(opts) {
|
|
|
+ const query = { parent: null };
|
|
|
+ const options = Object.assign({}, opts);
|
|
|
+ if (options.page == null) {
|
|
|
+ options.page = 1;
|
|
|
+ }
|
|
|
+ if (options.limit == null) {
|
|
|
+ options.limit = PAGE_ITEMS;
|
|
|
+ }
|
|
|
+
|
|
|
+ return this.paginate(query, options)
|
|
|
+ .catch((err) => {
|
|
|
+ debug('Error on pagination:', err);
|
|
|
+ });
|
|
|
+};
|
|
|
|
|
|
- schema.statics.findChildUserGroupsByParentIds = async function(parentIds, includeGrandChildren = false) {
|
|
|
- if (!Array.isArray(parentIds)) {
|
|
|
- throw Error('parentIds must be an array.');
|
|
|
- }
|
|
|
|
|
|
- const childUserGroups = await this.find({ parent: { $in: parentIds } });
|
|
|
+schema.statics.findChildUserGroupsByParentIds = async function(parentIds, includeGrandChildren = false) {
|
|
|
+ if (!Array.isArray(parentIds)) {
|
|
|
+ throw Error('parentIds must be an array.');
|
|
|
+ }
|
|
|
|
|
|
- let grandChildUserGroups: UserGroupDocument[] | null = null;
|
|
|
- if (includeGrandChildren) {
|
|
|
- const childUserGroupIds = childUserGroups.map(group => group._id);
|
|
|
- grandChildUserGroups = await this.find({ parent: { $in: childUserGroupIds } });
|
|
|
- }
|
|
|
+ const childUserGroups = await this.find({ parent: { $in: parentIds } });
|
|
|
|
|
|
- return {
|
|
|
- childUserGroups,
|
|
|
- grandChildUserGroups,
|
|
|
- };
|
|
|
- };
|
|
|
+ let grandChildUserGroups: UserGroupDocument[] | null = null;
|
|
|
+ if (includeGrandChildren) {
|
|
|
+ const childUserGroupIds = childUserGroups.map(group => group._id);
|
|
|
+ grandChildUserGroups = await this.find({ parent: { $in: childUserGroupIds } });
|
|
|
+ }
|
|
|
|
|
|
- schema.statics.removeCompletelyById = async function(deleteGroupId, action, transferToUserGroupId, user) { // TODO 85062: move this to the service layer
|
|
|
- const UserGroupRelation = mongoose.model('UserGroupRelation') as any; // TODO 85062: Typescriptize model
|
|
|
+ return {
|
|
|
+ childUserGroups,
|
|
|
+ grandChildUserGroups,
|
|
|
+ };
|
|
|
+};
|
|
|
|
|
|
- const groupToDelete = await this.findById(deleteGroupId);
|
|
|
- if (groupToDelete == null) {
|
|
|
- throw Error(`UserGroup data is not exists. id: ${deleteGroupId}`);
|
|
|
- }
|
|
|
- const deletedGroup = await groupToDelete.remove();
|
|
|
+// schema.statics.removeCompletelyById = async function(deleteGroupId, action, transferToUserGroupId, user) { // TODO 85062: move this to the service layer
|
|
|
+// const UserGroupRelation = mongoose.model('UserGroupRelation') as any; // TODO 85062: Typescriptize model
|
|
|
|
|
|
- await Promise.all([
|
|
|
- UserGroupRelation.removeAllByUserGroup(deletedGroup),
|
|
|
- crowi.pageService.handlePrivatePagesForDeletedGroup(deletedGroup, action, transferToUserGroupId, user),
|
|
|
- ]);
|
|
|
+// const groupToDelete = await this.findById(deleteGroupId);
|
|
|
+// if (groupToDelete == null) {
|
|
|
+// throw Error(`UserGroup data is not exists. id: ${deleteGroupId}`);
|
|
|
+// }
|
|
|
+// const deletedGroup = await groupToDelete.remove();
|
|
|
|
|
|
- return deletedGroup;
|
|
|
- };
|
|
|
+// await Promise.all([
|
|
|
+// UserGroupRelation.removeAllByUserGroup(deletedGroup),
|
|
|
+// crowi.pageService.handlePrivatePagesForDeletedGroup(deletedGroup, action, transferToUserGroupId, user),
|
|
|
+// ]);
|
|
|
|
|
|
- schema.statics.countUserGroups = function() {
|
|
|
- return this.estimatedDocumentCount();
|
|
|
- };
|
|
|
+// return deletedGroup;
|
|
|
+// };
|
|
|
|
|
|
- schema.statics.createGroup = async function(name, description, parentId) {
|
|
|
- // create without parent
|
|
|
- if (parentId == null) {
|
|
|
- return this.create({ name, description });
|
|
|
- }
|
|
|
-
|
|
|
- // create with parent
|
|
|
- const parent = await this.findOne({ _id: parentId });
|
|
|
- if (parent == null) {
|
|
|
- throw Error('Parent does not exist.');
|
|
|
- }
|
|
|
- return this.create({ name, description, parent });
|
|
|
- };
|
|
|
+schema.statics.countUserGroups = function() {
|
|
|
+ return this.estimatedDocumentCount();
|
|
|
+};
|
|
|
|
|
|
- schema.statics.findAllAncestorGroups = async function(parent, ancestors = [parent]) {
|
|
|
- if (parent == null) {
|
|
|
- return ancestors;
|
|
|
- }
|
|
|
+schema.statics.createGroup = async function(name, description, parentId) {
|
|
|
+ // create without parent
|
|
|
+ if (parentId == null) {
|
|
|
+ return this.create({ name, description });
|
|
|
+ }
|
|
|
+
|
|
|
+ // create with parent
|
|
|
+ const parent = await this.findOne({ _id: parentId });
|
|
|
+ if (parent == null) {
|
|
|
+ throw Error('Parent does not exist.');
|
|
|
+ }
|
|
|
+ return this.create({ name, description, parent });
|
|
|
+};
|
|
|
|
|
|
- const nextParent = await this.findOne({ _id: parent.parent });
|
|
|
- if (nextParent == null) {
|
|
|
- return ancestors;
|
|
|
- }
|
|
|
+schema.statics.findAllAncestorGroups = async function(parent, ancestors = [parent]) {
|
|
|
+ if (parent == null) {
|
|
|
+ return ancestors;
|
|
|
+ }
|
|
|
|
|
|
- ancestors.push(nextParent);
|
|
|
+ const nextParent = await this.findOne({ _id: parent.parent });
|
|
|
+ if (nextParent == null) {
|
|
|
+ return ancestors;
|
|
|
+ }
|
|
|
|
|
|
- return this.findAllAncestorGroups(nextParent, ancestors);
|
|
|
- };
|
|
|
+ ancestors.push(nextParent);
|
|
|
|
|
|
- return getOrCreateModel<UserGroupDocument, UserGroupModel>('UserGroup', schema);
|
|
|
+ return this.findAllAncestorGroups(nextParent, ancestors);
|
|
|
};
|
|
|
+
|
|
|
+return getOrCreateModel<UserGroupDocument, UserGroupModel>('UserGroup', schema);
|