user-group.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. module.exports = function(crowi) {
  2. var debug = require('debug')('crowi:models:userGroup')
  3. , mongoose = require('mongoose')
  4. , mongoosePaginate = require('mongoose-paginate')
  5. , ObjectId = mongoose.Schema.Types.ObjectId
  6. , USER_GROUP_PUBLIC_FIELDS = '_id image name createdAt'
  7. , PAGE_ITEMS = 50
  8. , userGroupSchema;
  9. userGroupSchema = new mongoose.Schema({
  10. userGroupId: String,
  11. image: String,
  12. name: { type: String, required: true },
  13. createdAt: { type: Date, default: Date.now },
  14. });
  15. userGroupSchema.plugin(mongoosePaginate);
  16. // TBD: グループ画像の更新
  17. // userGroupSchema.methods.updateImage = function(image, callback) {
  18. // this.image = image;
  19. // this.save(function(err, userGroupData) {
  20. // return callback(err, userGroupData);
  21. // });
  22. // };
  23. // TBD: グループ画像の削除
  24. // userGroupSchema.methods.deleteImage = function(callback) {
  25. // return this.updateImage(null, callback);
  26. // };
  27. // グループ公開情報のフィルター
  28. userGroupSchema.statics.filterToPublicFields = function(userGroup) {
  29. debug('UserGroup is', typeof userGroup, userGroup);
  30. if (typeof userGroup !== 'object' || !userGroup._id) {
  31. return userGroup;
  32. }
  33. var filteredGroup = {};
  34. var fields = USER_GROUP_PUBLIC_FIELDS.split(' ');
  35. for (var i = 0; i < fields.length; i++) {
  36. var key = fields[i];
  37. if (userGroup[key]) {
  38. filteredGroup[key] = userGroup[key];
  39. }
  40. }
  41. return filteredGroup;
  42. };
  43. // TBD: グループ検索
  44. // userGroupSchema.statics.findGroups = function(options, callback) {
  45. // var sort = options.sort || {createdAt: 1};
  46. // this.find()
  47. // .sort(sort)
  48. // .skip(options.skip || 0)
  49. // .limit(options.limit || 21)
  50. // .exec(function (err, userGroupData) {
  51. // callback(err, userGroupData);
  52. // });
  53. // };
  54. // すべてのグループを取得(オプション指定可)
  55. userGroupSchema.statics.findAllGroups = function(option) {
  56. debug('NoErrorOccured');
  57. var UserGroup = this;
  58. var option = option || {}
  59. , sort = option.sort || {createdAt: -1}
  60. , fields = option.fields || USER_GROUP_PUBLIC_FIELDS
  61. ;
  62. return new Promise(function(resolve, reject) {
  63. UserGroup
  64. .find()
  65. .select(fields)
  66. .sort(sort)
  67. .exec(function (err, userGroupData) {
  68. if (err) {
  69. return reject(err);
  70. }
  71. return resolve(userGroupData);
  72. });
  73. });
  74. };
  75. // TBD: IDによるグループ検索
  76. // userGroupSchema.statics.findGroupsByIds = function(ids, option) {
  77. // var UserGroup = this;
  78. // var option = option || {}
  79. // , sort = option.sort || {createdAt: -1}
  80. // , fields = option.fields || USER_GROUP_PUBLIC_FIELDS
  81. // ;
  82. // return new Promise(function(resolve, reject) {
  83. // UserGroup
  84. // .find({ _id: { $in: ids }})
  85. // .select(fields)
  86. // .sort(sort)
  87. // .exec(function (err, userGroupData) {
  88. // if (err) {
  89. // return reject(err);
  90. // }
  91. // return resolve(userGroupData);
  92. // });
  93. // });
  94. // };
  95. // ページネーション利用のグループ検索
  96. userGroupSchema.statics.findUserGroupsWithPagination = function(options, callback) {
  97. var sort = options.sort || {name: 1, createdAt: 1};
  98. // return callback(err, null);
  99. this.paginate({ page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
  100. if (err) {
  101. debug('Error on pagination:', err);
  102. return callback(err, null);
  103. }
  104. return callback(err, result);
  105. }, { sortBy : sort });
  106. };
  107. // TBD: グループ名によるグループ検索
  108. // userGroupSchema.statics.findUserGroupByName = function(name) {
  109. // var UserGroup = this;
  110. // return new Promise(function(resolve, reject) {
  111. // UserGroup.findOne({name: name}, function (err, userGroupData) {
  112. // if (err) {
  113. // return reject(err);
  114. // }
  115. // return resolve(userGroupData);
  116. // });
  117. // });
  118. // };
  119. // TBD: 登録可能グループ名確認
  120. // userGroupSchema.statics.isRegisterableName = function(name, callback) {
  121. // var UserGroup = this;
  122. // var userGroupnameUsable = true;
  123. // this.findOne({name: name}, function (err, userGroupData) {
  124. // if (userGroupData) {
  125. // userGroupnameUsable = false;
  126. // }
  127. // return callback(userGroupnameUsable);
  128. // });
  129. // };
  130. // TBD: グループの完全削除
  131. // userGroupSchema.statics.removeCompletelyById = function(id, callback) {
  132. // var UserGroup = this;
  133. // UserGroup.findById(id, function (err, userGroupData) {
  134. // if (!userGroupData) {
  135. // return callback(err, null);
  136. // }
  137. // debug('Removing userGroup:', userGroupData);
  138. // userGroupData.remove(function(err) {
  139. // if (err) {
  140. // return callback(err, null);
  141. // }
  142. // return callback(null, 1);
  143. // });
  144. // });
  145. // };
  146. // TBD: グループ生成(名前が要る)
  147. userGroupSchema.statics.createGroupByName = function(name, callback) {
  148. var UserGroup = this
  149. , newUserGroup = new UserGroup();
  150. newUserGroup.name = name;
  151. newUserGroup.createdAt = Date.now();
  152. newUserGroup.save(function(err, userGroupData) {
  153. return callback(err, userGroupData);
  154. });
  155. };
  156. // TBD: グループ画像パスの生成
  157. // userGroupSchema.statics.createGroupPictureFilePath = function(userGroup, name) {
  158. // var ext = '.' + name.match(/(.*)(?:\.([^.]+$))/)[2];
  159. // return 'userGroup/' + userGroup._id + ext;
  160. // };
  161. userGroupSchema.statics.USER_GROUP_PUBLIC_FIELDS = USER_GROUP_PUBLIC_FIELDS;
  162. userGroupSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  163. return mongoose.model('UserGroup', userGroupSchema);
  164. };