Extract UpdateTransaction
This commit is contained in:
parent
07804e4241
commit
10ea73663f
@ -43,6 +43,42 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
|||||||
|
|
||||||
transactionID := c.Param("transactionid")
|
transactionID := c.Param("transactionid")
|
||||||
if transactionID != "" {
|
if transactionID != "" {
|
||||||
|
h.UpdateTransaction(payload, amount, transactionID, c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
newTransaction := postgres.CreateTransactionParams{
|
||||||
|
Memo: payload.Memo,
|
||||||
|
Date: time.Time(payload.Date),
|
||||||
|
Amount: amount,
|
||||||
|
Status: postgres.TransactionStatus(payload.State),
|
||||||
|
CategoryID: payload.CategoryID,
|
||||||
|
AccountID: payload.AccountID,
|
||||||
|
}
|
||||||
|
|
||||||
|
if payload.Payee.IsAccount {
|
||||||
|
err := h.CreateTransferForOtherAccount(newTransaction, amount, payload, c)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, 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
|
||||||
|
}
|
||||||
|
|
||||||
|
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) UpdateTransaction(payload NewTransactionPayload, amount numeric.Numeric, transactionID string, c *gin.Context) {
|
||||||
editTransaction := postgres.UpdateTransactionParams{
|
editTransaction := postgres.UpdateTransactionParams{
|
||||||
Memo: payload.Memo,
|
Memo: payload.Memo,
|
||||||
Date: time.Time(payload.Date),
|
Date: time.Time(payload.Date),
|
||||||
@ -55,48 +91,19 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
|||||||
err := h.Service.UpdateTransaction(c.Request.Context(), editTransaction)
|
err := h.Service.UpdateTransaction(c.Request.Context(), editTransaction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("edit transaction: %w", err))
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("edit transaction: %w", err))
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
newTransaction := postgres.CreateTransactionParams{
|
|
||||||
Memo: payload.Memo,
|
|
||||||
Date: time.Time(payload.Date),
|
|
||||||
Amount: amount,
|
|
||||||
Status: postgres.TransactionStatus(payload.State),
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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, c *gin.Context) error {
|
||||||
newTransaction.GroupID = uuid.NullUUID{UUID: uuid.New(), Valid: true}
|
newTransaction.GroupID = uuid.NullUUID{UUID: uuid.New(), Valid: true}
|
||||||
newTransaction.Amount = amount.Neg()
|
newTransaction.Amount = amount.Neg()
|
||||||
newTransaction.AccountID = payload.Payee.ID.UUID
|
newTransaction.AccountID = payload.Payee.ID.UUID
|
||||||
|
|
||||||
_, err = h.Service.CreateTransaction(c.Request.Context(), newTransaction)
|
// transfer does not need category. Either it's account is off-budget or no category was supplied.
|
||||||
return err
|
newTransaction.CategoryID = uuid.NullUUID{}
|
||||||
|
|
||||||
|
_, err := h.Service.CreateTransaction(c.Request.Context(), newTransaction)
|
||||||
|
return fmt.Errorf("create transfer transaction: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPayeeID(context context.Context, payload NewTransactionPayload, h *Handler) (uuid.NullUUID, error) {
|
func GetPayeeID(context context.Context, payload NewTransactionPayload, h *Handler) (uuid.NullUUID, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user