user-group-relation.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. module.exports = function(crowi) {
  2. var debug = require('debug')('crowi:models:userGroupRelation')
  3. , mongoose = require('mongoose')
  4. , mongoosePaginate = require('mongoose-paginate')
  5. , ObjectId = mongoose.Schema.Types.ObjectId
  6. , PAGE_ITEMS = 50
  7. , userGroupRelationSchema;
  8. userGroupRelationSchema = new mongoose.Schema({
  9. userGroupRelationId: String,
  10. relatedGroup: { type: ObjectId, ref: 'UserGroup' },
  11. relatedUser: { type: ObjectId, ref: 'User' },
  12. createdAt: { type: Date, default: Date.now },
  13. },{
  14. toJSON: { getters: true },
  15. toObject: { getters: true }
  16. });
  17. userGroupRelationSchema.plugin(mongoosePaginate);
  18. // すべてのグループ所属関係を取得
  19. userGroupRelationSchema.statics.findAllRelation = function() {
  20. debug('findAllGroups is called');
  21. var UserGroupRelation = this;
  22. return new Promise(function(resolve, reject) {
  23. UserGroupRelation
  24. .find({ relatedGroup: group} )
  25. .populate('relatedUser')
  26. .exec(function (err, userGroupRelationData) {
  27. if (err) {
  28. return reject(err);
  29. }
  30. return resolve(userGroupRelationData);
  31. });
  32. });
  33. };
  34. // すべてのグループ所属関係を取得
  35. userGroupRelationSchema.statics.findAllRelationForUserGroup = function (group) {
  36. debug('findAllGroups is called', group);
  37. var UserGroupRelation = this;
  38. return new Promise(function (resolve, reject) {
  39. UserGroupRelation
  40. .find({ relatedGroup: group })
  41. .populate('relatedUser')
  42. .exec(function (err, userGroupRelationData) {
  43. if (err) {
  44. return reject(err);
  45. }
  46. return resolve(userGroupRelationData);
  47. });
  48. });
  49. };
  50. // ページネーション利用の検索
  51. userGroupRelationSchema.statics.findUserGroupRelationsWithPagination = function(options, callback) {
  52. this.paginate({}, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
  53. if (err) {
  54. debug('Error on pagination:', err);
  55. return callback(err, null);
  56. }
  57. return callback(err, result);
  58. });
  59. };
  60. // 関係性の生成
  61. userGroupRelationSchema.statics.createRelation = function(userGroup, user, callback) {
  62. var UserGroupRelation = this
  63. , newUserGroupRelation = new UserGroupRelation();
  64. newUserGroupRelation.relatedGroup = userGroup;
  65. newUserGroupRelation.relatedUser = user;
  66. newUserGroupRelation.createdAt = Date.now();
  67. debug('create new user-group-relation ', newUserGroupRelation);
  68. newUserGroupRelation.save(function(err, userGroupRelationData) {
  69. return callback(err, userGroupRelationData);
  70. });
  71. };
  72. // グループに紐づく関係性の全削除
  73. userGroupRelationSchema.statics.removeAllByUserGroup = function (userGroup, callback) {
  74. if (userGroup === null || userGroup === undefined) { return callback(null); }
  75. var UserGroupRelation = this
  76. var relations = UserGroupRelation.findAllRelation(userGroup);
  77. // 関係性削除の実装
  78. relations.array.forEach(relation => {
  79. UserGroupRelation.removeById(relation.id, function(err) {
  80. if (err) { return callback(err); }
  81. });
  82. });
  83. return callback(null);
  84. }
  85. // ユーザグループの関係性を削除
  86. userGroupRelationSchema.statics.removeById = function (id, callback) {
  87. var UserGroupRelation = this
  88. UserGroupRelation.findById(id, function (err, relationData) {
  89. debug(relationData);
  90. if (!relationData) {
  91. debug('Error on find a removing user-group-relation', err);
  92. return callback(err);
  93. }
  94. relationData.remove(function(err) {
  95. if (err) {
  96. return callback(err);
  97. }
  98. return callback(null);
  99. });
  100. });
  101. }
  102. userGroupRelationSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  103. return mongoose.model('UserGroupRelation', userGroupRelationSchema);
  104. };