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.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, 54 }), 55 ) 56 .catch((error) => { 57 console.log(error); 58 const message = i18n.t( 59 'pageSnmpAlerts.toast.errorDeleteDestination', 60 { 61 id, 62 }, 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', 89 successCount, 90 ); 91 toastMessages.push({ type: 'success', message }); 92 } 93 94 if (errorCount) { 95 const message = i18n.tc( 96 'pageSnmpAlerts.toast.errorBatchDelete', 97 errorCount, 98 ); 99 toastMessages.push({ type: 'error', message }); 100 } 101 102 return toastMessages; 103 }), 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 121export default SnmpAlertsStore; 122