From df5a5986b1a3b0c1bec723078814e91c19a52310 Mon Sep 17 00:00:00 2001 From: vimniky Date: Tue, 24 Apr 2018 23:32:31 +0800 Subject: [PATCH] Prevent notifier from being deleted when there are alerts referencing it --- app/models/notifier.js | 46 ++++++++++++++++++++++++++++++++++++++++- translations/en-us.yaml | 1 + 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/models/notifier.js b/app/models/notifier.js index 960cdb086..231ad6e43 100644 --- a/app/models/notifier.js +++ b/app/models/notifier.js @@ -1,10 +1,14 @@ import Resource from 'ember-api-store/models/resource'; import { inject as service } from '@ember/service'; -import { get } from '@ember/object'; +import { computed, get } from '@ember/object'; +import { hash, reject } from 'rsvp'; export default Resource.extend({ type: 'notifier', + growl: service(), + intl: service(), + globalStore: service(), modalService: service('modal'), init(...args) { @@ -33,6 +37,46 @@ export default Resource.extend({ }.property('slackConfig', 'pagerdutyConfig', 'emailConfig', 'webhookConfig'), + findAlerts() { + const globalStore = get(this, 'globalStore'); + const clusterId = get(this, 'clusterId'); + const clusterAlerts = globalStore.findAll('clusterAlert', {filter: {clusterId}}); + const projectAlerts = globalStore.findAll('projectAlert'); + return hash({ + clusterAlerts, + projectAlerts, + }).then(({ + clusterAlerts, + projectAlerts, + }) => { + const alerts = [ + ...clusterAlerts.content, + ...projectAlerts.content, + ].filter(alert => { + const recipients = get(alert, 'recipients'); + if (!recipients || recipients.length === 0) { + return false; + } + return recipients.some(recipient => recipient.notifierId === get(this, 'id')); + }); + + return alerts; + }); + }, + delete() { + const self = this; + const sup = self._super; + return this.findAlerts().then(alerts => { + if (alerts.length) { + const alertNames = alerts.map(alert => get(alert, 'displayName')).join(','); + get(this, 'growl') + .error(get(this, 'intl') + .t('notifierPage.deleteErrorMessage', {displayName: get(this, 'displayName'), alertNames})); + } else { + sup.apply(self, arguments); + } + }); + }, actions: { edit() { this.get('modalService').toggleModal('notifier/modal-new-edit', { diff --git a/translations/en-us.yaml b/translations/en-us.yaml index d93ef3e46..376b5991d 100644 --- a/translations/en-us.yaml +++ b/translations/en-us.yaml @@ -1336,6 +1336,7 @@ notifierPage: notifiers: Notifiers testedBtnLabel: OK testingBtnLabel: Testing + deleteErrorMessage: 'Notifier {displayName} is in use by: {alertNames}' notifierTypes: slack: Slack email: Email