user-group.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const debug = require('debug')('growi:models:userGroup');
  2. const mongoose = require('mongoose');
  3. const mongoosePaginate = require('mongoose-paginate');
  4. /*
  5. * define schema
  6. */
  7. const schema = new mongoose.Schema({
  8. userGroupId: String,
  9. name: { type: String, required: true, unique: true },
  10. createdAt: { type: Date, default: Date.now },
  11. });
  12. schema.plugin(mongoosePaginate);
  13. class UserGroup {
  14. /**
  15. * public fields for UserGroup model
  16. *
  17. * @readonly
  18. * @static
  19. * @memberof UserGroup
  20. */
  21. static get USER_GROUP_PUBLIC_FIELDS() {
  22. return '_id name createdAt';
  23. }
  24. /**
  25. * limit items num for pagination
  26. *
  27. * @readonly
  28. * @static
  29. * @memberof UserGroup
  30. */
  31. static get PAGE_ITEMS() {
  32. return 10;
  33. }
  34. /*
  35. * model static methods
  36. */
  37. // グループ画像パスの生成
  38. static createUserGroupPictureFilePath(userGroup, name) {
  39. const ext = `.${name.match(/(.*)(?:\.([^.]+$))/)[2]}`;
  40. return `userGroup/${userGroup._id}${ext}`;
  41. }
  42. // すべてのグループを取得(オプション指定可)
  43. static findAllGroups(option) {
  44. return this.find().exec();
  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. // 登録可能グループ名確認
  71. static isRegisterableName(name) {
  72. const query = { name };
  73. return this.findOne(query)
  74. .then((userGroupData) => {
  75. return (userGroupData == null);
  76. });
  77. }
  78. // グループの完全削除
  79. static async removeCompletelyById(deleteGroupId, action, selectedGroupId) {
  80. const PageGroupRelation = mongoose.model('PageGroupRelation');
  81. const UserGroupRelation = mongoose.model('UserGroupRelation');
  82. const Page = mongoose.model('Page');
  83. const groupToDelete = await this.findById(deleteGroupId);
  84. if (groupToDelete == null) {
  85. throw new Error('UserGroup data is not exists. id:', deleteGroupId);
  86. }
  87. const deletedGroup = await groupToDelete.remove();
  88. await Promise.all([
  89. UserGroupRelation.removeAllByUserGroup(deletedGroup),
  90. PageGroupRelation.removeAllByUserGroup(deletedGroup),
  91. Page.handlePrivatePagesForDeletedGroup(deletedGroup, action, selectedGroupId),
  92. ]);
  93. return deletedGroup;
  94. }
  95. // グループ生成(名前が要る)
  96. static createGroupByName(name) {
  97. return this.create({ name });
  98. }
  99. // グループ名の更新
  100. updateName(name) {
  101. // 名前を設定して更新
  102. this.name = name;
  103. return this.save();
  104. }
  105. }
  106. module.exports = function(crowi) {
  107. UserGroup.crowi = crowi;
  108. schema.loadClass(UserGroup);
  109. return mongoose.model('UserGroup', schema);
  110. };