user-group.js 3.0 KB

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