(client)
| 434 | } |
| 435 | |
| 436 | export async function getEndedGiveaways(client) { |
| 437 | try { |
| 438 | const wrapper = client?.db; |
| 439 | if (!wrapper || typeof wrapper.get !== 'function') { |
| 440 | return []; |
| 441 | } |
| 442 | |
| 443 | if (isPostgresSqlReady(wrapper)) { |
| 444 | const { pgConfig } = await import('../config/postgres.js'); |
| 445 | |
| 446 | const result = await wrapper.db.pool.query( |
| 447 | `SELECT id, guild_id, message_id, data, ends_at |
| 448 | FROM ${pgConfig.tables.giveaways} |
| 449 | WHERE ends_at <= NOW() |
| 450 | AND COALESCE((data->>'ended')::boolean, false) = false |
| 451 | ORDER BY ends_at ASC`, |
| 452 | ); |
| 453 | |
| 454 | return result.rows || []; |
| 455 | } |
| 456 | |
| 457 | if (wrapper.isDegraded?.()) { |
| 458 | logger.debug('Postgres SQL unavailable for ended giveaways; scanning key-value store'); |
| 459 | } |
| 460 | |
| 461 | return await getEndedGiveawaysFromKv(client); |
| 462 | } catch (error) { |
| 463 | logger.error('Error getting ended giveaways:', error); |
| 464 | try { |
| 465 | return await getEndedGiveawaysFromKv(client); |
| 466 | } catch { |
| 467 | return []; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | export async function markGiveawayEnded(client, giveawayId, endedData) { |
| 473 | try { |
no test coverage detected