diff --git a/convex/_model/users/_helpers/checkUserTournamentRelationship.ts b/convex/_model/users/_helpers/checkUserTournamentRelationship.ts index 041ba88d..6484cc21 100644 --- a/convex/_model/users/_helpers/checkUserTournamentRelationship.ts +++ b/convex/_model/users/_helpers/checkUserTournamentRelationship.ts @@ -10,20 +10,35 @@ export const checkUserTournamentRelationship = async ( return false; } - const userARegistrations = await ctx.db.query('tournamentRegistrations') - .withIndex('by_user', (q) => q.eq('userId', userIdA)) - .collect(); - const userBRegistrations = await ctx.db.query('tournamentRegistrations') - .withIndex('by_user', (q) => q.eq('userId', userIdB)) - .collect(); + const [ + userARegistrations, + userAOrganizers, + userBRegistrations, + userBOrganizers, + ] = await Promise.all([ + ctx.db.query('tournamentRegistrations') + .withIndex('by_user', (q) => q.eq('userId', userIdA)) + .collect(), + ctx.db.query('tournamentOrganizers') + .withIndex('by_user', (q) => q.eq('userId', userIdA)) + .collect(), + ctx.db.query('tournamentRegistrations') + .withIndex('by_user', (q) => q.eq('userId', userIdB)) + .collect(), + ctx.db.query('tournamentOrganizers') + .withIndex('by_user', (q) => q.eq('userId', userIdB)) + .collect(), + ]); const userATournamentIds = [ ...userARegistrations.map((r) => r.tournamentId), + ...userAOrganizers.map((o) => o.tournamentId), ]; - const userBTournamentIds = [ + const userBTournamentIds = new Set>([ ...userBRegistrations.map((r) => r.tournamentId), - ]; + ...userBOrganizers.map((o) => o.tournamentId), + ]); - return userATournamentIds.some((id) => new Set(userBTournamentIds).has(id)); + return userATournamentIds.some((id) => userBTournamentIds.has(id)); };