user-group-relation.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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('findAllRelationForUserGroup 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.findAllRelationForUser = function (user) {
  52. debug('findAllRelationForUser is called');
  53. var UserGroupRelation = this;
  54. return new Promise(function (resolve, reject) {
  55. UserGroupRelation
  56. .find({ relatedUser: user.id })
  57. .populate('relatedGroup')
  58. .exec(function (err, userGroupRelationData) {
  59. if (err) {
  60. return reject(err);
  61. }
  62. return resolve(userGroupRelationData);
  63. });
  64. });
  65. };
  66. // 指定グループリストに対するすべてのグループ所属関係を取得
  67. userGroupRelationSchema.statics.findAllRelationForUserGroups = function (userGroups, callback) {
  68. debug('findAllRelations is called', userGroups);
  69. var UserGroupRelation = this;
  70. var groupRelations = new Map();
  71. return new Promise(function (resolve, reject) {
  72. UserGroupRelation
  73. .find({ relatedGroup: { $in: userGroups} })
  74. .populate('relatedUser')
  75. .exec(function (err, userGroupRelationData) {
  76. if (err) {
  77. return reject(err);
  78. }
  79. debug(userGroupRelationData);
  80. return resolve(userGroupRelationData);
  81. });
  82. });
  83. };
  84. // ページネーション利用の検索
  85. userGroupRelationSchema.statics.findUserGroupRelationsWithPagination = function (userGroup, options, callback) {
  86. this.paginate({ relatedGroup: userGroup }, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
  87. if (err) {
  88. debug('Error on pagination:', err);
  89. return callback(err, null);
  90. }
  91. return callback(err, result);
  92. });
  93. };
  94. // グループとユーザを指定し、関係性が存在するかチェック
  95. userGroupRelationSchema.statics.checkIsRelatedUserForGroup = function (userData, userGroup, callback) {
  96. var UserGroupRelation = this;
  97. return new Promise(function (resolve, reject) {
  98. UserGroupRelation.count( {relatedGroup: userGroup.id, relatedUser: userData.id}, function (err, count) {
  99. if (err) {
  100. return reject(err);
  101. }
  102. debug('checkIsRelatedUserForGroup count : ', count);
  103. return resolve((0 < count));
  104. });
  105. });
  106. };
  107. // 関係性の生成
  108. userGroupRelationSchema.statics.createRelation = function(userGroup, user, callback) {
  109. var UserGroupRelation = this
  110. , newUserGroupRelation = new UserGroupRelation();
  111. if (userGroup == null || user == null) {
  112. return callback(new Error('userGroup or user is null'));
  113. }
  114. newUserGroupRelation.relatedGroup = userGroup;
  115. newUserGroupRelation.relatedUser = user;
  116. newUserGroupRelation.createdAt = Date.now();
  117. debug('create new user-group-relation ', newUserGroupRelation);
  118. newUserGroupRelation.save(function(err, userGroupRelationData) {
  119. return callback(err, userGroupRelationData);
  120. });
  121. };
  122. // グループに紐づく関係性の全削除
  123. userGroupRelationSchema.statics.removeAllByUserGroup = function (userGroup, callback) {
  124. if (userGroup === null || userGroup === undefined) { return callback(null); }
  125. var UserGroupRelation = this
  126. var relations = UserGroupRelation.findAllRelation(userGroup);
  127. // 関係性削除の実装
  128. relations.array.forEach(relation => {
  129. UserGroupRelation.removeById(relation.id, function(err) {
  130. if (err) { return callback(err); }
  131. });
  132. });
  133. return callback(null);
  134. }
  135. // ユーザグループの関係性を削除
  136. userGroupRelationSchema.statics.removeById = function (id, callback) {
  137. var UserGroupRelation = this
  138. UserGroupRelation.findById(id, function (err, relationData) {
  139. if (err) {
  140. debug('Error on find a removing user-group-relation', err);
  141. return callback(err);
  142. }
  143. debug('relationData is ', relationData);
  144. if (relationData == null || relationData == undefined) {
  145. debug('Cannot find user group relation by id', id);
  146. return callback(new Error('Cannot find user group relation by id'));
  147. }
  148. relationData.remove(function(err) {
  149. if (err) {
  150. return callback(err);
  151. }
  152. return callback(null);
  153. });
  154. });
  155. }
  156. userGroupRelationSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  157. return mongoose.model('UserGroupRelation', userGroupRelationSchema);
  158. };