1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const ChassisStore = {
5  namespaced: true,
6  state: {
7    chassis: [],
8  },
9  getters: {
10    chassis: (state) => state.chassis,
11  },
12  mutations: {
13    setChassisInfo: (state, data) => {
14      state.chassis = data.map((chassis) => {
15        const {
16          Id,
17          Status = {},
18          PartNumber,
19          SerialNumber,
20          ChassisType,
21          Manufacturer,
22          PowerState,
23          LocationIndicatorActive,
24          AssetTag,
25          MaxPowerWatts,
26          MinPowerWatts,
27          Name,
28        } = chassis;
29
30        return {
31          id: Id,
32          health: Status.Health,
33          partNumber: PartNumber,
34          serialNumber: SerialNumber,
35          chassisType: ChassisType,
36          manufacturer: Manufacturer,
37          powerState: PowerState,
38          statusState: Status.State,
39          healthRollup: Status.HealthRollup,
40          assetTag: AssetTag,
41          maxPowerWatts: MaxPowerWatts,
42          minPowerWatts: MinPowerWatts,
43          name: Name,
44          identifyLed: LocationIndicatorActive,
45          uri: chassis['@odata.id'],
46        };
47      });
48    },
49  },
50  actions: {
51    async getChassisInfo({ commit }) {
52      return await api
53        .get('/redfish/v1/Chassis')
54        .then(({ data: { Members = [] } }) =>
55          Members.map((member) => api.get(member['@odata.id']))
56        )
57        .then((promises) => api.all(promises))
58        .then((response) => {
59          const data = response.map(({ data }) => data);
60          commit('setChassisInfo', data);
61        })
62        .catch((error) => console.log(error));
63    },
64    async updateIdentifyLedValue({ dispatch }, led) {
65      const uri = led.uri;
66      const updatedIdentifyLedValue = {
67        LocationIndicatorActive: led.identifyLed,
68      };
69      return await api
70        .patch(uri, updatedIdentifyLedValue)
71        .then(() => dispatch('getChassisInfo'))
72        .catch((error) => {
73          dispatch('getChassisInfo');
74          console.log('error', error);
75          if (led.identifyLed) {
76            throw new Error(
77              i18n.t('pageInventory.toast.errorEnableIdentifyLed')
78            );
79          } else {
80            throw new Error(
81              i18n.t('pageInventory.toast.errorDisableIdentifyLed')
82            );
83          }
84        });
85    },
86  },
87};
88
89export default ChassisStore;
90