Update existing transaction if transactionid was passed

This commit is contained in:
Jan Bader 2022-02-25 22:05:15 +00:00
parent 2f4f8a7568
commit 07804e4241

View File

@ -41,36 +41,53 @@ func (h *Handler) newTransaction(c *gin.Context) {
return return
} }
newTransaction := postgres.CreateTransactionParams{ transactionID := c.Param("transactionid")
Memo: payload.Memo, if transactionID != "" {
Date: time.Time(payload.Date), editTransaction := postgres.UpdateTransactionParams{
Amount: amount, Memo: payload.Memo,
Status: postgres.TransactionStatus(payload.State), Date: time.Time(payload.Date),
} Amount: amount,
PayeeID: payload.Payee.ID,
CategoryID: payload.CategoryID,
ID: uuid.MustParse(transactionID),
}
if payload.Payee.IsAccount { err := h.Service.UpdateTransaction(c.Request.Context(), editTransaction)
err := h.CreateTransferForOtherAccount(newTransaction, amount, payload, err, c)
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transfer transaction: %w", err)) c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("edit transaction: %w", err))
return return
} }
} else { } else {
payeeID, err := GetPayeeID(c.Request.Context(), payload, h) newTransaction := postgres.CreateTransactionParams{
if err != nil { Memo: payload.Memo,
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create payee: %w", err)) Date: time.Time(payload.Date),
Amount: amount,
Status: postgres.TransactionStatus(payload.State),
} }
newTransaction.PayeeID = payeeID
}
newTransaction.CategoryID = payload.CategoryID if payload.Payee.IsAccount {
newTransaction.AccountID = payload.AccountID err := h.CreateTransferForOtherAccount(newTransaction, amount, payload, err, c)
transaction, err := h.Service.CreateTransaction(c.Request.Context(), newTransaction) if err != nil {
if err != nil { c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transfer transaction: %w", err))
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err)) return
return }
} } else {
payeeID, err := GetPayeeID(c.Request.Context(), payload, h)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create payee: %w", err))
}
newTransaction.PayeeID = payeeID
}
c.JSON(http.StatusOK, transaction) newTransaction.CategoryID = payload.CategoryID
newTransaction.AccountID = payload.AccountID
transaction, err := h.Service.CreateTransaction(c.Request.Context(), newTransaction)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
return
}
c.JSON(http.StatusOK, transaction)
}
} }
func (h *Handler) CreateTransferForOtherAccount(newTransaction postgres.CreateTransactionParams, amount numeric.Numeric, payload NewTransactionPayload, err error, c *gin.Context) error { func (h *Handler) CreateTransferForOtherAccount(newTransaction postgres.CreateTransactionParams, amount numeric.Numeric, payload NewTransactionPayload, err error, c *gin.Context) error {