Implement backend for reconcilation
This commit is contained in:
parent
d7058a49b0
commit
b4321395d9
@ -36,6 +36,10 @@ type ErrorResponse struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
type SuccessResponse struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
// LoadRoutes initializes all the routes.
|
||||
func (h *Handler) LoadRoutes(router *gin.Engine) {
|
||||
router.Use(enableCachingForStaticFiles())
|
||||
@ -52,6 +56,7 @@ func (h *Handler) LoadRoutes(router *gin.Engine) {
|
||||
authenticated.Use(h.verifyLoginWithForbidden)
|
||||
authenticated.GET("/dashboard", h.dashboard)
|
||||
authenticated.GET("/account/:accountid/transactions", h.transactionsForAccount)
|
||||
authenticated.POST("/account/:accountid/reconcile", h.reconcileTransactions)
|
||||
authenticated.POST("/account/:accountid", h.editAccount)
|
||||
authenticated.GET("/admin/clear-database", h.clearDatabase)
|
||||
|
||||
|
40
server/reconcile.go
Normal file
40
server/reconcile.go
Normal file
@ -0,0 +1,40 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ReconcileTransactionsRequest struct {
|
||||
TransactionIDs []uuid.UUID
|
||||
}
|
||||
|
||||
func (h *Handler) reconcileTransactions(c *gin.Context) {
|
||||
var request ReconcileTransactionsRequest
|
||||
err := c.BindJSON(&request)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("parse request: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := h.Service.BeginTx(c.Request.Context(), &sql.TxOptions{})
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("update transaction: %w", err))
|
||||
return
|
||||
}
|
||||
db := h.Service.WithTx(tx)
|
||||
for _, transactionID := range request.TransactionIDs {
|
||||
err := db.SetTransactionReconciled(c.Request.Context(), transactionID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("update transaction: %w", err))
|
||||
return
|
||||
}
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
c.JSON(http.StatusOK, SuccessResponse{fmt.Sprintf("Set status for %d transactions", len(request.TransactionIDs))})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user