Skip to content

Commit

Permalink
Fxied bugs in codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
somu-code committed Jan 12, 2024
1 parent 99c2633 commit 9b92ea1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
1 change: 1 addition & 0 deletions server/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type decodedUser = {
type decodedAdmin = {
id: number;
email: string;
name: string;
role: string;
iat: number;
exp: number;
Expand Down
1 change: 1 addition & 0 deletions server/src/custom-types/admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export type Admin = {
export type adminPayload = {
id: number;
email: string;
name: string | null;
role: string;
};
66 changes: 33 additions & 33 deletions server/src/routes/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "../custom-types/course-types";
import {
courseFromDBScheam,
courseIdSchema,
createCourseSchema,
signupSchema,
} from "../zod/zod-types";
Expand Down Expand Up @@ -53,7 +54,7 @@ adminRouter.post("/signin", async (req: Request, res: Response) => {
try {
const parsedInput = signupSchema.safeParse(req.body);
if (!parsedInput.success) {
return res.status(411).json({ message: "zod" });
return res.status(411).json({ message: parsedInput.error.format() });
} else {
const { email, password }: { email: string; password: string } =
parsedInput.data;
Expand All @@ -76,27 +77,20 @@ adminRouter.post("/signin", async (req: Request, res: Response) => {
const {
id,
email,
name,
role,
}: { id: number; email: string; role: string } = adminData;
const adminPayload: adminPayload = { id, email, role };
}: { id: number; email: string; name: string | null; role: string } = adminData;
const adminPayload: adminPayload = { id, email, name, role };
const adminToken: string = generateAdminJWT(adminPayload);
res.cookie("adminAccessToken", adminToken, {
domain: "localhost",
path: "/",
maxAge: 60 * 60 * 1000,
httpOnly: true,
secure: true,
sameSite: "strict",
});
res.cookie("adminLoggedIn", true, {
domain: "localhost",
path: "/",
maxAge: 60 * 60 * 1000,
secure: true,
sameSite: "strict",
});
}
return res.json({ message: "Logged in successfully" });
return res.json({ message: "Signin in successfully" });
}
}
} catch (error) {
Expand All @@ -119,7 +113,10 @@ adminRouter.get(
});
await prisma.$disconnect();
res.json({
id: adminData?.id,
email: adminData?.email,
name: adminData?.name,
role: adminData?.role
});
} catch (error) {
await prisma.$disconnect();
Expand All @@ -135,7 +132,6 @@ adminRouter.post(
async (_req: Request, res: Response) => {
try {
res.clearCookie("adminAccessToken");
res.clearCookie("adminLoggedIn");
res.json({ message: "Logged out successfully" });
} catch (error) {
console.error(error);
Expand All @@ -157,7 +153,6 @@ adminRouter.delete(
});
await prisma.$disconnect();
res.clearCookie("adminAccessToken");
res.clearCookie("adminLoggedIn");
res.json({ message: "Admin deleted successfully" });
} catch (error) {
await prisma.$disconnect();
Expand Down Expand Up @@ -210,7 +205,7 @@ adminRouter.put(
try {
const parsedInput = courseFromDBScheam.safeParse(req.body);
if (!parsedInput.success) {
return res.status(411).json({ message: "zod" });
return res.status(411).json({ message: parsedInput.error.format() });
} else {
const updatedCourse: CourseFromDB = parsedInput.data;
const decodedAdmin: decodedAdmin = req.decodedAdmin;
Expand Down Expand Up @@ -244,25 +239,30 @@ adminRouter.delete(
async (req: Request, res: Response) => {
try {
const decodedAdmin: decodedAdmin = req.decodedAdmin;
const { courseId }: { courseId: number } = await req.body;
const currentCourse: CourseFromDB | null = await prisma.course.findUnique(
{
where: { id: courseId },
},
);
if (!currentCourse) {
return res.status(404).json({ message: "Course does not exists" });
}
if (currentCourse.adminId === decodedAdmin.id) {
await prisma.course.delete({
where: { id: courseId },
});
await prisma.$disconnect();
return res.json({ message: "Course deleted successfully" });
const parsedInput = courseIdSchema.safeParse(req.body);
if (!parsedInput.success) {
return res.status(411).json({ message: parsedInput.error.format() })
} else {
return res
.status(403)
.json({ message: "The course does not belong to this admin." });
const { courseId } = parsedInput.data;
const currentCourse: CourseFromDB | null = await prisma.course.findUnique(
{
where: { id: courseId },
},
);
if (!currentCourse) {
return res.status(404).json({ message: "Course does not exists" });
}
if (currentCourse.adminId === decodedAdmin.id) {
await prisma.course.delete({
where: { id: courseId },
});
await prisma.$disconnect();
return res.json({ message: "Course deleted successfully" });
} else {
return res
.status(403)
.json({ message: "The course does not belong to this admin." });
}
}
} catch (error) {
await prisma.$disconnect();
Expand Down
6 changes: 5 additions & 1 deletion server/src/zod/zod-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ export const courseFromDBScheam = courseSchemaWithAdminId.extend({
});

export const purchaseCourseSchema = z.object({
courseId: z.number().min(1, "Course ID can't be less than 1"),
courseId: z.number().min(1, "CourseId can't be less than 1"),
});

export const courseIdSchema = z.object({
courseId: z.number().min(1, "CourseId can't be less than 1")
})

0 comments on commit 9b92ea1

Please sign in to comment.