Skip to content

Commit

Permalink
Merge pull request #86 from Ashutoshbind15/main
Browse files Browse the repository at this point in the history
index on the admin table and fix some syntax errors on the play page
  • Loading branch information
webdevcody authored Oct 18, 2024
2 parents e60bc43 + 25cb787 commit c475507
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
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 c475507

Please sign in to comment.