Implemented load Transactions
This commit is contained in:
parent
4ff63b458d
commit
17e640892a
|
@ -27,10 +27,11 @@ declare namespace FG {
|
|||
id: number;
|
||||
time: Date;
|
||||
amount: number;
|
||||
reversal?: this;
|
||||
reversal_id: number;
|
||||
sender_id?: string;
|
||||
receiver_id?: string;
|
||||
author_id?: string;
|
||||
original_id?: number;
|
||||
}
|
||||
interface Event {
|
||||
id: number;
|
||||
|
|
|
@ -77,7 +77,9 @@ export default defineComponent({
|
|||
}
|
||||
};
|
||||
|
||||
const isReversed = computed(() => props.transaction.reversal != undefined);
|
||||
const isReversed = computed(
|
||||
() => props.transaction.reversal_id != undefined || props.transaction.original_id != undefined
|
||||
);
|
||||
|
||||
const canReverse = computed(
|
||||
() =>
|
||||
|
|
|
@ -31,6 +31,10 @@ const state: BalanceInterface = {
|
|||
loading: 0
|
||||
};
|
||||
|
||||
function fixTransaction(t: FG.Transaction) {
|
||||
t.time = new Date(t.time);
|
||||
}
|
||||
|
||||
const mutations: MutationTree<BalanceInterface> = {
|
||||
setBalance(state, data: { userid: string; balance: BalanceResponse }) {
|
||||
state.balances.set(
|
||||
|
@ -59,6 +63,10 @@ const mutations: MutationTree<BalanceInterface> = {
|
|||
addTransaction(state, data: FG.Transaction) {
|
||||
state.transactions.push(data);
|
||||
},
|
||||
addTransactions(state, data: [FG.Transaction]) {
|
||||
state.transactions.push(...data);
|
||||
state.transactions.sort((a, b) => (a.time <= b.time ? -1 : 1));
|
||||
},
|
||||
reverseTransaction(state, data: { transaction: FG.Transaction; reversal: FG.Transaction }) {
|
||||
const idx = state.transactions.findIndex(value => value.id === data.transaction.id);
|
||||
data.transaction.reversal = data.reversal;
|
||||
|
@ -109,6 +117,25 @@ const actions: ActionTree<BalanceInterface, StateInterface> = {
|
|||
})
|
||||
.finally(() => commit('setLoading', false));
|
||||
},
|
||||
getTransactions(
|
||||
{ commit, rootState },
|
||||
payload: {
|
||||
userid?: string;
|
||||
filter?: { limit?: number; offset?: number; from?: Date; to?: Date };
|
||||
}
|
||||
) {
|
||||
commit('setLoading');
|
||||
if (!payload.userid) payload.userid = (<FG.User>rootState.user.currentUser).userid;
|
||||
if (!payload.filter) payload.filter = { limit: 10 };
|
||||
return axios
|
||||
.get(`/users/${payload.userid}/balance/transactions`, { params: payload.filter || {} })
|
||||
.then(({ data }: AxiosResponse<[FG.Transaction]>) => {
|
||||
data.forEach(t => fixTransaction(t));
|
||||
commit('addTransactions', data);
|
||||
return data;
|
||||
})
|
||||
.finally(() => commit('setLoading', false));
|
||||
},
|
||||
getLimit({ rootState, commit }) {
|
||||
commit('setLoading');
|
||||
axios
|
||||
|
@ -125,8 +152,9 @@ const actions: ActionTree<BalanceInterface, StateInterface> = {
|
|||
revert({ dispatch, commit }, transaction: FG.Transaction) {
|
||||
return axios
|
||||
.delete(`/balance/${transaction.id}`)
|
||||
.then((response: AxiosResponse<FG.Transaction>) => {
|
||||
commit('reverseTransaction', { transaction: transaction, reversal: response.data });
|
||||
.then(({ data }: AxiosResponse<FG.Transaction>) => {
|
||||
fixTransaction(data);
|
||||
commit('reverseTransaction', { transaction: transaction, reversal: data });
|
||||
dispatch('getBalance').catch(err => console.warn(err));
|
||||
});
|
||||
},
|
||||
|
@ -136,7 +164,7 @@ const actions: ActionTree<BalanceInterface, StateInterface> = {
|
|||
.put(`/users/${data.user}/balance`, data)
|
||||
.then((response: AxiosResponse<FG.Transaction>) => {
|
||||
const transaction = response.data;
|
||||
transaction.time = new Date(transaction.time);
|
||||
fixTransaction(transaction);
|
||||
commit('addTransaction', transaction);
|
||||
commit(state.balances.has(data.user) ? 'changeBalance' : 'setBalance', {
|
||||
userid: data.user,
|
||||
|
@ -152,6 +180,7 @@ const actions: ActionTree<BalanceInterface, StateInterface> = {
|
|||
.catch(err => {
|
||||
console.debug(err);
|
||||
// Maybe Balance changed
|
||||
void dispatch('getTransactions', {});
|
||||
return dispatch('getBalance', data.sender ? data.sender : data.user);
|
||||
})
|
||||
.finally(() => commit('setLoading', false));
|
||||
|
|
|
@ -9,3 +9,8 @@ export function hasPermissions(needed: string[], store: Store<StateInterface>) {
|
|||
const permissions = store.state.user.currentPermissions;
|
||||
return needed.every(value => permissions.includes(value));
|
||||
}
|
||||
|
||||
export function hasSomePermissions(needed: string[], store: Store<StateInterface>) {
|
||||
const permissions = store.state.user.currentPermissions;
|
||||
return needed.some(value => permissions.includes(value));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue