From ae56d253c2c55f341b32477fcd4ba95ca1c6a2fc Mon Sep 17 00:00:00 2001 From: Samuel Tariku Date: Sun, 13 Jul 2025 03:59:43 +0300 Subject: [PATCH] merge fixes --- db/migrations/000007_setting_data.up.sql | 4 +- gen/db/bet.sql.go | 119 +++++------------- gen/db/branch.sql.go | 24 ++-- gen/db/models.go | 13 ++ gen/db/settings.sql.go | 12 ++ gen/db/wallet.sql.go | 17 ++- internal/domain/bet.go | 6 + internal/domain/settings.go | 4 + internal/repository/settings.go | 6 - internal/services/bet/service.go | 11 +- internal/web_server/handlers/bet_handler.go | 38 ++---- .../web_server/handlers/ticket_handler.go | 3 +- internal/web_server/middleware.go | 1 - 13 files changed, 111 insertions(+), 147 deletions(-) diff --git a/db/migrations/000007_setting_data.up.sql b/db/migrations/000007_setting_data.up.sql index db01982..adbc6c2 100644 --- a/db/migrations/000007_setting_data.up.sql +++ b/db/migrations/000007_setting_data.up.sql @@ -4,6 +4,8 @@ VALUES ('max_number_of_outcomes', '30'), ('bet_amount_limit', '100000'), ('daily_ticket_limit', '50'), ('total_winnings_limit', '1000000'), - ('amount_for_bet_referral', '1000000') ON CONFLICT (key) DO + ('amount_for_bet_referral', '1000000') + ('cashback_amount_cap', '1000') + ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value; \ No newline at end of file diff --git a/gen/db/bet.sql.go b/gen/db/bet.sql.go index 4bf8d8e..cae3d8b 100644 --- a/gen/db/bet.sql.go +++ b/gen/db/bet.sql.go @@ -21,8 +21,8 @@ INSERT INTO bets ( outcomes_hash, fast_code ) -VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) -RETURNING id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, fast_code, processed +VALUES ($1, $2, $3, $4, $5, $6, $7) +RETURNING id, amount, total_odds, status, user_id, is_shop_bet, cashed_out, outcomes_hash, fast_code, processed, created_at, updated_at ` type CreateBetParams struct { @@ -57,6 +57,8 @@ func (q *Queries) CreateBet(ctx context.Context, arg CreateBetParams) (Bet, erro &i.OutcomesHash, &i.FastCode, &i.Processed, + &i.CreatedAt, + &i.UpdatedAt, ) return i, err } @@ -98,7 +100,7 @@ func (q *Queries) DeleteBetOutcome(ctx context.Context, betID int64) error { } const GetAllBets = `-- name: GetAllBets :many -SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, fast_code, processed, outcomes +SELECT id, amount, total_odds, status, user_id, is_shop_bet, cashed_out, outcomes_hash, fast_code, processed, created_at, updated_at, full_name, phone_number, outcomes FROM bet_with_outcomes wHERE ( user_id = $1 @@ -163,51 +165,10 @@ func (q *Queries) GetAllBets(ctx context.Context, arg GetAllBetsParams) ([]BetWi &i.OutcomesHash, &i.FastCode, &i.Processed, - &i.Outcomes, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const GetBetByBranchID = `-- name: GetBetByBranchID :many -SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, fast_code, processed, outcomes -FROM bet_with_outcomes -WHERE branch_id = $1 -` - -func (q *Queries) GetBetByBranchID(ctx context.Context, branchID pgtype.Int8) ([]BetWithOutcome, error) { - rows, err := q.db.Query(ctx, GetBetByBranchID, branchID) - if err != nil { - return nil, err - } - defer rows.Close() - var items []BetWithOutcome - for rows.Next() { - var i BetWithOutcome - if err := rows.Scan( - &i.ID, - &i.Amount, - &i.TotalOdds, - &i.Status, - &i.FullName, - &i.PhoneNumber, - &i.CompanyID, - &i.BranchID, - &i.UserID, - &i.CashedOut, - &i.CashoutID, &i.CreatedAt, &i.UpdatedAt, - &i.IsShopBet, - &i.OutcomesHash, - &i.FastCode, - &i.Processed, + &i.FullName, + &i.PhoneNumber, &i.Outcomes, ); err != nil { return nil, err @@ -220,40 +181,8 @@ func (q *Queries) GetBetByBranchID(ctx context.Context, branchID pgtype.Int8) ([ return items, nil } -const GetBetByCashoutID = `-- name: GetBetByCashoutID :one -SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, fast_code, processed, outcomes -FROM bet_with_outcomes -WHERE cashout_id = $1 -` - -func (q *Queries) GetBetByCashoutID(ctx context.Context, cashoutID string) (BetWithOutcome, error) { - row := q.db.QueryRow(ctx, GetBetByCashoutID, cashoutID) - var i BetWithOutcome - err := row.Scan( - &i.ID, - &i.Amount, - &i.TotalOdds, - &i.Status, - &i.FullName, - &i.PhoneNumber, - &i.CompanyID, - &i.BranchID, - &i.UserID, - &i.CashedOut, - &i.CashoutID, - &i.CreatedAt, - &i.UpdatedAt, - &i.IsShopBet, - &i.OutcomesHash, - &i.FastCode, - &i.Processed, - &i.Outcomes, - ) - return i, err -} - const GetBetByFastCode = `-- name: GetBetByFastCode :one -SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, fast_code, processed, outcomes +SELECT id, amount, total_odds, status, user_id, is_shop_bet, cashed_out, outcomes_hash, fast_code, processed, created_at, updated_at, full_name, phone_number, outcomes FROM bet_with_outcomes WHERE fast_code = $1 LIMIT 1 @@ -273,13 +202,17 @@ func (q *Queries) GetBetByFastCode(ctx context.Context, fastCode string) (BetWit &i.OutcomesHash, &i.FastCode, &i.Processed, + &i.CreatedAt, + &i.UpdatedAt, + &i.FullName, + &i.PhoneNumber, &i.Outcomes, ) return i, err } const GetBetByID = `-- name: GetBetByID :one -SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, fast_code, processed, outcomes +SELECT id, amount, total_odds, status, user_id, is_shop_bet, cashed_out, outcomes_hash, fast_code, processed, created_at, updated_at, full_name, phone_number, outcomes FROM bet_with_outcomes WHERE id = $1 ` @@ -298,13 +231,17 @@ func (q *Queries) GetBetByID(ctx context.Context, id int64) (BetWithOutcome, err &i.OutcomesHash, &i.FastCode, &i.Processed, + &i.CreatedAt, + &i.UpdatedAt, + &i.FullName, + &i.PhoneNumber, &i.Outcomes, ) return i, err } const GetBetByUserID = `-- name: GetBetByUserID :many -SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, fast_code, processed, outcomes +SELECT id, amount, total_odds, status, user_id, is_shop_bet, cashed_out, outcomes_hash, fast_code, processed, created_at, updated_at, full_name, phone_number, outcomes FROM bet_with_outcomes WHERE user_id = $1 ` @@ -329,6 +266,10 @@ func (q *Queries) GetBetByUserID(ctx context.Context, userID int64) ([]BetWithOu &i.OutcomesHash, &i.FastCode, &i.Processed, + &i.CreatedAt, + &i.UpdatedAt, + &i.FullName, + &i.PhoneNumber, &i.Outcomes, ); err != nil { return nil, err @@ -344,7 +285,6 @@ func (q *Queries) GetBetByUserID(ctx context.Context, userID int64) ([]BetWithOu const GetBetCount = `-- name: GetBetCount :one SELECT COUNT(*) FROM bets -WHERE user_id = $1 WHERE user_id = $1 AND outcomes_hash = $2 ` @@ -458,7 +398,7 @@ func (q *Queries) GetBetOutcomeByEventID(ctx context.Context, arg GetBetOutcomeB } const GetBetsForCashback = `-- name: GetBetsForCashback :many -SELECT id, amount, total_odds, status, full_name, phone_number, company_id, branch_id, user_id, cashed_out, cashout_id, created_at, updated_at, is_shop_bet, outcomes_hash, fast_code, processed, outcomes +SELECT id, amount, total_odds, status, user_id, is_shop_bet, cashed_out, outcomes_hash, fast_code, processed, created_at, updated_at, full_name, phone_number, outcomes FROM bet_with_outcomes WHERE status = 2 AND processed = false @@ -478,19 +418,16 @@ func (q *Queries) GetBetsForCashback(ctx context.Context) ([]BetWithOutcome, err &i.Amount, &i.TotalOdds, &i.Status, - &i.FullName, - &i.PhoneNumber, - &i.CompanyID, - &i.BranchID, &i.UserID, - &i.CashedOut, - &i.CashoutID, - &i.CreatedAt, - &i.UpdatedAt, &i.IsShopBet, + &i.CashedOut, &i.OutcomesHash, &i.FastCode, &i.Processed, + &i.CreatedAt, + &i.UpdatedAt, + &i.FullName, + &i.PhoneNumber, &i.Outcomes, ); err != nil { return nil, err diff --git a/gen/db/branch.sql.go b/gen/db/branch.sql.go index 09eafd2..88a96db 100644 --- a/gen/db/branch.sql.go +++ b/gen/db/branch.sql.go @@ -21,7 +21,7 @@ INSERT INTO branches ( is_self_owned ) VALUES ($1, $2, $3, $4, $5, $6) -RETURNING id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at +RETURNING id, name, location, profit_percent, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at ` type CreateBranchParams struct { @@ -47,6 +47,7 @@ func (q *Queries) CreateBranch(ctx context.Context, arg CreateBranchParams) (Bra &i.ID, &i.Name, &i.Location, + &i.ProfitPercent, &i.IsActive, &i.WalletID, &i.BranchManagerID, @@ -155,7 +156,7 @@ func (q *Queries) DeleteBranchOperation(ctx context.Context, arg DeleteBranchOpe } const GetAllBranches = `-- name: GetAllBranches :many -SELECT id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance, wallet_is_active +SELECT id, name, location, profit_percent, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance, wallet_is_active FROM branch_details WHERE ( company_id = $1 @@ -213,6 +214,7 @@ func (q *Queries) GetAllBranches(ctx context.Context, arg GetAllBranchesParams) &i.ID, &i.Name, &i.Location, + &i.ProfitPercent, &i.IsActive, &i.WalletID, &i.BranchManagerID, @@ -261,7 +263,7 @@ func (q *Queries) GetAllSupportedOperations(ctx context.Context) ([]SupportedOpe } const GetBranchByCashier = `-- name: GetBranchByCashier :one -SELECT branches.id, branches.name, branches.location, branches.is_active, branches.wallet_id, branches.branch_manager_id, branches.company_id, branches.is_self_owned, branches.created_at, branches.updated_at +SELECT branches.id, branches.name, branches.location, branches.profit_percent, branches.is_active, branches.wallet_id, branches.branch_manager_id, branches.company_id, branches.is_self_owned, branches.created_at, branches.updated_at FROM branch_cashiers JOIN branches ON branch_cashiers.branch_id = branches.id WHERE branch_cashiers.user_id = $1 @@ -274,6 +276,7 @@ func (q *Queries) GetBranchByCashier(ctx context.Context, userID int64) (Branch, &i.ID, &i.Name, &i.Location, + &i.ProfitPercent, &i.IsActive, &i.WalletID, &i.BranchManagerID, @@ -286,7 +289,7 @@ func (q *Queries) GetBranchByCashier(ctx context.Context, userID int64) (Branch, } const GetBranchByCompanyID = `-- name: GetBranchByCompanyID :many -SELECT id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance, wallet_is_active +SELECT id, name, location, profit_percent, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance, wallet_is_active FROM branch_details WHERE company_id = $1 ` @@ -304,6 +307,7 @@ func (q *Queries) GetBranchByCompanyID(ctx context.Context, companyID int64) ([] &i.ID, &i.Name, &i.Location, + &i.ProfitPercent, &i.IsActive, &i.WalletID, &i.BranchManagerID, @@ -327,7 +331,7 @@ func (q *Queries) GetBranchByCompanyID(ctx context.Context, companyID int64) ([] } const GetBranchByID = `-- name: GetBranchByID :one -SELECT id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance, wallet_is_active +SELECT id, name, location, profit_percent, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance, wallet_is_active FROM branch_details WHERE id = $1 ` @@ -339,6 +343,7 @@ func (q *Queries) GetBranchByID(ctx context.Context, id int64) (BranchDetail, er &i.ID, &i.Name, &i.Location, + &i.ProfitPercent, &i.IsActive, &i.WalletID, &i.BranchManagerID, @@ -355,7 +360,7 @@ func (q *Queries) GetBranchByID(ctx context.Context, id int64) (BranchDetail, er } const GetBranchByManagerID = `-- name: GetBranchByManagerID :many -SELECT id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance, wallet_is_active +SELECT id, name, location, profit_percent, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance, wallet_is_active FROM branch_details WHERE branch_manager_id = $1 ` @@ -373,6 +378,7 @@ func (q *Queries) GetBranchByManagerID(ctx context.Context, branchManagerID int6 &i.ID, &i.Name, &i.Location, + &i.ProfitPercent, &i.IsActive, &i.WalletID, &i.BranchManagerID, @@ -443,7 +449,7 @@ func (q *Queries) GetBranchOperations(ctx context.Context, branchID int64) ([]Ge } const SearchBranchByName = `-- name: SearchBranchByName :many -SELECT id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance, wallet_is_active +SELECT id, name, location, profit_percent, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance, wallet_is_active FROM branch_details WHERE name ILIKE '%' || $1 || '%' ` @@ -461,6 +467,7 @@ func (q *Queries) SearchBranchByName(ctx context.Context, dollar_1 pgtype.Text) &i.ID, &i.Name, &i.Location, + &i.ProfitPercent, &i.IsActive, &i.WalletID, &i.BranchManagerID, @@ -493,7 +500,7 @@ SET name = COALESCE($2, name), is_active = COALESCE($7, is_active), updated_at = CURRENT_TIMESTAMP WHERE id = $1 -RETURNING id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at +RETURNING id, name, location, profit_percent, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at ` type UpdateBranchParams struct { @@ -521,6 +528,7 @@ func (q *Queries) UpdateBranch(ctx context.Context, arg UpdateBranchParams) (Bra &i.ID, &i.Name, &i.Location, + &i.ProfitPercent, &i.IsActive, &i.WalletID, &i.BranchManagerID, diff --git a/gen/db/models.go b/gen/db/models.go index 6d988ac..cccf340 100644 --- a/gen/db/models.go +++ b/gen/db/models.go @@ -84,6 +84,8 @@ type Bet struct { OutcomesHash string `json:"outcomes_hash"` FastCode string `json:"fast_code"` Processed bool `json:"processed"` + CreatedAt pgtype.Timestamp `json:"created_at"` + UpdatedAt pgtype.Timestamp `json:"updated_at"` } type BetOutcome struct { @@ -115,6 +117,10 @@ type BetWithOutcome struct { OutcomesHash string `json:"outcomes_hash"` FastCode string `json:"fast_code"` Processed bool `json:"processed"` + CreatedAt pgtype.Timestamp `json:"created_at"` + UpdatedAt pgtype.Timestamp `json:"updated_at"` + FullName interface{} `json:"full_name"` + PhoneNumber pgtype.Text `json:"phone_number"` Outcomes []BetOutcome `json:"outcomes"` } @@ -128,6 +134,7 @@ type Branch struct { ID int64 `json:"id"` Name string `json:"name"` Location string `json:"location"` + ProfitPercent float32 `json:"profit_percent"` IsActive bool `json:"is_active"` WalletID int64 `json:"wallet_id"` BranchManagerID int64 `json:"branch_manager_id"` @@ -147,6 +154,7 @@ type BranchDetail struct { ID int64 `json:"id"` Name string `json:"name"` Location string `json:"location"` + ProfitPercent float32 `json:"profit_percent"` IsActive bool `json:"is_active"` WalletID int64 `json:"wallet_id"` BranchManagerID int64 `json:"branch_manager_id"` @@ -160,6 +168,11 @@ type BranchDetail struct { WalletIsActive pgtype.Bool `json:"wallet_is_active"` } +type BranchLocation struct { + Key string `json:"key"` + Value string `json:"value"` +} + type BranchOperation struct { ID int64 `json:"id"` OperationID int64 `json:"operation_id"` diff --git a/gen/db/settings.sql.go b/gen/db/settings.sql.go index d842661..23de72c 100644 --- a/gen/db/settings.sql.go +++ b/gen/db/settings.sql.go @@ -81,3 +81,15 @@ func (q *Queries) SaveSetting(ctx context.Context, arg SaveSettingParams) (Setti ) return i, err } + +const SetInitialData = `-- name: SetInitialData :exec +INSERT INTO settings (key, value) +VALUES ('max_number_of_outcomes', '30') ON CONFLICT (key) DO +UPDATE +SET value = EXCLUDED.value +` + +func (q *Queries) SetInitialData(ctx context.Context) error { + _, err := q.db.Exec(ctx, SetInitialData) + return err +} diff --git a/gen/db/wallet.sql.go b/gen/db/wallet.sql.go index 118f4ee..4b94209 100644 --- a/gen/db/wallet.sql.go +++ b/gen/db/wallet.sql.go @@ -228,9 +228,22 @@ WHERE wallet_id = $1 LIMIT 1 ` -func (q *Queries) GetBranchByWalletID(ctx context.Context, walletID int64) (Branch, error) { +type GetBranchByWalletIDRow struct { + ID int64 `json:"id"` + Name string `json:"name"` + Location string `json:"location"` + IsActive bool `json:"is_active"` + WalletID int64 `json:"wallet_id"` + BranchManagerID int64 `json:"branch_manager_id"` + CompanyID int64 `json:"company_id"` + IsSelfOwned bool `json:"is_self_owned"` + CreatedAt pgtype.Timestamp `json:"created_at"` + UpdatedAt pgtype.Timestamp `json:"updated_at"` +} + +func (q *Queries) GetBranchByWalletID(ctx context.Context, walletID int64) (GetBranchByWalletIDRow, error) { row := q.db.QueryRow(ctx, GetBranchByWalletID, walletID) - var i Branch + var i GetBranchByWalletIDRow err := row.Scan( &i.ID, &i.Name, diff --git a/internal/domain/bet.go b/internal/domain/bet.go index f9dcf39..a3f0c2b 100644 --- a/internal/domain/bet.go +++ b/internal/domain/bet.go @@ -98,6 +98,12 @@ type CreateBetReq struct { BranchID *int64 `json:"branch_id,omitempty" validate:"required" example:"1"` } +type CreateBetWithFastCodeReq struct { + FastCode string `json:"fast_code"` + Amount float32 `json:"amount"` + BranchID *int64 `json:"branch_id"` +} + type RandomBetReq struct { BranchID int64 `json:"branch_id" validate:"required" example:"1"` NumberOfBets int64 `json:"number_of_bets" validate:"required" example:"1"` diff --git a/internal/domain/settings.go b/internal/domain/settings.go index d619945..94e599f 100644 --- a/internal/domain/settings.go +++ b/internal/domain/settings.go @@ -22,6 +22,7 @@ type SettingList struct { DailyTicketPerIP int64 `json:"daily_ticket_limit"` TotalWinningLimit Currency `json:"total_winning_limit"` AmountForBetReferral Currency `json:"amount_for_bet_referral"` + CashbackAmountCap Currency `json:"cashback_amount_cap"` } type DBSettingList struct { @@ -30,6 +31,7 @@ type DBSettingList struct { DailyTicketPerIP ValidInt64 TotalWinningLimit ValidInt64 AmountForBetReferral ValidInt64 + CashbackAmountCap ValidInt64 } func ConvertInt64SettingsMap(dbSettingList *DBSettingList) map[string]*ValidInt64 { @@ -39,6 +41,7 @@ func ConvertInt64SettingsMap(dbSettingList *DBSettingList) map[string]*ValidInt6 "daily_ticket_limit": &dbSettingList.DailyTicketPerIP, "total_winnings_limit": &dbSettingList.TotalWinningLimit, "amount_for_bet_referral": &dbSettingList.AmountForBetReferral, + "cashback_amount_cap": &dbSettingList.CashbackAmountCap, } } @@ -49,5 +52,6 @@ func ConvertDBSetting(dbSettingList DBSettingList) SettingList { DailyTicketPerIP: dbSettingList.DailyTicketPerIP.Value, TotalWinningLimit: Currency(dbSettingList.TotalWinningLimit.Value), AmountForBetReferral: Currency(dbSettingList.AmountForBetReferral.Value), + CashbackAmountCap: Currency(dbSettingList.CashbackAmountCap.Value), } } diff --git a/internal/repository/settings.go b/internal/repository/settings.go index b49689a..477ba98 100644 --- a/internal/repository/settings.go +++ b/internal/repository/settings.go @@ -12,12 +12,6 @@ import ( func GetDBSettingList(settings []dbgen.Setting) (domain.SettingList, error) { var dbSettingList domain.DBSettingList - // var int64SettingsMap = map[string]*domain.ValidInt64{ - // "max_number_of_outcomes": &dbSettingList.MaxNumberOfOutcomes, - // "bet_amount_limit": &dbSettingList.BetAmountLimit, - // "daily_ticket_limit": &dbSettingList.DailyTicketPerIP, - // "total_winnings_limit": &dbSettingList.TotalWinningLimit, - // } var int64SettingsMap = domain.ConvertInt64SettingsMap(&dbSettingList) diff --git a/internal/services/bet/service.go b/internal/services/bet/service.go index 5fb06c5..a3952db 100644 --- a/internal/services/bet/service.go +++ b/internal/services/bet/service.go @@ -984,6 +984,7 @@ func (s *Service) SetBetToRemoved(ctx context.Context, id int64) error { } func (s *Service) ProcessBetCashback(ctx context.Context) error { + settingsList, err := s.settingSvc.GetSettingList(ctx) bets, err := s.betStore.GetBetsForCashback(ctx) if err != nil { s.mongoLogger.Error("failed to fetch bets", @@ -1024,24 +1025,22 @@ func (s *Service) ProcessBetCashback(ctx context.Context) error { continue } - wallets, err := s.walletSvc.GetCustomerWallet(ctx, bet.UserID.Value) + wallets, err := s.walletSvc.GetCustomerWallet(ctx, bet.UserID) if err != nil { s.mongoLogger.Error("failed to get wallets of a user", - zap.Int64("userID", bet.UserID.Value), + zap.Int64("userID", bet.UserID), zap.Error(err), ) continue } - - // TODO: get cashback amount cap (currently 1000) from settings in the db - cashbackAmount := math.Min(10, float64(calculateCashbackAmount(bet.Amount.Float32(), bet.TotalOdds))) + cashbackAmount := math.Min(float64(settingsList.CashbackAmountCap), float64(calculateCashbackAmount(bet.Amount.Float32(), bet.TotalOdds))) _, err = s.walletSvc.AddToWallet(ctx, wallets.StaticID, domain.ToCurrency(float32(cashbackAmount)), domain.ValidInt64{}, domain.TRANSFER_DIRECT, domain.PaymentDetails{}, fmt.Sprintf("cashback amount of %f added to users static wallet", cashbackAmount)) if err != nil { s.mongoLogger.Error("Failed to update wallet for user", - zap.Int64("userID", bet.UserID.Value), + zap.Int64("userID", bet.UserID), zap.Error(err)) } } diff --git a/internal/web_server/handlers/bet_handler.go b/internal/web_server/handlers/bet_handler.go index d8f552d..0c335a6 100644 --- a/internal/web_server/handlers/bet_handler.go +++ b/internal/web_server/handlers/bet_handler.go @@ -65,7 +65,7 @@ func (h *Handler) CreateBet(c *fiber.Ctx) error { // @Tags bet // @Accept json // @Produce json -// @Param createBetWithFastCode body domain.CreateBetReq true "Creates bet" +// @Param createBetWithFastCode body domain.CreateBetWithFastCodeReq true "Creates bet" // @Success 200 {object} domain.BetRes // @Failure 400 {object} response.APIResponse // @Failure 500 {object} response.APIResponse @@ -75,10 +75,7 @@ func (h *Handler) CreateBetWithFastCode(c *fiber.Ctx) error { role := c.Locals("role").(domain.Role) companyID := c.Locals("company_id").(domain.ValidInt64) - var req struct { - FastCode string `json:"fast_code"` - Amount float32 `json:"amount"` - } + var req domain.CreateBetWithFastCodeReq if err := c.BodyParser(&req); err != nil { h.mongoLoggerSvc.Error("Failed to parse CreateBet request", @@ -120,32 +117,13 @@ func (h *Handler) CreateBetWithFastCode(c *fiber.Ctx) error { }) } - user, err := h.userSvc.GetUserByID(c.Context(), userID) - if err != nil { - h.mongoLoggerSvc.Error("falied to get user information", - zap.Int("status_code", fiber.StatusInternalServerError), - zap.Error(err), - zap.Time("timestamp", time.Now()), - ) - return fiber.NewError(fiber.StatusBadRequest, "falied to get user information") - } - - branch, err := h.branchSvc.GetBranchByCompanyID(c.Context(), user.CompanyID.Value) - if err != nil { - h.mongoLoggerSvc.Error("falied to get branch of user", - zap.Int("status_code", fiber.StatusInternalServerError), - zap.Error(err), - zap.Time("timestamp", time.Now()), - ) - return fiber.NewError(fiber.StatusBadRequest, "falied to get branch of user") - } - + // This can be for both online and offline bets + // If bet is an online bet (if the customer role creates the bet on their own) + // then the branchID is null newReq := domain.CreateBetReq{ - Amount: req.Amount, - Outcomes: bet_outcomes, - BranchID: &branch[0].ID, - FullName: user.FirstName, - PhoneNumber: user.PhoneNumber, + Amount: req.Amount, + Outcomes: bet_outcomes, + BranchID: req.BranchID, } res, err := h.CreateBetInternal(c, newReq, userID, role, companyID) diff --git a/internal/web_server/handlers/ticket_handler.go b/internal/web_server/handlers/ticket_handler.go index 306ded4..b77f1e6 100644 --- a/internal/web_server/handlers/ticket_handler.go +++ b/internal/web_server/handlers/ticket_handler.go @@ -6,7 +6,6 @@ import ( "time" "github.com/SamuelTariku/FortuneBet-Backend/internal/domain" - "github.com/SamuelTariku/FortuneBet-Backend/internal/services/ticket" "github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/response" "github.com/gofiber/fiber/v2" "go.uber.org/zap" @@ -53,7 +52,7 @@ func (h *Handler) CreateTicket(c *fiber.Ctx) error { if err != nil { var statusCode int - + if isTicketError := h.ticketSvc.CheckTicketError(err); isTicketError { statusCode = fiber.StatusBadRequest } else { diff --git a/internal/web_server/middleware.go b/internal/web_server/middleware.go index 2329547..c25584b 100644 --- a/internal/web_server/middleware.go +++ b/internal/web_server/middleware.go @@ -2,7 +2,6 @@ package httpserver import ( "errors" - "fmt" "strings" "time"