Revert "fix backend"

This reverts commit afa8782eb2.
This commit is contained in:
Aditya Siregar
2025-09-16 19:31:14 +07:00
parent afa8782eb2
commit b37d21d366
4 changed files with 61 additions and 164 deletions
+47 -25
View File
@@ -102,42 +102,64 @@ func (r *VoucherRepository) GetRandomVouchersByRows(ctx context.Context, rows in
var allVouchers []entities.Voucher
var err error
// Get all non-winner vouchers (regardless of winner_number)
nonWinnerQuery := r.db.WithContext(ctx).Where("is_winner = ?", false)
err = nonWinnerQuery.Find(&allVouchers).Error
if err != nil {
return nil, err
}
// If winner_number is provided, also include vouchers with that specific winner_number
// (even if they're already winners, to have a pool to select from)
// First, try to get vouchers based on winner_number parameter
if winnerNumber != nil {
var winnerNumberVouchers []entities.Voucher
winnerQuery := r.db.WithContext(ctx).Where("winner_number = ?", *winnerNumber)
err = winnerQuery.Find(&winnerNumberVouchers).Error
// If winner_number is provided, filter by it and exclude already won vouchers
query := r.db.WithContext(ctx).Where("winner_number = ? AND is_winner = ?", *winnerNumber, false)
err = query.Find(&allVouchers).Error
if err != nil {
return nil, err
}
// Merge the two lists, avoiding duplicates
existingIDs := make(map[int64]bool)
for _, voucher := range allVouchers {
existingIDs[voucher.ID] = true
}
for _, voucher := range winnerNumberVouchers {
if !existingIDs[voucher.ID] {
allVouchers = append(allVouchers, voucher)
// If no vouchers found for the specified winner_number, fallback to winner_number = 0
if len(allVouchers) == 0 {
fallbackQuery := r.db.WithContext(ctx).Where("winner_number = ? AND is_winner = ?", 0, false)
err = fallbackQuery.Find(&allVouchers).Error
if err != nil {
return nil, err
}
}
}
// If still no vouchers found, get any vouchers available as fallback
if len(allVouchers) == 0 {
err = r.db.WithContext(ctx).Find(&allVouchers).Error
// If still no vouchers found, try without is_winner filter for winner_number = 0
if len(allVouchers) == 0 {
fallbackQuery2 := r.db.WithContext(ctx).Where("winner_number = ?", 0)
err = fallbackQuery2.Find(&allVouchers).Error
if err != nil {
return nil, err
}
}
// If still no vouchers found, get any vouchers available
if len(allVouchers) == 0 {
err = r.db.WithContext(ctx).Find(&allVouchers).Error
if err != nil {
return nil, err
}
}
} else {
// If winner_number is not provided, use default winner_number = 0 and exclude already won vouchers
query := r.db.WithContext(ctx).Where("winner_number = ? AND is_winner = ?", 0, false)
err = query.Find(&allVouchers).Error
if err != nil {
return nil, err
}
// If no vouchers found, try without is_winner filter for winner_number = 0
if len(allVouchers) == 0 {
fallbackQuery := r.db.WithContext(ctx).Where("winner_number = ?", 0)
err = fallbackQuery.Find(&allVouchers).Error
if err != nil {
return nil, err
}
}
// If still no vouchers found, get any vouchers available
if len(allVouchers) == 0 {
err = r.db.WithContext(ctx).Find(&allVouchers).Error
if err != nil {
return nil, err
}
}
}
if len(allVouchers) == 0 {