Improvements
This commit is contained in:
parent
22ec0433bf
commit
2423bdd3ee
@ -209,10 +209,38 @@ func (ynab *YNABImport) ImportTransactions(context context.Context, r io.Reader)
|
|||||||
|
|
||||||
func (ynab *YNABImport) ImportTransaction(payeeName string, context context.Context, transaction CreateTransactionParams, accountName string, openTransfers []Transfer, account *Account, amount Numeric) ([]Transfer, bool, error) {
|
func (ynab *YNABImport) ImportTransaction(payeeName string, context context.Context, transaction CreateTransactionParams, accountName string, openTransfers []Transfer, account *Account, amount Numeric) ([]Transfer, bool, error) {
|
||||||
if strings.HasPrefix(payeeName, "Transfer : ") {
|
if strings.HasPrefix(payeeName, "Transfer : ") {
|
||||||
|
openTransfers, shouldReturn, returnValue, returnValue1, returnValue2 := ynab.ImportTransferTransaction(payeeName, context, transaction, accountName, openTransfers, account, amount)
|
||||||
|
if shouldReturn {
|
||||||
|
return returnValue, returnValue1, returnValue2
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
shouldReturn, returnValue, returnValue1, returnValue2 := ynab.ImportRegularTransaction(context, payeeName, transaction)
|
||||||
|
if shouldReturn {
|
||||||
|
return returnValue, returnValue1, returnValue2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return openTransfers, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ynab *YNABImport) ImportRegularTransaction(context context.Context, payeeName string, transaction CreateTransactionParams) (bool, []Transfer, bool, error) {
|
||||||
|
payeeID, err := ynab.GetPayee(context, payeeName)
|
||||||
|
if err != nil {
|
||||||
|
return true, nil, true, fmt.Errorf("get payee %s: %w", payeeName, err)
|
||||||
|
}
|
||||||
|
transaction.PayeeID = payeeID
|
||||||
|
|
||||||
|
_, err = ynab.queries.CreateTransaction(context, transaction)
|
||||||
|
if err != nil {
|
||||||
|
return true, nil, true, fmt.Errorf("save transaction %v: %w", transaction, err)
|
||||||
|
}
|
||||||
|
return false, nil, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ynab *YNABImport) ImportTransferTransaction(payeeName string, context context.Context, transaction CreateTransactionParams, accountName string, openTransfers []Transfer, account *Account, amount Numeric) ([]Transfer, bool, []Transfer, bool, error) {
|
||||||
transferToAccountName := payeeName[11:]
|
transferToAccountName := payeeName[11:]
|
||||||
transferToAccount, err := ynab.GetAccount(context, transferToAccountName)
|
transferToAccount, err := ynab.GetAccount(context, transferToAccountName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, true, fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
return nil, true, nil, true, fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
transfer := Transfer{
|
transfer := Transfer{
|
||||||
@ -245,11 +273,11 @@ func (ynab *YNABImport) ImportTransaction(payeeName string, context context.Cont
|
|||||||
|
|
||||||
_, err = ynab.queries.CreateTransaction(context, transfer.CreateTransactionParams)
|
_, err = ynab.queries.CreateTransaction(context, transfer.CreateTransactionParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, true, fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err)
|
return nil, true, nil, true, fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err)
|
||||||
}
|
}
|
||||||
_, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams)
|
_, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, true, fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
return nil, true, nil, true, fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -257,19 +285,7 @@ func (ynab *YNABImport) ImportTransaction(payeeName string, context context.Cont
|
|||||||
if !found {
|
if !found {
|
||||||
openTransfers = append(openTransfers, transfer)
|
openTransfers = append(openTransfers, transfer)
|
||||||
}
|
}
|
||||||
} else {
|
return openTransfers, false, nil, false, nil
|
||||||
payeeID, err := ynab.GetPayee(context, payeeName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, true, fmt.Errorf("get payee %s: %w", payeeName, err)
|
|
||||||
}
|
|
||||||
transaction.PayeeID = payeeID
|
|
||||||
|
|
||||||
_, err = ynab.queries.CreateTransaction(context, transaction)
|
|
||||||
if err != nil {
|
|
||||||
return nil, true, fmt.Errorf("save transaction %v: %w", transaction, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return openTransfers, false, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func trimLastChar(s string) string {
|
func trimLastChar(s string) string {
|
||||||
|
@ -3,7 +3,6 @@ package server
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.javil.eu/jacob1123/budgeteer"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,7 +22,7 @@ func (h *Handler) newBudget(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userID := c.MustGet("token").(budgeteer.Token).GetID()
|
userID := MustGetToken(c).GetID()
|
||||||
budget, err := h.Service.NewBudget(c.Request.Context(), newBudget.Name, userID)
|
budget, err := h.Service.NewBudget(c.Request.Context(), newBudget.Name, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(http.StatusInternalServerError, err)
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
|
@ -3,13 +3,12 @@ package server
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.javil.eu/jacob1123/budgeteer"
|
|
||||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handler) dashboard(c *gin.Context) {
|
func (h *Handler) dashboard(c *gin.Context) {
|
||||||
userID := c.MustGet("token").(budgeteer.Token).GetID()
|
userID := MustGetToken(c).GetID()
|
||||||
budgets, err := h.Service.GetBudgetsForUser(c.Request.Context(), userID)
|
budgets, err := h.Service.GetBudgetsForUser(c.Request.Context(), userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -14,8 +14,18 @@ import (
|
|||||||
const (
|
const (
|
||||||
HeaderName = "Authorization"
|
HeaderName = "Authorization"
|
||||||
Bearer = "Bearer "
|
Bearer = "Bearer "
|
||||||
|
ParamName = "token"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func MustGetToken(c *gin.Context) budgeteer.Token { //nolint:ireturn
|
||||||
|
token := c.MustGet(ParamName)
|
||||||
|
if token, ok := token.(budgeteer.Token); !ok {
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
panic("Token is not a valid Token")
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, *ErrorResponse) { //nolint:ireturn
|
func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, *ErrorResponse) { //nolint:ireturn
|
||||||
tokenString := c.GetHeader(HeaderName)
|
tokenString := c.GetHeader(HeaderName)
|
||||||
if len(tokenString) <= len(Bearer) {
|
if len(tokenString) <= len(Bearer) {
|
||||||
@ -39,7 +49,7 @@ func (h *Handler) verifyLoginWithForbidden(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Set("token", token)
|
c.Set(ParamName, token)
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +61,7 @@ func (h *Handler) verifyLoginWithRedirect(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Set("token", token)
|
c.Set(ParamName, token)
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user