notification_edit_ctrl.ts 3.2 KB

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