user-group.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const debug = require('debug')('growi:models:userGroup');
  2. const mongoose = require('mongoose');
  3. const mongoosePaginate = require('mongoose-paginate');
  4. const ObjectId = mongoose.Schema.Types.ObjectId;
  5. /*
  6. * define schema
  7. */
  8. const schema = new mongoose.Schema({
  9. userGroupId: String,
  10. image: String,
  11. name: { type: String, required: true, unique: true },
  12. createdAt: { type: Date, default: Date.now },
  13. });
  14. schema.plugin(mongoosePaginate);
  15. class UserGroup {
  16. /**
  17. * public fields for UserGroup model
  18. *
  19. * @readonly
  20. * @static
  21. * @memberof UserGroup
  22. */
  23. static get USER_GROUP_PUBLIC_FIELDS() {
  24. return '_id image name createdAt';
  25. }
  26. /**
  27. * limit items num for pagination
  28. *
  29. * @readonly
  30. * @static
  31. * @memberof UserGroup
  32. */
  33. static get PAGE_ITEMS() {
  34. return 10;
  35. }
  36. /*
  37. * model static methods
  38. */
  39. // グループ画像パスの生成
  40. static createUserGroupPictureFilePath(userGroup, name) {
  41. var ext = '.' + name.match(/(.*)(?:\.([^.]+$))/)[2];
  42. return 'userGroup/' + userGroup._id + ext;
  43. }
  44. // すべてのグループを取得(オプション指定可)
  45. static findAllGroups(option) {
  46. return this.find().exec();
  47. }
  48. /**
  49. * find all entities with pagination
  50. *
  51. * @see https://github.com/edwardhotchkiss/mongoose-paginate
  52. *
  53. * @static
  54. * @param {any} opts mongoose-paginate options object
  55. * @returns {Promise<any>} mongoose-paginate result object
  56. * @memberof UserGroup
  57. */
  58. static findUserGroupsWithPagination(opts) {
  59. const query = {};
  60. const options = Object.assign({}, opts);
  61. if (options.page == null) {
  62. options.page = 1;
  63. }
  64. if (options.limit == null) {
  65. options.limit = UserGroup.PAGE_ITEMS;
  66. }
  67. return this.paginate(query, options)
  68. .catch((err) => {
  69. debug('Error on pagination:', err);
  70. });
  71. }
  72. // 登録可能グループ名確認
  73. static isRegisterableName(name) {
  74. const query = { name: name };
  75. return this.findOne(query)
  76. .then((userGroupData) => {
  77. return (userGroupData == null);
  78. });
  79. }
  80. // グループの完全削除
  81. static removeCompletelyById(id) {
  82. const PageGroupRelation = mongoose.model('PageGroupRelation');
  83. const UserGroupRelation = mongoose.model('UserGroupRelation');
  84. let removed = undefined;
  85. return this.findById(id)
  86. .then(userGroupData => {
  87. if (userGroupData == null) {
  88. throw new Exception('UserGroup data is not exists. id:', id);
  89. }
  90. return userGroupData.remove();
  91. })
  92. .then(removedUserGroupData => {
  93. removed = removedUserGroupData;
  94. })
  95. // remove relations
  96. .then(() => {
  97. return Promise.all([
  98. UserGroupRelation.removeAllByUserGroup(removed),
  99. PageGroupRelation.removeAllByUserGroup(removed),
  100. ]);
  101. })
  102. .then(() => {
  103. return removed;
  104. });
  105. }
  106. // グループ生成(名前が要る)
  107. static createGroupByName(name) {
  108. return this.create({name: name});
  109. }
  110. /*
  111. * instance methods
  112. */
  113. // グループ画像の更新
  114. updateImage(image) {
  115. this.image = image;
  116. return this.save();
  117. }
  118. // グループ画像の削除
  119. deleteImage() {
  120. return this.updateImage(null);
  121. }
  122. // グループ名の更新
  123. updateName(name) {
  124. // 名前を設定して更新
  125. this.name = name;
  126. return this.save();
  127. }
  128. }
  129. module.exports = function(crowi) {
  130. UserGroup.crowi = crowi;
  131. schema.loadClass(UserGroup);
  132. return mongoose.model('UserGroup', schema);
  133. };