page-group-relation.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. const debug = require('debug')('growi:models:pageGroupRelation');
  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. targetPage: { type: ObjectId, ref: 'Page', required: true },
  11. createdAt: { type: Date, default: Date.now },
  12. }, {
  13. toJSON: { getters: true },
  14. toObject: { getters: true }
  15. });
  16. // apply plugins
  17. schema.plugin(mongoosePaginate);
  18. /**
  19. * PageGroupRelation Class
  20. *
  21. * @class PageGroupRelation
  22. */
  23. class PageGroupRelation {
  24. /**
  25. * limit items num for pagination
  26. *
  27. * @readonly
  28. * @static
  29. * @memberof PageGroupRelation
  30. */
  31. static get PAGE_ITEMS() {
  32. return 50;
  33. }
  34. static set crowi(crowi) {
  35. this._crowi = crowi;
  36. }
  37. static get crowi() {
  38. return this._crowi;
  39. }
  40. /**
  41. * find all page and group relation
  42. *
  43. * @static
  44. * @returns {Promise<PageGroupRelation[]>}
  45. * @memberof PageGroupRelation
  46. */
  47. static findAllRelation() {
  48. return this
  49. .find()
  50. .populate('targetPage')
  51. .exec();
  52. }
  53. /**
  54. * find all page and group relation for UserGroup
  55. *
  56. * @static
  57. * @param {UserGroup} userGroup
  58. * @returns {Promise<PageGroupRelation[]>}
  59. * @memberof PageGroupRelation
  60. */
  61. static findAllRelationForUserGroup(userGroup) {
  62. debug('findAllRelationForUserGroup is called', userGroup);
  63. return this
  64. .find({ relatedGroup: userGroup.id })
  65. .populate('targetPage')
  66. .exec();
  67. }
  68. /**
  69. * find all entities with pagination
  70. *
  71. * @see https://github.com/edwardhotchkiss/mongoose-paginate
  72. *
  73. * @static
  74. * @param {UserGroup} userGroup
  75. * @param {any} opts mongoose-paginate options object
  76. * @returns {Promise<any>} mongoose-paginate result object
  77. * @memberof UserGroupRelation
  78. */
  79. // static findPageGroupRelationsWithPagination(userGroup, opts) {
  80. // const query = { relatedGroup: userGroup };
  81. // const options = Object.assign({}, opts);
  82. // if (options.page == null) {
  83. // options.page = 1;
  84. // }
  85. // if (options.limit == null) {
  86. // options.limit = UserGroupRelation.PAGE_ITEMS;
  87. // }
  88. // return this.paginate(query, options);
  89. // }
  90. /**
  91. * find the relation or create(if not exists) for page and group
  92. *
  93. * @static
  94. * @param {Page} page
  95. * @param {UserGroup} userGroup
  96. * @returns {Promise<PageGroupRelation>}
  97. * @memberof PageGroupRelation
  98. */
  99. static findOrCreateRelationForPageAndGroup(page, userGroup) {
  100. const query = { targetPage: page.id, relatedGroup: userGroup.id };
  101. return this
  102. .count(query)
  103. .then((count) => {
  104. // return (0 < count);
  105. if (0 < count) {
  106. return this.find(query).exec();
  107. }
  108. else {
  109. return this.createRelation(userGroup, page);
  110. }
  111. });
  112. }
  113. /**
  114. * find page and group relation for Page
  115. *
  116. * @static
  117. * @param {Page} page
  118. * @returns {Promise<PageGroupRelation[]>}
  119. * @memberof PageGroupRelation
  120. */
  121. static findByPage(page) {
  122. if (page == null) {
  123. return null;
  124. }
  125. return this
  126. .findOne({ targetPage: page.id })
  127. .populate('relatedGroup')
  128. .exec();
  129. }
  130. /**
  131. * get is exists granted group for relatedPage and relatedUser
  132. *
  133. * @static
  134. * @param {any} pageData relatedPage
  135. * @param {any} userData relatedUser
  136. * @returns is exists granted group(or not)
  137. * @memberof PageGroupRelation
  138. */
  139. static isExistsGrantedGroupForPageAndUser(pageData, userData) {
  140. var UserGroupRelation = PageGroupRelation.crowi.model('UserGroupRelation');
  141. return this.findByPage(pageData)
  142. .then(pageRelation => {
  143. return UserGroupRelation.isRelatedUserForGroup(userData, pageRelation.relatedGroup);
  144. });
  145. }
  146. /**
  147. * create page and group relation
  148. *
  149. * @static
  150. * @param {any} userGroup
  151. * @param {any} page
  152. * @returns
  153. * @memberof PageGroupRelation
  154. */
  155. static createRelation(userGroup, page) {
  156. return this.create({
  157. relatedGroup: userGroup.id,
  158. targetPage: page.id,
  159. });
  160. }
  161. /**
  162. * remove all relation for UserGroup
  163. *
  164. * @static
  165. * @param {UserGroup} userGroup related group for remove
  166. * @returns {Promise<any>}
  167. * @memberof PageGroupRelation
  168. */
  169. static removeAllByUserGroup(userGroup) {
  170. return this.deleteMany({ relatedGroup: userGroup });
  171. }
  172. /**
  173. * remove all relation for Page
  174. *
  175. * @static
  176. * @param {Page} page related page for remove
  177. * @returns {Promise<any>}
  178. * @memberof PageGroupRelation
  179. */
  180. static removeAllByPage(page) {
  181. return this.findByPage(page)
  182. .then(relation => {
  183. if (relation != null) {
  184. relation.remove();
  185. }
  186. });
  187. }
  188. /**
  189. * remove relation by id
  190. *
  191. * @static
  192. * @param {ObjectId} id for remove
  193. * @returns {Promise<any>}
  194. * @memberof PageGroupRelation
  195. */
  196. static removeById(id) {
  197. return this.findById(id)
  198. .then((relationData) => {
  199. if (relationData == null) {
  200. throw new Exception('PageGroupRelation data is not exists. id:', id);
  201. }
  202. else {
  203. relationData.remove();
  204. }
  205. });
  206. }
  207. }
  208. module.exports = function(crowi) {
  209. PageGroupRelation.crowi = crowi;
  210. schema.loadClass(PageGroupRelation);
  211. return mongoose.model('PageGroupRelation', schema);
  212. };