import types from '../mutation-types'; import PlatformAPI from '../../api/platform'; import { throwErrorMessage } from '../utils/api'; export const state = { accounts: [], users: [], agentBots: [], installationConfigs: [], uiFlags: { isFetchingAccounts: false, isFetchingUsers: false, isFetchingAgentBots: false, isFetchingConfigs: false, isCreating: false, isUpdating: false, isDeleting: false, }, }; export const getters = { getAccounts: $state => $state.accounts, getUsers: $state => $state.users, getPlatformAgentBots: $state => $state.agentBots, getInstallationConfigs: $state => $state.installationConfigs, getUIFlags: $state => $state.uiFlags, }; export const actions = { // ── Accounts ────────────────────────────────────────── fetchAccounts: async ({ commit }, params) => { commit(types.SET_PLATFORM_UI_FLAG, { isFetchingAccounts: true }); try { const response = await PlatformAPI.getAccounts(params); const payload = response.data?.data || response.data || []; commit(types.SET_PLATFORM_ACCOUNTS, payload); return payload; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isFetchingAccounts: false }); } return null; }, createAccount: async ({ commit }, accountData) => { commit(types.SET_PLATFORM_UI_FLAG, { isCreating: true }); try { const response = await PlatformAPI.createAccount(accountData); const payload = response.data?.data || response.data; commit(types.ADD_PLATFORM_ACCOUNT, payload); return payload; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isCreating: false }); } return null; }, updateAccount: async ({ commit }, { id, data }) => { commit(types.SET_PLATFORM_UI_FLAG, { isUpdating: true }); try { const response = await PlatformAPI.updateAccount(id, data); commit(types.EDIT_PLATFORM_ACCOUNT, response.data); return response.data; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isUpdating: false }); } return null; }, deleteAccount: async ({ commit }, id) => { commit(types.SET_PLATFORM_UI_FLAG, { isDeleting: true }); try { await PlatformAPI.deleteAccount(id); commit(types.DELETE_PLATFORM_ACCOUNT, id); } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isDeleting: false }); } }, // ── Users ───────────────────────────────────────────── fetchUsers: async ({ commit }, params) => { commit(types.SET_PLATFORM_UI_FLAG, { isFetchingUsers: true }); try { const response = await PlatformAPI.getUsers(params); const payload = response.data?.data || response.data || []; commit(types.SET_PLATFORM_USERS, payload); return payload; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isFetchingUsers: false }); } return null; }, createUser: async ({ commit }, userData) => { commit(types.SET_PLATFORM_UI_FLAG, { isCreating: true }); try { const response = await PlatformAPI.createUser(userData); const payload = response.data?.data || response.data; commit(types.ADD_PLATFORM_USER, payload); return payload; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isCreating: false }); } return null; }, updateUser: async ({ commit }, { id, data }) => { commit(types.SET_PLATFORM_UI_FLAG, { isUpdating: true }); try { const response = await PlatformAPI.updateUser(id, data); commit(types.EDIT_PLATFORM_USER, response.data); return response.data; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isUpdating: false }); } return null; }, deleteUser: async ({ commit }, id) => { commit(types.SET_PLATFORM_UI_FLAG, { isDeleting: true }); try { await PlatformAPI.deleteUser(id); commit(types.DELETE_PLATFORM_USER, id); } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isDeleting: false }); } }, // ── Agent Bots (platform-level) ─────────────────────── fetchAgentBots: async ({ commit }, params) => { commit(types.SET_PLATFORM_UI_FLAG, { isFetchingAgentBots: true }); try { const response = await PlatformAPI.getAgentBots(params); const payload = response.data?.data || response.data || []; commit(types.SET_PLATFORM_AGENT_BOTS, payload); return payload; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isFetchingAgentBots: false }); } return null; }, createAgentBot: async ({ commit }, botData) => { commit(types.SET_PLATFORM_UI_FLAG, { isCreating: true }); try { const response = await PlatformAPI.createAgentBot(botData); const payload = response.data?.data || response.data; commit(types.ADD_PLATFORM_AGENT_BOT, payload); return payload; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isCreating: false }); } return null; }, updateAgentBot: async ({ commit }, { id, data }) => { commit(types.SET_PLATFORM_UI_FLAG, { isUpdating: true }); try { const response = await PlatformAPI.updateAgentBot(id, data); commit(types.EDIT_PLATFORM_AGENT_BOT, response.data); return response.data; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isUpdating: false }); } return null; }, deleteAgentBot: async ({ commit }, id) => { commit(types.SET_PLATFORM_UI_FLAG, { isDeleting: true }); try { await PlatformAPI.deleteAgentBot(id); commit(types.DELETE_PLATFORM_AGENT_BOT, id); } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isDeleting: false }); } }, // ── Installation Config ─────────────────────────────── fetchInstallationConfigs: async ({ commit }, params) => { commit(types.SET_PLATFORM_UI_FLAG, { isFetchingConfigs: true }); try { const response = await PlatformAPI.getInstallationConfigs(params); const payload = response.data?.data || response.data || []; commit(types.SET_INSTALLATION_CONFIGS, payload); return payload; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isFetchingConfigs: false }); } return null; }, createInstallationConfig: async ({ commit }, configData) => { commit(types.SET_PLATFORM_UI_FLAG, { isCreating: true }); try { const response = await PlatformAPI.createInstallationConfig(configData); const payload = response.data?.data || response.data; commit(types.ADD_INSTALLATION_CONFIG, payload); return payload; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isCreating: false }); } return null; }, updateInstallationConfig: async ({ commit }, { id, data }) => { commit(types.SET_PLATFORM_UI_FLAG, { isUpdating: true }); try { const response = await PlatformAPI.updateInstallationConfig(id, data); commit(types.EDIT_INSTALLATION_CONFIG, response.data); return response.data; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isUpdating: false }); } return null; }, deleteInstallationConfig: async ({ commit }, id) => { commit(types.SET_PLATFORM_UI_FLAG, { isDeleting: true }); try { await PlatformAPI.deleteInstallationConfig(id); commit(types.DELETE_INSTALLATION_CONFIG, id); } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isDeleting: false }); } }, // ── Banners ─────────────────────────────────────────── fetchBanners: async ({ commit }, params) => { commit(types.SET_PLATFORM_UI_FLAG, { isFetchingBanners: true }); try { const response = await PlatformAPI.getBanners(params); const payload = response.data?.data || response.data || []; commit(types.SET_PLATFORM_BANNERS, payload); return payload; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isFetchingBanners: false }); } return null; }, createBanner: async ({ commit }, bannerData) => { commit(types.SET_PLATFORM_UI_FLAG, { isCreating: true }); try { const response = await PlatformAPI.createBanner(bannerData); const payload = response.data?.data || response.data; commit(types.ADD_PLATFORM_BANNER, payload); return payload; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isCreating: false }); } return null; }, updateBanner: async ({ commit }, { id, data }) => { commit(types.SET_PLATFORM_UI_FLAG, { isUpdating: true }); try { const response = await PlatformAPI.updateBanner(id, data); commit(types.EDIT_PLATFORM_BANNER, response.data); return response.data; } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isUpdating: false }); } return null; }, deleteBanner: async ({ commit }, id) => { commit(types.SET_PLATFORM_UI_FLAG, { isDeleting: true }); try { await PlatformAPI.deleteBanner(id); commit(types.DELETE_PLATFORM_BANNER, id); } catch (error) { throwErrorMessage(error); } finally { commit(types.SET_PLATFORM_UI_FLAG, { isDeleting: false }); } }, }; export const mutations = { [types.SET_PLATFORM_UI_FLAG]($state, data) { $state.uiFlags = { ...$state.uiFlags, ...data }; }, // Accounts [types.SET_PLATFORM_ACCOUNTS]($state, data) { $state.accounts = Array.isArray(data) ? data : data?.payload || []; }, [types.ADD_PLATFORM_ACCOUNT]($state, account) { $state.accounts.unshift(account); }, [types.EDIT_PLATFORM_ACCOUNT]($state, account) { const idx = $state.accounts.findIndex(a => a.id === account.id); if (idx !== -1) $state.accounts[idx] = account; }, [types.DELETE_PLATFORM_ACCOUNT]($state, id) { $state.accounts = $state.accounts.filter(a => a.id !== id); }, // Users [types.SET_PLATFORM_USERS]($state, data) { $state.users = Array.isArray(data) ? data : data?.payload || []; }, [types.ADD_PLATFORM_USER]($state, user) { $state.users.unshift(user); }, [types.EDIT_PLATFORM_USER]($state, user) { const idx = $state.users.findIndex(u => u.id === user.id); if (idx !== -1) $state.users[idx] = user; }, [types.DELETE_PLATFORM_USER]($state, id) { $state.users = $state.users.filter(u => u.id !== id); }, // Agent Bots [types.SET_PLATFORM_AGENT_BOTS]($state, data) { $state.agentBots = Array.isArray(data) ? data : data?.payload || []; }, [types.ADD_PLATFORM_AGENT_BOT]($state, bot) { $state.agentBots.unshift(bot); }, [types.EDIT_PLATFORM_AGENT_BOT]($state, bot) { const idx = $state.agentBots.findIndex(b => b.id === bot.id); if (idx !== -1) $state.agentBots[idx] = bot; }, [types.DELETE_PLATFORM_AGENT_BOT]($state, id) { $state.agentBots = $state.agentBots.filter(b => b.id !== id); }, // Installation Configs [types.SET_INSTALLATION_CONFIGS]($state, data) { $state.installationConfigs = Array.isArray(data) ? data : data?.payload || []; }, [types.ADD_INSTALLATION_CONFIG]($state, config) { $state.installationConfigs.unshift(config); }, [types.EDIT_INSTALLATION_CONFIG]($state, config) { const idx = $state.installationConfigs.findIndex( c => c.id === config.id ); if (idx !== -1) $state.installationConfigs[idx] = config; }, [types.DELETE_INSTALLATION_CONFIG]($state, id) { $state.installationConfigs = $state.installationConfigs.filter( c => c.id !== id ); }, }; export default { namespaced: true, actions, state, getters, mutations, };