-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
368 lines (309 loc) · 9.62 KB
/
model.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package postgres
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"github.com/jmoiron/sqlx"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
"github.com/code-payments/code-server/pkg/code/data/action"
"github.com/code-payments/code-server/pkg/code/data/intent"
)
const (
tableName = "codewallet__core_action"
)
type model struct {
Id sql.NullInt64 `db:"id"`
Intent string `db:"intent"`
IntentType uint `db:"intent_type"`
ActionId uint `db:"action_id"`
ActionType uint `db:"action_type"`
Source string `db:"source"`
Destination sql.NullString `db:"destination"`
Quantity sql.NullInt64 `db:"quantity"`
InitiatorPhoneNumber sql.NullString `db:"initiator_phone_number"`
State uint `db:"state"`
CreatedAt time.Time `db:"created_at"`
}
func toModel(obj *action.Record) (*model, error) {
if err := obj.Validate(); err != nil {
return nil, err
}
var destination sql.NullString
if obj.Destination != nil {
destination.Valid = true
destination.String = *obj.Destination
}
var quantity sql.NullInt64
if obj.Quantity != nil {
quantity.Valid = true
quantity.Int64 = int64(*obj.Quantity)
}
var initiatorPhoneNumber sql.NullString
if obj.InitiatorPhoneNumber != nil {
initiatorPhoneNumber.Valid = true
initiatorPhoneNumber.String = *obj.InitiatorPhoneNumber
}
return &model{
Intent: obj.Intent,
IntentType: uint(obj.IntentType),
ActionId: uint(obj.ActionId),
ActionType: uint(obj.ActionType),
Source: obj.Source,
Destination: destination,
Quantity: quantity,
InitiatorPhoneNumber: initiatorPhoneNumber,
State: uint(obj.State),
CreatedAt: obj.CreatedAt,
}, nil
}
func fromModel(obj *model) *action.Record {
var destination *string
if obj.Destination.Valid {
destination = &obj.Destination.String
}
var quantity *uint64
if obj.Quantity.Valid {
value := uint64(obj.Quantity.Int64)
quantity = &value
}
var initiatorPhoneNumber *string
if obj.InitiatorPhoneNumber.Valid {
initiatorPhoneNumber = &obj.InitiatorPhoneNumber.String
}
return &action.Record{
Id: uint64(obj.Id.Int64),
Intent: obj.Intent,
IntentType: intent.Type(obj.IntentType),
ActionId: uint32(obj.ActionId),
ActionType: action.Type(obj.ActionType),
Source: obj.Source,
Destination: destination,
Quantity: quantity,
InitiatorPhoneNumber: initiatorPhoneNumber,
State: action.State(obj.State),
CreatedAt: obj.CreatedAt,
}
}
func (m *model) dbUpdate(ctx context.Context, db *sqlx.DB) error {
return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
var quantityUpdateStmt string
params := []interface{}{
m.Intent,
m.ActionId,
m.State,
}
if m.ActionType == uint(action.CloseDormantAccount) {
quantityUpdateStmt = ", quantity = $4"
params = append(params, m.Quantity)
}
query := fmt.Sprintf(`UPDATE `+tableName+`
SET state = $3%s
WHERE intent = $1 AND action_id = $2
RETURNING id, intent, intent_type, action_id, action_type, source, destination, quantity, initiator_phone_number, state, created_at
`, quantityUpdateStmt)
err := tx.QueryRowxContext(
ctx,
query,
params...,
).StructScan(m)
if err != nil {
return pgutil.CheckNoRows(err, action.ErrActionNotFound)
}
return nil
})
}
func dbPutAllInTx(ctx context.Context, tx *sqlx.Tx, models []*model) ([]*model, error) {
var res []*model
query := `INSERT INTO ` + tableName + ` (intent, intent_type, action_id, action_type, source, destination, quantity, initiator_phone_number, state, created_at) VALUES `
var parameters []interface{}
for i, model := range models {
if model.CreatedAt.IsZero() {
model.CreatedAt = time.Now()
}
baseIndex := len(parameters)
query += fmt.Sprintf(
`($%d, $%d, $%d, $%d, $%d, $%d, $%d, $%d, $%d, $%d)`,
baseIndex+1, baseIndex+2, baseIndex+3, baseIndex+4, baseIndex+5, baseIndex+6, baseIndex+7, baseIndex+8, baseIndex+9, baseIndex+10,
)
if i != len(models)-1 {
query += ","
}
parameters = append(
parameters,
model.Intent,
model.IntentType,
model.ActionId,
model.ActionType,
model.Source,
model.Destination,
model.Quantity,
model.InitiatorPhoneNumber,
model.State,
model.CreatedAt,
)
}
query += ` RETURNING id, intent, intent_type, action_id, action_type, source, destination, quantity, initiator_phone_number, state, created_at`
err := tx.SelectContext(
ctx,
&res,
query,
parameters...,
)
if err != nil {
return nil, pgutil.CheckUniqueViolation(err, action.ErrActionExists)
}
return res, nil
}
func dbGetById(ctx context.Context, db *sqlx.DB, intent string, actionId uint32) (*model, error) {
res := &model{}
query := `SELECT id, intent, intent_type, action_id, action_type, source, destination, quantity, initiator_phone_number, state, created_at
FROM ` + tableName + `
WHERE intent = $1 AND action_id = $2
LIMIT 1`
err := db.GetContext(ctx, res, query, intent, actionId)
if err != nil {
return nil, pgutil.CheckNoRows(err, action.ErrActionNotFound)
}
return res, nil
}
func dbGetAllByIntent(ctx context.Context, db *sqlx.DB, intent string) ([]*model, error) {
res := []*model{}
query := `SELECT id, intent, intent_type, action_id, action_type, source, destination, quantity, initiator_phone_number, state, created_at
FROM ` + tableName + `
WHERE intent = $1
ORDER BY action_id ASC`
err := db.SelectContext(ctx, &res, query, intent)
if err != nil {
return nil, pgutil.CheckNoRows(err, action.ErrActionNotFound)
}
if len(res) == 0 {
return nil, action.ErrActionNotFound
}
return res, nil
}
func dbGetAllByAddress(ctx context.Context, db *sqlx.DB, address string) ([]*model, error) {
res := []*model{}
query := `SELECT id, intent, intent_type, action_id, action_type, source, destination, quantity, initiator_phone_number, state, created_at
FROM ` + tableName + `
WHERE source = $1 OR destination = $1`
err := db.SelectContext(ctx, &res, query, address)
if err != nil {
return nil, pgutil.CheckNoRows(err, action.ErrActionNotFound)
}
if len(res) == 0 {
return nil, action.ErrActionNotFound
}
return res, nil
}
func dbGetNetBalance(ctx context.Context, db *sqlx.DB, account string) (int64, error) {
var res sql.NullInt64
query := `SELECT
(SELECT COALESCE(SUM(quantity), 0) FROM ` + tableName + ` WHERE destination = $1 AND state != $2) -
(SELECT COALESCE(SUM(quantity), 0) FROM ` + tableName + ` WHERE source = $1 AND state != $2);`
err := pgutil.ExecuteInTx(ctx, db, sql.LevelRepeatableRead, func(tx *sqlx.Tx) error {
return tx.GetContext(
ctx,
&res,
query,
account,
action.StateRevoked,
)
})
if err != nil {
return 0, err
}
if !res.Valid {
return 0, nil
}
return res.Int64, nil
}
func dbGetNetBalanceBatch(ctx context.Context, db *sqlx.DB, accounts ...string) (map[string]int64, error) {
if len(accounts) == 0 {
return make(map[string]int64), nil
}
type Row struct {
Account string `db:"account"`
NetBalance int64 `db:"net_balance"`
}
rows := []*Row{}
individualFilters := make([]string, len(accounts))
for i, vault := range accounts {
individualFilters[i] = fmt.Sprintf("'%s'", vault)
}
accountFilter := strings.Join(individualFilters, ",")
query := fmt.Sprintf(
`(SELECT destination AS account, COALESCE(SUM(quantity), 0) AS net_balance FROM `+tableName+` WHERE destination IN (%s) AND state != $1 GROUP BY destination)
UNION
(SELECT source AS account, -COALESCE(SUM(quantity), 0) AS net_balance FROM `+tableName+` WHERE source IN (%s) AND state != $1 GROUP BY source);`,
accountFilter, accountFilter,
)
err := pgutil.ExecuteInTx(ctx, db, sql.LevelRepeatableRead, func(tx *sqlx.Tx) error {
return tx.SelectContext(
ctx,
&rows,
query,
action.StateRevoked,
)
})
if err != nil {
return nil, err
}
res := make(map[string]int64)
for _, account := range accounts {
res[account] = 0
}
for _, row := range rows {
res[row.Account] += row.NetBalance
}
return res, nil
}
func dbGetGiftCardClaimedAction(ctx context.Context, db *sqlx.DB, giftCardVault string) (*model, error) {
res := []*model{}
query := `SELECT id, intent, intent_type, action_id, action_type, source, destination, quantity, initiator_phone_number, state, created_at
FROM ` + tableName + `
WHERE source = $1 AND action_type = $2 AND state != $3
LIMIT 2`
err := db.SelectContext(
ctx,
&res,
query,
giftCardVault,
action.NoPrivacyWithdraw,
action.StateRevoked,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, action.ErrActionNotFound)
}
if len(res) == 0 {
return nil, action.ErrActionNotFound
} else if len(res) > 1 {
return nil, action.ErrMultipleActionsFound
}
return res[0], nil
}
func dbGetGiftCardAutoReturnAction(ctx context.Context, db *sqlx.DB, giftCardVault string) (*model, error) {
res := []*model{}
query := `SELECT id, intent, intent_type, action_id, action_type, source, destination, quantity, initiator_phone_number, state, created_at
FROM ` + tableName + `
WHERE source = $1 AND action_type = $2 AND state != $3
LIMIT 2`
err := db.SelectContext(
ctx,
&res,
query,
giftCardVault,
action.CloseDormantAccount,
action.StateRevoked,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, action.ErrActionNotFound)
}
if len(res) == 0 {
return nil, action.ErrActionNotFound
} else if len(res) > 1 {
return nil, action.ErrMultipleActionsFound
}
return res[0], nil
}