user-group.ts 3.9 KB

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