user-group.ts 3.9 KB

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