import axios from 'axios'; // eslint-disable-next-line no-unused-vars import url from '@/plugins/routes' const state = { users: [], activeUser: { username: null, }, allUsers: [], user: null, errorMails: null, errorMail: null, }; const mutations = { setAllUsers: (state, users) => { state.allUsers = [] state.allUsers = users for (let i = 0; i < state.allUsers.length; i++) { state.allUsers[i].fullName = state.allUsers[i].firstname + " " + state.allUsers[i].lastname } }, setActiveUser: (state, user) => { if (state.activeUser.username === user.username) { state.activeUser = {username: null} } else { state.activeUser = user } state.errorMail = null }, setUsers: (state, users) => { // eslint-disable-next-line no-console console.log('users', users) for (let user in users) { // eslint-disable-next-line no-console console.log('user', user) let list = {} for (let creditList in users[user]['creditList']) { let amount = mutations.createAmount(users[user]['creditList'][creditList]) let credit = mutations.createCredit(users[user]['creditList'][creditList]) let sum = mutations.createSum(credit, amount) list[creditList] = [{...credit}, {...amount}, {...sum}] } let existUser = state.users.find(a => {return a.username === user}) // eslint-disable-next-line no-console console.log(existUser) if (existUser) { existUser.username = users[user].username existUser.firstname = users[user].firstname existUser.lastname = users[user].lastname existUser.limit = users[user].limit existUser.locked = users[user].locked existUser.autoLock = users[user].autoLock existUser.creditList = list } else { state.users.push({ username: users[user].username, firstname: users[user].firstname, lastname: users[user].lastname, limit: users[user].limit, locked: users[user].locked, autoLock: users[user].autoLock, creditList: list, expand: false }) } } mutations.sortUsers(state) // eslint-disable-next-line no-console console.log(state.users) }, createAmount(creditList) { let amount = { type:'Schulden', jan_amount: 0 - creditList.jan.depts, feb_amount: 0 - creditList.feb.depts, maer_amount: 0 - creditList.maer.depts, apr_amount:0 - creditList.apr.depts, mai_amount: 0 - creditList.mai.depts, jun_amount: 0 - creditList.jun.depts, jul_amount: 0 - creditList.jul.depts, aug_amount: 0 - creditList.aug.depts, sep_amount: 0 - creditList.sep.depts, okt_amount: 0 - creditList.okt.depts, nov_amount: 0 - creditList.nov.depts, dez_amount: 0 - creditList.dez.depts, last: 0 - creditList['last'] } amount.sum = amount.jan_amount + amount.feb_amount + amount.maer_amount + amount.apr_amount + amount.mai_amount + amount.jun_amount + amount.jul_amount + amount.aug_amount + amount.sep_amount + amount.okt_amount + amount.nov_amount + amount.dez_amount return amount }, createCredit(creditList) { let credit = { type:'Guthaben', jan_amount: creditList.jan.credit, feb_amount: creditList.feb.credit, maer_amount: creditList.maer.credit, apr_amount: creditList.apr.credit, mai_amount: creditList.mai.credit, jun_amount: creditList.jun.credit, jul_amount: creditList.jul.credit, aug_amount: creditList.aug.credit, sep_amount: creditList.sep.credit, okt_amount: creditList.okt.credit, nov_amount: creditList.nov.credit, dez_amount: creditList.dez.credit } credit.sum = credit.jan_amount + credit.feb_amount + credit.maer_amount + credit.apr_amount + credit.mai_amount + credit.jun_amount + credit.jul_amount + credit.aug_amount + credit.sep_amount + credit.okt_amount + credit.nov_amount + credit.dez_amount return credit }, createSum(credit, amount) { let sum = { type:'Summe', jan_amount: credit.jan_amount + amount.jan_amount, feb_amount: credit.feb_amount + amount.feb_amount, maer_amount: credit.maer_amount + amount.maer_amount, apr_amount: credit.apr_amount + amount.apr_amount, mai_amount: credit.mai_amount + amount.mai_amount, jun_amount: credit.jun_amount + amount.jun_amount, jul_amount: credit.jul_amount + amount.jul_amount, aug_amount: credit.aug_amount + amount.aug_amount, sep_amount: credit.sep_amount + amount.sep_amount, okt_amount: credit.okt_amount + amount.okt_amount, nov_amount: credit.nov_amount + amount.nov_amount, dez_amount: credit.dez_amount + amount.dez_amount, } sum.sum = sum.jan_amount + sum.feb_amount + sum.maer_amount + sum.apr_amount + sum.mai_amount + sum.jun_amount + sum.jul_amount + sum.aug_amount + sum.sep_amount + sum.okt_amount + sum.nov_amount + sum.dez_amount return sum }, sortUsers: (state) => { state.users = state.users.sort((a,b) => { if (a.lastname > b.lastname) return 1 if (a.lastname < b.lastname) return -1 if (a.firstname > b.firstname) return 1 if (a.firstname < b.firstname) return -1 return 0 }) }, updateUsers: (state, data) => { let index = state.users.indexOf(state.users.find(a => {return a.username === data.username})) if (data.creditLists !== undefined) { let list = {} for (let creditList in data.creditLists) { let amount = mutations.createAmount(data.creditLists[creditList]) let credit = mutations.createCredit(data.creditLists[creditList]) let sum = mutations.createSum(credit, amount) list[creditList] = [{...credit}, {...amount}, {...sum}] } state.users[index].creditList = list } if (data.locked !== undefined) state.users[index].locked = data.locked if (data.limit !== undefined) state.users[index].limit = data.limit if (data.autoLock !== undefined) state.users[index].autoLock = data.autoLock }, setMails: (state, data) => { state.errorMails = data }, setMail: (state, data) => { state.errorMail = data } }; const actions = { // eslint-disable-next-line no-unused-vars async getAllUsers({commit, rootState, dispatch}) { // eslint-disable-next-line no-console console.log(rootState) try { const response = await axios.post(url.searchUser, {searchString: ""}, {headers: {Token: rootState.login.user.accessToken}}) commit('setAllUsers', response.data) } catch (err) { // eslint-disable-next-line no-console if (err.response.status === 401) dispatch('logout') } }, async getUsers({commit, rootState, dispatch}) { try { const response = await axios.get(url.getFinanzerMain, {headers: {Token: rootState.login.user.accessToken}}) // eslint-disable-next-line no-console console.log('response', response.data) commit('setUsers', response.data) } catch (err) { // eslint-disable-next-line no-console console.log(err) if (err.response) if (err.response.status === 401) dispatch('logout') } }, setActiveUser({ commit }, user) { commit('setActiveUser', user) }, async addAmount({commit, rootState, dispatch}, data) { try { const response = await axios.post(url.finanzerAddAmount, {userId: data.user.username, amount: data.amount * 100, year: data.year, month: data.month}, {headers: {Token: rootState.login.user.accessToken}}) const creditLists = {...response.data} delete creditLists.locked commit('updateUsers', {creditLists: creditLists, locked: response.data.locked, username: data.user.username}) } catch (err) { if (err.response) if (err.response.status === 401) dispatch('logout') } }, async addCredit({commit, rootState, dispatch}, data) { try { const response = await axios.post(url.finanzerAddCredit, {userId: data.user.username, credit: data.credit * 100, year: data.year, month: data.month}, {headers: {Token: rootState.login.user.accessToken}}) const creditLists = {...response.data} delete creditLists.locked commit('updateUsers', {creditLists: creditLists, locked: response.data.locked, username: data.user.username}) } catch (err) { if (err.response) if (err.response.status === 401) dispatch('logout') } }, async doLock({commit, rootState, dispatch}, data) { try { const response = await axios.post(url.lockUser, {userId: data.user.username, locked: data.locked}, {headers: {Token: rootState.login.user.accessToken}}) commit('updateUsers', response.data) } catch (e) { if (e.response) if(e.response.status === 401) dispatch('logout') } }, async saveConfig({commit, rootState, dispatch}, data) { try { const response = await axios.post(url.finanzerSetConfig, {userId: data.user.username, limit: data.limit * 100, autoLock: data.autoLock}, {headers: {Token: rootState.login.user.accessToken}}) commit('updateUsers', response.data) } catch (e) { if (e.response) if(e.response.status === 401) dispatch('logout') } }, async addUser({commit, rootState, dispatch}, data) { try { const response = await axios.post(url.finanzerAddUser, {userId: data.username}, {headers: {Token: rootState.login.user.accessToken}}) commit('setUsers', response.data) } catch (e) { if (e.response) if (e.response.status === 401) dispatch('logout') } }, async sendMails({commit, rootState, dispatch}) { try { const response = await axios.get(url.finanzerSendAllMail, {headers: {Token: rootState.login.user.accessToken}}) commit('setMails', response.data) } catch (e) { if (e.response) if (e.response.status === 401) dispatch('logout') } }, async sendMail({commit, rootState, dispatch}, data) { try { const response = await axios.post(url.finanzerSendOneMail, {userId: data.username}, {headers: {Token: rootState.login.user.accessToken}}) commit('setMail', response.data) } catch (e) { if (e.response) if (e.response.status === 401) dispatch('logout') } } }; const getters = { users: state => { return state.users }, activeUser: state => { return state.activeUser }, allUsers: state => { return state.allUsers }, user: state => { return state.user }, errorMails: state => { return state.errorMails }, errorMail: state => { return state.errorMail } }; export default { state, mutations, actions, getters }