notification_edit_ctrl.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import _ from 'lodash';
  2. import { appEvents, coreModule } from 'app/core/core';
  3. export class AlertNotificationEditCtrl {
  4. theForm: any;
  5. navModel: any;
  6. testSeverity = 'critical';
  7. notifiers: any;
  8. notifierTemplateId: string;
  9. isNew: boolean;
  10. model: any;
  11. defaults: any = {
  12. type: 'email',
  13. sendReminder: false,
  14. frequency: '15m',
  15. settings: {
  16. httpMethod: 'POST',
  17. autoResolve: true,
  18. uploadImage: true,
  19. },
  20. isDefault: false,
  21. };
  22. /** @ngInject */
  23. constructor(private $routeParams, private backendSrv, private $location, private $templateCache, navModelSrv) {
  24. this.navModel = navModelSrv.getNav('alerting', 'channels', 0);
  25. this.isNew = !this.$routeParams.id;
  26. this.backendSrv
  27. .get(`/api/alert-notifiers`)
  28. .then(notifiers => {
  29. this.notifiers = notifiers;
  30. // add option templates
  31. for (let notifier of this.notifiers) {
  32. this.$templateCache.put(this.getNotifierTemplateId(notifier.type), notifier.optionsTemplate);
  33. }
  34. if (!this.$routeParams.id) {
  35. this.navModel.breadcrumbs.push({ text: 'New channel' });
  36. this.navModel.node = { text: 'New channel' };
  37. return _.defaults(this.model, this.defaults);
  38. }
  39. return this.backendSrv.get(`/api/alert-notifications/${this.$routeParams.id}`).then(result => {
  40. this.navModel.breadcrumbs.push({ text: result.name });
  41. this.navModel.node = { text: result.name };
  42. result.settings = _.defaults(result.settings, this.defaults.settings);
  43. return result;
  44. });
  45. })
  46. .then(model => {
  47. this.model = model;
  48. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  49. });
  50. }
  51. save() {
  52. if (!this.theForm.$valid) {
  53. return;
  54. }
  55. if (this.model.id) {
  56. this.backendSrv
  57. .put(`/api/alert-notifications/${this.model.id}`, this.model)
  58. .then(res => {
  59. this.model = res;
  60. appEvents.emit('alert-success', ['Notification updated', '']);
  61. })
  62. .catch(err => {
  63. if (err.data && err.data.error) {
  64. appEvents.emit('alert-error', [err.data.error]);
  65. }
  66. });
  67. } else {
  68. this.backendSrv
  69. .post(`/api/alert-notifications`, this.model)
  70. .then(res => {
  71. appEvents.emit('alert-success', ['Notification created', '']);
  72. this.$location.path('alerting/notifications');
  73. })
  74. .catch(err => {
  75. if (err.data && err.data.error) {
  76. appEvents.emit('alert-error', [err.data.error]);
  77. }
  78. });
  79. }
  80. }
  81. getNotifierTemplateId(type) {
  82. return `notifier-options-${type}`;
  83. }
  84. typeChanged() {
  85. this.model.settings = _.defaults({}, this.defaults.settings);
  86. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  87. }
  88. testNotification() {
  89. if (!this.theForm.$valid) {
  90. return;
  91. }
  92. var payload = {
  93. name: this.model.name,
  94. type: this.model.type,
  95. frequency: this.model.frequency,
  96. settings: this.model.settings,
  97. };
  98. this.backendSrv.post(`/api/alert-notifications/test`, payload).then(res => {
  99. appEvents.emit('alert-success', ['Test notification sent', '']);
  100. });
  101. }
  102. }
  103. coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);