(client: TelegramClient, lottery: any, winners: any[])
| 524 | for (const winner of winners) { |
| 525 | // Get available prize from warehouse |
| 526 | const prize = getNextAvailablePrize(lottery.prize_warehouse || "default"); |
| 527 | const prizeText = prize ? prize.prize_text : "恭喜中奖!"; |
| 528 | |
| 529 | const now = Date.now(); |
| 530 | const expiresAt = now + (lottery.claim_timeout * 1000); |
| 531 | |
| 532 | // Insert winner record using standard schema |
| 533 | const stmt = db.prepare(` |
| 534 | INSERT INTO lottery_winners (lottery_id, user_id, username, first_name, last_name, prize_text, status, assigned_at, expires_at) |
| 535 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 536 | `); |
| 537 | |
| 538 | stmt.run( |
| 539 | lottery.id, |
| 540 | String(winner.user_id), |
| 541 | winner.username || null, |
| 542 | winner.first_name || null, |
| 543 | winner.last_name || null, |
| 544 | prizeText, |
| 545 | PrizeStatus.PENDING, |
| 546 | now, |
| 547 | expiresAt |
| 548 | ); |
| 549 | |
| 550 | // Consume prize stock if from warehouse |
| 551 | if (prize) { |
| 552 | consumePrize(prize.id); |
| 553 | } |
| 554 | } |
| 555 | }); |
| 556 | |
| 557 | transaction(); |
| 558 | |
| 559 | // Send prizes after transaction completes (if auto-send mode) |
| 560 | if (lottery.distribution_mode === DistributionMode.AUTO_SEND) { |
| 561 | for (const winner of winners) { |
| 562 | const winnerRecord = getLotteryWinners(lottery.id).find(w => w.user_id === winner.user_id); |
| 563 | if (winnerRecord) { |
| 564 | await sendPrizeToWinner(client, winner, winnerRecord.prize_text, lottery); |
| 565 | await new Promise(resolve => setTimeout(resolve, 1000)); // Rate limiting |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | } catch (error) { |
| 570 | console.error("Failed to distribute prizes:", error); |
| 571 | throw error; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | // Send prize message to winner |
| 576 | async function sendPrizeToWinner(client: TelegramClient, winner: any, prizeText: string, lottery: any): Promise<boolean> { |
| 577 | try { |
| 578 | const displayName = winner.username ? `@${winner.username}` : |
| 579 | (winner.first_name || winner.last_name || `用户 ${winner.user_id}`); |
| 580 | |
| 581 | const prizeMessage = |
| 582 | `🎉 <b>恭喜中奖!</b>\n\n` + |
| 583 | `🏆 <b>活动名称:</b> ${htmlEscape(lottery.title)}\n` + |
no test coverage detected