user-group.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // TBD: グループ名によるグループ検索
  73. static findUserGroupByName(name) {
  74. const query = { name: name };
  75. return this.findOne(query);
  76. }
  77. // 登録可能グループ名確認
  78. static isRegisterableName(name) {
  79. const query = { name: name };
  80. return this.findOne(query)
  81. .then((userGroupData) => {
  82. return (userGroupData == null);
  83. });
  84. }
  85. // グループの完全削除
  86. static removeCompletelyById(id) {
  87. const PageGroupRelation = mongoose.model('PageGroupRelation');
  88. const UserGroupRelation = mongoose.model('UserGroupRelation');
  89. let removed = undefined;
  90. return this.findById(id)
  91. .then(userGroupData => {
  92. if (userGroupData == null) {
  93. throw new Exception('UserGroup data is not exists. id:', id);
  94. }
  95. return userGroupData.remove();
  96. })
  97. .then(removedUserGroupData => {
  98. removed = removedUserGroupData;
  99. })
  100. // remove relations
  101. .then(() => {
  102. return Promise.all([
  103. UserGroupRelation.removeAllByUserGroup(removed),
  104. PageGroupRelation.removeAllByUserGroup(removed),
  105. ]);
  106. })
  107. .then(() => {
  108. return removed;
  109. });
  110. }
  111. // グループ生成(名前が要る)
  112. static createGroupByName(name) {
  113. return this.create({name: name});
  114. }
  115. /*
  116. * instance methods
  117. */
  118. // グループ画像の更新
  119. updateImage(image) {
  120. this.image = image;
  121. return this.save();
  122. }
  123. // グループ画像の削除
  124. deleteImage() {
  125. return this.updateImage(null);
  126. }
  127. // グループ名の更新
  128. updateName(name) {
  129. // 名前を設定して更新
  130. this.name = name;
  131. return this.save();
  132. }
  133. }
  134. module.exports = function(crowi) {
  135. UserGroup.crowi = crowi;
  136. schema.loadClass(UserGroup);
  137. return mongoose.model('UserGroup', schema);
  138. };