(lotteryId: number, participant: any)
| 359 | // Check current participant count |
| 360 | const countStmt = db.prepare(`SELECT COUNT(*) as count FROM lottery_participants WHERE lottery_id = ?`); |
| 361 | const countResult = countStmt.get(lotteryId) as { count: number } | undefined; |
| 362 | const currentCount = countResult?.count || 0; |
| 363 | |
| 364 | // Check lottery max participants |
| 365 | const lotteryStmt = db.prepare(`SELECT max_participants FROM lottery_config WHERE id = ?`); |
| 366 | const lottery = lotteryStmt.get(lotteryId) as { max_participants: number } | undefined; |
| 367 | |
| 368 | if (!lottery || currentCount >= lottery.max_participants) { |
| 369 | throw new Error("Lottery full or not found"); |
| 370 | } |
| 371 | |
| 372 | // Check if user already participated |
| 373 | const existsStmt = db.prepare(`SELECT 1 FROM lottery_participants WHERE lottery_id = ? AND user_id = ?`); |
| 374 | const exists = existsStmt.get(lotteryId, participant.user_id); |
| 375 | |
| 376 | if (exists) { |
| 377 | throw new Error("User already participated"); |
| 378 | } |
| 379 | |
| 380 | // Add participant |
| 381 | const insertStmt = db.prepare(` |
| 382 | INSERT INTO lottery_participants (lottery_id, user_id, username, first_name, last_name, joined_at) |
| 383 | VALUES (?, ?, ?, ?, ?, ?) |
| 384 | `); |
| 385 | insertStmt.run(lotteryId, String(participant.user_id), participant.username || null, participant.first_name || null, participant.last_name || null, Date.now()); |
| 386 | }); |
| 387 | |
| 388 | transaction(); |
| 389 | return true; |
| 390 | } catch (error) { |
| 391 | console.error("Failed to add participant:", error); |
| 392 | return false; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | function getLotteryParticipants(lotteryId: number): any[] { |
| 397 | if (!db) return []; |
| 398 | |
| 399 | const stmt = db.prepare(` |
| 400 | SELECT * FROM lottery_participants |
| 401 | WHERE lottery_id = ? ORDER BY joined_at |
| 402 | `); |
| 403 | return stmt.all(lotteryId); |
| 404 | } |
| 405 |
no test coverage detected