[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>
<div>
<q-page padding>
<q-card>
<q-card-section>
<q-table :rows="rows" row-key="userid" :columns="columns" />
</q-card-section>
</q-card>
<q-table :rows="rows" row-key="userid" :columns="columns">
<template #top-right>
<div class="full-width row q-gutter-sm">
<q-input
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>
</div>
</template>
@ -13,17 +24,24 @@
<script lang="ts">
// TODO: Fill usefull data
import { ref, defineComponent, onMounted } from 'vue';
import { ref, defineComponent, computed, onBeforeMount } from 'vue';
import { useBalanceStore } from '../store';
import { useUserStore } from 'src/plugins/user/store';
export default defineComponent({
// name: 'PageName'
setup() {
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 = [
{
@ -33,8 +51,9 @@ export default defineComponent({
required: true,
align: 'left',
sortable: true,
format: (val: string) => userStore.users.find((a) => a.userid === val)?.display_name,
},
{
/*{
name: 'credit',
label: 'Haben',
field: 'credit',
@ -45,6 +64,12 @@ export default defineComponent({
label: 'Soll',
field: 'debit',
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',
@ -53,7 +78,8 @@ export default defineComponent({
(row.credit - row.debit).toFixed(2),
},
];
return { rows, columns };
return { rows, columns, limit, setLimits: store.setLimits };
},
});
</script>

View File

@ -16,10 +16,16 @@ export interface TransactionsResponse {
count?: number;
}
export interface UserLimit {
userid: string;
limit?: number;
}
import { defineStore } from 'pinia';
import { useMainStore } from 'src/stores';
import { AxiosResponse } from 'axios';
import { Notify } from 'quasar';
import { useUserStore } from 'src/plugins/user/store';
function fixTransaction(t: FG.Transaction) {
t.time = new Date(t.time);
@ -33,6 +39,7 @@ export const useBalanceStore = defineStore({
shortcuts: [] as number[],
transactions: [] as FG.Transaction[],
_balances_dirty: 0,
user_limits: [] as Array<UserLimit>,
}),
getters: {
@ -163,5 +170,22 @@ export const useBalanceStore = defineStore({
}
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 });
}
});
},
},
});