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