Skip to content

Commit

Permalink
add admin mutation and query helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutoshbind15 committed Oct 18, 2024
1 parent c475507 commit c451621
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 43 deletions.
57 changes: 15 additions & 42 deletions convex/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ZombieSurvival } from "../simulators/zombie-survival";
import { api, internal } from "./_generated/api";
import { runModel } from "../models";
import { getAuthUserId } from "@convex-dev/auth/server";
import { adminMutationBuilder } from "./users";

const LEVELS = [
{
Expand Down Expand Up @@ -148,23 +149,11 @@ export const addMap = mutation({
},
});

export const publishMap = mutation({
export const publishMap = adminMutationBuilder({
args: {
map: v.array(v.array(v.string())),
},
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);

if (!userId) {
throw new Error("User not authenticated");
}

const isAdmin = await ctx.runQuery(api.users.isAdmin);

if (!isAdmin) {
throw new Error("Publishing maps is available only for admins");
}

const maps = await ctx.db
.query("maps")
.filter((q) => q.neq("level", undefined))
Expand All @@ -175,7 +164,7 @@ export const publishMap = mutation({
await ctx.db.insert("maps", {
grid: args.map,
level: lastLevel + 1,
submittedBy: userId,
submittedBy: ctx.admin.id,
isReviewed: true,
});
},
Expand Down Expand Up @@ -240,40 +229,24 @@ export const getMaps = query({
},
});

export const approveMap = mutation({
export const approveMap = adminMutationBuilder({
args: {
mapId: v.id("maps"),
},
handler: async (ctx, args) => {
// todo: take this out into a helper function

const userId = await getAuthUserId(ctx);
if (userId === null) {
throw new Error("Unauthorized");
}

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

if (!admin) {
throw new Error("Unauthorized");
} else {
const maps = await ctx.db.query("maps").collect();
const maps = await ctx.db.query("maps").collect();

const lastLevel = maps.reduce((acc, map) => {
if (map.level) {
return Math.max(acc, map.level);
}
return acc;
}, 0);
const lastLevel = maps.reduce((acc, map) => {
if (map.level) {
return Math.max(acc, map.level);
}
return acc;
}, 0);

await ctx.db.patch(args.mapId, {
isReviewed: true,
level: lastLevel + 1,
});
}
await ctx.db.patch(args.mapId, {
isReviewed: true,
level: lastLevel + 1,
});
},
});

Expand Down
59 changes: 58 additions & 1 deletion convex/users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { getAuthUserId } from "@convex-dev/auth/server";
import { query } from "./_generated/server";
import { mutation, query } from "./_generated/server";
import {
customQuery,
customCtx,
customMutation,
} from "convex-helpers/server/customFunctions";

export const viewer = query({
args: {},
Expand Down Expand Up @@ -42,3 +47,55 @@ export const isAdmin = query({
return !!admin;
},
});

export const adminQueryBuilder = customQuery(
query,
customCtx(async (ctx) => {
const userId = await getAuthUserId(ctx);

if (userId === null) {
throw new Error("Unauthorized");
}

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

if (!admin) {
throw new Error("Admin only invocation called from non-admin user");
}

return {
admin: {
id: admin.userId,
},
};
}),
);

export const adminMutationBuilder = customMutation(
mutation,
customCtx(async (ctx) => {
const userId = await getAuthUserId(ctx);

if (userId === null) {
throw new Error("Unauthorized");
}

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

if (!admin) {
throw new Error("Admin only invocation called from non-admin user");
}

return {
admin: {
id: admin.userId,
},
};
}),
);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"convex": "^1.16.0",
"convex-helpers": "^0.1.61",
"date-fns": "^4.1.0",
"lucide-react": "^0.453.0",
"next": "14.2.5",
Expand Down

0 comments on commit c451621

Please sign in to comment.