2020-10-31 20:30:02 +00:00
|
|
|
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex';
|
|
|
|
import { StateInterface } from 'src/store';
|
|
|
|
import { axios } from 'src/boot/axios';
|
|
|
|
import { AxiosResponse } from 'axios';
|
|
|
|
|
|
|
|
interface BalanceResponse {
|
|
|
|
balance: number;
|
|
|
|
credit: number;
|
|
|
|
debit: number;
|
|
|
|
}
|
|
|
|
|
2021-01-21 13:24:46 +00:00
|
|
|
export interface UserBalance extends BalanceResponse {
|
|
|
|
limit: number | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface BalanceInterface {
|
|
|
|
balances: Map<string, UserBalance>;
|
|
|
|
shortcuts: Array<number>;
|
2020-11-15 13:18:28 +00:00
|
|
|
loading: number;
|
2020-10-31 20:30:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 13:24:46 +00:00
|
|
|
export interface StateInterfaceBalance extends StateInterface {
|
|
|
|
balance: BalanceInterface;
|
|
|
|
}
|
|
|
|
|
2020-10-31 20:30:02 +00:00
|
|
|
const state: BalanceInterface = {
|
2021-01-21 13:24:46 +00:00
|
|
|
balances: new Map<string, UserBalance>(),
|
|
|
|
shortcuts: [],
|
2020-11-15 13:18:28 +00:00
|
|
|
loading: 0
|
2020-10-31 20:30:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const mutations: MutationTree<BalanceInterface> = {
|
2021-01-21 13:24:46 +00:00
|
|
|
setBalance(state, data: { userid: string; balance: BalanceResponse }) {
|
|
|
|
state.balances.set(
|
|
|
|
data.userid,
|
|
|
|
Object.assign({ limit: state.balances.get(data.userid)?.limit || null }, data.balance)
|
|
|
|
);
|
2020-10-31 20:30:02 +00:00
|
|
|
},
|
2021-01-21 13:24:46 +00:00
|
|
|
changeBalance(state, data: { userid: string; amount: number }) {
|
|
|
|
const user = <UserBalance>state.balances.get(data.userid);
|
|
|
|
if (data.amount < 0) user.debit += data.amount;
|
|
|
|
else user.credit += data.amount;
|
|
|
|
user.balance += data.amount;
|
2020-10-31 20:30:02 +00:00
|
|
|
},
|
2021-01-21 13:24:46 +00:00
|
|
|
setLimit(state, data: { userid: string; limit: number | null }) {
|
|
|
|
if (state.balances.has(data.userid))
|
|
|
|
(<UserBalance>state.balances.get(data.userid)).limit = data.limit;
|
|
|
|
else state.balances.set(data.userid, { balance: 0, debit: 0, credit: 0, limit: data.limit });
|
2020-11-15 13:18:28 +00:00
|
|
|
},
|
|
|
|
setLoading(state, data = true) {
|
|
|
|
if (data) state.loading += 1;
|
|
|
|
else state.loading -= 1;
|
2021-01-21 13:24:46 +00:00
|
|
|
},
|
|
|
|
setShortcuts(state, data: Array<number>) {
|
|
|
|
state.shortcuts = data.sort().reverse();
|
2020-10-31 20:30:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const actions: ActionTree<BalanceInterface, StateInterface> = {
|
2021-01-21 13:24:46 +00:00
|
|
|
getShortcuts({ commit, state, rootState }, force = false) {
|
|
|
|
if (force || state.shortcuts.length == 0) {
|
|
|
|
commit('setLoading');
|
|
|
|
const user = <FG.User>rootState.user.currentUser;
|
|
|
|
return axios
|
|
|
|
.get(`/users/${user.userid}/balance/shortcuts`)
|
|
|
|
.then(({ data }: AxiosResponse<BalanceResponse>) => {
|
|
|
|
commit('setShortcuts', data);
|
|
|
|
return data;
|
|
|
|
})
|
|
|
|
.finally(() => commit('setLoading', false));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getBalance({ commit, rootState }, user: FG.User | undefined = undefined) {
|
2020-11-15 13:18:28 +00:00
|
|
|
commit('setLoading');
|
2021-01-21 13:24:46 +00:00
|
|
|
if (!user) user = <FG.User>rootState.user.currentUser;
|
|
|
|
return axios
|
|
|
|
.get(`/users/${user.userid}/balance`)
|
2020-10-31 20:30:02 +00:00
|
|
|
.then(({ data }: AxiosResponse<BalanceResponse>) => {
|
2021-01-21 13:24:46 +00:00
|
|
|
commit('setBalance', { userid: user?.userid, balance: data });
|
|
|
|
return data;
|
2020-11-15 13:18:28 +00:00
|
|
|
})
|
|
|
|
.finally(() => commit('setLoading', false));
|
2020-10-31 20:30:02 +00:00
|
|
|
},
|
2020-11-15 13:18:28 +00:00
|
|
|
getLimit({ rootState, commit }) {
|
|
|
|
commit('setLoading');
|
2020-10-31 20:30:02 +00:00
|
|
|
axios
|
2020-11-04 22:53:10 +00:00
|
|
|
/* eslint-disable-next-line @typescript-eslint/restrict-template-expressions */
|
|
|
|
.get(`/users/${rootState.user.currentUser?.userid}/balance/limit`)
|
2020-10-31 20:30:02 +00:00
|
|
|
.then(({ data }) => {
|
|
|
|
console.log(data);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.warn(err);
|
2020-11-15 13:18:28 +00:00
|
|
|
})
|
|
|
|
.finally(() => commit('setLoading', false));
|
2020-10-31 20:30:02 +00:00
|
|
|
},
|
2021-01-21 13:24:46 +00:00
|
|
|
revert({ dispatch }, transaction: FG.Transaction) {
|
|
|
|
return axios.delete(`/balance/${transaction.id}`).then(() => {
|
|
|
|
dispatch('getBalance').catch(err => console.warn(err));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
changeBalance({ dispatch, commit }, data: { amount: number; user: string; sender?: string }) {
|
2020-11-15 13:18:28 +00:00
|
|
|
commit('setLoading');
|
2021-01-21 13:24:46 +00:00
|
|
|
return axios
|
|
|
|
.put(`/users/${data.user}/balance`, data)
|
|
|
|
.then((response: AxiosResponse<FG.Transaction>) => {
|
|
|
|
commit(state.balances.has(data.user) ? 'changeBalance' : 'setBalance', {
|
|
|
|
userid: data.user,
|
|
|
|
amount: data.amount
|
|
|
|
});
|
|
|
|
if (data.sender)
|
|
|
|
commit(state.balances.has(data.sender) ? 'changeBalance' : 'setBalance', {
|
|
|
|
userid: data.sender,
|
|
|
|
amount: -1 * data.amount
|
|
|
|
});
|
|
|
|
return response.data;
|
2020-10-31 20:30:02 +00:00
|
|
|
})
|
2021-01-21 13:24:46 +00:00
|
|
|
.catch(err => {
|
|
|
|
// Maybe Balance changed
|
2020-10-31 20:30:02 +00:00
|
|
|
dispatch('getBalance').catch(err => console.warn(err));
|
2021-01-21 13:24:46 +00:00
|
|
|
console.warn(err);
|
2020-10-31 20:30:02 +00:00
|
|
|
})
|
2020-11-15 13:18:28 +00:00
|
|
|
.finally(() => commit('setLoading', false));
|
2020-10-31 20:30:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getters: GetterTree<BalanceInterface, StateInterface> = {};
|
|
|
|
|
|
|
|
const balance: Module<BalanceInterface, StateInterface> = {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
mutations,
|
|
|
|
actions,
|
|
|
|
getters
|
|
|
|
};
|
|
|
|
|
|
|
|
export default balance;
|