Skip to content

Commit eb3c925

Browse files
author
Armin
committed
refactor database schema and models to include transaction ID for user transactions
1 parent 01d16d3 commit eb3c925

File tree

4 files changed

+39
-58
lines changed

4 files changed

+39
-58
lines changed

.gitignore

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,2 @@
1-
# If you prefer the allow list template instead of the deny list, see community template:
2-
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3-
#
4-
# Binaries for programs and plugins
5-
*.exe
6-
*.exe~
7-
*.dll
8-
*.so
9-
*.dylib
10-
11-
# Test binary, built with `go test -c`
12-
*.test
13-
14-
# Code coverage profiles and other test artifacts
15-
*.out
16-
coverage.*
17-
*.coverprofile
18-
profile.cov
19-
20-
# Dependency directories (remove the comment below to include it)
21-
# vendor/
22-
23-
# Go workspace file
24-
go.work
25-
go.work.sum
26-
27-
# env file
28-
.env
29-
30-
# Editor/IDE
31-
# .idea/
32-
# .vscode/
1+
.idea
2+
.vscode

internal/balance/db/db.sql

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
CREATE TABLE user_balances (
2-
user_id BIGINT PRIMARY KEY,
2+
id BIGINT PRIMARY KEY AUTO_INCREMENT,
3+
user_id BIGINT NOT NULL,
34
balance BIGINT NOT NULL DEFAULT 0,
4-
last_updated TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
5-
)ENGINE=InnoDB;
5+
last_updated DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
6+
) ENGINE=InnoDB;
67

78
CREATE TABLE user_transactions (
8-
user_id BIGINT NOT NULL,
9-
amount BIGINT NOT NULL, -- Positive for deposit, negative for withdrawal
10-
transaction_type VARCHAR(50) NOT NULL, -- e.g., 'deposit', 'withdrawal', 'transfer'
11-
description TEXT,
12-
created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
9+
id BIGINT PRIMARY KEY AUTO_INCREMENT,
10+
user_id BIGINT NOT NULL,
11+
amount BIGINT NOT NULL,
12+
transaction_type VARCHAR(50) NOT NULL,
13+
description TEXT,
14+
transaction_id VARCHAR(50) NOT NULL,
15+
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
1316
) ENGINE=InnoDB;
1417

1518
CREATE TABLE sms_status (
16-
user_id BIGINT NOT NULL,
17-
status VARCHAR(50) NOT NULL,
18-
type VARCHAR(50) NOT NULL,
19-
recipient VARCHAR(20) NOT NULL,
20-
Provider VARCHAR(50) NOT NULL default '',
21-
created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
22-
updated_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
23-
) ENGINE=InnoDB;
19+
id BIGINT PRIMARY KEY AUTO_INCREMENT,
20+
user_id BIGINT NOT NULL,
21+
status VARCHAR(50) NOT NULL,
22+
type VARCHAR(50) NOT NULL,
23+
recipient VARCHAR(20) NOT NULL,
24+
provider VARCHAR(50) NOT NULL DEFAULT '',
25+
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
26+
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
27+
) ENGINE=InnoDB;

internal/balance/service.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"fmt"
88
"sms-gateway/app"
99
"sms-gateway/internal/model"
10+
11+
"github.com/google/uuid"
1012
)
1113

1214
type transactionType string
@@ -46,7 +48,7 @@ type DeductBalanceRequest struct {
4648
func DeductBalance(ctx context.Context, req DeductBalanceRequest) (err error) {
4749
price := calculatePrice(req.Type, req.Quantity)
4850

49-
tx, err := app.DB.DB.BeginTxx(ctx, nil)
51+
tx, err := app.DB.BeginTxx(ctx, nil)
5052
if err != nil {
5153
return err
5254
}
@@ -70,13 +72,15 @@ func DeductBalance(ctx context.Context, req DeductBalanceRequest) (err error) {
7072
return errors.New("insufficient balance")
7173
}
7274

73-
const insertTransactionQuery = `INSERT INTO user_transactions (user_id, amount, transaction_type, description) VALUES (?, ?, ?, ?)`
75+
txID := uuid.NewString()
76+
const insertTransactionQuery = `INSERT INTO user_transactions (user_id, amount, transaction_type, description, transaction_id) VALUES (?, ?, ?, ?, ?)`
7477
if _, err = tx.ExecContext(ctx,
7578
insertTransactionQuery,
7679
req.CustomerID,
7780
-price,
7881
Withdrawal,
79-
descriptionGenerator(req.Type, req.Quantity)); err != nil {
82+
descriptionGenerator(req.Type, req.Quantity),
83+
txID); err != nil {
8084
return err
8185
}
8286

@@ -92,10 +96,11 @@ type UserTransaction struct {
9296
Amount int64 `db:"amount"`
9397
TransactionType transactionType `db:"transaction_type"`
9498
Description string `db:"description"`
99+
TransactionID string `db:"transaction_id" json:"transaction_id"`
95100
}
96101

97102
func GetUserTransactions(ctx context.Context, userID string) ([]UserTransaction, error) {
98-
const query = `SELECT user_id, amount, transaction_type, description FROM user_transactions WHERE user_id = ?`
103+
const query = `SELECT user_id, amount, transaction_type, description, transaction_id FROM user_transactions WHERE user_id = ?`
99104

100105
var transactions []UserTransaction
101106
if err := app.DB.SelectContext(ctx, &transactions, query, userID); err != nil {
@@ -174,8 +179,9 @@ func AddBalance(ctx context.Context, req AddBalanceRequest) (err error) {
174179
description = fmt.Sprintf("افزایش موجودی به میزان %d", req.Amount)
175180
}
176181

177-
const insertTransactionQuery = `INSERT INTO user_transactions (user_id, amount, transaction_type, description) VALUES (?, ?, ?, ?)`
178-
if _, err = tx.ExecContext(ctx, insertTransactionQuery, req.CustomerID, req.Amount, Deposit, description); err != nil {
182+
txID := uuid.NewString()
183+
const insertTransactionQuery = `INSERT INTO user_transactions (user_id, amount, transaction_type, description, transaction_id) VALUES (?, ?, ?, ?, ?)`
184+
if _, err = tx.ExecContext(ctx, insertTransactionQuery, req.CustomerID, req.Amount, Deposit, description, txID); err != nil {
179185
return err
180186
}
181187

internal/model/model.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const (
88
)
99

1010
type SMS struct {
11-
CustomerID int64 `json:"customer_id"`
12-
Text string `json:"text"`
13-
Recipients []string `json:"recipients"`
14-
Type Type `json:"type"`
11+
CustomerID int64 `json:"customer_id"`
12+
Text string `json:"text"`
13+
Recipients []string `json:"recipients"`
14+
Type Type `json:"type"`
15+
TransactionID string `json:"transaction_id"`
1516
}

0 commit comments

Comments
 (0)