From 17e640892aef8b403e064a35dae3efe2b8708729 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 29 Jan 2021 02:29:27 +0100 Subject: [PATCH] Implemented load Transactions --- src/flaschengeist.d.ts | 3 +- .../balance/components/Transaction.vue | 4 ++- src/plugins/balance/store/balance.ts | 35 +++++++++++++++++-- src/utils/permission.ts | 5 +++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/flaschengeist.d.ts b/src/flaschengeist.d.ts index e00d65c..b89ac7f 100644 --- a/src/flaschengeist.d.ts +++ b/src/flaschengeist.d.ts @@ -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; diff --git a/src/plugins/balance/components/Transaction.vue b/src/plugins/balance/components/Transaction.vue index e64cd31..68a5e21 100644 --- a/src/plugins/balance/components/Transaction.vue +++ b/src/plugins/balance/components/Transaction.vue @@ -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( () => diff --git a/src/plugins/balance/store/balance.ts b/src/plugins/balance/store/balance.ts index e675b72..c74f81d 100644 --- a/src/plugins/balance/store/balance.ts +++ b/src/plugins/balance/store/balance.ts @@ -31,6 +31,10 @@ const state: BalanceInterface = { loading: 0 }; +function fixTransaction(t: FG.Transaction) { + t.time = new Date(t.time); +} + const mutations: MutationTree = { setBalance(state, data: { userid: string; balance: BalanceResponse }) { state.balances.set( @@ -59,6 +63,10 @@ const mutations: MutationTree = { 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 = { }) .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 = (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 = { revert({ dispatch, commit }, transaction: FG.Transaction) { return axios .delete(`/balance/${transaction.id}`) - .then((response: AxiosResponse) => { - commit('reverseTransaction', { transaction: transaction, reversal: response.data }); + .then(({ data }: AxiosResponse) => { + fixTransaction(data); + commit('reverseTransaction', { transaction: transaction, reversal: data }); dispatch('getBalance').catch(err => console.warn(err)); }); }, @@ -136,7 +164,7 @@ const actions: ActionTree = { .put(`/users/${data.user}/balance`, data) .then((response: AxiosResponse) => { 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 = { .catch(err => { console.debug(err); // Maybe Balance changed + void dispatch('getTransactions', {}); return dispatch('getBalance', data.sender ? data.sender : data.user); }) .finally(() => commit('setLoading', false)); diff --git a/src/utils/permission.ts b/src/utils/permission.ts index 763ca2a..aa5a2b8 100644 --- a/src/utils/permission.ts +++ b/src/utils/permission.ts @@ -9,3 +9,8 @@ export function hasPermissions(needed: string[], store: Store) { const permissions = store.state.user.currentPermissions; return needed.every(value => permissions.includes(value)); } + +export function hasSomePermissions(needed: string[], store: Store) { + const permissions = store.state.user.currentPermissions; + return needed.some(value => permissions.includes(value)); +}