Yimaru-BackEnd/internal/domain/bet.go
Samuel Tariku c00110a503 feat: Enhance league, odds, events and bets functionality
- Updated league handling to ensure valid page size checks and improved error handling for sport ID parsing.
- Introduced new endpoint to update global league settings with comprehensive validation and error logging.
- Refactored odds settings management, including saving, removing, and updating odds settings with enhanced validation.
- Added tenant slug retrieval by token, ensuring proper user and company validation.
- Improved middleware to check for active company status and adjusted route permissions for various endpoints.
- Added SQL script to fix auto-increment desynchronization across multiple tables.
2025-10-05 23:45:31 +03:00

352 lines
11 KiB
Go

package domain
import (
"time"
dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db"
"github.com/jackc/pgx/v5/pgtype"
)
// The Odd ID here is not the odd id from our database
// but the raw_odd_id from the betapi.from within the raw_odds json
// This can be refactor later
type BetOutcome struct {
ID int64 `json:"id" example:"1"`
BetID int64 `json:"bet_id" example:"1"`
EventID int64 `json:"event_id" example:"1"`
OddID int64 `json:"odd_id" example:"1"`
SportID int64 `json:"sport_id" example:"1"`
HomeTeamName string `json:"home_team_name" example:"Manchester"`
AwayTeamName string `json:"away_team_name" example:"Liverpool"`
MarketID int64 `json:"market_id" example:"1"`
MarketName string `json:"market_name" example:"Fulltime Result"`
Odd float32 `json:"odd" example:"1.5"`
OddName string `json:"odd_name" example:"1"`
OddHeader string `json:"odd_header" example:"1"`
OddHandicap string `json:"odd_handicap" example:"1"`
Status OutcomeStatus `json:"status" example:"1"`
Expires time.Time `json:"expires" example:"2025-04-08T12:00:00Z"`
}
type CreateBetOutcome struct {
BetID int64 `json:"bet_id" example:"1"`
EventID int64 `json:"event_id" example:"1"`
OddID int64 `json:"odd_id" example:"1"`
SportID int64 `json:"sport_id" example:"1"`
HomeTeamName string `json:"home_team_name" example:"Manchester"`
AwayTeamName string `json:"away_team_name" example:"Liverpool"`
MarketID int64 `json:"market_id" example:"1"`
MarketName string `json:"market_name" example:"Fulltime Result"`
Odd float32 `json:"odd" example:"1.5"`
OddName string `json:"odd_name" example:"1"`
OddHeader string `json:"odd_header" example:"1"`
OddHandicap string `json:"odd_handicap" example:"1"`
Expires time.Time `json:"expires" example:"2025-04-08T12:00:00Z"`
}
// If it is a ShopBet then UserID and Fullname will be the cashier
// If it is a DigitalBet then UserID and Fullname will be the user
type Bet struct {
ID int64
Amount Currency
TotalOdds float32
Status OutcomeStatus
UserID int64
CompanyID int64
IsShopBet bool
CashedOut bool
FastCode string
CreatedAt time.Time
}
type BetFilter struct {
Status ValidOutcomeStatus
UserID ValidInt64
CompanyID ValidInt64
CashedOut ValidBool
IsShopBet ValidBool
Query ValidString
CreatedBefore ValidTime
CreatedAfter ValidTime
Limit ValidInt32
Offset ValidInt32
}
type Flag struct {
ID int64
BetID int64
OddID int64
Reason string
FlaggedAt time.Time
Resolved bool
}
type GetBet struct {
ID int64
Amount Currency
TotalOdds float32
Status OutcomeStatus
FullName string
PhoneNumber string
UserID int64
CompanyID int64
IsShopBet bool
CashedOut bool
Outcomes []BetOutcome
FastCode string
CreatedAt time.Time
}
type CreateBet struct {
Amount Currency
TotalOdds float32
Status OutcomeStatus
UserID int64
CompanyID int64
IsShopBet bool
OutcomesHash string
FastCode string
}
type CreateBetOutcomeReq struct {
EventID int64 `json:"event_id" example:"1"`
OddID int64 `json:"odd_id" example:"1"`
MarketID int64 `json:"market_id" example:"1"`
}
type CreateBetReq struct {
Outcomes []CreateBetOutcomeReq `json:"outcomes" validate:"required"`
Amount float32 `json:"amount" validate:"required,gt=0" example:"100.0"`
BranchID *int64 `json:"branch_id,omitempty" example:"1"`
}
type CreateBetWithFastCodeReq struct {
FastCode string `json:"fast_code"`
Amount float32 `json:"amount"`
BranchID *int64 `json:"branch_id"`
}
type CreateFlagReq struct {
BetID int64
OddID int64
Reason string
}
type RandomBetReq struct {
BranchID int64 `json:"branch_id" validate:"required" example:"1"`
NumberOfBets int64 `json:"number_of_bets" validate:"required" example:"1"`
}
type CreateBetRes struct {
ID int64 `json:"id" example:"1"`
Amount float32 `json:"amount" example:"100.0"`
TotalOdds float32 `json:"total_odds" example:"4.22"`
Status OutcomeStatus `json:"status" example:"1"`
UserID int64 `json:"user_id" example:"2"`
CompanyID int64 `json:"company_id" example:"1"`
IsShopBet bool `json:"is_shop_bet" example:"false"`
CreatedNumber int64 `json:"created_number" example:"2"`
FastCode string `json:"fast_code"`
}
type BetRes struct {
ID int64 `json:"id" example:"1"`
Outcomes []BetOutcome `json:"outcomes"`
Amount float32 `json:"amount" example:"100.0"`
TotalOdds float32 `json:"total_odds" example:"4.22"`
Status OutcomeStatus `json:"status" example:"1"`
Fullname string `json:"full_name" example:"John Smith"`
UserID int64 `json:"user_id" example:"2"`
CompanyID int64 `json:"company_id" example:"1"`
IsShopBet bool `json:"is_shop_bet" example:"false"`
CashedOut bool `json:"cashed_out" example:"false"`
CreatedAt time.Time `json:"created_at" example:"2025-04-08T12:00:00Z"`
FastCode string `json:"fast_code"`
}
type BetOutcomeViewRes struct {
ID int64 `json:"id"`
BetID int64 `json:"bet_id"`
SportID int64 `json:"sport_id"`
EventID int64 `json:"event_id"`
OddID int64 `json:"odd_id"`
HomeTeamName string `json:"home_team_name"`
AwayTeamName string `json:"away_team_name"`
MarketID int64 `json:"market_id"`
MarketName string `json:"market_name"`
Odd float32 `json:"odd"`
OddName string `json:"odd_name"`
OddHeader string `json:"odd_header"`
OddHandicap string `json:"odd_handicap"`
Status OutcomeStatus `json:"status"`
Expires time.Time `json:"expires"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Amount int64 `json:"amount"`
TotalOdds float32 `json:"total_odds"`
}
type BetOutcomeViewFilter struct {
OutcomeStatus ValidOutcomeStatus
CompanyID ValidInt64
Limit ValidInt32
Offset ValidInt32
}
func ConvertCreateBetRes(bet Bet, createdNumber int64) CreateBetRes {
return CreateBetRes{
ID: bet.ID,
Amount: bet.Amount.Float32(),
TotalOdds: bet.TotalOdds,
Status: bet.Status,
UserID: bet.UserID,
CompanyID: bet.CompanyID,
CreatedNumber: createdNumber,
IsShopBet: bet.IsShopBet,
FastCode: bet.FastCode,
}
}
func ConvertBet(bet GetBet) BetRes {
return BetRes{
ID: bet.ID,
Amount: bet.Amount.Float32(),
TotalOdds: bet.TotalOdds,
Status: bet.Status,
Fullname: bet.FullName,
UserID: bet.UserID,
CompanyID: bet.CompanyID,
Outcomes: bet.Outcomes,
IsShopBet: bet.IsShopBet,
CashedOut: bet.CashedOut,
CreatedAt: bet.CreatedAt,
FastCode: bet.FastCode,
}
}
func ConvertDBBet(bet dbgen.Bet) Bet {
return Bet{
ID: bet.ID,
Amount: Currency(bet.Amount),
TotalOdds: bet.TotalOdds,
Status: OutcomeStatus(bet.Status),
UserID: bet.UserID,
CompanyID: bet.CompanyID,
IsShopBet: bet.IsShopBet,
CashedOut: bet.CashedOut,
FastCode: bet.FastCode,
CreatedAt: bet.CreatedAt.Time,
}
}
func ConvertDBBetOutcomes(outcome dbgen.BetOutcome) BetOutcome {
return BetOutcome{
ID: outcome.ID,
BetID: outcome.BetID,
SportID: outcome.SportID,
EventID: outcome.EventID,
OddID: outcome.OddID,
HomeTeamName: outcome.HomeTeamName,
AwayTeamName: outcome.AwayTeamName,
MarketID: outcome.MarketID,
MarketName: outcome.MarketName,
Odd: outcome.Odd,
OddName: outcome.OddName,
OddHeader: outcome.OddHeader,
OddHandicap: outcome.OddHandicap,
Status: OutcomeStatus(outcome.Status),
Expires: outcome.Expires.Time,
}
}
func ConvertDBBetOutcomesView(outcome dbgen.GetBetOutcomeViewByEventIDRow) BetOutcomeViewRes {
return BetOutcomeViewRes{
ID: outcome.ID,
BetID: outcome.BetID,
SportID: outcome.SportID,
EventID: outcome.EventID,
OddID: outcome.OddID,
HomeTeamName: outcome.HomeTeamName,
AwayTeamName: outcome.AwayTeamName,
MarketID: outcome.MarketID,
MarketName: outcome.MarketName,
Odd: outcome.Odd,
OddName: outcome.OddName,
OddHeader: outcome.OddHeader,
OddHandicap: outcome.OddHandicap,
Status: OutcomeStatus(outcome.Status),
Expires: outcome.Expires.Time,
FirstName: outcome.FirstName,
LastName: outcome.LastName,
Amount: outcome.Amount,
TotalOdds: outcome.TotalOdds,
}
}
func ConvertDBBetWithOutcomes(bet dbgen.BetWithOutcome) GetBet {
var outcomes []BetOutcome = make([]BetOutcome, 0, len(bet.Outcomes))
for _, outcome := range bet.Outcomes {
outcomes = append(outcomes, ConvertDBBetOutcomes(outcome))
}
return GetBet{
ID: bet.ID,
CompanyID: bet.CompanyID,
Amount: Currency(bet.Amount),
TotalOdds: bet.TotalOdds,
Status: OutcomeStatus(bet.Status),
FullName: bet.FullName.(string),
PhoneNumber: bet.PhoneNumber.String,
UserID: bet.UserID,
IsShopBet: bet.IsShopBet,
CashedOut: bet.CashedOut,
Outcomes: outcomes,
FastCode: bet.FastCode,
CreatedAt: bet.CreatedAt.Time,
}
}
func ConvertDBFlag(flag dbgen.Flag) Flag {
return Flag{
ID: flag.ID,
BetID: flag.BetID.Int64,
OddID: flag.OddsMarketID.Int64,
Reason: flag.Reason.String,
FlaggedAt: flag.FlaggedAt.Time,
Resolved: flag.Resolved.Bool,
}
}
func ConvertDBCreateBetOutcome(betOutcome CreateBetOutcome) dbgen.CreateBetOutcomeParams {
return dbgen.CreateBetOutcomeParams{
BetID: betOutcome.BetID,
EventID: betOutcome.EventID,
SportID: betOutcome.SportID,
OddID: betOutcome.OddID,
HomeTeamName: betOutcome.HomeTeamName,
AwayTeamName: betOutcome.AwayTeamName,
MarketID: betOutcome.MarketID,
MarketName: betOutcome.MarketName,
Odd: betOutcome.Odd,
OddName: betOutcome.OddName,
OddHeader: betOutcome.OddHeader,
OddHandicap: betOutcome.OddHandicap,
Expires: pgtype.Timestamp{
Time: betOutcome.Expires,
Valid: true,
},
}
}
func ConvertCreateBet(bet CreateBet) dbgen.CreateBetParams {
return dbgen.CreateBetParams{
Amount: int64(bet.Amount),
TotalOdds: bet.TotalOdds,
Status: int32(bet.Status),
UserID: bet.UserID,
CompanyID: bet.CompanyID,
IsShopBet: bet.IsShopBet,
OutcomesHash: bet.OutcomesHash,
FastCode: bet.FastCode,
}
}