-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
399 lines (370 loc) · 10.8 KB
/
group.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package sqlite
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"github.com/georgysavva/scany/v2/sqlscan"
"github.com/mattn/go-sqlite3"
"github.com/rs/xid"
"github.com/samber/lo"
"github.com/waylen888/tab-buddy/calc"
"github.com/waylen888/tab-buddy/db"
"github.com/waylen888/tab-buddy/db/entity"
)
func (s *sqlite) GetGroups(userID string) ([]entity.Group, error) {
ctx, cancel := context.WithTimeout(context.TODO(), s.timeout)
defer cancel()
var groups []entity.Group
if err := sqlscan.Select(
ctx, s.rwDB, &groups, `
SELECT id, name, create_at, update_at
FROM "group"
WHERE id IN (SELECT group_id FROM group_member WHERE user_id = @user_id);`,
sql.Named("user_id", userID),
); err != nil {
return nil, err
}
return groups, nil
}
func (s *sqlite) GetGroup(ID string, userID string) (entity.Group, error) {
ctx, cancel := context.WithTimeout(context.TODO(), s.timeout)
defer cancel()
group := entity.Group{}
if err := sqlscan.Get(
ctx, s.rwDB, &group, `
SELECT *
FROM "group"
WHERE id = @id
AND id IN (SELECT group_id FROM group_member WHERE user_id = @user_id);`,
sql.Named("id", ID),
sql.Named("user_id", userID),
); err != nil {
return entity.Group{}, err
}
return group, nil
}
func (s *sqlite) CreateGroup(name string, ownerID string) (entity.Group, error) {
if ownerID == "" {
return entity.Group{}, fmt.Errorf("ownerID is empty")
}
group := entity.Group{
ID: xid.New().String(),
Name: name,
CreateAt: time.Now(),
}
return group, s.WithTx(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(
ctx,
`INSERT INTO "group" (id, name, create_at, update_at) VALUES (@id, @name, @create_at, @update_at)`,
sql.Named("id", group.ID),
sql.Named("name", group.Name),
sql.Named("create_at", group.CreateAt),
sql.Named("update_at", group.UpdateAt),
)
if err != nil {
return fmt.Errorf("insert into group: %w", err)
}
_, err = tx.ExecContext(
ctx,
`INSERT INTO group_member (group_id, user_id) VALUES (@group_id, @user_id)`,
sql.Named("group_id", group.ID),
sql.Named("user_id", ownerID),
)
return err
})
}
func (s *sqlite) UpdateGroup(ID string, name string, convertToTwd bool) (entity.Group, error) {
ctx, cancel := context.WithTimeout(context.TODO(), s.timeout)
defer cancel()
var group entity.Group
err := sqlscan.Get(
ctx, s.rwDB, &group,
`UPDATE "group"
SET name = @name,
convert_to_twd = @convert_to_twd,
update_at = @update_at
WHERE id = @id RETURNING *`,
sql.Named("id", ID),
sql.Named("name", name),
sql.Named("convert_to_twd", convertToTwd),
sql.Named("update_at", time.Now()),
)
return group, err
}
func (s *sqlite) DeleteGroup(ID string) error {
ctx, cancel := context.WithTimeout(context.TODO(), s.timeout)
defer cancel()
_, err := s.rwDB.ExecContext(
ctx,
`DELETE FROM "group" WHERE id = @id`,
sql.Named("id", ID),
)
return err
}
func (s *sqlite) GetGroupExpenses(groupID string) ([]entity.ExpenseWithSplitUser, error) {
ctx, cancel := context.WithTimeout(context.TODO(), s.timeout)
defer cancel()
var expenses []entity.ExpenseWithSplitUser
if err := sqlscan.Select(
ctx, s.rwDB, &expenses,
`SELECT id, amount, description, date, currency_code, category, twd_rate, create_at, update_at, created_by
FROM expense
JOIN group_expense
ON expense.id = group_expense.expense_id
WHERE group_id = @group_id
ORDER BY "date" DESC, "create_at" ASC
`,
sql.Named("group_id", groupID),
); err != nil {
return nil, err
}
for i := range expenses {
expense := &expenses[i]
err := sqlscan.Select(
ctx, s.rwDB, &expense.SplitUsers,
`SELECT id, username, display_name, email, create_at, update_at, paid, owed, amount
FROM user JOIN user_expense
ON user.id = user_expense.user_id
WHERE user_expense.expense_id = @id`,
sql.Named("id", expense.ID),
)
if err != nil {
return expenses, err
}
}
return expenses, nil
}
func (s *sqlite) GetExpense(ID string) (entity.ExpenseWithSplitUser, error) {
ctx, cancel := context.WithTimeout(context.TODO(), s.timeout)
defer cancel()
var expense entity.ExpenseWithSplitUser
if err := sqlscan.Get(
ctx, s.rwDB, &expense,
`SELECT
id, amount, description, date, currency_code, category, twd_rate, note, create_at, update_at, created_by
FROM expense
WHERE id = @id`,
sql.Named("id", ID),
); err != nil {
return entity.ExpenseWithSplitUser{}, err
}
err := sqlscan.Select(
ctx, s.rwDB, &expense.SplitUsers,
`SELECT id, username, display_name, email, create_at, update_at, paid, owed
FROM user JOIN user_expense
ON user.id = user_expense.user_id
WHERE user_expense.expense_id = @id`,
sql.Named("id", ID),
)
return expense, err
}
func (s *sqlite) ExpenseAccessPermissions(userID string, expenseID string) error {
ctx, cancel := context.WithTimeout(context.TODO(), s.timeout)
defer cancel()
var ok bool
err := sqlscan.Get(ctx, s.rwDB, &ok, `
SELECT 1
FROM group_member
JOIN group_expense
ON group_member.group_id = group_expense.group_id
WHERE group_expense.expense_id = @expense_id AND group_member.user_id = @user_id`,
sql.Named("expense_id", expenseID),
sql.Named("user_id", userID),
)
return err
}
func (s *sqlite) CreateExpense(args entity.CreateExpenseArguments) (entity.Expense, error) {
ctx, cancel := context.WithTimeout(context.TODO(), s.timeout)
defer cancel()
currency, err := s.GetCurrency(args.CurrencyCode)
if err != nil {
return entity.Expense{}, err
}
expense := entity.Expense{
ID: xid.New().String(),
Amount: args.Amount,
Description: args.Description,
Date: args.Date,
CurrencyCode: currency.Code,
Category: args.Category,
TWDRate: args.TWDRate,
Note: args.Note,
CreateAt: time.Now(),
CreatedBy: args.CreateByUserID,
}
tx, err := s.rwDB.BeginTx(ctx, nil)
if err != nil {
return expense, err
}
defer tx.Rollback()
_, err = tx.ExecContext(
ctx,
`INSERT INTO expense (id, amount, description, date, currency_code, category, twd_rate, note, create_at, update_at, created_by)
VALUES (@id, @amount, @description, @date, @currency_code, @category, @twd_rate, @note, @create_at, @update_at, @created_by)`,
sql.Named("id", expense.ID),
sql.Named("amount", expense.Amount),
sql.Named("description", expense.Description),
sql.Named("date", expense.Date),
sql.Named("currency_code", expense.CurrencyCode),
sql.Named("category", expense.Category),
sql.Named("twd_rate", expense.TWDRate),
sql.Named("note", expense.Note),
sql.Named("create_at", expense.CreateAt),
sql.Named("update_at", expense.UpdateAt),
sql.Named("created_by", expense.CreatedBy),
)
if err != nil {
return expense, err
}
_, err = tx.ExecContext(ctx, `INSERT INTO group_expense(group_id, expense_id) VALUES (@group_id, @expense_id);`,
sql.Named("group_id", args.GroupID),
sql.Named("expense_id", expense.ID),
)
if err != nil {
return expense, err
}
for _, user := range args.SplitUsers {
_, err = tx.ExecContext(
ctx,
`INSERT INTO user_expense(user_id, expense_id, type, amount, paid, owed)
VALUES (@user_id, @expense_id, @type, @amount, @paid, @owed)`,
sql.Named("user_id", user.ID),
sql.Named("expense_id", expense.ID),
sql.Named("type", 0),
sql.Named("amount",
calc.SplitValue(expense.Amount, lo.Map(args.SplitUsers, func(su entity.SplitUser, _ int) calc.SplitUser {
return calc.SplitUser{
ID: su.ID,
Paid: su.Paid,
Owed: su.Owed,
}
}), user.ID).StringFixed(int32(currency.DecimalDigits)),
),
sql.Named("paid", user.Paid),
sql.Named("owed", user.Owed),
)
if err != nil {
return expense, err
}
}
return expense, tx.Commit()
}
func (s *sqlite) UpdateExpense(args entity.UpdateExpenseArguments) (entity.Expense, error) {
ctx, cancel := context.WithTimeout(context.TODO(), s.timeout)
defer cancel()
currency, err := s.GetCurrency(args.CurrencyCode)
if err != nil {
return entity.Expense{}, err
}
var expense entity.Expense
tx, err := s.rwDB.BeginTx(ctx, nil)
if err != nil {
return entity.Expense{}, err
}
defer tx.Rollback()
err = sqlscan.Get(
ctx,
tx,
&expense,
`
UPDATE expense
SET amount = @amount,
description = @description,
date = @date,
currency_code = @currency_code,
category = @category,
twd_rate = @twd_rate,
note = @note,
update_at = @update_at
WHERE id = @id
RETURNING *;
`,
sql.Named("id", args.ExpenseID),
sql.Named("amount", args.Amount),
sql.Named("description", args.Description),
sql.Named("date", args.Date),
sql.Named("currency_code", args.CurrencyCode),
sql.Named("category", args.Category),
sql.Named("twd_rate", args.TWDRate),
sql.Named("note", args.Note),
sql.Named("update_at", time.Now()),
)
if err != nil {
return entity.Expense{}, err
}
_, err = tx.ExecContext(
ctx,
`DELETE FROM user_expense WHERE expense_id = @expense_id`,
sql.Named("expense_id", args.ExpenseID),
)
if err != nil {
return entity.Expense{}, err
}
for _, user := range args.SplitUsers {
_, err = tx.ExecContext(
ctx,
`INSERT INTO user_expense(user_id, expense_id, type, amount, paid, owed)
VALUES (@user_id, @expense_id, @type, @amount, @paid, @owed)`,
sql.Named("user_id", user.ID),
sql.Named("expense_id", expense.ID),
sql.Named("type", 0),
sql.Named("amount",
calc.SplitValue(expense.Amount, lo.Map(args.SplitUsers, func(su entity.SplitUser, _ int) calc.SplitUser {
return calc.SplitUser{
ID: su.ID,
Paid: su.Paid,
Owed: su.Owed,
}
}), user.ID).StringFixed(int32(currency.DecimalDigits)),
),
sql.Named("paid", user.Paid),
sql.Named("owed", user.Owed),
)
if err != nil {
return expense, err
}
}
return expense, tx.Commit()
}
func (s *sqlite) GetGroupMembers(ID string) ([]entity.User, error) {
ctx, cancel := context.WithTimeout(context.TODO(), s.timeout)
defer cancel()
var members []entity.User
if err := sqlscan.Select(
ctx, s.rwDB, &members,
`SELECT *
FROM "user"
WHERE id IN (SELECT user_id FROM group_member WHERE group_id = @group_id);`,
sql.Named("group_id", ID),
); err != nil {
return nil, err
}
return members, nil
}
func (s *sqlite) AddUserToGroupByUsername(groupID string, username *string, email *string) error {
ctx, cancel := context.WithTimeout(context.TODO(), s.timeout)
defer cancel()
_, err := s.rwDB.ExecContext(
ctx,
`INSERT INTO group_member (group_id, user_id)
VALUES (
(SELECT id FROM "group" WHERE id = @group_id),
(
SELECT id FROM "user"
WHERE 1 = 1
AND (1 = (CASE WHEN @username IS NULL THEN 1 ELSE 0 END) OR username = @username)
AND (1 = (CASE WHEN @email IS NULL THEN 1 ELSE 0 END) OR email = @email)
)
)`,
sql.Named("group_id", groupID),
sql.Named("username", username),
sql.Named("email", email),
)
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) && sqliteErr.ExtendedCode == 1555 {
return db.ErrUserAlreadyInGroup
}
return err
}