create bet auto generates fast code

This commit is contained in:
Asher Samuel 2025-07-01 23:28:07 +03:00
parent 0e42facebf
commit 163dae4e44
8 changed files with 40 additions and 13 deletions

View File

@ -56,6 +56,7 @@ CREATE TABLE IF NOT EXISTS bets (
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_shop_bet BOOLEAN NOT NULL,
outcomes_hash TEXT NOT NULL,
fast_code VARCHAR(10) NOT NULL,
UNIQUE(cashout_id),
CHECK (
user_id IS NOT NULL

View File

@ -10,9 +10,10 @@ INSERT INTO bets (
is_shop_bet,
cashout_id,
company_id,
outcomes_hash
outcomes_hash,
fast_code
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
RETURNING *;
-- name: CreateBetOutcome :copyfrom
INSERT INTO bet_outcomes (

View File

@ -23,10 +23,11 @@ INSERT INTO bets (
is_shop_bet,
cashout_id,
company_id,
outcomes_hash
outcomes_hash,
fast_code
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
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
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
`
type CreateBetParams struct {
@ -41,6 +42,7 @@ type CreateBetParams struct {
CashoutID string `json:"cashout_id"`
CompanyID pgtype.Int8 `json:"company_id"`
OutcomesHash string `json:"outcomes_hash"`
FastCode string `json:"fast_code"`
}
func (q *Queries) CreateBet(ctx context.Context, arg CreateBetParams) (Bet, error) {
@ -56,6 +58,7 @@ func (q *Queries) CreateBet(ctx context.Context, arg CreateBetParams) (Bet, erro
arg.CashoutID,
arg.CompanyID,
arg.OutcomesHash,
arg.FastCode,
)
var i Bet
err := row.Scan(
@ -74,6 +77,7 @@ func (q *Queries) CreateBet(ctx context.Context, arg CreateBetParams) (Bet, erro
&i.UpdatedAt,
&i.IsShopBet,
&i.OutcomesHash,
&i.FastCode,
)
return i, err
}
@ -115,7 +119,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, outcomes
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, outcomes
FROM bet_with_outcomes
wHERE (
branch_id = $1
@ -191,6 +195,7 @@ func (q *Queries) GetAllBets(ctx context.Context, arg GetAllBetsParams) ([]BetWi
&i.UpdatedAt,
&i.IsShopBet,
&i.OutcomesHash,
&i.FastCode,
&i.Outcomes,
); err != nil {
return nil, err
@ -204,7 +209,7 @@ func (q *Queries) GetAllBets(ctx context.Context, arg GetAllBetsParams) ([]BetWi
}
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, outcomes
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, outcomes
FROM bet_with_outcomes
WHERE branch_id = $1
`
@ -234,6 +239,7 @@ func (q *Queries) GetBetByBranchID(ctx context.Context, branchID pgtype.Int8) ([
&i.UpdatedAt,
&i.IsShopBet,
&i.OutcomesHash,
&i.FastCode,
&i.Outcomes,
); err != nil {
return nil, err
@ -247,7 +253,7 @@ func (q *Queries) GetBetByBranchID(ctx context.Context, branchID pgtype.Int8) ([
}
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, outcomes
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, outcomes
FROM bet_with_outcomes
WHERE cashout_id = $1
`
@ -271,13 +277,14 @@ func (q *Queries) GetBetByCashoutID(ctx context.Context, cashoutID string) (BetW
&i.UpdatedAt,
&i.IsShopBet,
&i.OutcomesHash,
&i.FastCode,
&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, outcomes
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, outcomes
FROM bet_with_outcomes
WHERE id = $1
`
@ -301,13 +308,14 @@ func (q *Queries) GetBetByID(ctx context.Context, id int64) (BetWithOutcome, err
&i.UpdatedAt,
&i.IsShopBet,
&i.OutcomesHash,
&i.FastCode,
&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, outcomes
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, outcomes
FROM bet_with_outcomes
WHERE user_id = $1
`
@ -337,6 +345,7 @@ func (q *Queries) GetBetByUserID(ctx context.Context, userID pgtype.Int8) ([]Bet
&i.UpdatedAt,
&i.IsShopBet,
&i.OutcomesHash,
&i.FastCode,
&i.Outcomes,
); err != nil {
return nil, err

View File

@ -89,6 +89,7 @@ type Bet struct {
UpdatedAt pgtype.Timestamp `json:"updated_at"`
IsShopBet bool `json:"is_shop_bet"`
OutcomesHash string `json:"outcomes_hash"`
FastCode string `json:"fast_code"`
}
type BetOutcome struct {
@ -125,6 +126,7 @@ type BetWithOutcome struct {
UpdatedAt pgtype.Timestamp `json:"updated_at"`
IsShopBet bool `json:"is_shop_bet"`
OutcomesHash string `json:"outcomes_hash"`
FastCode string `json:"fast_code"`
Outcomes []BetOutcome `json:"outcomes"`
}

View File

@ -53,6 +53,7 @@ type Bet struct {
IsShopBet bool
CashedOut bool
CashoutID string
FastCode string
CreatedAt time.Time
}
@ -80,6 +81,7 @@ type GetBet struct {
CashedOut bool
CashoutID string
Outcomes []BetOutcome
FastCode string
CreatedAt time.Time
}
@ -95,6 +97,7 @@ type CreateBet struct {
IsShopBet bool
CashoutID string
OutcomesHash string
FastCode string
}
type CreateBetOutcomeReq struct {

View File

@ -15,3 +15,12 @@ func GenerateOTP() string {
num := 100000 + rand.UintN(899999)
return fmt.Sprintf("%d", num) // 6 digit random number [100,000 - 999,999]
}
func GenerateFastCode() string {
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var code string
for i := 0; i < 6; i++ {
code += string(letters[rand.UintN(uint(len(letters)))])
}
return code
}

View File

@ -135,6 +135,7 @@ func convertCreateBet(bet domain.CreateBet) dbgen.CreateBetParams {
},
IsShopBet: bet.IsShopBet,
CashoutID: bet.CashoutID,
FastCode: bet.FastCode,
}
}

View File

@ -17,6 +17,7 @@ import (
"time"
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
"github.com/SamuelTariku/FortuneBet-Backend/internal/pkgs/helpers"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/branch"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/event"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/odds"
@ -207,7 +208,6 @@ func (s *Service) PlaceBet(ctx context.Context, req domain.CreateBetReq, userID
var totalOdds float32 = 1
for _, outcomeReq := range req.Outcomes {
fmt.Println("reqq: ", outcomeReq)
newOutcome, err := s.GenerateBetOutcome(ctx, outcomeReq.EventID, outcomeReq.MarketID, outcomeReq.OddID)
if err != nil {
s.mongoLogger.Error("failed to generate outcome",
@ -249,6 +249,8 @@ func (s *Service) PlaceBet(ctx context.Context, req domain.CreateBetReq, userID
return domain.CreateBetRes{}, err
}
fastCode := helpers.GenerateFastCode()
newBet := domain.CreateBet{
Amount: domain.ToCurrency(req.Amount),
TotalOdds: totalOdds,
@ -257,6 +259,7 @@ func (s *Service) PlaceBet(ctx context.Context, req domain.CreateBetReq, userID
PhoneNumber: req.PhoneNumber,
CashoutID: cashoutID,
OutcomesHash: outcomesHash,
FastCode: fastCode,
}
switch role {
@ -385,7 +388,6 @@ func (s *Service) PlaceBet(ctx context.Context, req domain.CreateBetReq, userID
newBet.UserID = domain.ValidInt64{Value: userID, Valid: true}
newBet.IsShopBet = false
default:
s.mongoLogger.Error("unknown role type",
zap.String("role", string(role)),
@ -394,7 +396,6 @@ func (s *Service) PlaceBet(ctx context.Context, req domain.CreateBetReq, userID
return domain.CreateBetRes{}, fmt.Errorf("Unknown Role Type")
}
fmt.Println("Bet is: ", newBet)
bet, err := s.CreateBet(ctx, newBet)
if err != nil {
s.mongoLogger.Error("failed to create bet",