77 "fmt"
88 "sms-gateway/app"
99 "sms-gateway/internal/model"
10+
11+ "github.com/google/uuid"
1012)
1113
1214type transactionType string
@@ -46,7 +48,7 @@ type DeductBalanceRequest struct {
4648func 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
97102func 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
0 commit comments