xref: /openbmc/webui-vue/src/store/modules/Settings/SnmpAlertsStore.js (revision de23ea23d88451a2fa2774ec72053772603c23ae)
1import api, { getResponseCount } from '@/store/api';
2import i18n from '@/i18n';
3
4const 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) =>
33          response.data.Members.map((user) => user['@odata.id']),
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.global.t(
43            'pageSnmpAlerts.toast.errorLoadSnmpDetails',
44          );
45          throw new Error(message);
46        });
47    },
48    async deleteDestination({ dispatch }, id) {
49      const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
50      return await api
51        .delete(`${snmpAlertUrl}/${id}`)
52        .then(() => dispatch('getSnmpDetails'))
53        .then(() =>
54          i18n.global.t('pageSnmpAlerts.toast.successDeleteDestination', {
55            id,
56          }),
57        )
58        .catch((error) => {
59          console.log(error);
60          const message = i18n.global.t(
61            'pageSnmpAlerts.toast.errorDeleteDestination',
62            {
63              id,
64            },
65          );
66          throw new Error(message);
67        });
68    },
69    async deleteMultipleDestinations({ dispatch }, destination) {
70      const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
71      const promises = destination.map(({ id }) => {
72        return api.delete(`${snmpAlertUrl}/${id}`).catch((error) => {
73          console.log(error);
74          return error;
75        });
76      });
77      return await api
78        .all(promises)
79        .then((response) => {
80          dispatch('getSnmpDetails');
81          return response;
82        })
83        .then(
84          api.spread((...responses) => {
85            const { successCount, errorCount } = getResponseCount(responses);
86            let toastMessages = [];
87
88            if (successCount) {
89              const message = i18n.global.t(
90                'pageSnmpAlerts.toast.successBatchDelete',
91                successCount,
92              );
93              toastMessages.push({ type: 'success', message });
94            }
95
96            if (errorCount) {
97              const message = i18n.global.t(
98                'pageSnmpAlerts.toast.errorBatchDelete',
99                errorCount,
100              );
101              toastMessages.push({ type: 'error', message });
102            }
103
104            return toastMessages;
105          }),
106        );
107    },
108    async addDestination({ dispatch }, { data }) {
109      const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
110      return await api
111        .post(snmpAlertUrl, data)
112        .then(() => dispatch('getSnmpDetails'))
113        .then(() => i18n.global.t('pageSnmpAlerts.toast.successAddDestination'))
114        .catch((error) => {
115          console.log(error);
116          const message = i18n.global.t(
117            'pageSnmpAlerts.toast.errorAddDestination',
118          );
119          throw new Error(message);
120        });
121    },
122  },
123};
124
125export default SnmpAlertsStore;
126