Skip to content

Commit

Permalink
feat: update roles on dashboard, fix auth
Browse files Browse the repository at this point in the history
  • Loading branch information
anishshobithps committed May 7, 2024
1 parent 67f0aeb commit 46bec95
Show file tree
Hide file tree
Showing 19 changed files with 390 additions and 136 deletions.
23 changes: 9 additions & 14 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
/** @type {import('next').NextConfig} */

const nextConfig = {
async redirects() {
return [
{
source: "/api/verify/:id",
destination: "/coordinators/verify/:id",
permanent: true,
},
];
},
images: {
formats: ["image/avif", "image/webp"],
remotePatterns: [
// localhost
{
protocol: "http",
hostname: "localhost",
port: "3000",
pathname: "/**",
},
// vercel
{
protocol: "https",
hostname: "tiara2024.vercel.app",
port: "",
pathname: "/**",
},
{
protocol: "https",
hostname: "assets.tiarasjec.in",
Expand Down
40 changes: 5 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"lucide-react": "^0.376.0",
"maath": "^0.10.7",
"next": "^14.2.3",
"next-auth": "^5.0.0-beta.16",
"next-auth": "^5.0.0-beta.17",
"nodemailer": "^6.9.13",
"preline": "^2.1.0",
"razorpay": "^2.9.3",
Expand Down
2 changes: 1 addition & 1 deletion src/app/(content)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use client";;
"use client";
import Lenis from "@/components/shared/lenis";
import { LabIntro } from "@/components/story/intro";
import { HeroParallax } from "@/components/widgets/Hero";
Expand Down
2 changes: 1 addition & 1 deletion src/app/(content)/register/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,4 @@ const Register: React.FC = () => {
);
};

export default Register;
export default Register;
101 changes: 89 additions & 12 deletions src/app/admin/_components/columnsUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@ import { ColumnDef } from "@tanstack/react-table";
import { Payment, User } from "@prisma/client";
import { ArrowUpDown } from "lucide-react";
import { Button } from "@/components/ui/button";
import { MoreHorizontal } from "lucide-react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { toast } from "@/components/ui/use-toast";
import { UserRole } from "@prisma/client";

async function updateRole({
id,
role,
name,
}: {
id: string;
role: Lowercase<UserRole>;
name: string;
}) {
const res = await fetch(`/api/admin/${id}/make-${role}`, {
method: "POST",
});
if (res.status === 200) {
toast({
title: `${name} is now a ${role}`,
});
} else {
toast({
title: `Failed to make ${name} a ${role}`,
});
}
}

export const columns: ColumnDef<User & { payment: Payment[] }>[] = [
{
Expand Down Expand Up @@ -71,22 +105,12 @@ export const columns: ColumnDef<User & { payment: Payment[] }>[] = [
variant="ghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Amount
Role
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
);
},
accessorKey: "payment_amount",
cell: ({ row }) => {
return row.original.payment.map((payment, i) => (
<div key={i}>
{new Intl.NumberFormat("en-IN", {
style: "currency",
currency: "INR",
}).format(payment.amount / 100)}
</div>
));
},
accessorKey: "role",
},
{
header: ({ column }) => {
Expand All @@ -102,4 +126,57 @@ export const columns: ColumnDef<User & { payment: Payment[] }>[] = [
},
accessorKey: "contact",
},
{
id: "actions",
cell: ({ row }) => {
const user = row.original;
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-8 w-8 p-0">
<span className="sr-only">Open menu</span>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={() =>
updateRole({
id: user.id,
role: "admin",
name: user.name!,
})
}
>
Make Admin
</DropdownMenuItem>
<DropdownMenuItem
onClick={() =>
updateRole({
id: user.id,
role: "coordinator",
name: user.name!,
})
}
>
Make Coordinator
</DropdownMenuItem>
<DropdownMenuItem
onClick={() =>
updateRole({
id: user.id,
role: "participant",
name: user.name!,
})
}
>
Make User
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
},
},
];
34 changes: 34 additions & 0 deletions src/app/api/admin/[id]/make-admin/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { prisma } from "@/lib/prisma";
import { auth } from "@/auth";
import { NextRequest, NextResponse } from "next/server";
import { UserRole } from "@prisma/client";

export async function POST(request: NextRequest, context: { params: { id: string } }) {
const session = await auth();
if (!session) {
return NextResponse.json(
{ message: "Unauthorized", isOk: false },
{ status: 401 }
);
}

if (session.user.role !== UserRole.ADMIN) {
return NextResponse.json(
{ message: "Forbidden", isOk: false },
{ status: 403 }
);
}

const { id } = context.params;

const user = await prisma.user.update({
where: {
id: id,
},
data: {
role: UserRole.ADMIN,
},
});

return NextResponse.json({ user }, { status: 200 });
}
34 changes: 34 additions & 0 deletions src/app/api/admin/[id]/make-coordinator/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { prisma } from "@/lib/prisma";
import { auth } from "@/auth";
import { NextRequest, NextResponse } from "next/server";
import { UserRole } from "@prisma/client";

export async function POST(request: NextRequest, context: { params: { id: string } }) {
const session = await auth();
if (!session) {
return NextResponse.json(
{ message: "Unauthorized", isOk: false },
{ status: 401 }
);
}

if (session.user.role !== UserRole.ADMIN) {
return NextResponse.json(
{ message: "Forbidden", isOk: false },
{ status: 403 }
);
}

const { id } = context.params;

const user = await prisma.user.update({
where: {
id: id,
},
data: {
role: UserRole.COORDINATOR,
},
});

return NextResponse.json({ user }, { status: 200 });
}
34 changes: 34 additions & 0 deletions src/app/api/admin/[id]/make-participant/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { prisma } from "@/lib/prisma";
import { auth } from "@/auth";
import { NextRequest, NextResponse } from "next/server";
import { UserRole } from "@prisma/client";

export async function POST(request: NextRequest, context: { params: { id: string } }) {
const session = await auth();
if (!session) {
return NextResponse.json(
{ message: "Unauthorized", isOk: false },
{ status: 401 }
);
}

if (session.user.role !== UserRole.ADMIN) {
return NextResponse.json(
{ message: "Forbidden", isOk: false },
{ status: 403 }
);
}

const { id } = context.params;

const user = await prisma.user.update({
where: {
id: id,
},
data: {
role: UserRole.PARTICIPANT,
},
});

return NextResponse.json({ user }, { status: 200 });
}
2 changes: 2 additions & 0 deletions src/app/api/admin/payments/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export async function GET() {
});

// for (const prismaPayment of payments) {
// console.log("ID: ", prismaPayment.razorpayPaymentId);
// const payment = await razorpay.payments.fetch(prismaPayment.razorpayPaymentId);
// await prisma.payment.update({
// where: {
Expand All @@ -34,6 +35,7 @@ export async function GET() {
// amount: parseFloat(payment.amount.toString()),
// }
// })
// console.log("ID: Done", prismaPayment.razorpayPaymentId);
// }

return NextResponse.json(payments);
Expand Down
34 changes: 34 additions & 0 deletions src/app/api/coordinators/verify/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { prisma } from "@/lib/prisma";
import { auth } from "@/auth";
import { NextRequest, NextResponse } from "next/server";
import { UserRole } from "@prisma/client";

export async function GET(
request: NextRequest,
context: { params: { id: string } }
) {
const session = await auth();
if (!session) {
return NextResponse.json(
{ message: "Unauthorized", isOk: false },
{ status: 401 }
);
}

if (session.user.role !== UserRole.ADMIN) {
return NextResponse.json(
{ message: "Forbidden", isOk: false },
{ status: 403 }
);
}

const { id } = context.params;

const user = await prisma.user.findUnique({
where: {
id: id,
},
});

return NextResponse.json({ user }, { status: 200 });
}
Loading

0 comments on commit 46bec95

Please sign in to comment.