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
}
newTransaction := postgres.CreateTransactionParams{
Memo: payload.Memo,
Date: time.Time(payload.Date),
Amount: amount,
Status: postgres.TransactionStatus(payload.State),
}
transactionID := c.Param("transactionid")
if transactionID != "" {
editTransaction := postgres.UpdateTransactionParams{
Memo: payload.Memo,
Date: time.Time(payload.Date),
Amount: amount,
PayeeID: payload.Payee.ID,
CategoryID: payload.CategoryID,
ID: uuid.MustParse(transactionID),
}
if payload.Payee.IsAccount {
err := h.CreateTransferForOtherAccount(newTransaction, amount, payload, err, c)
err := h.Service.UpdateTransaction(c.Request.Context(), editTransaction)
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
}
} else {
payeeID, err := GetPayeeID(c.Request.Context(), payload, h)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create payee: %w", err))
newTransaction := postgres.CreateTransactionParams{
Memo: payload.Memo,
Date: time.Time(payload.Date),
Amount: amount,
Status: postgres.TransactionStatus(payload.State),
}
newTransaction.PayeeID = payeeID
}
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
}
if payload.Payee.IsAccount {
err := h.CreateTransferForOtherAccount(newTransaction, amount, payload, err, c)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transfer transaction: %w", err))
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 {