user-group-relation.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. .exec();
  92. }
  93. /**
  94. * find all entities with pagination
  95. *
  96. * @see https://github.com/edwardhotchkiss/mongoose-paginate
  97. *
  98. * @static
  99. * @param {UserGroup} userGroup
  100. * @param {any} opts mongoose-paginate options object
  101. * @returns {Promise<any>} mongoose-paginate result object
  102. * @memberof UserGroupRelation
  103. */
  104. static findUserGroupRelationsWithPagination(userGroup, opts) {
  105. const query = { relatedGroup: userGroup };
  106. const options = Object.assign({}, opts);
  107. if (options.page == null) {
  108. options.page = 1;
  109. }
  110. if (options.limit == null) {
  111. options.limit = UserGroupRelation.PAGE_ITEMS;
  112. }
  113. return this.paginate(query, options)
  114. .catch((err) => {
  115. debug('Error on pagination:', err);
  116. });
  117. }
  118. /**
  119. * find one result by related group id and related user
  120. *
  121. * @static
  122. * @param {string} userGroupId find query param for relatedGroup
  123. * @param {User} userData find query param for relatedUser
  124. * @returns {Promise<UserGroupRelation>}
  125. * @memberof UserGroupRelation
  126. */
  127. static findByGroupIdAndUser(userGroupId, userData) {
  128. const query = {
  129. relatedGroup: userGroupId,
  130. relatedUser: userData.id
  131. };
  132. return this
  133. .findOne(query)
  134. .populate('relatedUser')
  135. .populate('relatedGroup')
  136. .exec();
  137. }
  138. /**
  139. * find all "not" related user for UserGroup
  140. *
  141. * @static
  142. * @param {UserGroup} userGroup for find users not related
  143. * @returns {Promise<User>}
  144. * @memberof UserGroupRelation
  145. */
  146. static findUserByNotRelatedGroup(userGroup) {
  147. const User = UserGroupRelation.crowi.model('User');
  148. return this.findAllRelationForUserGroup(userGroup)
  149. .then((relations) => {
  150. const relatedUserIds = relations.map((relation) => {
  151. return relation.relatedUser.id;
  152. });
  153. const query = { _id: { $nin: relatedUserIds }, status: User.STATUS_ACTIVE };
  154. debug('findUserByNotRelatedGroup ', query);
  155. return User.find(query).exec();
  156. });
  157. }
  158. /**
  159. * get if the user has relation for group
  160. *
  161. * @static
  162. * @param {User} userData
  163. * @param {UserGroup} userGroup
  164. * @returns {Promise<boolean>} is user related for group(or not)
  165. * @memberof UserGroupRelation
  166. */
  167. static isRelatedUserForGroup(userData, userGroup) {
  168. const query = {
  169. relatedGroup: userGroup.id,
  170. relatedUser: userData.id
  171. };
  172. return this
  173. .count(query)
  174. .exec()
  175. .then((count) => {
  176. // return true or false of the relation is exists(not count)
  177. return (0 < count);
  178. });
  179. }
  180. /**
  181. * create user and group relation
  182. *
  183. * @static
  184. * @param {UserGroup} userGroup
  185. * @param {User} user
  186. * @returns {Promise<UserGroupRelation>} created relation
  187. * @memberof UserGroupRelation
  188. */
  189. static createRelation(userGroup, user) {
  190. return this.create({
  191. relatedGroup: userGroup.id,
  192. relatedUser: user.id
  193. });
  194. }
  195. /**
  196. * remove all relation for UserGroup
  197. *
  198. * @static
  199. * @param {UserGroup} userGroup related group for remove
  200. * @returns {Promise<any>}
  201. * @memberof UserGroupRelation
  202. */
  203. static removeAllByUserGroup(userGroup) {
  204. return this.findAllRelationForUserGroup(userGroup)
  205. .then((relations) => {
  206. if (relations == null) {
  207. return;
  208. }
  209. else {
  210. relations.map((relation) => {
  211. relation.remove();
  212. });
  213. }
  214. });
  215. }
  216. /**
  217. * remove relation by id
  218. *
  219. * @static
  220. * @param {ObjectId} id
  221. * @returns {Promise<any>}
  222. * @memberof UserGroupRelation
  223. */
  224. static removeById(id) {
  225. return this.findById(id)
  226. .then((relationData) => {
  227. if (relationData == null) {
  228. throw new Exception('UserGroupRelation data is not exists. id:', id);
  229. }
  230. else {
  231. relationData.remove();
  232. }
  233. });
  234. }
  235. }
  236. module.exports = function(crowi) {
  237. UserGroupRelation.crowi = crowi;
  238. schema.loadClass(UserGroupRelation);
  239. return mongoose.model('UserGroupRelation', schema);
  240. };