Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hkirat committed Jul 27, 2024
1 parent 2649c16 commit 4b15073
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 128 deletions.
297 changes: 169 additions & 128 deletions src/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const courseSchema = z.object({
export default function Courses() {
const [isLoading, setIsLoading] = useState<boolean>(false);
const router = useRouter();
const [email, setEmail] = useState('');
const [adminPassword, setAdminPassword] = useState('');

const form = useForm<z.infer<typeof courseSchema>>({
resolver: zodResolver(courseSchema),
Expand Down Expand Up @@ -76,134 +78,173 @@ export default function Courses() {
};

return (
<Card className="mx-auto w-full max-w-6xl overflow-y-auto lg:mt-10">
<CardHeader>
<CardTitle>Create a new course</CardTitle>
<CardDescription>Fill in the course details below</CardDescription>
</CardHeader>
<CardContent className="grid gap-4 p-4 pt-0">
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="grid gap-4 lg:grid-cols-2"
<div>
<Card className="mx-auto w-full max-w-6xl overflow-y-auto lg:mt-10">
<CardHeader>
<CardTitle>Create a new course</CardTitle>
<CardDescription>Fill in the course details below</CardDescription>
</CardHeader>
<CardContent className="grid gap-4 p-4 pt-0">
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="grid gap-4 lg:grid-cols-2"
>
<FormField
control={form.control}
name="title"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Title</FormLabel>
<FormControl>
<Input placeholder="Enter the Course name" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="imageUrl"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Image url</FormLabel>
<FormControl>
<Input placeholder="Enter the url of Image" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="description"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
<Textarea
placeholder="Enter the Description of course"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="slug"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Slug</FormLabel>
<FormControl>
<Input placeholder="Enter the Course slug" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="id"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Id</FormLabel>
<FormControl>
<Input placeholder="Enter the Course ID" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="adminSecret"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Admin Secret</FormLabel>
<FormControl>
<Input placeholder="Enter the Admin Secret" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="appxCourseId"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>app x course id</FormLabel>
<FormControl>
<Input
placeholder="Enter the appx course ID"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="discordRoleId"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Discord Role Id</FormLabel>
<FormControl>
<Input
placeholder="Enter the Discord Role Id"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="lg:col-span-2">
{isLoading ? (
<Button>Loading...</Button>
) : (
<Button type="submit">Create</Button>
)}
</div>
</form>
</Form>
</CardContent>
</Card>
<Card className="mx-auto w-full max-w-6xl overflow-y-auto lg:mt-10">
<CardHeader>
<CardTitle>Allow user another account in cohort 3</CardTitle>
</CardHeader>
<CardContent className="grid gap-4 p-4 pt-0">
<input
type="text"
placeholder="Email"
onChange={(e) => {
setEmail(e.target.value);
}}
></input>
<input
type="text"
placeholder="Admin Password"
onChange={(e) => {
setAdminPassword(e.target.value);
}}
></input>
<button
onClick={() => {
axios.post('/api/admin/discordReset', {
email,
adminPassword,
});
}}
>
<FormField
control={form.control}
name="title"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Title</FormLabel>
<FormControl>
<Input placeholder="Enter the Course name" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="imageUrl"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Image url</FormLabel>
<FormControl>
<Input placeholder="Enter the url of Image" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="description"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
<Textarea
placeholder="Enter the Description of course"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="slug"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Slug</FormLabel>
<FormControl>
<Input placeholder="Enter the Course slug" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="id"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Id</FormLabel>
<FormControl>
<Input placeholder="Enter the Course ID" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="adminSecret"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Admin Secret</FormLabel>
<FormControl>
<Input placeholder="Enter the Admin Secret" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="appxCourseId"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>app x course id</FormLabel>
<FormControl>
<Input placeholder="Enter the appx course ID" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="discordRoleId"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Discord Role Id</FormLabel>
<FormControl>
<Input placeholder="Enter the Discord Role Id" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="lg:col-span-2">
{isLoading ? (
<Button>Loading...</Button>
) : (
<Button type="submit">Create</Button>
)}
</div>
</form>
</Form>
</CardContent>
</Card>
Reset
</button>
</CardContent>
</Card>
</div>
);
}
52 changes: 52 additions & 0 deletions src/app/api/admin/discordReset/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { NextRequest, NextResponse } from 'next/server';
import db from '@/db';
import { z } from 'zod';

const requestBodySchema = z.object({
adminPassword: z.string(),
email: z.string().email(),
});

export async function POST(req: NextRequest) {
const parseResult = requestBodySchema.safeParse(await req.json());

if (!parseResult.success) {
return NextResponse.json(
{ error: parseResult.error.message },
{ status: 400 },
);
}

const { adminPassword, email } = parseResult.data;

if (adminPassword !== process.env.ADMIN_SECRET) {
return NextResponse.json({}, { status: 401 });
}

const user = await db.user.findFirst({
where: {
email,
},
});

if (!user) {
return NextResponse.json({ msg: 'User not found' }, { status: 404 });
}

const response = await db.discordConnectBulk.updateMany({
where: {
userId: user.id,
cohortId: '3',
},
data: {
cohortId: Math.random().toString(),
},
});

return NextResponse.json(
{ data: response },
{
status: 200,
},
);
}

0 comments on commit 4b15073

Please sign in to comment.