GlobalNotificationSetting.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const debug = require('debug')('growi:models:GlobalNotificationSetting');
  2. const mongoose = require('mongoose');
  3. /**
  4. * parent schema in this model
  5. */
  6. const notificationSchema = new mongoose.Schema({
  7. isEnabled: { type: Boolean, required: true, default: true },
  8. triggerPath: { type: String, required: true },
  9. triggerEvents: { type: [String] },
  10. });
  11. /**
  12. * create child schemas inherited from parentSchema
  13. * all child schemas are stored in globalnotificationsettings collection
  14. * @link{http://url.com module_name}
  15. * @param {object} parentSchema
  16. * @param {string} modelName
  17. * @param {string} discriminatorKey
  18. */
  19. const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
  20. const Notification = mongoose.model(modelName, parentSchema);
  21. const mailNotification = Notification.discriminator('mail', new mongoose.Schema({
  22. toEmail: String,
  23. }, {discriminatorKey: discriminatorKey}));
  24. const slackNotification = Notification.discriminator('slack', new mongoose.Schema({
  25. slackChannels: String,
  26. }, {discriminatorKey: discriminatorKey}));
  27. return {
  28. Parent: Notification,
  29. Mail: mailNotification,
  30. Slack: slackNotification,
  31. };
  32. };
  33. /**
  34. * GlobalNotificationSetting Class
  35. * @class GlobalNotificationSetting
  36. */
  37. class GlobalNotificationSetting {
  38. constructor(crowi) {
  39. this.crowi = crowi;
  40. }
  41. /**
  42. * enable notification setting
  43. * @param {string} id
  44. */
  45. static async enable(id) {
  46. // save
  47. // return Notification
  48. }
  49. /**
  50. * disable notification setting
  51. * @param {string} id
  52. */
  53. static async disable(id) {
  54. const setting = await this.findOne({_id: id});
  55. setting.isEnabled = false;
  56. setting.save();
  57. return setting;
  58. }
  59. /**
  60. * find a list of notification settings by path and a list of events
  61. * @param {string} path
  62. * @param {string} event
  63. * @param {boolean} enabled
  64. */
  65. static async findSettingByPathAndEvent(path, event, enabled) {
  66. let settings;
  67. if (enabled == null) {
  68. settings = this.find();
  69. }
  70. else {
  71. settings = this.find({isEnabled: enabled});
  72. }
  73. return await settings;
  74. }
  75. }
  76. module.exports = function(crowi) {
  77. GlobalNotificationSetting.crowi = crowi;
  78. notificationSchema.loadClass(GlobalNotificationSetting);
  79. return createChildSchemas(notificationSchema, 'GlobalNotificationSetting', 'type');
  80. };