Implement reconciliation #26

Merged
jacob1123 merged 13 commits from reconcilation into master 2022-02-27 22:16:02 +01:00
2 changed files with 45 additions and 0 deletions
Showing only changes of commit b4321395d9 - Show all commits

View File

@ -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
View 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))})
}