Compare commits

...

5 Commits

4 changed files with 205 additions and 73 deletions

View File

@ -9,14 +9,17 @@ import (
type Transfer struct {
gorm.Model
FromCardID uint
FromCard *Card
ToCardID uint
ToCard *Card
Value uint64
Date time.Time
UserID uint
User *User
FromCardID uint
FromCard *Card
ToCardID uint
ToCard *Card
Value uint64
HaveDifferentCurrencies bool
FromValue uint64
ToValue uint64
Date time.Time
UserID uint
User *User
}
// Implements db.UserIdentifiable:1
@ -36,34 +39,65 @@ 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")
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_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
}
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{}
if err := tx.Find(toCard, t.ToCardID).Error; err != nil {
return err
}
if toCard.UserID != t.UserID {
return ERROR_TRANSFER_TOCARD_INVALID_USERID
if err := t.validateTransfer(fromCard, toCard); err != nil {
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 {
return err
}
@ -71,39 +105,43 @@ func (t *Transfer) BeforeCreate(tx *gorm.DB) error {
}
var (
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")
ERROR_TRANSFER_TOCARD_INSUFFICIENT_BALANCE = errors.New("Transfer's ToCard's Balance is lower than Value of ToValue 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
}
if original.FromCardID == 0 {
return ERROR_TRANSFER_FROMCARD_IDZERO
}
var origFromCard Card
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
}
origFromCard.Balance += original.Value
if err := tx.Save(origFromCard).Error; err != nil {
var origToCard *Card
if err := tx.Find(origToCard, original.ToCardID).Error; err != nil {
return err
}
if original.ToCardID == 0 {
return ERROR_TRANSFER_FROMCARD_IDZERO
diffCurs := origFromCard.CurrencyID != origToCard.CurrencyID
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.Find(&origToCard, original.ToCardID).Error; err != nil {
if err := tx.Save(origFromCard).Error; err != nil {
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 {
return err
}
@ -112,25 +150,35 @@ func (t *Transfer) BeforeUpdate(tx *gorm.DB) error {
if err := tx.Find(fromCard, t.FromCardID).Error; err != nil {
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{}
if err := tx.Find(toCard, t.ToCardID).Error; err != nil {
return err
}
if toCard.UserID != t.UserID {
return ERROR_TRANSFER_TOCARD_INVALID_USERID
if err := t.validateTransfer(fromCard, toCard); err != nil {
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 {
return err
}
@ -142,18 +190,31 @@ func (t *Transfer) AfterDelete(tx *gorm.DB) error {
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
}
if toCard.Balance < t.Value {
return ERROR_TRANSFER_TOCARD_INSUFFICIENT_BALANCE
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
}
toCard.Balance -= t.Value
if err := tx.Save(toCard).Error; err != nil {
return err
}

View File

@ -1,12 +1,22 @@
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,
@ -15,6 +25,8 @@ 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),
}
}
@ -51,9 +63,20 @@ 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
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()})
return
}

View File

@ -1,18 +1,52 @@
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,
}
}
@ -52,7 +86,7 @@ func TransferGetAll(c *gin.Context) {
}
dbc := db.Connect()
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()})
return
}
@ -82,6 +116,8 @@ 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)
}
@ -107,6 +143,8 @@ 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)

View File

@ -24,13 +24,16 @@ type ErrorResponse struct {
}
type DbCard struct {
ID uint `json:"id" example:"1"`
Name string `json:"name" example:"CreditCard"`
Balance uint64 `json:"balance" example:"1000"`
HaveCreditLine bool `json:"have_credit_line" example:"true"`
CreditLine uint64 `json:"credit_line" example:"500000"`
LastDigits string `json:"last_digits" example:"1111"`
CurrencyID uint `json:"currency_id" example:"1"`
ID uint `json:"id" example:"1"`
Name string `json:"name" example:"CreditCard"`
Balance uint64 `json:"balance" example:"1000"`
HaveCreditLine bool `json:"have_credit_line" example:"true"`
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 {
@ -86,7 +89,14 @@ 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 {