Extract method CreateReconcilationTransaction
This commit is contained in:
parent
27dd6e923c
commit
2775578713
@ -42,6 +42,7 @@ func (h *Handler) reconcileTransactions(c *gin.Context) {
|
|||||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("begin tx: %w", err))
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("begin tx: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
db := h.Service.WithTx(tx)
|
db := h.Service.WithTx(tx)
|
||||||
for _, transactionID := range request.TransactionIDs {
|
for _, transactionID := range request.TransactionIDs {
|
||||||
err := db.SetTransactionReconciled(c.Request.Context(), transactionID)
|
err := db.SetTransactionReconciled(c.Request.Context(), transactionID)
|
||||||
@ -50,38 +51,46 @@ func (h *Handler) reconcileTransactions(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = tx.Commit()
|
|
||||||
if err != nil {
|
|
||||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("commit: %w", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var reconciliationTransaction *postgres.DisplayTransaction
|
reconciliationTransaction, err := h.CreateReconcilationTransaction(request, accountUUID, db, c)
|
||||||
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 {
|
if err != nil {
|
||||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("insert new transaction: %w", err))
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("insert new transaction: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
transaction, err := h.Service.GetTransaction(c.Request.Context(), newTransaction.ID)
|
err = tx.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("get created transaction: %w", err))
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("commit: %w", err))
|
||||||
return
|
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)),
|
||||||
ReconciliationTransaction: reconciliationTransaction,
|
ReconciliationTransaction: reconciliationTransaction,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*Handler) CreateReconcilationTransaction(request ReconcileTransactionsRequest, accountUUID uuid.UUID, db *postgres.Queries, c *gin.Context) (*postgres.DisplayTransaction, error) {
|
||||||
|
if request.ReconcilationTransactionAmount.IsZero() {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
createTransaction := postgres.CreateTransactionParams{
|
||||||
|
Date: time.Now(),
|
||||||
|
Memo: "Reconciliation Transaction",
|
||||||
|
Amount: request.ReconcilationTransactionAmount,
|
||||||
|
AccountID: accountUUID,
|
||||||
|
Status: "Reconciled",
|
||||||
|
}
|
||||||
|
newTransaction, err := db.CreateTransaction(c.Request.Context(), createTransaction)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("insert new transaction: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction, err := db.GetTransaction(c.Request.Context(), newTransaction.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("get created transaction: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &transaction, nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user