user-group-relation.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. const debug = require('debug')('growi:models:userGroupRelation');
  2. const mongoose = require('mongoose');
  3. const mongoosePaginate = require('mongoose-paginate');
  4. const ObjectId = mongoose.Schema.Types.ObjectId;
  5. /*
  6. * define schema
  7. */
  8. const schema = new mongoose.Schema({
  9. relatedGroup: { type: ObjectId, ref: 'UserGroup', required: true },
  10. relatedUser: { type: ObjectId, ref: 'User', required: true },
  11. createdAt: { type: Date, default: Date.now, required: true },
  12. });
  13. schema.plugin(mongoosePaginate);
  14. /**
  15. * UserGroupRelation Class
  16. *
  17. * @class UserGroupRelation
  18. */
  19. class UserGroupRelation {
  20. /**
  21. * limit items num for pagination
  22. *
  23. * @readonly
  24. * @static
  25. * @memberof UserGroupRelation
  26. */
  27. static get PAGE_ITEMS() {
  28. return 50;
  29. }
  30. static set crowi(crowi) {
  31. this._crowi = crowi;
  32. }
  33. static get crowi() {
  34. return this._crowi;
  35. }
  36. /**
  37. * find all user and group relation
  38. *
  39. * @static
  40. * @returns {Promise<UserGroupRelation[]>}
  41. * @memberof UserGroupRelation
  42. */
  43. static findAllRelation() {
  44. return this
  45. .find()
  46. .populate('relatedUser')
  47. .populate('relatedGroup')
  48. .exec();
  49. }
  50. /**
  51. * find all user and group relation of UserGroup
  52. *
  53. * @static
  54. * @param {UserGroup} userGroup
  55. * @returns {Promise<UserGroupRelation[]>}
  56. * @memberof UserGroupRelation
  57. */
  58. static findAllRelationForUserGroup(userGroup) {
  59. debug('findAllRelationForUserGroup is called', userGroup);
  60. return this
  61. .find({ relatedGroup: userGroup })
  62. .populate('relatedUser')
  63. .exec();
  64. }
  65. /**
  66. * find all user and group relation of UserGroups
  67. *
  68. * @static
  69. * @param {UserGroup[]} userGroups
  70. * @returns {Promise<UserGroupRelation[]>}
  71. * @memberof UserGroupRelation
  72. */
  73. static findAllRelationForUserGroups(userGroups) {
  74. return this
  75. .find({ relatedGroup: { $in: userGroups } })
  76. .populate('relatedUser')
  77. .exec();
  78. }
  79. /**
  80. * find all user and group relation of User
  81. *
  82. * @static
  83. * @param {User} user
  84. * @returns {Promise<UserGroupRelation[]>}
  85. * @memberof UserGroupRelation
  86. */
  87. static findAllRelationForUser(user) {
  88. return this
  89. .find({ relatedUser: user.id })
  90. .populate('relatedGroup')
  91. // filter documents only relatedGroup is not null
  92. .then(userGroupRelations => {
  93. return userGroupRelations.filter(relation => {
  94. return relation.relatedGroup != null;
  95. });
  96. });
  97. }
  98. /**
  99. * find all entities with pagination
  100. *
  101. * @see https://github.com/edwardhotchkiss/mongoose-paginate
  102. *
  103. * @static
  104. * @param {UserGroup} userGroup
  105. * @param {any} opts mongoose-paginate options object
  106. * @returns {Promise<any>} mongoose-paginate result object
  107. * @memberof UserGroupRelation
  108. */
  109. static findUserGroupRelationsWithPagination(userGroup, opts) {
  110. const query = { relatedGroup: userGroup };
  111. const options = Object.assign({}, opts);
  112. if (options.page == null) {
  113. options.page = 1;
  114. }
  115. if (options.limit == null) {
  116. options.limit = UserGroupRelation.PAGE_ITEMS;
  117. }
  118. return this.paginate(query, options)
  119. .catch((err) => {
  120. debug('Error on pagination:', err);
  121. });
  122. }
  123. /**
  124. * find one result by related group id and related user
  125. *
  126. * @static
  127. * @param {string} userGroupId find query param for relatedGroup
  128. * @param {User} userData find query param for relatedUser
  129. * @returns {Promise<UserGroupRelation>}
  130. * @memberof UserGroupRelation
  131. */
  132. static findByGroupIdAndUser(userGroupId, userData) {
  133. const query = {
  134. relatedGroup: userGroupId,
  135. relatedUser: userData.id
  136. };
  137. return this
  138. .findOne(query)
  139. .populate('relatedUser')
  140. .populate('relatedGroup')
  141. .exec();
  142. }
  143. /**
  144. * find all "not" related user for UserGroup
  145. *
  146. * @static
  147. * @param {UserGroup} userGroup for find users not related
  148. * @returns {Promise<User>}
  149. * @memberof UserGroupRelation
  150. */
  151. static findUserByNotRelatedGroup(userGroup) {
  152. const User = UserGroupRelation.crowi.model('User');
  153. return this.findAllRelationForUserGroup(userGroup)
  154. .then((relations) => {
  155. const relatedUserIds = relations.map((relation) => {
  156. return relation.relatedUser.id;
  157. });
  158. const query = { _id: { $nin: relatedUserIds }, status: User.STATUS_ACTIVE };
  159. debug('findUserByNotRelatedGroup ', query);
  160. return User.find(query).exec();
  161. });
  162. }
  163. /**
  164. * get if the user has relation for group
  165. *
  166. * @static
  167. * @param {User} userData
  168. * @param {UserGroup} userGroup
  169. * @returns {Promise<boolean>} is user related for group(or not)
  170. * @memberof UserGroupRelation
  171. */
  172. static isRelatedUserForGroup(userData, userGroup) {
  173. const query = {
  174. relatedGroup: userGroup.id,
  175. relatedUser: userData.id
  176. };
  177. return this
  178. .count(query)
  179. .exec()
  180. .then((count) => {
  181. // return true or false of the relation is exists(not count)
  182. return (0 < count);
  183. });
  184. }
  185. /**
  186. * create user and group relation
  187. *
  188. * @static
  189. * @param {UserGroup} userGroup
  190. * @param {User} user
  191. * @returns {Promise<UserGroupRelation>} created relation
  192. * @memberof UserGroupRelation
  193. */
  194. static createRelation(userGroup, user) {
  195. return this.create({
  196. relatedGroup: userGroup.id,
  197. relatedUser: user.id
  198. });
  199. }
  200. /**
  201. * remove all relation for UserGroup
  202. *
  203. * @static
  204. * @param {UserGroup} userGroup related group for remove
  205. * @returns {Promise<any>}
  206. * @memberof UserGroupRelation
  207. */
  208. static removeAllByUserGroup(userGroup) {
  209. return this.deleteMany({ relatedGroup: userGroup });
  210. }
  211. /**
  212. * remove relation by id
  213. *
  214. * @static
  215. * @param {ObjectId} id
  216. * @returns {Promise<any>}
  217. * @memberof UserGroupRelation
  218. */
  219. static removeById(id) {
  220. return this.findById(id)
  221. .then((relationData) => {
  222. if (relationData == null) {
  223. throw new Exception('UserGroupRelation data is not exists. id:', id);
  224. }
  225. else {
  226. relationData.remove();
  227. }
  228. });
  229. }
  230. }
  231. module.exports = function(crowi) {
  232. UserGroupRelation.crowi = crowi;
  233. schema.loadClass(UserGroupRelation);
  234. return mongoose.model('UserGroupRelation', schema);
  235. };