global-notification-setting.js 2.3 KB

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