user-group-relation.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.checkIsRelatedUserForGroup = function (userData, userGroup, callback) {
  80. var UserGroupRelation = this;
  81. return new Promise(function (resolve, reject) {
  82. UserGroupRelation.count( {relatedGroup: userGroup, relatedUser: userData}, function (err, count) {
  83. if (err) {
  84. return reject(err);
  85. }
  86. // isRelated == (1 < count)
  87. return resolve((1 < count));
  88. });
  89. });
  90. };
  91. // 関係性の生成
  92. userGroupRelationSchema.statics.createRelation = function(userGroup, user, callback) {
  93. var UserGroupRelation = this
  94. , newUserGroupRelation = new UserGroupRelation();
  95. if (userGroup == null || user == null) {
  96. return callback(new Error('userGroup or user is null'));
  97. }
  98. newUserGroupRelation.relatedGroup = userGroup;
  99. newUserGroupRelation.relatedUser = user;
  100. newUserGroupRelation.createdAt = Date.now();
  101. debug('create new user-group-relation ', newUserGroupRelation);
  102. newUserGroupRelation.save(function(err, userGroupRelationData) {
  103. return callback(err, userGroupRelationData);
  104. });
  105. };
  106. // グループに紐づく関係性の全削除
  107. userGroupRelationSchema.statics.removeAllByUserGroup = function (userGroup, callback) {
  108. if (userGroup === null || userGroup === undefined) { return callback(null); }
  109. var UserGroupRelation = this
  110. var relations = UserGroupRelation.findAllRelation(userGroup);
  111. // 関係性削除の実装
  112. relations.array.forEach(relation => {
  113. UserGroupRelation.removeById(relation.id, function(err) {
  114. if (err) { return callback(err); }
  115. });
  116. });
  117. return callback(null);
  118. }
  119. // ユーザグループの関係性を削除
  120. userGroupRelationSchema.statics.removeById = function (id, callback) {
  121. var UserGroupRelation = this
  122. UserGroupRelation.findById(id, function (err, relationData) {
  123. if (err) {
  124. debug('Error on find a removing user-group-relation', err);
  125. return callback(err);
  126. }
  127. debug('relationData is ', relationData);
  128. if (relationData == null || relationData == undefined) {
  129. debug('Cannot find user group relation by id', id);
  130. return callback(new Error('Cannot find user group relation by id'));
  131. }
  132. relationData.remove(function(err) {
  133. if (err) {
  134. return callback(err);
  135. }
  136. return callback(null);
  137. });
  138. });
  139. }
  140. userGroupRelationSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  141. return mongoose.model('UserGroupRelation', userGroupRelationSchema);
  142. };