Compare commits
No commits in common. "957f1e83637b6192dd2c6a519cdd989a5c95ca7f" and "e1a46f5fc7895cd6403e49d0cb88f2190a19f782" have entirely different histories.
957f1e8363
...
e1a46f5fc7
153
db/transfer.go
153
db/transfer.go
|
@ -14,9 +14,6 @@ type Transfer struct {
|
|||
ToCardID uint
|
||||
ToCard *Card
|
||||
Value uint64
|
||||
HaveDifferentCurrencies bool
|
||||
FromValue uint64
|
||||
ToValue uint64
|
||||
Date time.Time
|
||||
UserID uint
|
||||
User *User
|
||||
|
@ -39,109 +36,74 @@ func (t *Transfer) SetUserID(id uint) {
|
|||
|
||||
var (
|
||||
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 or FromValue")
|
||||
ERROR_TRANSFER_FROMCARD_INSUFFICIENT_BALANCE = errors.New("FromCard's Balance is lower than Transfer's Value")
|
||||
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 {
|
||||
fromCard := &Card{}
|
||||
if err := tx.Find(fromCard, t.FromCardID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
toCard := &Card{}
|
||||
if err := tx.Find(toCard, t.ToCardID).Error; err != nil {
|
||||
return err
|
||||
if fromCard.UserID != t.UserID {
|
||||
return ERROR_TRANSFER_FROMCARD_INVALID_USERID
|
||||
}
|
||||
if err := t.validateTransfer(fromCard, toCard); err != nil {
|
||||
return err
|
||||
if fromCard.Balance < t.Value {
|
||||
return ERROR_TRANSFER_FROMCARD_INSUFFICIENT_BALANCE
|
||||
}
|
||||
|
||||
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 := &Card{}
|
||||
if err := tx.Find(toCard, t.ToCardID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if toCard.UserID != t.UserID {
|
||||
return ERROR_TRANSFER_TOCARD_INVALID_USERID
|
||||
}
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
ERROR_TRANSFER_TOCARD_INSUFFICIENT_BALANCE = errors.New("Transfer's ToCard's Balance is lower than Value of ToValue of transfer")
|
||||
ERROR_TRANSFER_FROMCARD_IDZERO = errors.New("Transfer's FromCardID is zero")
|
||||
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 {
|
||||
var original *Transfer
|
||||
if err := tx.Find(original, t.ID).Error; err != nil {
|
||||
var original Transfer
|
||||
if err := tx.Find(&original, t.ID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
var origFromCard *Card
|
||||
if err := tx.Find(origFromCard, original.FromCardID).Error; err != nil {
|
||||
if original.FromCardID == 0 {
|
||||
return ERROR_TRANSFER_FROMCARD_IDZERO
|
||||
}
|
||||
var origFromCard Card
|
||||
if err := tx.Find(&origFromCard, original.FromCardID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
var origToCard *Card
|
||||
if err := tx.Find(origToCard, original.ToCardID).Error; err != nil {
|
||||
origFromCard.Balance += original.Value
|
||||
if err := tx.Save(origFromCard).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
diffCurs := origFromCard.CurrencyID != origToCard.CurrencyID
|
||||
if !diffCurs {
|
||||
origFromCard.Balance += original.Value
|
||||
if original.ToCardID == 0 {
|
||||
return ERROR_TRANSFER_FROMCARD_IDZERO
|
||||
}
|
||||
var origToCard Card
|
||||
if err := tx.Find(&origToCard, original.ToCardID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
||||
if err := tx.Save(origFromCard).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Save(origToCard).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -150,71 +112,48 @@ func (t *Transfer) BeforeUpdate(tx *gorm.DB) error {
|
|||
if err := tx.Find(fromCard, t.FromCardID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
toCard := &Card{}
|
||||
if err := tx.Find(toCard, t.ToCardID).Error; err != nil {
|
||||
return err
|
||||
if fromCard.UserID != t.UserID {
|
||||
return ERROR_TRANSFER_FROMCARD_INVALID_USERID
|
||||
}
|
||||
if err := t.validateTransfer(fromCard, toCard); err != nil {
|
||||
return err
|
||||
if fromCard.Balance < t.Value {
|
||||
return ERROR_TRANSFER_FROMCARD_INSUFFICIENT_BALANCE
|
||||
}
|
||||
|
||||
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 := &Card{}
|
||||
if err := tx.Find(toCard, t.ToCardID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if toCard.UserID != t.UserID {
|
||||
return ERROR_TRANSFER_TOCARD_INVALID_USERID
|
||||
}
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Transfer) AfterDelete(tx *gorm.DB) error {
|
||||
fromCard := &Card{}
|
||||
if err := tx.Find(fromCard, t.FromCardID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
fromCard.Balance += t.Value
|
||||
if err := tx.Save(fromCard).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
toCard := &Card{}
|
||||
if err := tx.Find(toCard, t.ToCardID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
diffCurs := fromCard.CurrencyID != toCard.CurrencyID
|
||||
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
|
||||
}
|
||||
if err := tx.Save(toCard).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,22 +1,12 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.qowevisa.me/Qowevisa/fin-check-api/db"
|
||||
"git.qowevisa.me/Qowevisa/fin-check-api/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
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{
|
||||
ID: inp.ID,
|
||||
Name: inp.Name,
|
||||
|
@ -25,8 +15,6 @@ var cardTransform func(inp *db.Card) types.DbCard = func(inp *db.Card) types.DbC
|
|||
CreditLine: inp.CreditLine,
|
||||
LastDigits: inp.LastDigits,
|
||||
CurrencyID: inp.CurrencyID,
|
||||
Currency: curr,
|
||||
DisplayName: fmt.Sprintf("%s •%s", inp.Name, inp.LastDigits),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,20 +51,9 @@ func CardGetAll(c *gin.Context) {
|
|||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
preloadCurrencies := c.DefaultQuery("preload_currencies", "false")
|
||||
shouldPreloadCurrencies := false
|
||||
if val, err := strconv.ParseBool(preloadCurrencies); err == nil {
|
||||
shouldPreloadCurrencies = val
|
||||
}
|
||||
dbc := db.Connect()
|
||||
var entities []*db.Card
|
||||
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 {
|
||||
if err := dbc.Find(&entities, db.Card{UserID: userID}).Error; err != nil {
|
||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,52 +1,18 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.qowevisa.me/Qowevisa/fin-check-api/db"
|
||||
"git.qowevisa.me/Qowevisa/fin-check-api/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
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{
|
||||
ID: inp.ID,
|
||||
FromCardID: inp.FromCardID,
|
||||
ToCardID: inp.ToCardID,
|
||||
Value: inp.Value,
|
||||
FromValue: inp.FromValue,
|
||||
ToValue: inp.ToValue,
|
||||
Date: inp.Date,
|
||||
//
|
||||
ShowValue: showValue,
|
||||
HaveDifferentCurrencies: haveDiffCurrs,
|
||||
FromCard: fromCard,
|
||||
ToCard: toCard,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,7 +52,7 @@ func TransferGetAll(c *gin.Context) {
|
|||
}
|
||||
dbc := db.Connect()
|
||||
var entities []*db.Transfer
|
||||
if err := dbc.Preload("FromCard.Currency").Preload("ToCard.Currency").Find(&entities, db.Transfer{UserID: userID}).Error; err != nil {
|
||||
if err := dbc.Find(&entities, db.Transfer{UserID: userID}).Error; err != nil {
|
||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
@ -116,8 +82,6 @@ func TransferAdd(c *gin.Context) {
|
|||
dst.ToCardID = src.ToCardID
|
||||
dst.Value = src.Value
|
||||
dst.Date = src.Date
|
||||
dst.FromValue = src.FromValue
|
||||
dst.ToValue = src.ToValue
|
||||
})(c)
|
||||
}
|
||||
|
||||
|
@ -143,8 +107,6 @@ func TransferPutId(c *gin.Context) {
|
|||
dst.ToCardID = src.ToCardID
|
||||
dst.Value = src.Value
|
||||
dst.Date = src.Date
|
||||
dst.FromValue = src.FromValue
|
||||
dst.ToValue = src.ToValue
|
||||
},
|
||||
transferTransform,
|
||||
)(c)
|
||||
|
|
|
@ -31,9 +31,6 @@ type DbCard struct {
|
|||
CreditLine uint64 `json:"credit_line" example:"500000"`
|
||||
LastDigits string `json:"last_digits" example:"1111"`
|
||||
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 {
|
||||
|
@ -89,14 +86,7 @@ type DbTransfer struct {
|
|||
FromCardID uint `json:"from_card_id" example:"1"`
|
||||
ToCardID uint `json:"to_card_id" example:"1"`
|
||||
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"`
|
||||
// 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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user