user-group.js 4.1 KB

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