page-group-relation.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. module.exports = function(crowi) {
  2. var debug = require('debug')('crowi:models:pageGroupRelation')
  3. , mongoose = require('mongoose')
  4. , mongoosePaginate = require('mongoose-paginate')
  5. , ObjectId = mongoose.Schema.Types.ObjectId
  6. , PAGE_ITEMS = 50
  7. , pageGroupRelationSchema;
  8. pageGroupRelationSchema = new mongoose.Schema({
  9. pageGroupRelationId: String,
  10. relatedGroup: { type: ObjectId, ref: 'UserGroup', required: true },
  11. targetPage: { type: ObjectId, ref: 'Page', required: true },
  12. createdAt: { type: Date, default: Date.now },
  13. },{
  14. toJSON: { getters: true },
  15. toObject: { getters: true }
  16. });
  17. pageGroupRelationSchema.plugin(mongoosePaginate);
  18. // すべてのグループ所属関係を取得
  19. pageGroupRelationSchema.statics.findAllRelation = function() {
  20. debug('findAllRelations is called');
  21. var PageGroupRelation = this;
  22. return PageGroupRelation.find({ relatedGroup: group} )
  23. .populate('targetPage')
  24. .exec();
  25. };
  26. // 指定グループに対するすべてのグループ所属関係を取得
  27. pageGroupRelationSchema.statics.findAllRelationForUserGroup = function (userGroup) {
  28. debug('findAllRelation is called', userGroup);
  29. var PageGroupRelation = this;
  30. return PageGroupRelation.find({ relatedGroup: userGroup.id })
  31. .populate('targetPage')
  32. .exec();
  33. };
  34. // ページネーション利用の検索
  35. pageGroupRelationSchema.statics.findPageGroupRelationsWithPagination = function (userGroup, options, callback) {
  36. this.paginate({ relatedGroup: userGroup }, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
  37. if (err) {
  38. debug('Error on pagination:', err);
  39. return callback(err, null);
  40. }
  41. return callback(err, result);
  42. });
  43. };
  44. // ページとグループを元に関係性が存在するか確認
  45. pageGroupRelationSchema.statics.isExistsRelationForPageAndGroup = function (page, userGroup) {
  46. var PageGroupRelation = this
  47. return new Promise(function (resolve, reject) {
  48. PageGroupRelation
  49. .count({ targetPage: page.id, relatedGroup: userGroup.id })
  50. .exec(function (err, count) {
  51. if (err) {
  52. return reject(err);
  53. }
  54. return resolve(0 < count);
  55. });
  56. });
  57. };
  58. // ページを元に関係性を取得
  59. pageGroupRelationSchema.statics.findByPage = function(page) {
  60. var PageGroupRelation = this
  61. return PageGroupRelation.find({ targetPage: page.id })
  62. .populate('relatedGroup')
  63. .exec();
  64. };
  65. // 関係性の生成
  66. pageGroupRelationSchema.statics.createRelation = function(userGroup, page) {
  67. var PageGroupRelation = this
  68. , newPageGroupRelation = new PageGroupRelation();
  69. debug('create new page-group-relation for group ', userGroup);
  70. newPageGroupRelation.relatedGroup = userGroup.id;
  71. newPageGroupRelation.targetPage = page.id;
  72. newPageGroupRelation.createdAt = Date.now();
  73. debug('create new page-group-relation ', newPageGroupRelation);
  74. return newPageGroupRelation.save();
  75. };
  76. // グループに紐づく関係性の全削除
  77. pageGroupRelationSchema.statics.removeAllByUserGroup = function (userGroup) {
  78. var PageGroupRelation = this
  79. return PageGroupRelation.findAllRelationForUserGroup(userGroup)
  80. .then( function(relations) {
  81. if (relations == null) {
  82. resolve();
  83. }
  84. else {
  85. relations.map(relation => relation.remove());
  86. }
  87. })
  88. }
  89. // ページに紐づく関係性の全削除
  90. pageGroupRelationSchema.statics.removeAllByPage = function (page) {
  91. var PageGroupRelation = this
  92. debug('removeAllByPage is called', page);
  93. return PageGroupRelation.findByPage(page)
  94. .then(function(relations) {
  95. debug('remove relations are ', relations);
  96. if (relations == null) {
  97. resolve();
  98. }
  99. else {
  100. relations.map(relation => relation.remove());
  101. }
  102. });
  103. }
  104. // ユーザグループの関係性を削除
  105. pageGroupRelationSchema.statics.removeById = function (id, callback) {
  106. var PageGroupRelation = this
  107. PageGroupRelation.findById(id, function (err, relationData) {
  108. if (err) {
  109. debug('Error on find a removing user-group-relation', err);
  110. return callback(err);
  111. }
  112. debug('relationData is ', relationData);
  113. if (relationData == null || relationData == undefined) {
  114. debug('Cannot find user group relation by id', id);
  115. return callback(new Error('Cannot find user group relation by id'));
  116. }
  117. relationData.remove(function(err) {
  118. if (err) {
  119. return callback(err);
  120. }
  121. return callback(null);
  122. });
  123. });
  124. }
  125. pageGroupRelationSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  126. return mongoose.model('PageGroupRelation', pageGroupRelationSchema);
  127. };