page-group-relation.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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' },
  11. targetPage: { type: ObjectId, ref: 'Page' },
  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 new Promise(function(resolve, reject) {
  23. PageGroupRelation
  24. .find({ relatedGroup: group} )
  25. .populate('targetPage')
  26. .exec(function (err, pageGroupRelationData) {
  27. if (err) {
  28. return reject(err);
  29. }
  30. return resolve(pageGroupRelationData);
  31. });
  32. });
  33. };
  34. // 指定グループに対するすべてのグループ所属関係を取得
  35. pageGroupRelationSchema.statics.findAllRelationForUserGroup = function (userGroup) {
  36. debug('findAllRelation is called', userGroup);
  37. var PageGroupRelation = this;
  38. return new Promise(function (resolve, reject) {
  39. PageGroupRelation
  40. .find({ relatedGroup: userGroup })
  41. .populate('targetPage')
  42. .exec(function (err, pageGroupRelationData) {
  43. if (err) {
  44. return reject(err);
  45. }
  46. return resolve(pageGroupRelationData);
  47. });
  48. });
  49. };
  50. // 指定グループリストに対するすべてのグループ所属関係を取得
  51. pageGroupRelationSchema.statics.findAllRelationForUserGroups = function (userGroups, callback) {
  52. debug('findAllRelations is called', userGroups);
  53. var PageGroupRelation = this;
  54. var groupRelations = new Map();
  55. return new Promise(function (resolve, reject) {
  56. PageGroupRelation
  57. .find({ relatedGroup: { $in: userGroups} })
  58. .populate('targetPage')
  59. .exec(function (err, pageGroupRelationData) {
  60. if (err) {
  61. return reject(err);
  62. }
  63. debug(pageGroupRelationData);
  64. return resolve(pageGroupRelationData);
  65. });
  66. });
  67. };
  68. // ページネーション利用の検索
  69. pageGroupRelationSchema.statics.findPageGroupRelationsWithPagination = function (userGroup, options, callback) {
  70. this.paginate({ relatedGroup: userGroup }, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
  71. if (err) {
  72. debug('Error on pagination:', err);
  73. return callback(err, null);
  74. }
  75. return callback(err, result);
  76. });
  77. };
  78. // ページを元に関係性を取得
  79. pageGroupRelationSchema.statics.findByPage = function(page) {
  80. var PageGroupRelation = this
  81. PageGroupRelation
  82. .findOne({ targetPage: page})
  83. .exec(function (err, data) {
  84. if (err) {
  85. return reject(err);
  86. }
  87. return resolve(data);
  88. });
  89. };
  90. // 関係性の生成
  91. pageGroupRelationSchema.statics.createRelation = function(userGroup, page, callback) {
  92. var PageGroupRelation = this
  93. , newPageGroupRelation = new PageGroupRelation();
  94. if (userGroup == null || page == null) {
  95. return callback(new Error('userGroup or page is null'));
  96. }
  97. newPageGroupRelation.relatedGroup = userGroup;
  98. newPageGroupRelation.targetPage = page;
  99. newPageGroupRelation.createdAt = Date.now();
  100. debug('create new page-group-relation ', newPageGroupRelation);
  101. newPageGroupRelation.save(function(err, pageGroupRelationData) {
  102. return callback(err, pageGroupRelationData);
  103. });
  104. };
  105. // グループに紐づく関係性の全削除
  106. pageGroupRelationSchema.statics.removeAllByUserGroup = function (userGroup, callback) {
  107. if (userGroup === null) { return callback(null); }
  108. var PageGroupRelation = this
  109. var relations = PageGroupRelation.findAllRelation(userGroup);
  110. // 関係性削除の実装
  111. relations.array.forEach(relation => {
  112. PageGroupRelation.removeById(relation.id, function(err) {
  113. if (err) { return callback(err); }
  114. });
  115. });
  116. return callback(null);
  117. }
  118. // ページに紐づくグループの関係性の削除
  119. pageGroupRelationSchema.statics.removeRelationByUserGroupAndPage = function (userGroup, page, callback) {
  120. if (userGroup == null) { return callback(null); }
  121. if (page == null) { return callback(null); }
  122. var PageGroupRelation = this
  123. var relation = PageGroupRelation.findOne()({relatedGroup: userGroup.id, targetPage: page.id });
  124. if (relation == null) { return callback(); }
  125. PageGroupRelation.removeById(relation.id, function (err) {
  126. if (err) { return callback(err); }
  127. });
  128. return callback(null);
  129. }
  130. // ユーザグループの関係性を削除
  131. pageGroupRelationSchema.statics.removeById = function (id, callback) {
  132. var PageGroupRelation = this
  133. PageGroupRelation.findById(id, function (err, relationData) {
  134. if (err) {
  135. debug('Error on find a removing user-group-relation', err);
  136. return callback(err);
  137. }
  138. debug('relationData is ', relationData);
  139. if (relationData == null || relationData == undefined) {
  140. debug('Cannot find user group relation by id', id);
  141. return callback(new Error('Cannot find user group relation by id'));
  142. }
  143. relationData.remove(function(err) {
  144. if (err) {
  145. return callback(err);
  146. }
  147. return callback(null);
  148. });
  149. });
  150. }
  151. pageGroupRelationSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  152. return mongoose.model('PageGroupRelation', pageGroupRelationSchema);
  153. };