userGroup.js 5.9 KB

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