user-group-relation.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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('findAllRelations 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 (userGroup) {
  36. debug('findAllRelation is called', userGroup);
  37. var UserGroupRelation = this;
  38. return new Promise(function (resolve, reject) {
  39. UserGroupRelation
  40. .find({ relatedGroup: userGroup })
  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.findAllRelationForUserGroups = function (userGroups, callback) {
  52. debug('findAllRelations is called', userGroups);
  53. var UserGroupRelation = this;
  54. var groupRelations = new Map();
  55. return new Promise(function (resolve, reject) {
  56. UserGroupRelation
  57. .find({ relatedGroup: { $in: userGroups} })
  58. .populate('relatedUser')
  59. .exec(function (err, userGroupRelationData) {
  60. if (err) {
  61. return reject(err);
  62. }
  63. debug(userGroupRelationData);
  64. return resolve(userGroupRelationData);
  65. });
  66. });
  67. };
  68. // ページネーション利用の検索
  69. userGroupRelationSchema.statics.findUserGroupRelationsWithPagination = function (userGroup, options, callback) {
  70. this.paginate({ relatedGroup: userGroup }, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
  71. if (err) {
  72. debug('Error on pagination:', err);
  73. return callback(err, null);
  74. }
  75. return callback(err, result);
  76. });
  77. };
  78. // 関係性の生成
  79. userGroupRelationSchema.statics.createRelation = function(userGroup, user, callback) {
  80. var UserGroupRelation = this
  81. , newUserGroupRelation = new UserGroupRelation();
  82. if (userGroup == null || user == null) {
  83. return callback(new Error('userGroup or user is null'));
  84. }
  85. newUserGroupRelation.relatedGroup = userGroup;
  86. newUserGroupRelation.relatedUser = user;
  87. newUserGroupRelation.createdAt = Date.now();
  88. debug('create new user-group-relation ', newUserGroupRelation);
  89. newUserGroupRelation.save(function(err, userGroupRelationData) {
  90. return callback(err, userGroupRelationData);
  91. });
  92. };
  93. // グループに紐づく関係性の全削除
  94. userGroupRelationSchema.statics.removeAllByUserGroup = function (userGroup, callback) {
  95. if (userGroup === null || userGroup === undefined) { return callback(null); }
  96. var UserGroupRelation = this
  97. var relations = UserGroupRelation.findAllRelation(userGroup);
  98. // 関係性削除の実装
  99. relations.array.forEach(relation => {
  100. UserGroupRelation.removeById(relation.id, function(err) {
  101. if (err) { return callback(err); }
  102. });
  103. });
  104. return callback(null);
  105. }
  106. // ユーザグループの関係性を削除
  107. userGroupRelationSchema.statics.removeById = function (id, callback) {
  108. var UserGroupRelation = this
  109. UserGroupRelation.findById(id, function (err, relationData) {
  110. if (err) {
  111. debug('Error on find a removing user-group-relation', err);
  112. return callback(err);
  113. }
  114. debug('relationData is ', relationData);
  115. if (relationData == null || relationData == undefined) {
  116. debug('Cannot find user group relation by id', id);
  117. return callback(new Error('Cannot find user group relation by id'));
  118. }
  119. relationData.remove(function(err) {
  120. if (err) {
  121. return callback(err);
  122. }
  123. return callback(null);
  124. });
  125. });
  126. }
  127. userGroupRelationSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  128. return mongoose.model('UserGroupRelation', userGroupRelationSchema);
  129. };