Skip to content

Commit

Permalink
deleted seniors, request, and chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-j-edwards committed Apr 28, 2024
1 parent 4a3fe98 commit 31ab487
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 5 deletions.
7 changes: 6 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ model Senior {
folder String @default("")
Files File[]
ChapterID String @db.ObjectId
chapter Chapter @relation(fields: [ChapterID], references: [id])
chapter Chapter @relation(fields: [ChapterID], references: [id], onDelete: Cascade)
}

model File {
Expand Down Expand Up @@ -131,6 +131,8 @@ model ChapterRequest {
motivation String
availabilities String
questions String
chapter Chapter?
}

model Chapter {
Expand All @@ -144,6 +146,9 @@ model Chapter {
// Google Drive API related fields
chapterFolder String @default("")
permissions String[]
chapterRequestId String @db.ObjectId @unique
chapterRequest ChapterRequest @relation(fields: [chapterRequestId], references: [id], onDelete: Cascade)
}

model Resource {
Expand Down
10 changes: 10 additions & 0 deletions src/app/api/chapter/[chapterId]/route.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { DeleteChapterResponse } from "./route.schema";

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);
};
23 changes: 23 additions & 0 deletions src/app/api/chapter/[chapterId]/route.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { z } from "zod";

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"),
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(),
}),
]);
52 changes: 49 additions & 3 deletions src/app/api/chapter/[chapterId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextResponse } from "next/server";
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";

export const DELETE = withSessionAndRole(["ADMIN"], async ({ params }) => {
// TODO
Expand All @@ -15,11 +16,21 @@ export const DELETE = withSessionAndRole(["ADMIN"], async ({ params }) => {
where: {
id: chapterId,
},
include: {
students: true,
seniors: true,
},
});

if (chapter == null) {
// If no ID is found, chapter has been deleted by another admin.
return NextResponse.json("ok");
return NextResponse.json(
DeleteChapterResponse.parse({
code: "CHAPTER_NOT_FOUND",
message: "Chapter not found",
}),
{ status: 404 }
);
}

await Promise.allSettled(
Expand All @@ -31,5 +42,40 @@ export const DELETE = withSessionAndRole(["ADMIN"], async ({ params }) => {
)
);

return NextResponse.json("ok");
await prisma.chapterRequest.delete({
where: {
id: chapter.chapterRequestId,
},
});

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({
code: "SUCCESS",
message: "The chapter was successfully deleted",
}),
{ status: 200 }
);
});
1 change: 1 addition & 0 deletions src/app/api/handle-chapter-request/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const POST = withSession(async ({ req }) => {
data: {
chapterName: chapterRequest.university,
location: chapterRequest.universityAddress,
chapterRequestId: chapterRequest.id,
},
});

Expand Down
5 changes: 4 additions & 1 deletion src/components/AdminHomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 SearchableContainer from "./SearchableContainer";

type ChapterWithUser = Prisma.ChapterGetPayload<{
Expand All @@ -32,7 +33,9 @@ const AdminHomePage = ({ chapters }: AdminHomePageProps) => {
options.push({
name: "Remove Chapter",
onClick: async () => {
return;
const response = await DeleteChapter(chapter.id);
window.location.reload();
return response;
},
color: "#ef6767",
icon: <FontAwesomeIcon icon={faTrashCan} />,
Expand Down

0 comments on commit 31ab487

Please sign in to comment.