(guildId, userId)
| 532 | } |
| 533 | |
| 534 | export async function getOpenTicketCountForUser(guildId, userId) { |
| 535 | try { |
| 536 | if (!db.initialized) { |
| 537 | await db.initialize(); |
| 538 | } |
| 539 | |
| 540 | if (db.db?.pool && typeof db.db.isAvailable === 'function' && db.db.isAvailable()) { |
| 541 | const { pgConfig } = await import('../config/postgres.js'); |
| 542 | const result = await db.db.pool.query( |
| 543 | `SELECT COUNT(*)::int AS count FROM ${pgConfig.tables.tickets} |
| 544 | WHERE guild_id = $1 |
| 545 | AND data->>'userId' = $2 |
| 546 | AND data->>'status' = 'open'`, |
| 547 | [guildId, userId] |
| 548 | ); |
| 549 | |
| 550 | return Number(result.rows?.[0]?.count || 0); |
| 551 | } |
| 552 | |
| 553 | if (typeof db.list === 'function') { |
| 554 | const ticketKeys = await db.list(`guild:${guildId}:ticket:`); |
| 555 | let count = 0; |
| 556 | |
| 557 | for (const key of ticketKeys) { |
| 558 | const ticket = await getFromDb(key, null); |
| 559 | if (ticket && ticket.userId === userId && ticket.status === 'open') { |
| 560 | count += 1; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return count; |
| 565 | } |
| 566 | |
| 567 | return 0; |
| 568 | } catch (error) { |
| 569 | logger.error(`Error counting open tickets for user ${userId} in guild ${guildId}:`, error); |
| 570 | return 0; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | export async function saveTicketData(guildId, channelId, data) { |
| 575 | if (!db.initialized) { |
no test coverage detected