Skip to content

Commit

Permalink
Co-authored-by: Nick Doan <[email protected]>
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-j-edwards committed Apr 28, 2024
1 parent 31ab487 commit 59d9361
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 53 deletions.
7 changes: 5 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ model User {
Seniors Senior[] @relation(fields: [SeniorIDs], references: [id])
approved Approval @default(PENDING)
ChapterID String? @db.ObjectId
Chapter Chapter? @relation(fields: [ChapterID], references: [id])
Chapter Chapter? @relation(fields: [ChapterID], references: [id], onDelete: SetNull)
userRequest UserRequest?
}
Expand Down Expand Up @@ -149,6 +149,8 @@ model Chapter {
chapterRequestId String @db.ObjectId @unique
chapterRequest ChapterRequest @relation(fields: [chapterRequestId], references: [id], onDelete: Cascade)
userRequests UserRequest[]
}

model Resource {
Expand All @@ -168,6 +170,7 @@ model UserRequest {
// @deprecated - We ended up not using this anywhere
approved Approval @default(PENDING)
uid String @unique @db.ObjectId
chapterId String @db.ObjectId
chapterId String @db.ObjectId
chapter Chapter @relation(fields: [chapterId], references: [id], onDelete: Cascade)
user User @relation(fields: [uid], references: [id])
}
7 changes: 3 additions & 4 deletions src/app/api/chapter/[chapterId]/route.client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { DeleteChapterResponse } from "./route.schema";
import { deleteChapterResponse } from "./route.schema";

export const DeleteChapter = async (chapterId: string) => {
export const deleteChapter = async (chapterId: string) => {
const response = await fetch(`/api/chapter/${chapterId}`, {
method: "DELETE",
});
const json = await response.json();
console.log(json);
return DeleteChapterResponse.parse(json);
return deleteChapterResponse.parse(json);
};
13 changes: 2 additions & 11 deletions src/app/api/chapter/[chapterId]/route.schema.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import { z } from "zod";

export const DeleteChapterResponse = z.discriminatedUnion("code", [
export const deleteChapterResponse = z.discriminatedUnion("code", [
z.object({
code: z.literal("SUCCESS"),
message: z.literal("The chapter was successfully deleted"),
}),

z.object({
code: z.literal("INVALID_CHAPTER_ID"),
code: z.literal("CHAPTER_NOT_FOUND"),
message: z.literal("The chapter id could not be found"),
}),

z.object({
code: z.literal("INVALID_REQUEST"),
data: z.literal("The request was invalid"),
}),

z.object({
code: z.literal("UNKNOWN"),
data: z.any(),
}),
]);
35 changes: 3 additions & 32 deletions src/app/api/chapter/[chapterId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ import { withSessionAndRole } from "@server/decorator";
import { prisma } from "@server/db/client";
import { driveV3 } from "@server/service";
import { NextResponse } from "next/server";
import { DeleteChapterResponse } from "./route.schema";
import { deleteChapterResponse } from "./route.schema";

export const DELETE = withSessionAndRole(["ADMIN"], async ({ params }) => {
// TODO
// 1. Implement route.client.ts
// 2. Implement route.schema.ts
// 3. Finish deleting chapter
// 4. Add it to AdminHomePage

const chapterId = params.params.chapterId;
const chapter = await prisma.chapter.findUnique({
where: {
Expand All @@ -25,7 +19,7 @@ export const DELETE = withSessionAndRole(["ADMIN"], async ({ params }) => {
if (chapter == null) {
// If no ID is found, chapter has been deleted by another admin.
return NextResponse.json(
DeleteChapterResponse.parse({
deleteChapterResponse.parse({
code: "CHAPTER_NOT_FOUND",
message: "Chapter not found",
}),
Expand All @@ -48,31 +42,8 @@ export const DELETE = withSessionAndRole(["ADMIN"], async ({ params }) => {
},
});

await prisma.userRequest.deleteMany({
where: {
chapterId: chapterId,
},
});

chapter.students.forEach(async (student) => {
await prisma.user.update({
where: {
id: student.id,
},
data: {
ChapterID: null,
},
});
});

await prisma.chapter.delete({
where: {
id: chapterId,
},
});

return NextResponse.json(
DeleteChapterResponse.parse({
deleteChapterResponse.parse({
code: "SUCCESS",
message: "The chapter was successfully deleted",
}),
Expand Down
11 changes: 7 additions & 4 deletions src/components/AdminHomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { faTrashCan } from "@fortawesome/free-solid-svg-icons";
import { TileEdit } from "./TileGrid/TileEdit";
import { InfoTile } from "./TileGrid";
import { fullName } from "@utils";
import { DeleteChapter } from "@api/chapter/[chapterId]/route.client";
import { deleteChapter } from "@api/chapter/[chapterId]/route.client";
import { useRouter } from "next/navigation";
import SearchableContainer from "./SearchableContainer";

type ChapterWithUser = Prisma.ChapterGetPayload<{
Expand All @@ -18,6 +19,9 @@ type AdminHomePageProps = {
};

const AdminHomePage = ({ chapters }: AdminHomePageProps) => {
const userContext = useContext(UserContext);
const router = useRouter();

return (
<SearchableContainer
elements={chapters}
Expand All @@ -33,9 +37,8 @@ const AdminHomePage = ({ chapters }: AdminHomePageProps) => {
options.push({
name: "Remove Chapter",
onClick: async () => {
const response = await DeleteChapter(chapter.id);
window.location.reload();
return response;
const response = await deleteChapter(chapter.id);
router.refresh();
},
color: "#ef6767",
icon: <FontAwesomeIcon icon={faTrashCan} />,
Expand Down

0 comments on commit 59d9361

Please sign in to comment.