page-group-relation.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. .catch((err) => {
  90. debug('Error on pagination:', err);
  91. });
  92. }
  93. /**
  94. * find the relation or create(if not exists) for page and group
  95. *
  96. * @static
  97. * @param {Page} page
  98. * @param {UserGroup} userGroup
  99. * @returns {Promise<PageGroupRelation>}
  100. * @memberof PageGroupRelation
  101. */
  102. static findOrCreateRelationForPageAndGroup(page, userGroup) {
  103. const query = { targetPage: page.id, relatedGroup: userGroup.id };
  104. return this
  105. .count(query)
  106. .then((count) => {
  107. // return (0 < count);
  108. if (0 < count) {
  109. return this.find(query).exec();
  110. }
  111. else {
  112. return this.createRelation(userGroup, page);
  113. }
  114. })
  115. .catch((err) => {
  116. debug('An Error occured.', err);
  117. return reject(err);
  118. });
  119. }
  120. /**
  121. * find page and group relation for Page
  122. *
  123. * @static
  124. * @param {Page} page
  125. * @returns {Promise<PageGroupRelation[]>}
  126. * @memberof PageGroupRelation
  127. */
  128. static findByPage(page) {
  129. if (page == null) {
  130. return null;
  131. }
  132. return this
  133. .find({ targetPage: page.id })
  134. .populate('relatedGroup')
  135. .exec();
  136. }
  137. /**
  138. * get is exists granted group for relatedPage and relatedUser
  139. *
  140. * @static
  141. * @param {any} pageData relatedPage
  142. * @param {any} userData relatedUser
  143. * @returns is exists granted group(or not)
  144. * @memberof PageGroupRelation
  145. */
  146. static isExistsGrantedGroupForPageAndUser(pageData, userData) {
  147. var UserGroupRelation = PageGroupRelation.crowi.model('UserGroupRelation');
  148. return this.findByPage(pageData)
  149. .then((pageRelations) => {
  150. return pageRelations.map((pageRelation) => {
  151. return UserGroupRelation.isRelatedUserForGroup(userData, pageRelation.relatedGroup);
  152. });
  153. })
  154. .then((checkPromises) => {
  155. return Promise.all(checkPromises);
  156. })
  157. .then((checkResults) => {
  158. var checkResult = false;
  159. checkResults.map((result) => {
  160. if (result) {
  161. checkResult = true;
  162. }
  163. });
  164. return checkResult;
  165. })
  166. .catch((err) => {
  167. return reject(err);
  168. });
  169. }
  170. /**
  171. * create page and group relation
  172. *
  173. * @static
  174. * @param {any} userGroup
  175. * @param {any} page
  176. * @returns
  177. * @memberof PageGroupRelation
  178. */
  179. static createRelation(userGroup, page) {
  180. return this.create({
  181. relatedGroup: userGroup.id,
  182. targetPage: page.id,
  183. });
  184. }
  185. /**
  186. * remove all relation for UserGroup
  187. *
  188. * @static
  189. * @param {UserGroup} userGroup related group for remove
  190. * @returns {Promise<any>}
  191. * @memberof PageGroupRelation
  192. */
  193. static removeAllByUserGroup(userGroup) {
  194. return this.findAllRelationForUserGroup(userGroup)
  195. .then((relations) => {
  196. if (relations == null) {
  197. return;
  198. }
  199. else {
  200. relations.map((relation) => {
  201. relation.remove();
  202. });
  203. }
  204. });
  205. }
  206. /**
  207. * remove all relation for Page
  208. *
  209. * @static
  210. * @param {Page} page related page for remove
  211. * @returns {Promise<any>}
  212. * @memberof PageGroupRelation
  213. */
  214. static removeAllByPage(page) {
  215. return this.findByPage(page)
  216. .then((relations) => {
  217. debug('remove relations are ', relations);
  218. if (relations == null) {
  219. return;
  220. }
  221. else {
  222. relations.map((relation) => {
  223. relation.remove();
  224. });
  225. }
  226. });
  227. }
  228. /**
  229. * remove relation by id
  230. *
  231. * @static
  232. * @param {ObjectId} id for remove
  233. * @returns {Promise<any>}
  234. * @memberof PageGroupRelation
  235. */
  236. static removeById(id) {
  237. return this.findById(id)
  238. .then((relationData) => {
  239. if (relationData == null) {
  240. throw new Exception('PageGroupRelation data is not exists. id:', id);
  241. }
  242. else {
  243. relationData.remove();
  244. }
  245. })
  246. .catch((err) => {
  247. debug('Error on find a removing page-group-relation', err);
  248. return reject(err);
  249. });
  250. }
  251. }
  252. module.exports = function(crowi) {
  253. PageGroupRelation.crowi = crowi;
  254. schema.loadClass(PageGroupRelation);
  255. return mongoose.model('PageGroupRelation', schema);
  256. };