Compare commits

...

5 Commits

4 changed files with 205 additions and 73 deletions

View File

@ -9,14 +9,17 @@ import (
type Transfer struct { type Transfer struct {
gorm.Model gorm.Model
FromCardID uint FromCardID uint
FromCard *Card FromCard *Card
ToCardID uint ToCardID uint
ToCard *Card ToCard *Card
Value uint64 Value uint64
Date time.Time HaveDifferentCurrencies bool
UserID uint FromValue uint64
User *User ToValue uint64
Date time.Time
UserID uint
User *User
} }
// Implements db.UserIdentifiable:1 // Implements db.UserIdentifiable:1
@ -36,34 +39,65 @@ func (t *Transfer) SetUserID(id uint) {
var ( var (
ERROR_TRANSFER_FROMCARD_INVALID_USERID = errors.New("Transfer's UserID and FromCard's UserID are not equal") ERROR_TRANSFER_FROMCARD_INVALID_USERID = errors.New("Transfer's UserID and FromCard's UserID are not equal")
ERROR_TRANSFER_FROMCARD_INSUFFICIENT_BALANCE = errors.New("FromCard's Balance is lower than Transfer's Value") ERROR_TRANSFER_FROMCARD_INSUFFICIENT_BALANCE = errors.New("FromCard's Balance is lower than Transfer's Value or FromValue")
ERROR_TRANSFER_TOCARD_INVALID_USERID = errors.New("Transfer's UserID and ToCard's UserID are not equal") ERROR_TRANSFER_TOCARD_INVALID_USERID = errors.New("Transfer's UserID and ToCard's UserID are not equal")
ERROR_TRANSFER_CURRENCY_INCOSISTENCE = errors.New("Transfer is using differenct currencies but FromValue or ToValue is not set approprietly")
) )
func (t *Transfer) validateTransfer(fromCard, toCard *Card) error {
if fromCard.UserID != t.UserID {
return ERROR_TRANSFER_FROMCARD_INVALID_USERID
}
if toCard.UserID != t.UserID {
return ERROR_TRANSFER_TOCARD_INVALID_USERID
}
diffCurs := fromCard.CurrencyID != toCard.CurrencyID
if diffCurs && (t.FromValue == 0 || t.ToValue == 0) {
return ERROR_TRANSFER_CURRENCY_INCOSISTENCE
}
if !diffCurs && fromCard.Balance < t.Value {
return ERROR_TRANSFER_FROMCARD_INSUFFICIENT_BALANCE
}
if diffCurs && fromCard.Balance < t.FromValue {
return ERROR_TRANSFER_FROMCARD_INSUFFICIENT_BALANCE
}
return nil
}
func (t *Transfer) BeforeCreate(tx *gorm.DB) error { func (t *Transfer) BeforeCreate(tx *gorm.DB) error {
fromCard := &Card{} fromCard := &Card{}
if err := tx.Find(fromCard, t.FromCardID).Error; err != nil { if err := tx.Find(fromCard, t.FromCardID).Error; err != nil {
return err return err
} }
if fromCard.UserID != t.UserID {
return ERROR_TRANSFER_FROMCARD_INVALID_USERID
}
if fromCard.Balance < t.Value {
return ERROR_TRANSFER_FROMCARD_INSUFFICIENT_BALANCE
}
fromCard.Balance -= t.Value
if err := tx.Save(fromCard).Error; err != nil {
return err
}
//
toCard := &Card{} toCard := &Card{}
if err := tx.Find(toCard, t.ToCardID).Error; err != nil { if err := tx.Find(toCard, t.ToCardID).Error; err != nil {
return err return err
} }
if toCard.UserID != t.UserID { if err := t.validateTransfer(fromCard, toCard); err != nil {
return ERROR_TRANSFER_TOCARD_INVALID_USERID return err
} }
toCard.Balance += t.Value
t.HaveDifferentCurrencies = fromCard.CurrencyID != toCard.CurrencyID
// on same CurrencyID fromCard and toCard should use Value
if !t.HaveDifferentCurrencies {
fromCard.Balance -= t.Value
if err := tx.Save(fromCard).Error; err != nil {
return err
}
//
toCard.Balance += t.Value
if err := tx.Save(toCard).Error; err != nil {
return err
}
return nil
}
// on DIFFERENT CurrencyID fromCard should use FromValue and toCard should use ToValue
fromCard.Balance -= t.FromValue
if err := tx.Save(fromCard).Error; err != nil {
return err
}
//
toCard.Balance += t.ToValue
if err := tx.Save(toCard).Error; err != nil { if err := tx.Save(toCard).Error; err != nil {
return err return err
} }
@ -71,39 +105,43 @@ func (t *Transfer) BeforeCreate(tx *gorm.DB) error {
} }
var ( var (
ERROR_TRANSFER_FROMCARD_IDZERO = errors.New("Transfer's FromCardID is zero") ERROR_TRANSFER_TOCARD_INSUFFICIENT_BALANCE = errors.New("Transfer's ToCard's Balance is lower than Value of ToValue of transfer")
ERROR_TRANSFER_TOCARD_IDZERO = errors.New("Transfer's ToCardID is zero")
ERROR_TRANSFER_TOCARD_INSUFFICIENT_BALANCE = errors.New("Transfer's ToCard's Balance is lower than value of transfer")
) )
func (t *Transfer) BeforeUpdate(tx *gorm.DB) error { func (t *Transfer) BeforeUpdate(tx *gorm.DB) error {
var original Transfer var original *Transfer
if err := tx.Find(&original, t.ID).Error; err != nil { if err := tx.Find(original, t.ID).Error; err != nil {
return err return err
} }
if original.FromCardID == 0 { var origFromCard *Card
return ERROR_TRANSFER_FROMCARD_IDZERO if err := tx.Find(origFromCard, original.FromCardID).Error; err != nil {
}
var origFromCard Card
if err := tx.Find(&origFromCard, original.FromCardID).Error; err != nil {
return err return err
} }
origFromCard.Balance += original.Value var origToCard *Card
if err := tx.Save(origFromCard).Error; err != nil { if err := tx.Find(origToCard, original.ToCardID).Error; err != nil {
return err return err
} }
if original.ToCardID == 0 { diffCurs := origFromCard.CurrencyID != origToCard.CurrencyID
return ERROR_TRANSFER_FROMCARD_IDZERO if !diffCurs {
origFromCard.Balance += original.Value
if origToCard.Balance < original.Value {
return ERROR_TRANSFER_TOCARD_INSUFFICIENT_BALANCE
}
origToCard.Balance -= original.Value
} else {
if original.FromValue == 0 || original.ToValue == 0 {
return ERROR_TRANSFER_CURRENCY_INCOSISTENCE
}
origFromCard.Balance += original.FromValue
if origToCard.Balance < original.ToValue {
return ERROR_TRANSFER_TOCARD_INSUFFICIENT_BALANCE
}
origToCard.Balance -= original.ToValue
} }
var origToCard Card if err := tx.Save(origFromCard).Error; err != nil {
if err := tx.Find(&origToCard, original.ToCardID).Error; err != nil {
return err return err
} }
if origToCard.Balance < original.Value {
return ERROR_TRANSFER_TOCARD_INSUFFICIENT_BALANCE
}
origToCard.Balance -= original.Value
if err := tx.Save(origToCard).Error; err != nil { if err := tx.Save(origToCard).Error; err != nil {
return err return err
} }
@ -112,25 +150,35 @@ func (t *Transfer) BeforeUpdate(tx *gorm.DB) error {
if err := tx.Find(fromCard, t.FromCardID).Error; err != nil { if err := tx.Find(fromCard, t.FromCardID).Error; err != nil {
return err return err
} }
if fromCard.UserID != t.UserID {
return ERROR_TRANSFER_FROMCARD_INVALID_USERID
}
if fromCard.Balance < t.Value {
return ERROR_TRANSFER_FROMCARD_INSUFFICIENT_BALANCE
}
fromCard.Balance -= t.Value
if err := tx.Save(fromCard).Error; err != nil {
return err
}
//
toCard := &Card{} toCard := &Card{}
if err := tx.Find(toCard, t.ToCardID).Error; err != nil { if err := tx.Find(toCard, t.ToCardID).Error; err != nil {
return err return err
} }
if toCard.UserID != t.UserID { if err := t.validateTransfer(fromCard, toCard); err != nil {
return ERROR_TRANSFER_TOCARD_INVALID_USERID return err
} }
toCard.Balance += t.Value
t.HaveDifferentCurrencies = fromCard.CurrencyID != toCard.CurrencyID
// on same CurrencyID fromCard and toCard should use Value
if !t.HaveDifferentCurrencies {
fromCard.Balance -= t.Value
if err := tx.Save(fromCard).Error; err != nil {
return err
}
//
toCard.Balance += t.Value
if err := tx.Save(toCard).Error; err != nil {
return err
}
return nil
}
// on DIFFERENT CurrencyID fromCard should use FromValue and toCard should use ToValue
fromCard.Balance -= t.FromValue
if err := tx.Save(fromCard).Error; err != nil {
return err
}
//
toCard.Balance += t.ToValue
if err := tx.Save(toCard).Error; err != nil { if err := tx.Save(toCard).Error; err != nil {
return err return err
} }
@ -142,18 +190,31 @@ func (t *Transfer) AfterDelete(tx *gorm.DB) error {
if err := tx.Find(fromCard, t.FromCardID).Error; err != nil { if err := tx.Find(fromCard, t.FromCardID).Error; err != nil {
return err return err
} }
fromCard.Balance += t.Value
if err := tx.Save(fromCard).Error; err != nil {
return err
}
toCard := &Card{} toCard := &Card{}
if err := tx.Find(toCard, t.ToCardID).Error; err != nil { if err := tx.Find(toCard, t.ToCardID).Error; err != nil {
return err return err
} }
if toCard.Balance < t.Value { diffCurs := fromCard.CurrencyID != toCard.CurrencyID
return ERROR_TRANSFER_TOCARD_INSUFFICIENT_BALANCE if !diffCurs {
fromCard.Balance += t.Value
if toCard.Balance < t.Value {
return ERROR_TRANSFER_TOCARD_INSUFFICIENT_BALANCE
}
toCard.Balance -= t.Value
return nil
} else {
if t.FromValue == 0 || t.ToValue == 0 {
return ERROR_TRANSFER_CURRENCY_INCOSISTENCE
}
fromCard.Balance += t.FromValue
if toCard.Balance < t.ToValue {
return ERROR_TRANSFER_TOCARD_INSUFFICIENT_BALANCE
}
toCard.Balance -= t.ToValue
}
if err := tx.Save(fromCard).Error; err != nil {
return err
} }
toCard.Balance -= t.Value
if err := tx.Save(toCard).Error; err != nil { if err := tx.Save(toCard).Error; err != nil {
return err return err
} }

View File

@ -1,12 +1,22 @@
package handlers package handlers
import ( import (
"fmt"
"strconv"
"git.qowevisa.me/Qowevisa/fin-check-api/db" "git.qowevisa.me/Qowevisa/fin-check-api/db"
"git.qowevisa.me/Qowevisa/fin-check-api/types" "git.qowevisa.me/Qowevisa/fin-check-api/types"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gorm.io/gorm"
) )
var cardTransform func(inp *db.Card) types.DbCard = func(inp *db.Card) types.DbCard { var cardTransform func(inp *db.Card) types.DbCard = func(inp *db.Card) types.DbCard {
var curr types.DbCurrency
if inp.Currency != nil {
curr = currencyTransform(inp.Currency)
} else {
curr = types.DbCurrency{}
}
return types.DbCard{ return types.DbCard{
ID: inp.ID, ID: inp.ID,
Name: inp.Name, Name: inp.Name,
@ -15,6 +25,8 @@ var cardTransform func(inp *db.Card) types.DbCard = func(inp *db.Card) types.DbC
CreditLine: inp.CreditLine, CreditLine: inp.CreditLine,
LastDigits: inp.LastDigits, LastDigits: inp.LastDigits,
CurrencyID: inp.CurrencyID, CurrencyID: inp.CurrencyID,
Currency: curr,
DisplayName: fmt.Sprintf("%s •%s", inp.Name, inp.LastDigits),
} }
} }
@ -51,9 +63,20 @@ func CardGetAll(c *gin.Context) {
c.JSON(500, types.ErrorResponse{Message: err.Error()}) c.JSON(500, types.ErrorResponse{Message: err.Error()})
return return
} }
preloadCurrencies := c.DefaultQuery("preload_currencies", "false")
shouldPreloadCurrencies := false
if val, err := strconv.ParseBool(preloadCurrencies); err == nil {
shouldPreloadCurrencies = val
}
dbc := db.Connect() dbc := db.Connect()
var entities []*db.Card var entities []*db.Card
if err := dbc.Find(&entities, db.Card{UserID: userID}).Error; err != nil { var tx *gorm.DB
if shouldPreloadCurrencies {
tx = dbc.Preload("Currency")
} else {
tx = dbc
}
if err := tx.Find(&entities, db.Card{UserID: userID}).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()}) c.JSON(500, types.ErrorResponse{Message: err.Error()})
return return
} }

View File

@ -1,18 +1,52 @@
package handlers package handlers
import ( import (
"fmt"
"git.qowevisa.me/Qowevisa/fin-check-api/db" "git.qowevisa.me/Qowevisa/fin-check-api/db"
"git.qowevisa.me/Qowevisa/fin-check-api/types" "git.qowevisa.me/Qowevisa/fin-check-api/types"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
var transferTransform func(inp *db.Transfer) types.DbTransfer = func(inp *db.Transfer) types.DbTransfer { var transferTransform func(inp *db.Transfer) types.DbTransfer = func(inp *db.Transfer) types.DbTransfer {
var fromCard types.DbCard
var toCard types.DbCard
if inp.FromCard != nil {
fromCard = cardTransform(inp.FromCard)
}
if inp.ToCard != nil {
toCard = cardTransform(inp.ToCard)
}
haveDiffCurrs := false
if inp.FromCard != nil && inp.FromCard.Currency != nil && inp.ToCard != nil && inp.ToCard.Currency != nil {
haveDiffCurrs = inp.FromCard.CurrencyID != inp.ToCard.CurrencyID
}
var showValue string
if haveDiffCurrs {
showValue = fmt.Sprintf("%d.%02d%s -> %d.%02d%s",
inp.FromValue/100,
inp.FromValue%100,
inp.FromCard.Currency.Symbol,
inp.ToValue/100,
inp.ToValue%100,
inp.ToCard.Currency.Symbol,
)
} else {
showValue = fmt.Sprintf("%d.%02d", inp.Value/100, inp.Value%100)
}
return types.DbTransfer{ return types.DbTransfer{
ID: inp.ID, ID: inp.ID,
FromCardID: inp.FromCardID, FromCardID: inp.FromCardID,
ToCardID: inp.ToCardID, ToCardID: inp.ToCardID,
Value: inp.Value, Value: inp.Value,
FromValue: inp.FromValue,
ToValue: inp.ToValue,
Date: inp.Date, Date: inp.Date,
//
ShowValue: showValue,
HaveDifferentCurrencies: haveDiffCurrs,
FromCard: fromCard,
ToCard: toCard,
} }
} }
@ -52,7 +86,7 @@ func TransferGetAll(c *gin.Context) {
} }
dbc := db.Connect() dbc := db.Connect()
var entities []*db.Transfer var entities []*db.Transfer
if err := dbc.Find(&entities, db.Transfer{UserID: userID}).Error; err != nil { if err := dbc.Preload("FromCard.Currency").Preload("ToCard.Currency").Find(&entities, db.Transfer{UserID: userID}).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()}) c.JSON(500, types.ErrorResponse{Message: err.Error()})
return return
} }
@ -82,6 +116,8 @@ func TransferAdd(c *gin.Context) {
dst.ToCardID = src.ToCardID dst.ToCardID = src.ToCardID
dst.Value = src.Value dst.Value = src.Value
dst.Date = src.Date dst.Date = src.Date
dst.FromValue = src.FromValue
dst.ToValue = src.ToValue
})(c) })(c)
} }
@ -107,6 +143,8 @@ func TransferPutId(c *gin.Context) {
dst.ToCardID = src.ToCardID dst.ToCardID = src.ToCardID
dst.Value = src.Value dst.Value = src.Value
dst.Date = src.Date dst.Date = src.Date
dst.FromValue = src.FromValue
dst.ToValue = src.ToValue
}, },
transferTransform, transferTransform,
)(c) )(c)

View File

@ -24,13 +24,16 @@ type ErrorResponse struct {
} }
type DbCard struct { type DbCard struct {
ID uint `json:"id" example:"1"` ID uint `json:"id" example:"1"`
Name string `json:"name" example:"CreditCard"` Name string `json:"name" example:"CreditCard"`
Balance uint64 `json:"balance" example:"1000"` Balance uint64 `json:"balance" example:"1000"`
HaveCreditLine bool `json:"have_credit_line" example:"true"` HaveCreditLine bool `json:"have_credit_line" example:"true"`
CreditLine uint64 `json:"credit_line" example:"500000"` CreditLine uint64 `json:"credit_line" example:"500000"`
LastDigits string `json:"last_digits" example:"1111"` LastDigits string `json:"last_digits" example:"1111"`
CurrencyID uint `json:"currency_id" example:"1"` CurrencyID uint `json:"currency_id" example:"1"`
Currency DbCurrency `json:"currency"`
// Purely UI things
DisplayName string `json:"display_name" example:"CreditCard •4444"`
} }
type DbCategory struct { type DbCategory struct {
@ -86,7 +89,14 @@ type DbTransfer struct {
FromCardID uint `json:"from_card_id" example:"1"` FromCardID uint `json:"from_card_id" example:"1"`
ToCardID uint `json:"to_card_id" example:"1"` ToCardID uint `json:"to_card_id" example:"1"`
Value uint64 `json:"value" example:"20000"` Value uint64 `json:"value" example:"20000"`
FromValue uint64 `json:"from_value" example:"20000"`
ToValue uint64 `json:"to_value" example:"20000"`
Date time.Time `json:"date" example:"29/11/2001 12:00"` Date time.Time `json:"date" example:"29/11/2001 12:00"`
// Purely UI things
ShowValue string `json:"show_value" example:"10.35$"`
HaveDifferentCurrencies bool `json:"have_diff_currs" example:"false"`
FromCard DbCard `json:"from_card"`
ToCard DbCard `json:"to_card"`
} }
type DbItem struct { type DbItem struct {