Stack of changes to get gin, scs, nosurf running.
This commit is contained in:
@@ -5,16 +5,15 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
lotteryTicketHandlers "synlotto-website/internal/handlers/lottery/tickets"
|
||||
thunderballrules "synlotto-website/internal/rules/thunderball"
|
||||
services "synlotto-website/internal/services/draws"
|
||||
lotteryTicketService "synlotto-website/internal/services/tickets"
|
||||
drawsSvc "synlotto-website/internal/services/draws"
|
||||
|
||||
"synlotto-website/internal/helpers"
|
||||
"synlotto-website/internal/models"
|
||||
)
|
||||
|
||||
// ToDo: SQL in here needs to me moved out the handler!
|
||||
// RunTicketMatching finds unmatched tickets, matches them to draw results,
|
||||
// updates match/prize fields, and writes a summary log entry.
|
||||
func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, error) {
|
||||
stats := models.MatchRunStats{}
|
||||
|
||||
@@ -31,11 +30,9 @@ func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, er
|
||||
defer rows.Close()
|
||||
|
||||
var pending []models.Ticket
|
||||
|
||||
for rows.Next() {
|
||||
var t models.Ticket
|
||||
var b1, b2, b3, b4, b5, b6, bo1, bo2 sql.NullInt64
|
||||
|
||||
if err := rows.Scan(
|
||||
&t.Id, &t.GameType, &t.DrawDate,
|
||||
&b1, &b2, &b3, &b4, &b5, &b6,
|
||||
@@ -43,7 +40,6 @@ func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, er
|
||||
); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
t.Ball1 = int(b1.Int64)
|
||||
t.Ball2 = int(b2.Int64)
|
||||
t.Ball3 = int(b3.Int64)
|
||||
@@ -58,32 +54,32 @@ func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, er
|
||||
|
||||
for _, t := range pending {
|
||||
matchTicket := models.MatchTicket{
|
||||
ID: t.Id,
|
||||
GameType: t.GameType,
|
||||
DrawDate: t.DrawDate,
|
||||
Balls: helpers.BuildBallsSlice(t),
|
||||
BonusBalls: helpers.BuildBonusSlice(t),
|
||||
}
|
||||
|
||||
draw := services.GetDrawResultForTicket(db, t.GameType, t.DrawDate)
|
||||
result := lotteryTicketHandlers.MatchTicketToDraw(matchTicket, draw, thunderballrules.ThunderballPrizeRules)
|
||||
|
||||
if result.MatchedDrawID == 0 {
|
||||
draw := drawsSvc.GetDrawResultForTicket(db, t.GameType, t.DrawDate)
|
||||
if draw.DrawID == 0 {
|
||||
// No draw yet → skip
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := db.Exec(`
|
||||
mainMatches := helpers.CountMatches(matchTicket.Balls, draw.Balls)
|
||||
bonusMatches := helpers.CountMatches(matchTicket.BonusBalls, draw.BonusBalls)
|
||||
prizeTier := GetPrizeTier(matchTicket.GameType, mainMatches, bonusMatches, thunderballrules.ThunderballPrizeRules)
|
||||
isWinner := prizeTier != ""
|
||||
|
||||
if _, err := db.Exec(`
|
||||
UPDATE my_tickets
|
||||
SET matched_main = ?, matched_bonus = ?, prize_tier = ?, is_winner = ?
|
||||
WHERE id = ?
|
||||
`, result.MatchedMain, result.MatchedBonus, result.PrizeTier, result.IsWinner, t.Id)
|
||||
if err != nil {
|
||||
`, mainMatches, bonusMatches, prizeTier, isWinner, t.Id); err != nil {
|
||||
log.Println("⚠️ Failed to update ticket match:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
stats.TicketsMatched++
|
||||
if result.IsWinner {
|
||||
if isWinner {
|
||||
stats.WinnersFound++
|
||||
}
|
||||
}
|
||||
@@ -96,6 +92,7 @@ func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, er
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// UpdateMissingPrizes fills in prize labels/amounts for already-matched winners that lack labels.
|
||||
func UpdateMissingPrizes(db *sql.DB) error {
|
||||
type TicketInfo struct {
|
||||
ID int
|
||||
@@ -140,8 +137,7 @@ func UpdateMissingPrizes(db *sql.DB) error {
|
||||
query := fmt.Sprintf(`SELECT prize%d_per_winner FROM prizes_thunderball WHERE draw_date = ?`, idx)
|
||||
|
||||
var amount int
|
||||
err := db.QueryRow(query, t.DrawDate).Scan(&amount)
|
||||
if err != nil {
|
||||
if err := db.QueryRow(query, t.DrawDate).Scan(&amount); err != nil {
|
||||
log.Printf("❌ Prize lookup failed for ticket %d: %v", t.ID, err)
|
||||
continue
|
||||
}
|
||||
@@ -151,11 +147,9 @@ func UpdateMissingPrizes(db *sql.DB) error {
|
||||
label = fmt.Sprintf("£%.2f", float64(amount))
|
||||
}
|
||||
|
||||
_, err = db.Exec(`
|
||||
if _, err := db.Exec(`
|
||||
UPDATE my_tickets SET prize_amount = ?, prize_label = ? WHERE id = ?
|
||||
`, float64(amount), label, t.ID)
|
||||
|
||||
if err != nil {
|
||||
`, float64(amount), label, t.ID); err != nil {
|
||||
log.Printf("❌ Failed to update ticket %d: %v", t.ID, err)
|
||||
} else {
|
||||
log.Printf("✅ Updated ticket %d → %s", t.ID, label)
|
||||
@@ -165,6 +159,7 @@ func UpdateMissingPrizes(db *sql.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RefreshTicketPrizes recomputes and writes prize info for all tickets.
|
||||
func RefreshTicketPrizes(db *sql.DB) error {
|
||||
type TicketRow struct {
|
||||
ID int
|
||||
@@ -200,13 +195,11 @@ func RefreshTicketPrizes(db *sql.DB) error {
|
||||
|
||||
for _, row := range tickets {
|
||||
matchTicket := models.MatchTicket{
|
||||
GameType: row.GameType,
|
||||
DrawDate: row.DrawDate,
|
||||
Balls: helpers.BuildBallsFromNulls(row.B1, row.B2, row.B3, row.B4, row.B5, row.B6),
|
||||
BonusBalls: helpers.BuildBonusFromNulls(row.Bonus1, row.Bonus2),
|
||||
}
|
||||
|
||||
draw := services.GetDrawResultForTicket(db, row.GameType, row.DrawDate)
|
||||
draw := drawsSvc.GetDrawResultForTicket(db, row.GameType, row.DrawDate)
|
||||
if draw.DrawID == 0 {
|
||||
log.Printf("❌ No draw result for %s (%s)", row.DrawDate, row.GameType)
|
||||
continue
|
||||
@@ -214,19 +207,16 @@ func RefreshTicketPrizes(db *sql.DB) error {
|
||||
|
||||
mainMatches := helpers.CountMatches(matchTicket.Balls, draw.Balls)
|
||||
bonusMatches := helpers.CountMatches(matchTicket.BonusBalls, draw.BonusBalls)
|
||||
// ToDo: this isn't a lottery ticket service really its a draw one
|
||||
prizeTier := lotteryTicketService.GetPrizeTier(row.GameType, mainMatches, bonusMatches, thunderballrules.ThunderballPrizeRules)
|
||||
prizeTier := GetPrizeTier(row.GameType, mainMatches, bonusMatches, thunderballrules.ThunderballPrizeRules)
|
||||
isWinner := prizeTier != ""
|
||||
|
||||
var label string
|
||||
var amount float64
|
||||
if row.GameType == "Thunderball" {
|
||||
idx, ok := thunderballrules.GetThunderballPrizeIndex(mainMatches, bonusMatches)
|
||||
if ok {
|
||||
if idx, ok := thunderballrules.GetThunderballPrizeIndex(mainMatches, bonusMatches); ok {
|
||||
query := fmt.Sprintf(`SELECT prize%d_per_winner FROM prizes_thunderball WHERE draw_date = ?`, idx)
|
||||
var val int
|
||||
err := db.QueryRow(query, row.DrawDate).Scan(&val)
|
||||
if err == nil {
|
||||
if err := db.QueryRow(query, row.DrawDate).Scan(&val); err == nil {
|
||||
amount = float64(val)
|
||||
if val > 0 {
|
||||
label = fmt.Sprintf("£%.2f", amount)
|
||||
@@ -245,15 +235,15 @@ func RefreshTicketPrizes(db *sql.DB) error {
|
||||
SET matched_main = ?, matched_bonus = ?, prize_tier = ?, is_winner = ?, prize_amount = ?, prize_label = ?
|
||||
WHERE id = ?
|
||||
`, mainMatches, bonusMatches, prizeTier, isWinner, amount, label, row.ID)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("❌ Failed to update ticket %d: %v", row.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
rowsAffected, _ := res.RowsAffected()
|
||||
log.Printf("✅ Ticket %d updated — rows affected: %d | Tier: %s | Label: %s | Matches: %d+%d",
|
||||
row.ID, rowsAffected, prizeTier, label, mainMatches, bonusMatches)
|
||||
if rowsAffected, _ := res.RowsAffected(); rowsAffected > 0 {
|
||||
log.Printf("✅ Ticket %d updated — rows affected: %d | Tier: %s | Label: %s | Matches: %d+%d",
|
||||
row.ID, rowsAffected, prizeTier, label, mainMatches, bonusMatches)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user