102 lines
2.9 KiB
Go
102 lines
2.9 KiB
Go
package server
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.javil.eu/jacob1123/budgeteer/postgres"
|
|
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type ReconcileTransactionsRequest struct {
|
|
TransactionIDs []uuid.UUID `json:"transactionIds"`
|
|
ReconcilationTransactionAmount string `json:"reconciliationTransactionAmount"`
|
|
}
|
|
|
|
type ReconcileTransactionsResponse struct {
|
|
Message string
|
|
ReconciliationTransaction *postgres.DisplayTransaction
|
|
}
|
|
|
|
func (h *Handler) reconcileTransactions(c echo.Context) error {
|
|
accountID := c.Param("accountid")
|
|
accountUUID, err := uuid.Parse(accountID)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err)
|
|
}
|
|
|
|
var request ReconcileTransactionsRequest
|
|
err = c.Bind(&request)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("parse request: %w", err))
|
|
}
|
|
|
|
var amount numeric.Numeric
|
|
err = amount.Set(request.ReconcilationTransactionAmount)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("parse request: %w", err))
|
|
}
|
|
|
|
tx, err := h.Service.BeginTx(c.Request().Context(), &sql.TxOptions{})
|
|
if err != nil {
|
|
return fmt.Errorf("begin tx: %w", err)
|
|
}
|
|
|
|
db := h.Service.WithTx(tx)
|
|
for _, transactionID := range request.TransactionIDs {
|
|
err := db.SetTransactionReconciled(c.Request().Context(), transactionID)
|
|
if err != nil {
|
|
return fmt.Errorf("update transaction: %w", err)
|
|
}
|
|
}
|
|
|
|
reconciliationTransaction, err := h.CreateReconcilationTransaction(amount, accountUUID, db, c)
|
|
if err != nil {
|
|
return fmt.Errorf("insert new transaction: %w", err)
|
|
}
|
|
|
|
err = h.Service.SetLastReconciled(c.Request().Context(), accountUUID)
|
|
if err != nil {
|
|
return fmt.Errorf("set last reconciled: %w", err)
|
|
}
|
|
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
return fmt.Errorf("commit: %w", err)
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, ReconcileTransactionsResponse{
|
|
Message: fmt.Sprintf("Set status for %d transactions", len(request.TransactionIDs)),
|
|
ReconciliationTransaction: reconciliationTransaction,
|
|
})
|
|
}
|
|
|
|
func (*Handler) CreateReconcilationTransaction(amount numeric.Numeric, accountUUID uuid.UUID, db *postgres.Queries, c echo.Context) (*postgres.DisplayTransaction, error) {
|
|
if amount.IsZero() {
|
|
return nil, nil //nolint: nilnil
|
|
}
|
|
|
|
createTransaction := postgres.CreateTransactionParams{
|
|
Date: time.Now(),
|
|
Memo: "Reconciliation Transaction",
|
|
Amount: amount,
|
|
AccountID: accountUUID,
|
|
Status: "Reconciled",
|
|
}
|
|
transactionUUID, 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(), transactionUUID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get created transaction: %w", err)
|
|
}
|
|
|
|
return &transaction, nil
|
|
}
|