Implement reconciliation #26
@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||||
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
|
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
|
||||||
@ -13,17 +14,24 @@ import (
|
|||||||
|
|
||||||
type ReconcileTransactionsRequest struct {
|
type ReconcileTransactionsRequest struct {
|
||||||
TransactionIDs []uuid.UUID `json:"transactionIds"`
|
TransactionIDs []uuid.UUID `json:"transactionIds"`
|
||||||
ReconcilationTransactionAmount numeric.Numeric `json:"reconcilationTransactionAmount"`
|
ReconcilationTransactionAmount numeric.Numeric `json:"reconciliationTransactionAmount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReconcileTransactionsResponse struct {
|
type ReconcileTransactionsResponse struct {
|
||||||
Message string
|
Message string
|
||||||
ReconcilationTransaction []postgres.GetTransactionsForAccountRow
|
ReconciliationTransaction *postgres.DisplayTransaction
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) reconcileTransactions(c *gin.Context) {
|
func (h *Handler) reconcileTransactions(c *gin.Context) {
|
||||||
|
accountID := c.Param("accountid")
|
||||||
|
accountUUID, err := uuid.Parse(accountID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var request ReconcileTransactionsRequest
|
var request ReconcileTransactionsRequest
|
||||||
err := c.BindJSON(&request)
|
err = c.BindJSON(&request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("parse request: %w", err))
|
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("parse request: %w", err))
|
||||||
return
|
return
|
||||||
@ -48,8 +56,32 @@ func (h *Handler) reconcileTransactions(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var reconciliationTransaction *postgres.DisplayTransaction
|
||||||
|
if !request.ReconcilationTransactionAmount.IsZero() {
|
||||||
|
createTransaction := postgres.CreateTransactionParams{
|
||||||
|
Date: time.Now(),
|
||||||
|
Memo: "Reconciliation Transaction",
|
||||||
|
Amount: request.ReconcilationTransactionAmount,
|
||||||
|
AccountID: accountUUID,
|
||||||
|
Status: "Reconciled",
|
||||||
|
}
|
||||||
|
newTransaction, err := h.Service.CreateTransaction(c.Request.Context(), createTransaction)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("insert new transaction: %w", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction, err := h.Service.GetTransaction(c.Request.Context(), newTransaction.ID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("get created transaction: %w", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
reconciliationTransaction = &transaction
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, ReconcileTransactionsResponse{
|
c.JSON(http.StatusOK, ReconcileTransactionsResponse{
|
||||||
Message: fmt.Sprintf("Set status for %d transactions", len(request.TransactionIDs)),
|
Message: fmt.Sprintf("Set status for %d transactions", len(request.TransactionIDs)),
|
||||||
ReconcilationTransaction: nil,
|
ReconciliationTransaction: reconciliationTransaction,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -177,7 +177,7 @@ export const useAccountStore = defineStore("budget/account", {
|
|||||||
transaction.Reconciled = value;
|
transaction.Reconciled = value;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async SubmitReconcilation(reconcilationTransactionAmount : number) {
|
async SubmitReconcilation(reconciliationTransactionAmount : number) {
|
||||||
const account = this.CurrentAccount!;
|
const account = this.CurrentAccount!;
|
||||||
const reconciledTransactions = this.TransactionsList.filter(x => x.Reconciled);
|
const reconciledTransactions = this.TransactionsList.filter(x => x.Reconciled);
|
||||||
for (const transaction of reconciledTransactions) {
|
for (const transaction of reconciledTransactions) {
|
||||||
@ -187,7 +187,7 @@ export const useAccountStore = defineStore("budget/account", {
|
|||||||
}
|
}
|
||||||
const result = await POST("/account/" + this.CurrentAccountID + "/reconcile", JSON.stringify({
|
const result = await POST("/account/" + this.CurrentAccountID + "/reconcile", JSON.stringify({
|
||||||
transactionIDs: reconciledTransactions.map(x => x.ID),
|
transactionIDs: reconciledTransactions.map(x => x.ID),
|
||||||
reconcilationTransactionAmount: reconcilationTransactionAmount,
|
reconciliationTransactionAmount: reconciliationTransactionAmount,
|
||||||
}));
|
}));
|
||||||
const response = await result.json();
|
const response = await result.json();
|
||||||
console.log("Reconcile: " + response.message);
|
console.log("Reconcile: " + response.message);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user