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          Location,
29        } = chassis;
30
31        return {
32          id: Id,
33          health: Status.Health,
34          partNumber: PartNumber,
35          serialNumber: SerialNumber,
36          chassisType: ChassisType,
37          manufacturer: Manufacturer,
38          powerState: PowerState,
39          statusState: Status.State,
40          healthRollup: Status.HealthRollup,
41          assetTag: AssetTag,
42          maxPowerWatts: MaxPowerWatts,
43          minPowerWatts: MinPowerWatts,
44          name: Name,
45          identifyLed: LocationIndicatorActive,
46          uri: chassis['@odata.id'],
47          locationNumber: Location?.PartLocation?.ServiceLabel,
48        };
49      });
50    },
51  },
52  actions: {
53    async getChassisInfo({ commit }) {
54      return await api
55        .get('/redfish/v1/Chassis')
56        .then(({ data: { Members = [] } }) =>
57          Members.map((member) => api.get(member['@odata.id']))
58        )
59        .then((promises) => api.all(promises))
60        .then((response) => {
61          const data = response.map(({ data }) => data);
62          commit('setChassisInfo', data);
63        })
64        .catch((error) => console.log(error));
65    },
66    async updateIdentifyLedValue({ dispatch }, led) {
67      const uri = led.uri;
68      const updatedIdentifyLedValue = {
69        LocationIndicatorActive: led.identifyLed,
70      };
71      return await api
72        .patch(uri, updatedIdentifyLedValue)
73        .then(() => dispatch('getChassisInfo'))
74        .catch((error) => {
75          dispatch('getChassisInfo');
76          console.log('error', error);
77          if (led.identifyLed) {
78            throw new Error(
79              i18n.t('pageInventory.toast.errorEnableIdentifyLed')
80            );
81          } else {
82            throw new Error(
83              i18n.t('pageInventory.toast.errorDisableIdentifyLed')
84            );
85          }
86        });
87    },
88  },
89};
90
91export default ChassisStore;
92