user-group.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.findAllGroups = function(option) {
  29. var UserGroup = this;
  30. return new Promise(function(resolve, reject) {
  31. UserGroup
  32. .find()
  33. .exec(function (err, userGroupData) {
  34. if (err) {
  35. return reject(err);
  36. }
  37. return resolve(userGroupData);
  38. });
  39. });
  40. };
  41. // ページネーション利用のグループ検索
  42. userGroupSchema.statics.findUserGroupsWithPagination = function(options, callback) {
  43. var sort = options.sort || {name: 1, createdAt: 1};
  44. this.paginate({}, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
  45. if (err) {
  46. debug('Error on pagination:', err);
  47. return callback(err, null);
  48. }
  49. return callback(err, result);
  50. }, { sortBy : sort });
  51. };
  52. // TBD: グループ名によるグループ検索
  53. userGroupSchema.statics.findUserGroupByName = function(name) {
  54. var UserGroup = this;
  55. return new Promise(function(resolve, reject) {
  56. UserGroup.findOne({name: name}, function (err, userGroupData) {
  57. if (err) {
  58. return reject(err);
  59. }
  60. return resolve(userGroupData);
  61. });
  62. });
  63. };
  64. // 登録可能グループ名確認
  65. userGroupSchema.statics.isRegisterableName = function(name, callback) {
  66. var UserGroup = this;
  67. var userGroupnameUsable = true;
  68. this.findOne({name: name}, function (err, userGroupData) {
  69. if (userGroupData) {
  70. userGroupnameUsable = false;
  71. }
  72. return callback(userGroupnameUsable);
  73. });
  74. };
  75. // グループの完全削除
  76. userGroupSchema.statics.removeCompletelyById = function(id, callback) {
  77. var UserGroup = this;
  78. UserGroup.findById(id, function (err, userGroupData) {
  79. if (!userGroupData) {
  80. return callback(err, null);
  81. }
  82. debug('Removing userGroup:', userGroupData);
  83. userGroupData.remove(function(err) {
  84. if (err) {
  85. return callback(err, null);
  86. }
  87. return callback(null, 1);
  88. });
  89. });
  90. };
  91. // TBD: グループ生成(名前が要る)
  92. userGroupSchema.statics.createGroupByName = function(name, callback) {
  93. var UserGroup = this
  94. , newUserGroup = new UserGroup();
  95. newUserGroup.name = name;
  96. newUserGroup.createdAt = Date.now();
  97. newUserGroup.save(function(err, userGroupData) {
  98. return callback(err, userGroupData);
  99. });
  100. };
  101. // TBD: グループ画像パスの生成
  102. userGroupSchema.statics.createGroupPictureFilePath = function(userGroup, name) {
  103. var ext = '.' + name.match(/(.*)(?:\.([^.]+$))/)[2];
  104. return 'userGroup/' + userGroup._id + ext;
  105. };
  106. // userGroupSchema.statics.USER_GROUP_PUBLIC_FIELDS = USER_GROUP_PUBLIC_FIELDS;
  107. userGroupSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  108. return mongoose.model('UserGroup', userGroupSchema);
  109. };