Skip to content

Commit

Permalink
index on the admin table
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutoshbind15 committed Oct 18, 2024
1 parent 22cd75c commit 25cb787
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
1 change: 0 additions & 1 deletion app/play/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ export default function PlayPage() {

<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
{filteredMaps.map((map) => (
<Card key={map._id} className="flex flex-col h-full">
<Card
key={map._id}
className={cn(
Expand Down
16 changes: 10 additions & 6 deletions convex/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,12 @@ export const getMaps = query({
return null;
}

const admins = await ctx.db.query("admins").collect();
const isAdmin = admins.some((admin) => admin.userId === userId);
const admin = await ctx.db
.query("admins")
.withIndex("by_userId", (q) => q.eq("userId", userId))
.first();

if (isAdmin) {
if (admin) {
return await ctx.db
.query("maps")
.filter((q) => q.eq(q.field("isReviewed"), args.isReviewed))
Expand Down Expand Up @@ -250,10 +252,12 @@ export const approveMap = mutation({
throw new Error("Unauthorized");
}

const admins = await ctx.db.query("admins").collect();
const isAdmin = admins.some((admin) => admin.userId === userId);
const admin = await ctx.db
.query("admins")
.withIndex("by_userId", (q) => q.eq("userId", userId))
.first();

if (!isAdmin) {
if (!admin) {
throw new Error("Unauthorized");
} else {
const maps = await ctx.db.query("maps").collect();
Expand Down
2 changes: 1 addition & 1 deletion convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ export default defineSchema({
}).index("by_mapId_userId", ["mapId", "userId"]),
admins: defineTable({
userId: v.id("users"),
}),
}).index("by_userId", ["userId"]),
});
7 changes: 5 additions & 2 deletions convex/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ export const isAdmin = query({
return false;
}

const admins = await ctx.db.query("admins").collect();
const admin = await ctx.db
.query("admins")
.withIndex("by_userId", (q) => q.eq("userId", userId))
.first();

return admins.some((admin) => admin.userId === userId);
return !!admin;
},
});

0 comments on commit 25cb787

Please sign in to comment.