user-group-relation.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. const debug = require('debug')('crowi: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. * get if the user has relation for group
  141. *
  142. * @static
  143. * @param {User} userData
  144. * @param {UserGroup} userGroup
  145. * @returns {Promise<boolean>} is user related for group(or not)
  146. * @memberof UserGroupRelation
  147. */
  148. static isRelatedUserForGroup(userData, userGroup) {
  149. const query = {
  150. relatedGroup: userGroup.id,
  151. relatedUser: userData.id
  152. }
  153. return this
  154. .count(query)
  155. .exec()
  156. .then((count) => {
  157. // return true or false of the relation is exists(not count)
  158. return (0 < count);
  159. })
  160. .catch((err) => {
  161. debug('An Error occured.', err);
  162. reject(err);
  163. });
  164. }
  165. /**
  166. * create user and group relation
  167. *
  168. * @static
  169. * @param {UserGroup} userGroup
  170. * @param {User} user
  171. * @returns {Promise<UserGroupRelation>} created relation
  172. * @memberof UserGroupRelation
  173. */
  174. static createRelation(userGroup, user) {
  175. return this.create({
  176. relatedGroup: userGroup.id,
  177. relatedUser: user.id
  178. });
  179. }
  180. /**
  181. * remove all relation for UserGroup
  182. *
  183. * @static
  184. * @param {UserGroup} userGroup related group for remove
  185. * @returns {Promise<any>}
  186. * @memberof UserGroupRelation
  187. */
  188. static removeAllByUserGroup(userGroup) {
  189. return this.findAllRelationForUserGroup(userGroup)
  190. .then((relations) => {
  191. if (relations == null) {
  192. return;
  193. }
  194. else {
  195. relations.map((relation) => {
  196. relation.remove();
  197. });
  198. }
  199. });
  200. }
  201. /**
  202. * remove relation by id
  203. *
  204. * @static
  205. * @param {ObjectId} id
  206. * @returns {Promise<any>}
  207. * @memberof UserGroupRelation
  208. */
  209. static removeById(id) {
  210. return this.findById(id)
  211. .then((relationData) => {
  212. if (relationData == null) {
  213. throw new Exception('UserGroupRelation data is not exists. id:', id);
  214. }
  215. else {
  216. relationData.remove();
  217. }
  218. })
  219. .catch((err) => {
  220. debug('Error on find a removing user-group-relation', err);
  221. reject(err);
  222. });
  223. }
  224. }
  225. module.exports = function (crowi) {
  226. UserGroupRelation.crowi = crowi;
  227. schema.loadClass(UserGroupRelation);
  228. return mongoose.model('UserGroupRelation', schema);
  229. }