[balance] add limits

set limits for all users
get limits for all users
This commit is contained in:
Tim Gröger 2021-04-17 22:08:43 +02:00
parent a83ba72cfa
commit dc58fa8e1d
2 changed files with 60 additions and 10 deletions

View File

@ -1,11 +1,22 @@
<template> <template>
<div> <div>
<q-page padding> <q-page padding>
<q-card> <q-table :rows="rows" row-key="userid" :columns="columns">
<q-card-section> <template #top-right>
<q-table :rows="rows" row-key="userid" :columns="columns" /> <div class="full-width row q-gutter-sm">
</q-card-section> <q-input
</q-card> v-model.number="limit"
label="Limit"
type="number"
step="0.01"
suffix="€"
filled
dense
/>
<q-btn label="Limits Setzen" color="primary" dense @click="setLimits(limit)" />
</div>
</template>
</q-table>
</q-page> </q-page>
</div> </div>
</template> </template>
@ -13,17 +24,24 @@
<script lang="ts"> <script lang="ts">
// TODO: Fill usefull data // TODO: Fill usefull data
import { ref, defineComponent, onMounted } from 'vue'; import { ref, defineComponent, computed, onBeforeMount } from 'vue';
import { useBalanceStore } from '../store'; import { useBalanceStore } from '../store';
import { useUserStore } from 'src/plugins/user/store';
export default defineComponent({ export default defineComponent({
// name: 'PageName' // name: 'PageName'
setup() { setup() {
const store = useBalanceStore(); const store = useBalanceStore();
const userStore = useUserStore();
onMounted(() => void store.getBalances().then((balances) => rows.value.push(...balances))); onBeforeMount(() => {
void store.getBalances();
void userStore.getUsers();
void store.getLimits();
});
const rows = ref(store.balances); const rows = computed(() => store.balances);
const limit = ref<number>();
const columns = [ const columns = [
{ {
@ -33,8 +51,9 @@ export default defineComponent({
required: true, required: true,
align: 'left', align: 'left',
sortable: true, sortable: true,
format: (val: string) => userStore.users.find((a) => a.userid === val)?.display_name,
}, },
{ /*{
name: 'credit', name: 'credit',
label: 'Haben', label: 'Haben',
field: 'credit', field: 'credit',
@ -45,6 +64,12 @@ export default defineComponent({
label: 'Soll', label: 'Soll',
field: 'debit', field: 'debit',
format: (val: number) => val.toFixed(2), format: (val: number) => val.toFixed(2),
},*/
{
name: 'limit',
label: 'Limit',
format: (_: undefined, row: { userid: string }) =>
store.user_limits.find((a) => a.userid === row.userid)?.limit?.toFixed(2),
}, },
{ {
name: 'balance', name: 'balance',
@ -53,7 +78,8 @@ export default defineComponent({
(row.credit - row.debit).toFixed(2), (row.credit - row.debit).toFixed(2),
}, },
]; ];
return { rows, columns };
return { rows, columns, limit, setLimits: store.setLimits };
}, },
}); });
</script> </script>

View File

@ -16,10 +16,16 @@ export interface TransactionsResponse {
count?: number; count?: number;
} }
export interface UserLimit {
userid: string;
limit?: number;
}
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { useMainStore } from 'src/stores'; import { useMainStore } from 'src/stores';
import { AxiosResponse } from 'axios'; import { AxiosResponse } from 'axios';
import { Notify } from 'quasar'; import { Notify } from 'quasar';
import { useUserStore } from 'src/plugins/user/store';
function fixTransaction(t: FG.Transaction) { function fixTransaction(t: FG.Transaction) {
t.time = new Date(t.time); t.time = new Date(t.time);
@ -33,6 +39,7 @@ export const useBalanceStore = defineStore({
shortcuts: [] as number[], shortcuts: [] as number[],
transactions: [] as FG.Transaction[], transactions: [] as FG.Transaction[],
_balances_dirty: 0, _balances_dirty: 0,
user_limits: [] as Array<UserLimit>,
}), }),
getters: { getters: {
@ -163,5 +170,22 @@ export const useBalanceStore = defineStore({
} }
this._balances_dirty = 0; this._balances_dirty = 0;
}, },
async getLimits() {
const { data } = await api.get<Array<UserLimit>>('users/balance/limit');
this.user_limits = data;
},
async setLimits(limit: number) {
await api.put('users/balance/limit', { limit });
useUserStore().users.forEach((user) => {
const user_limit = this.user_limits.find((a) => a.userid === user.userid);
if (user_limit) {
user_limit.limit = limit;
} else {
this.user_limits.push({ userid: user.userid, limit });
}
});
},
}, },
}); });