page-group-relation.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.findAllRelationForUserGroup(userGroup)
  171. .then((relations) => {
  172. if (relations == null) {
  173. return;
  174. }
  175. else {
  176. relations.map((relation) => {
  177. relation.remove();
  178. });
  179. }
  180. });
  181. }
  182. /**
  183. * remove all relation for Page
  184. *
  185. * @static
  186. * @param {Page} page related page for remove
  187. * @returns {Promise<any>}
  188. * @memberof PageGroupRelation
  189. */
  190. static removeAllByPage(page) {
  191. return this.findByPage(page)
  192. .then(relation => {
  193. if (relation != null) {
  194. relation.remove();
  195. }
  196. });
  197. }
  198. /**
  199. * remove relation by id
  200. *
  201. * @static
  202. * @param {ObjectId} id for remove
  203. * @returns {Promise<any>}
  204. * @memberof PageGroupRelation
  205. */
  206. static removeById(id) {
  207. return this.findById(id)
  208. .then((relationData) => {
  209. if (relationData == null) {
  210. throw new Exception('PageGroupRelation data is not exists. id:', id);
  211. }
  212. else {
  213. relationData.remove();
  214. }
  215. });
  216. }
  217. }
  218. module.exports = function(crowi) {
  219. PageGroupRelation.crowi = crowi;
  220. schema.loadClass(PageGroupRelation);
  221. return mongoose.model('PageGroupRelation', schema);
  222. };