GlobalNotificationSettingClass.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * GlobalNotificationSetting Class
  3. * @class GlobalNotificationSetting
  4. */
  5. class GlobalNotificationSetting {
  6. constructor(crowi) {
  7. this.crowi = crowi;
  8. }
  9. /**
  10. * enable notification setting
  11. * @param {string} id
  12. */
  13. static async enable(id) {
  14. const setting = await this.findOne({_id: id});
  15. setting.isEnabled = true;
  16. setting.save();
  17. return setting;
  18. }
  19. /**
  20. * disable notification setting
  21. * @param {string} id
  22. */
  23. static async disable(id) {
  24. const setting = await this.findOne({_id: id});
  25. setting.isEnabled = false;
  26. setting.save();
  27. return setting;
  28. }
  29. /**
  30. * find all notification settings
  31. */
  32. static async findAll() {
  33. const settings = await this.find().sort({ triggerPath: 1 });
  34. return settings;
  35. }
  36. /**
  37. * find a list of notification settings by path and a list of events
  38. * @param {string} path
  39. * @param {string} event
  40. */
  41. static async findSettingByPathAndEvent(path, event) {
  42. const pathsToMatch = generatePathsToMatch(path);
  43. const settings = await this.find({
  44. triggerPath: {$in: pathsToMatch},
  45. triggerEvents: event,
  46. isEnabled: true
  47. })
  48. .sort({ triggerPath: 1 });
  49. return settings;
  50. }
  51. }
  52. // move this to util
  53. // remove this from models/page
  54. const cutOffLastSlash = path => {
  55. const lastSlash = path.lastIndexOf('/');
  56. return path.substr(0, lastSlash);
  57. };
  58. const generatePathsOnTree = (path, pathList) => {
  59. pathList.push(path);
  60. if (path === '') {
  61. return pathList;
  62. }
  63. const newPath = cutOffLastSlash(path);
  64. return generatePathsOnTree(newPath, pathList);
  65. };
  66. const generatePathsToMatch = (originalPath) => {
  67. const pathList = generatePathsOnTree(originalPath, []);
  68. return pathList.map(path => {
  69. if (path !== originalPath) {
  70. return path + '/*';
  71. }
  72. else {
  73. return path;
  74. }
  75. });
  76. };
  77. module.exports = GlobalNotificationSetting;