user-group.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. return this.findById(id)
  88. .then((userGroupData) => {
  89. if (userGroupData == null) {
  90. throw new Exception('UserGroup data is not exists. id:', id);
  91. }
  92. else {
  93. return userGroupData.remove();
  94. }
  95. });
  96. }
  97. // グループ生成(名前が要る)
  98. static createGroupByName(name) {
  99. return this.create({name: name});
  100. }
  101. /*
  102. * instance methods
  103. */
  104. // グループ画像の更新
  105. updateImage(image) {
  106. this.image = image;
  107. return this.save();
  108. }
  109. // グループ画像の削除
  110. deleteImage() {
  111. return this.updateImage(null);
  112. }
  113. // グループ名の更新
  114. updateName(name) {
  115. // 名前を設定して更新
  116. this.name = name;
  117. return this.save();
  118. }
  119. }
  120. module.exports = function(crowi) {
  121. UserGroup.crowi = crowi;
  122. schema.loadClass(UserGroup);
  123. return mongoose.model('UserGroup', schema);
  124. };