user-group.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. const debug = require('debug')('growi:models:userGroup');
  2. const mongoose = require('mongoose');
  3. const mongoosePaginate = require('mongoose-paginate-v2');
  4. /*
  5. * define schema
  6. */
  7. const ObjectIdType = mongoose.Schema.Types.ObjectId;
  8. const ObjectId = mongoose.Types.ObjectId;
  9. const schema = new mongoose.Schema({
  10. userGroupId: String,
  11. name: { type: String, required: true, unique: true },
  12. createdAt: { type: Date, default: Date.now },
  13. parent: { type: ObjectIdType, ref: 'UserGroup', index: true },
  14. description: { type: String },
  15. });
  16. schema.plugin(mongoosePaginate);
  17. class UserGroup {
  18. /**
  19. * public fields for UserGroup model
  20. *
  21. * @readonly
  22. * @static
  23. * @memberof UserGroup
  24. */
  25. static get USER_GROUP_PUBLIC_FIELDS() {
  26. return '_id name createdAt parent description';
  27. }
  28. /**
  29. * limit items num for pagination
  30. *
  31. * @readonly
  32. * @static
  33. * @memberof UserGroup
  34. */
  35. static get PAGE_ITEMS() {
  36. return 10;
  37. }
  38. /*
  39. * model static methods
  40. */
  41. // Generate image path
  42. static createUserGroupPictureFilePath(userGroup, name) {
  43. const ext = `.${name.match(/(.*)(?:\.([^.]+$))/)[2]}`;
  44. return `userGroup/${userGroup._id}${ext}`;
  45. }
  46. /**
  47. * find all entities with pagination
  48. *
  49. * @see https://github.com/edwardhotchkiss/mongoose-paginate
  50. *
  51. * @static
  52. * @param {any} opts mongoose-paginate options object
  53. * @returns {Promise<any>} mongoose-paginate result object
  54. * @memberof UserGroup
  55. */
  56. static findUserGroupsWithPagination(opts) {
  57. const query = {};
  58. const options = Object.assign({}, opts);
  59. if (options.page == null) {
  60. options.page = 1;
  61. }
  62. if (options.limit == null) {
  63. options.limit = UserGroup.PAGE_ITEMS;
  64. }
  65. return this.paginate(query, options)
  66. .catch((err) => {
  67. debug('Error on pagination:', err);
  68. });
  69. }
  70. // Check if registerable
  71. static isRegisterableName(name) {
  72. const query = { name };
  73. return this.findOne(query)
  74. .then((userGroupData) => {
  75. return (userGroupData == null);
  76. });
  77. }
  78. // Delete completely
  79. static async removeCompletelyById(deleteGroupId, action, transferToUserGroupId, user) {
  80. const UserGroupRelation = mongoose.model('UserGroupRelation');
  81. const groupToDelete = await this.findById(deleteGroupId);
  82. if (groupToDelete == null) {
  83. throw new Error('UserGroup data is not exists. id:', deleteGroupId);
  84. }
  85. const deletedGroup = await groupToDelete.remove();
  86. await Promise.all([
  87. UserGroupRelation.removeAllByUserGroup(deletedGroup),
  88. UserGroup.crowi.pageService.handlePrivatePagesForDeletedGroup(deletedGroup, action, transferToUserGroupId, user),
  89. ]);
  90. return deletedGroup;
  91. }
  92. static countUserGroups() {
  93. return this.estimatedDocumentCount();
  94. }
  95. static async createGroup(name, description, parentId) {
  96. // create without parent
  97. if (parentId == null) {
  98. return this.create({ name, description });
  99. }
  100. // create with parent
  101. const parent = await this.findOne({ _id: parentId });
  102. if (parent == null) {
  103. throw Error('Parent does not exist.');
  104. }
  105. return this.create({ name, description, parent });
  106. }
  107. async updateName(name) {
  108. this.name = name;
  109. await this.save();
  110. }
  111. }
  112. module.exports = function(crowi) {
  113. UserGroup.crowi = crowi;
  114. schema.loadClass(UserGroup);
  115. return mongoose.model('UserGroup', schema);
  116. };