user-group.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { getOrCreateModel } from '@growi/core';
  2. import mongoose, {
  3. Schema, Model, Document,
  4. } from 'mongoose';
  5. import mongoosePaginate from 'mongoose-paginate-v2';
  6. import { IUserGroup } from '~/interfaces/user';
  7. export interface UserGroupDocument extends IUserGroup, Document {}
  8. export interface UserGroupModel extends Model<UserGroupDocument> {
  9. [x:string]: any, // for old methods
  10. PAGE_ITEMS: 10,
  11. }
  12. /*
  13. * define schema
  14. */
  15. const ObjectId = mongoose.Schema.Types.ObjectId;
  16. const schema = new Schema<UserGroupDocument, UserGroupModel>({
  17. name: { type: String, required: true, unique: true },
  18. parent: { type: ObjectId, ref: 'UserGroup', index: true },
  19. description: { type: String, default: '' },
  20. }, {
  21. timestamps: true,
  22. });
  23. schema.plugin(mongoosePaginate);
  24. const PAGE_ITEMS = 10;
  25. schema.statics.findUserGroupsWithPagination = function(opts) {
  26. const query = { parent: null };
  27. const options = Object.assign({}, opts);
  28. if (options.page == null) {
  29. options.page = 1;
  30. }
  31. if (options.limit == null) {
  32. options.limit = PAGE_ITEMS;
  33. }
  34. return this.paginate(query, options)
  35. .catch((err) => {
  36. // debug('Error on pagination:', err); TODO: add logger
  37. });
  38. };
  39. schema.statics.findChildUserGroupsByParentIds = async function(parentIds, includeGrandChildren = false) {
  40. if (!Array.isArray(parentIds)) {
  41. throw Error('parentIds must be an array.');
  42. }
  43. const childUserGroups = await this.find({ parent: { $in: parentIds } });
  44. let grandChildUserGroups: UserGroupDocument[] | null = null;
  45. if (includeGrandChildren) {
  46. const childUserGroupIds = childUserGroups.map(group => group._id);
  47. grandChildUserGroups = await this.find({ parent: { $in: childUserGroupIds } });
  48. }
  49. return {
  50. childUserGroups,
  51. grandChildUserGroups,
  52. };
  53. };
  54. schema.statics.countUserGroups = function() {
  55. return this.estimatedDocumentCount();
  56. };
  57. schema.statics.createGroup = async function(name, description, parentId) {
  58. // create without parent
  59. if (parentId == null) {
  60. return this.create({ name, description });
  61. }
  62. // create with parent
  63. const parent = await this.findOne({ _id: parentId });
  64. if (parent == null) {
  65. throw Error('Parent does not exist.');
  66. }
  67. return this.create({ name, description, parent });
  68. };
  69. /**
  70. * Find all ancestor groups starting from the UserGroup of the initial "group".
  71. * Set "ancestors" as "[]" if the initial group is unnecessary as result.
  72. * @param groups UserGroupDocument
  73. * @param ancestors UserGroupDocument[]
  74. * @returns UserGroupDocument[]
  75. */
  76. schema.statics.findGroupsWithAncestorsRecursively = async function(group, ancestors = [group]) {
  77. if (group == null) {
  78. return ancestors;
  79. }
  80. const parent = await this.findOne({ _id: group.parent });
  81. if (parent == null) {
  82. return ancestors;
  83. }
  84. ancestors.unshift(parent);
  85. return this.findGroupsWithAncestorsRecursively(parent, ancestors);
  86. };
  87. /**
  88. * Find all descendant groups starting from the UserGroups in the initial groups in "groups".
  89. * Set "descendants" as "[]" if the initial groups are unnecessary as result.
  90. * @param groups UserGroupDocument[] including at least one UserGroup
  91. * @param descendants UserGroupDocument[]
  92. * @returns UserGroupDocument[]
  93. */
  94. schema.statics.findGroupsWithDescendantsRecursively = async function(groups, descendants = groups) {
  95. const nextGroups = await this.find({ parent: { $in: groups.map(g => g._id) } });
  96. if (nextGroups.length === 0) {
  97. return descendants;
  98. }
  99. return this.findGroupsWithDescendantsRecursively(nextGroups, descendants.concat(nextGroups));
  100. };
  101. schema.statics.findGroupsWithDescendantsById = async function(groupId) {
  102. const root = await this.findOne({ _id: groupId });
  103. if (root == null) {
  104. throw Error('The root user group does not exist');
  105. }
  106. return this.findGroupsWithDescendantsRecursively([root]);
  107. };
  108. export default getOrCreateModel<UserGroupDocument, UserGroupModel>('UserGroup', schema);