user-group-relation.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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', required: true },
  11. relatedUser: { type: ObjectId, ref: 'User', required: true },
  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. // グループのIDとユーザのIDから関係性を取得
  95. userGroupRelationSchema.statics.findByGroupIdAndUser = function (userGroupId, userData, callback) {
  96. var UserGroupRelation = this;
  97. return new Promise(function (resolve, reject) {
  98. UserGroupRelation
  99. .findOne({ relatedGroup: userGroupId, relatedUser: userData.id })
  100. .populate('relatedUser')
  101. .populate('relatedGroup')
  102. .exec(function (err, userGroupRelationData) {
  103. if (err) {
  104. return reject(err);
  105. }
  106. debug(userGroupRelationData);
  107. return resolve(userGroupRelationData);
  108. });
  109. });
  110. }
  111. // グループとユーザを指定し、関係性が存在するかチェック
  112. userGroupRelationSchema.statics.checkIsRelatedUserForGroup = function (userData, userGroup) {
  113. var UserGroupRelation = this;
  114. debug('checkIsRelatedUserForGroup is called.', userData, userGroup);
  115. return new Promise(function (resolve, reject) {
  116. UserGroupRelation.count( {relatedGroup: userGroup.id, relatedUser: userData.id}, function (err, count) {
  117. if (err) {
  118. debug('An Error occured.', err);
  119. return reject(err);
  120. }
  121. debug('checkIsRelatedUserForGroup count : ', count);
  122. return resolve((0 < count));
  123. });
  124. });
  125. };
  126. // 関係性の生成
  127. userGroupRelationSchema.statics.createRelation = function(userGroup, user, callback) {
  128. var UserGroupRelation = this
  129. , newUserGroupRelation = new UserGroupRelation();
  130. if (userGroup == null || user == null) {
  131. return callback(new Error('userGroup or user is null'));
  132. }
  133. newUserGroupRelation.relatedGroup = userGroup;
  134. newUserGroupRelation.relatedUser = user;
  135. newUserGroupRelation.createdAt = Date.now();
  136. debug('create new user-group-relation ', newUserGroupRelation);
  137. newUserGroupRelation.save(function(err, userGroupRelationData) {
  138. return callback(err, userGroupRelationData);
  139. });
  140. };
  141. // グループに紐づく関係性の全削除
  142. userGroupRelationSchema.statics.removeAllByUserGroup = function (userGroup, callback) {
  143. if (userGroup === null || userGroup === undefined) { return callback(null); }
  144. var UserGroupRelation = this
  145. var relations = UserGroupRelation.findAllRelation(userGroup);
  146. // 関係性削除の実装
  147. relations.array.forEach(relation => {
  148. UserGroupRelation.removeById(relation.id, function(err) {
  149. if (err) { return callback(err); }
  150. });
  151. });
  152. return callback(null);
  153. }
  154. // ユーザグループの関係性を削除
  155. userGroupRelationSchema.statics.removeById = function (id, callback) {
  156. var UserGroupRelation = this
  157. UserGroupRelation.findById(id, function (err, relationData) {
  158. if (err) {
  159. debug('Error on find a removing user-group-relation', err);
  160. return callback(err);
  161. }
  162. debug('relationData is ', relationData);
  163. if (relationData == null || relationData == undefined) {
  164. debug('Cannot find user group relation by id', id);
  165. return callback(new Error('Cannot find user group relation by id'));
  166. }
  167. relationData.remove(function(err) {
  168. if (err) {
  169. return callback(err);
  170. }
  171. return callback(null);
  172. });
  173. });
  174. }
  175. userGroupRelationSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  176. return mongoose.model('UserGroupRelation', userGroupRelationSchema);
  177. };