user-group-relation.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. var UserGroupRelation = this;
  61. return this
  62. .find({ relatedGroup: userGroup })
  63. .populate('relatedUser')
  64. .exec();
  65. }
  66. /**
  67. * find all user and group relation of UserGroups
  68. *
  69. * @static
  70. * @param {UserGroup[]} userGroups
  71. * @returns {Promise<UserGroupRelation[]>}
  72. * @memberof UserGroupRelation
  73. */
  74. static findAllRelationForUserGroups(userGroups) {
  75. return this
  76. .find({ relatedGroup: { $in: userGroups } })
  77. .populate('relatedUser')
  78. .exec();
  79. }
  80. /**
  81. * find all user and group relation of User
  82. *
  83. * @static
  84. * @param {User} user
  85. * @returns {Promise<UserGroupRelation[]>}
  86. * @memberof UserGroupRelation
  87. */
  88. static findAllRelationForUser(user) {
  89. return this
  90. .find({ relatedUser: user.id })
  91. .populate('relatedGroup')
  92. .exec();
  93. }
  94. /**
  95. * find all entities with pagination
  96. *
  97. * @see https://github.com/edwardhotchkiss/mongoose-paginate
  98. *
  99. * @static
  100. * @param {UserGroup} userGroup
  101. * @param {any} opts mongoose-paginate options object
  102. * @returns {Promise<any>} mongoose-paginate result object
  103. * @memberof UserGroupRelation
  104. */
  105. static findUserGroupRelationsWithPagination(userGroup, opts) {
  106. const query = { relatedGroup: userGroup };
  107. const options = Object.assign({}, opts);
  108. if (options.page == null) {
  109. options.page = 1;
  110. }
  111. if (options.limit == null) {
  112. options.limit = UserGroupRelation.PAGE_ITEMS;
  113. }
  114. return this.paginate(query, options)
  115. .catch((err) => {
  116. debug('Error on pagination:', err);
  117. });
  118. }
  119. /**
  120. * find one result by related group id and related user
  121. *
  122. * @static
  123. * @param {string} userGroupId find query param for relatedGroup
  124. * @param {User} userData find query param for relatedUser
  125. * @returns {Promise<UserGroupRelation>}
  126. * @memberof UserGroupRelation
  127. */
  128. static findByGroupIdAndUser(userGroupId, userData) {
  129. const query = {
  130. relatedGroup: userGroupId,
  131. relatedUser: userData.id
  132. }
  133. return this
  134. .findOne(query)
  135. .populate('relatedUser')
  136. .populate('relatedGroup')
  137. .exec();
  138. }
  139. /**
  140. * find all "not" related user for UserGroup
  141. *
  142. * @static
  143. * @param {UserGroup} userGroup for find users not related
  144. * @returns {Promise<User>}
  145. * @memberof UserGroupRelation
  146. */
  147. static findUserByNotRelatedGroup(userGroup) {
  148. const User = UserGroupRelation.crowi.model('User');
  149. return this.findAllRelationForUserGroup(userGroup)
  150. .then((relations) => {
  151. const relatedUserIds = relations.map((relation) => {
  152. return relation.relatedUser.id;
  153. });
  154. const query = { _id: { $nin: relatedUserIds }, status: User.STATUS_ACTIVE };
  155. debug("findUserByNotRelatedGroup ", query);
  156. return User.find(query).exec();
  157. });
  158. }
  159. /**
  160. * get if the user has relation for group
  161. *
  162. * @static
  163. * @param {User} userData
  164. * @param {UserGroup} userGroup
  165. * @returns {Promise<boolean>} is user related for group(or not)
  166. * @memberof UserGroupRelation
  167. */
  168. static isRelatedUserForGroup(userData, userGroup) {
  169. const query = {
  170. relatedGroup: userGroup.id,
  171. relatedUser: userData.id
  172. }
  173. return this
  174. .count(query)
  175. .exec()
  176. .then((count) => {
  177. // return true or false of the relation is exists(not count)
  178. return (0 < count);
  179. })
  180. .catch((err) => {
  181. debug('An Error occured.', err);
  182. reject(err);
  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.findAllRelationForUserGroup(userGroup)
  210. .then((relations) => {
  211. if (relations == null) {
  212. return;
  213. }
  214. else {
  215. relations.map((relation) => {
  216. relation.remove();
  217. });
  218. }
  219. });
  220. }
  221. /**
  222. * remove relation by id
  223. *
  224. * @static
  225. * @param {ObjectId} id
  226. * @returns {Promise<any>}
  227. * @memberof UserGroupRelation
  228. */
  229. static removeById(id) {
  230. return this.findById(id)
  231. .then((relationData) => {
  232. if (relationData == null) {
  233. throw new Exception('UserGroupRelation data is not exists. id:', id);
  234. }
  235. else {
  236. relationData.remove();
  237. }
  238. })
  239. .catch((err) => {
  240. debug('Error on find a removing user-group-relation', err);
  241. reject(err);
  242. });
  243. }
  244. }
  245. module.exports = function (crowi) {
  246. UserGroupRelation.crowi = crowi;
  247. schema.loadClass(UserGroupRelation);
  248. return mongoose.model('UserGroupRelation', schema);
  249. }