Merge pull request 'Improve handling of transactions' (#12) from transaction-handling into master
Reviewed-on: #12
This commit is contained in:
commit
4019656e4d
@ -35,8 +35,6 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%v\n", payload)
|
|
||||||
|
|
||||||
amount := postgres.Numeric{}
|
amount := postgres.Numeric{}
|
||||||
err = amount.Set(payload.Amount)
|
err = amount.Set(payload.Amount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -44,17 +42,37 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
payeeID := payload.Payee.ID
|
||||||
|
if !payeeID.Valid && payload.Payee.Name != "" {
|
||||||
|
newPayee := postgres.CreatePayeeParams{
|
||||||
|
Name: payload.Payee.Name,
|
||||||
|
BudgetID: payload.BudgetID,
|
||||||
|
}
|
||||||
|
payee, err := h.Service.CreatePayee(c.Request.Context(), newPayee)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create payee: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
payeeID = uuid.NullUUID{
|
||||||
|
UUID: payee.ID,
|
||||||
|
Valid: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
newTransaction := postgres.CreateTransactionParams{
|
newTransaction := postgres.CreateTransactionParams{
|
||||||
Memo: payload.Memo,
|
Memo: payload.Memo,
|
||||||
Date: time.Time(payload.Date),
|
Date: time.Time(payload.Date),
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
AccountID: payload.AccountID,
|
AccountID: payload.AccountID,
|
||||||
PayeeID: payload.Payee.ID,
|
PayeeID: payeeID,
|
||||||
CategoryID: payload.Category.ID,
|
CategoryID: payload.Category.ID,
|
||||||
Status: postgres.TransactionStatus(payload.State),
|
Status: postgres.TransactionStatus(payload.State),
|
||||||
}
|
}
|
||||||
_, err = h.Service.CreateTransaction(c.Request.Context(), newTransaction)
|
transaction, err := h.Service.CreateTransaction(c.Request.Context(), newTransaction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, transaction)
|
||||||
}
|
}
|
||||||
|
61
web/src/components/TransactionInputRow.vue
Normal file
61
web/src/components/TransactionInputRow.vue
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import { computed, ref } from "vue";
|
||||||
|
import Autocomplete, { Suggestion } from '../components/Autocomplete.vue'
|
||||||
|
import { useAccountStore } from '../stores/budget-account'
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
budgetid: string
|
||||||
|
accountid: string
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const TransactionDate = ref(new Date().toISOString().substring(0, 10));
|
||||||
|
const Payee = ref<Suggestion | undefined>(undefined);
|
||||||
|
const Category = ref<Suggestion | undefined>(undefined);
|
||||||
|
const Memo = ref("");
|
||||||
|
const Amount = ref("0");
|
||||||
|
|
||||||
|
const payload = computed(() => JSON.stringify({
|
||||||
|
budget_id: props.budgetid,
|
||||||
|
account_id: props.accountid,
|
||||||
|
date: TransactionDate.value,
|
||||||
|
payee: Payee.value,
|
||||||
|
category: Category.value,
|
||||||
|
memo: Memo.value,
|
||||||
|
amount: Amount.value,
|
||||||
|
state: "Uncleared"
|
||||||
|
}));
|
||||||
|
|
||||||
|
const accountStore = useAccountStore();
|
||||||
|
function saveTransaction(e: MouseEvent) {
|
||||||
|
e.preventDefault();
|
||||||
|
accountStore.saveTransaction(payload.value);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<tr>
|
||||||
|
<td style="width: 90px;" class="text-sm">
|
||||||
|
<input class="border-b-2 border-black" type="date" v-model="TransactionDate" />
|
||||||
|
</td>
|
||||||
|
<td style="max-width: 150px;">
|
||||||
|
<Autocomplete v-model="Payee" type="payees" />
|
||||||
|
</td>
|
||||||
|
<td style="max-width: 200px;">
|
||||||
|
<Autocomplete v-model="Category" type="categories" />
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input class="block w-full border-b-2 border-black" type="text" v-model="Memo" />
|
||||||
|
</td>
|
||||||
|
<td style="width: 80px;" class="text-right">
|
||||||
|
<input
|
||||||
|
class="text-right block w-full border-b-2 border-black"
|
||||||
|
type="currency"
|
||||||
|
v-model="Amount"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
<td style="width: 20px;">
|
||||||
|
<input type="submit" @click="saveTransaction" value="Save" />
|
||||||
|
</td>
|
||||||
|
<td style="width: 20px;"></td>
|
||||||
|
</tr>
|
||||||
|
</template>
|
@ -1,9 +1,8 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { computed, ref } from "vue"
|
import { computed, ref } from "vue"
|
||||||
import Autocomplete, { Suggestion } from '../components/Autocomplete.vue'
|
|
||||||
import Currency from "../components/Currency.vue";
|
import Currency from "../components/Currency.vue";
|
||||||
import TransactionRow from "../components/TransactionRow.vue";
|
import TransactionRow from "../components/TransactionRow.vue";
|
||||||
import { POST } from "../api";
|
import TransactionInputRow from "../components/TransactionInputRow.vue";
|
||||||
import { useAccountStore } from "../stores/budget-account";
|
import { useAccountStore } from "../stores/budget-account";
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
@ -11,27 +10,6 @@ const props = defineProps<{
|
|||||||
accountid: string
|
accountid: string
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const TransactionDate = ref(new Date().toISOString().substring(0, 10));
|
|
||||||
const Payee = ref<Suggestion | undefined>(undefined);
|
|
||||||
const Category = ref<Suggestion | undefined>(undefined);
|
|
||||||
const Memo = ref("");
|
|
||||||
const Amount = ref(0);
|
|
||||||
|
|
||||||
function saveTransaction(e: MouseEvent) {
|
|
||||||
e.preventDefault();
|
|
||||||
POST("/transaction/new", JSON.stringify({
|
|
||||||
budget_id: props.budgetid,
|
|
||||||
account_id: props.accountid,
|
|
||||||
date: TransactionDate.value,
|
|
||||||
payee: Payee.value,
|
|
||||||
category: Category.value,
|
|
||||||
memo: Memo.value,
|
|
||||||
amount: Amount,
|
|
||||||
state: "Uncleared"
|
|
||||||
}))
|
|
||||||
.then(x => x.json());
|
|
||||||
}
|
|
||||||
|
|
||||||
const accountStore = useAccountStore();
|
const accountStore = useAccountStore();
|
||||||
const CurrentAccount = computed(() => accountStore.CurrentAccount);
|
const CurrentAccount = computed(() => accountStore.CurrentAccount);
|
||||||
const TransactionsList = computed(() => accountStore.TransactionsList);
|
const TransactionsList = computed(() => accountStore.TransactionsList);
|
||||||
@ -53,31 +31,7 @@ const TransactionsList = computed(() => accountStore.TransactionsList);
|
|||||||
<td style="width: 20px;"></td>
|
<td style="width: 20px;"></td>
|
||||||
<td style="width: 20px;"></td>
|
<td style="width: 20px;"></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<TransactionInputRow :budgetid="budgetid" :accountid="accountid" />
|
||||||
<td style="width: 90px;" class="text-sm">
|
|
||||||
<input class="border-b-2 border-black" type="date" v-model="TransactionDate" />
|
|
||||||
</td>
|
|
||||||
<td style="max-width: 150px;">
|
|
||||||
<Autocomplete v-model="Payee" type="payees" />
|
|
||||||
</td>
|
|
||||||
<td style="max-width: 200px;">
|
|
||||||
<Autocomplete v-model="Category" type="categories" />
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<input class="block w-full border-b-2 border-black" type="text" v-model="Memo" />
|
|
||||||
</td>
|
|
||||||
<td style="width: 80px;" class="text-right">
|
|
||||||
<input
|
|
||||||
class="text-right block w-full border-b-2 border-black"
|
|
||||||
type="currency"
|
|
||||||
v-model="Amount"
|
|
||||||
/>
|
|
||||||
</td>
|
|
||||||
<td style="width: 20px;">
|
|
||||||
<input type="submit" @click="saveTransaction" value="Save" />
|
|
||||||
</td>
|
|
||||||
<td style="width: 20px;"></td>
|
|
||||||
</tr>
|
|
||||||
<TransactionRow
|
<TransactionRow
|
||||||
v-for="(transaction, index) in TransactionsList"
|
v-for="(transaction, index) in TransactionsList"
|
||||||
:transaction="transaction"
|
:transaction="transaction"
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import { GET } from "../api";
|
import { GET, POST } from "../api";
|
||||||
import { useSessionStore } from "./session";
|
import { useSessionStore } from "./session";
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
@ -7,7 +7,7 @@ interface State {
|
|||||||
CurrentAccountID: string | null,
|
CurrentAccountID: string | null,
|
||||||
Categories: Map<string, Category>,
|
Categories: Map<string, Category>,
|
||||||
Months: Map<number, Map<number, Map<string, Category>>>,
|
Months: Map<number, Map<number, Map<string, Category>>>,
|
||||||
Transactions: [],
|
Transactions: any[],
|
||||||
Assignments: []
|
Assignments: []
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,6 +119,11 @@ export const useAccountStore = defineStore("budget/account", {
|
|||||||
logout() {
|
logout() {
|
||||||
this.$reset()
|
this.$reset()
|
||||||
},
|
},
|
||||||
|
async saveTransaction(payload: string) {
|
||||||
|
const result = await POST("/transaction/new", payload);
|
||||||
|
const response = await result.json();
|
||||||
|
this.Transactions.unshift(response);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user