Add situation when transfer will be from one currency on the card to different currency
This commit is contained in:
parent
e1a46f5fc7
commit
4199603be3
189
db/transfer.go
189
db/transfer.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user