/
txn.go
77 lines (64 loc) · 1.52 KB
/
txn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2017 Kirill Zhuharev. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:generate goqueryset -in txn.go
package models
import "time"
// TxnType of txn
type TxnType uint
const (
// In incominc txn
In TxnType = iota + 1
// Out outgoing txn
Out
// QiwiCard payment from Qiwi card
QiwiCard
)
// Txn qiwi transaction
// gen:qs
type Txn struct {
ID uint `gorm:"index"`
TxnType TxnType
ProviderID uint // ?
Amount float64
CreatedAt time.Time `gorm:"index"`
Fee float64
Status Status
WalletID uint
}
// Status represent status of txn
type Status uint
const (
// Waiting txn created but not processed
Waiting Status = iota + 1
// Success txn
Success
// Error represent txn with an error
Error
)
// CreateMultipleTxns insert transactions in on txn
func CreateMultipleTxns(walletID uint, txns []Txn) (err error) {
tx := db.Begin()
for _, txn := range txns {
txn.WalletID = walletID
// ignore this errors
_ = tx.Create(txn).Error
}
err = tx.Commit().Error
return
}
// GetWalletTxns get lasts wallet txns
func GetWalletTxns(walletID uint) (res []Txn, err error) {
err = NewTxnQuerySet(db).WalletIDEq(walletID).OrderDescByID().Limit(50).All(&res)
return
}
// GetLastTxn return last txn on wallet with walletID
func GetLastTxn(walletID uint) (txnID uint, err error) {
txn := new(Txn)
err = NewTxnQuerySet(db).WalletIDEq(walletID).OrderDescByID().One(txn)
if err != nil {
return
}
txnID = txn.ID
return
}