user-group.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 = 10
  8. , userGroupSchema;
  9. userGroupSchema = new mongoose.Schema({
  10. userGroupId: String,
  11. image: String,
  12. name: { type: String, required: true, unique: 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. console.log('');
  63. return new Promise(function(resolve, reject) {
  64. UserGroup
  65. .find()
  66. .exec(function (err, userGroupData) {
  67. if (err) {
  68. return reject(err);
  69. }
  70. return resolve(userGroupData);
  71. });
  72. });
  73. };
  74. // TBD: IDによるグループ検索
  75. // userGroupSchema.statics.findGroupsByIds = function(ids, option) {
  76. // var UserGroup = this;
  77. // var option = option || {}
  78. // , sort = option.sort || {createdAt: -1}
  79. // , fields = option.fields || USER_GROUP_PUBLIC_FIELDS
  80. // ;
  81. // return new Promise(function(resolve, reject) {
  82. // UserGroup
  83. // .find({ _id: { $in: ids }})
  84. // .select(fields)
  85. // .sort(sort)
  86. // .exec(function (err, userGroupData) {
  87. // if (err) {
  88. // return reject(err);
  89. // }
  90. // return resolve(userGroupData);
  91. // });
  92. // });
  93. // };
  94. // ページネーション利用のグループ検索
  95. userGroupSchema.statics.findUserGroupsWithPagination = function(options, callback) {
  96. var sort = options.sort || {name: 1, createdAt: 1};
  97. this.paginate({}, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
  98. if (err) {
  99. debug('Error on pagination:', err);
  100. return callback(err, null);
  101. }
  102. return callback(err, result);
  103. }, { sortBy : sort });
  104. };
  105. // TBD: グループ名によるグループ検索
  106. userGroupSchema.statics.findUserGroupByName = function(name) {
  107. var UserGroup = this;
  108. return new Promise(function(resolve, reject) {
  109. UserGroup.findOne({name: name}, function (err, userGroupData) {
  110. if (err) {
  111. return reject(err);
  112. }
  113. return resolve(userGroupData);
  114. });
  115. });
  116. };
  117. // TBD: 登録可能グループ名確認
  118. userGroupSchema.statics.isRegisterableName = function(name, callback) {
  119. var UserGroup = this;
  120. var userGroupnameUsable = true;
  121. this.findOne({name: name}, function (err, userGroupData) {
  122. if (userGroupData) {
  123. userGroupnameUsable = false;
  124. }
  125. return callback(userGroupnameUsable);
  126. });
  127. };
  128. // TBD: グループの完全削除
  129. userGroupSchema.statics.removeCompletelyById = function(id, callback) {
  130. var UserGroup = this;
  131. UserGroup.findById(id, function (err, userGroupData) {
  132. if (!userGroupData) {
  133. return callback(err, null);
  134. }
  135. debug('Removing userGroup:', userGroupData);
  136. userGroupData.remove(function(err) {
  137. if (err) {
  138. return callback(err, null);
  139. }
  140. return callback(null, 1);
  141. });
  142. });
  143. };
  144. // TBD: グループ生成(名前が要る)
  145. userGroupSchema.statics.createGroupByName = function(name, callback) {
  146. var UserGroup = this
  147. , newUserGroup = new UserGroup();
  148. newUserGroup.name = name;
  149. newUserGroup.createdAt = Date.now();
  150. newUserGroup.save(function(err, userGroupData) {
  151. return callback(err, userGroupData);
  152. });
  153. };
  154. // TBD: グループ画像パスの生成
  155. userGroupSchema.statics.createGroupPictureFilePath = function(userGroup, name) {
  156. var ext = '.' + name.match(/(.*)(?:\.([^.]+$))/)[2];
  157. return 'userGroup/' + userGroup._id + ext;
  158. };
  159. // userGroupSchema.statics.USER_GROUP_PUBLIC_FIELDS = USER_GROUP_PUBLIC_FIELDS;
  160. userGroupSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  161. return mongoose.model('UserGroup', userGroupSchema);
  162. };