notification_edit_ctrl.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 (let 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. return result;
  41. });
  42. })
  43. .then(model => {
  44. this.model = model;
  45. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  46. });
  47. }
  48. save() {
  49. if (!this.theForm.$valid) {
  50. return;
  51. }
  52. if (this.model.id) {
  53. this.backendSrv.put(`/api/alert-notifications/${this.model.id}`, this.model).then(res => {
  54. this.model = res;
  55. appEvents.emit('alert-success', ['Notification updated', '']);
  56. });
  57. } else {
  58. this.backendSrv.post(`/api/alert-notifications`, this.model).then(res => {
  59. appEvents.emit('alert-success', ['Notification created', '']);
  60. this.$location.path('alerting/notifications');
  61. });
  62. }
  63. }
  64. getNotifierTemplateId(type) {
  65. return `notifier-options-${type}`;
  66. }
  67. typeChanged() {
  68. this.model.settings = {};
  69. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  70. }
  71. testNotification() {
  72. if (!this.theForm.$valid) {
  73. return;
  74. }
  75. var payload = {
  76. name: this.model.name,
  77. type: this.model.type,
  78. settings: this.model.settings,
  79. };
  80. this.backendSrv.post(`/api/alert-notifications/test`, payload).then(res => {
  81. appEvents.emit('alert-success', ['Test notification sent', '']);
  82. });
  83. }
  84. }
  85. coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);