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