Konstantin Aladyshev | 7c1cfe7 | 2023-05-16 09:03:25 +0000 | [diff] [blame] | 1 | import api, { getResponseCount } from '@/store/api'; |
| 2 | import i18n from '@/i18n'; |
| 3 | |
| 4 | const SnmpAlertsStore = { |
| 5 | namespaced: true, |
| 6 | state: { |
| 7 | allSnmpDetails: [], |
| 8 | }, |
| 9 | getters: { |
| 10 | allSnmpDetails(state) { |
| 11 | return state.allSnmpDetails; |
| 12 | }, |
| 13 | }, |
| 14 | mutations: { |
| 15 | setSnmpDetails(state, allSnmpDetails) { |
| 16 | state.allSnmpDetails = allSnmpDetails; |
| 17 | }, |
| 18 | }, |
| 19 | actions: { |
| 20 | async getSnmpAlertUrl() { |
| 21 | return await api |
| 22 | .get('/redfish/v1/') |
| 23 | .then((response) => api.get(response.data.EventService['@odata.id'])) |
| 24 | .then((response) => api.get(response.data.Subscriptions['@odata.id'])) |
| 25 | .then((response) => response.data['@odata.id']) |
| 26 | .catch((error) => console.log('Error', error)); |
| 27 | }, |
| 28 | async getSnmpDetails({ commit, dispatch }) { |
| 29 | const snmpAlertUrl = await dispatch('getSnmpAlertUrl'); |
| 30 | return await api |
| 31 | .get(snmpAlertUrl) |
| 32 | .then((response) => |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 33 | response.data.Members.map((user) => user['@odata.id']), |
Konstantin Aladyshev | 7c1cfe7 | 2023-05-16 09:03:25 +0000 | [diff] [blame] | 34 | ) |
| 35 | .then((userIds) => api.all(userIds.map((user) => api.get(user)))) |
| 36 | .then((users) => { |
| 37 | const snmpDetailsData = users.map((user) => user.data); |
| 38 | commit('setSnmpDetails', snmpDetailsData); |
| 39 | }) |
| 40 | .catch((error) => { |
| 41 | console.log(error); |
| 42 | const message = i18n.t('pageSnmpAlerts.toast.errorLoadSnmpDetails'); |
| 43 | throw new Error(message); |
| 44 | }); |
| 45 | }, |
| 46 | async deleteDestination({ dispatch }, id) { |
| 47 | const snmpAlertUrl = await dispatch('getSnmpAlertUrl'); |
| 48 | return await api |
| 49 | .delete(`${snmpAlertUrl}/${id}`) |
| 50 | .then(() => dispatch('getSnmpDetails')) |
| 51 | .then(() => |
| 52 | i18n.t('pageSnmpAlerts.toast.successDeleteDestination', { |
| 53 | id, |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 54 | }), |
Konstantin Aladyshev | 7c1cfe7 | 2023-05-16 09:03:25 +0000 | [diff] [blame] | 55 | ) |
| 56 | .catch((error) => { |
| 57 | console.log(error); |
| 58 | const message = i18n.t( |
| 59 | 'pageSnmpAlerts.toast.errorDeleteDestination', |
| 60 | { |
| 61 | id, |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 62 | }, |
Konstantin Aladyshev | 7c1cfe7 | 2023-05-16 09:03:25 +0000 | [diff] [blame] | 63 | ); |
| 64 | throw new Error(message); |
| 65 | }); |
| 66 | }, |
| 67 | async deleteMultipleDestinations({ dispatch }, destination) { |
| 68 | const snmpAlertUrl = await dispatch('getSnmpAlertUrl'); |
| 69 | const promises = destination.map(({ id }) => { |
| 70 | return api.delete(`${snmpAlertUrl}/${id}`).catch((error) => { |
| 71 | console.log(error); |
| 72 | return error; |
| 73 | }); |
| 74 | }); |
| 75 | return await api |
| 76 | .all(promises) |
| 77 | .then((response) => { |
| 78 | dispatch('getSnmpDetails'); |
| 79 | return response; |
| 80 | }) |
| 81 | .then( |
| 82 | api.spread((...responses) => { |
| 83 | const { successCount, errorCount } = getResponseCount(responses); |
| 84 | let toastMessages = []; |
| 85 | |
| 86 | if (successCount) { |
| 87 | const message = i18n.tc( |
| 88 | 'pageSnmpAlerts.toast.successBatchDelete', |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 89 | successCount, |
Konstantin Aladyshev | 7c1cfe7 | 2023-05-16 09:03:25 +0000 | [diff] [blame] | 90 | ); |
| 91 | toastMessages.push({ type: 'success', message }); |
| 92 | } |
| 93 | |
| 94 | if (errorCount) { |
| 95 | const message = i18n.tc( |
| 96 | 'pageSnmpAlerts.toast.errorBatchDelete', |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 97 | errorCount, |
Konstantin Aladyshev | 7c1cfe7 | 2023-05-16 09:03:25 +0000 | [diff] [blame] | 98 | ); |
| 99 | toastMessages.push({ type: 'error', message }); |
| 100 | } |
| 101 | |
| 102 | return toastMessages; |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 103 | }), |
Konstantin Aladyshev | 7c1cfe7 | 2023-05-16 09:03:25 +0000 | [diff] [blame] | 104 | ); |
| 105 | }, |
| 106 | async addDestination({ dispatch }, { data }) { |
| 107 | const snmpAlertUrl = await dispatch('getSnmpAlertUrl'); |
| 108 | return await api |
| 109 | .post(snmpAlertUrl, data) |
| 110 | .then(() => dispatch('getSnmpDetails')) |
| 111 | .then(() => i18n.t('pageSnmpAlerts.toast.successAddDestination')) |
| 112 | .catch((error) => { |
| 113 | console.log(error); |
| 114 | const message = i18n.t('pageSnmpAlerts.toast.errorAddDestination'); |
| 115 | throw new Error(message); |
| 116 | }); |
| 117 | }, |
| 118 | }, |
| 119 | }; |
| 120 | |
| 121 | export default SnmpAlertsStore; |