Skip to content

Commit

Permalink
deleted seniors, request, and chapter (#173)
Browse files Browse the repository at this point in the history
* deleted seniors, request, and chapter

* Co-authored-by: Nick Doan <[email protected]>

* pr changes

* deleted seniors, request, and chapter
  • Loading branch information
nathan-j-edwards authored Apr 28, 2024
1 parent 4a3fe98 commit cde0fb2
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 14 deletions.
14 changes: 11 additions & 3 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 @@ -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,11 @@ 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)
userRequests UserRequest[]
}

model Resource {
Expand All @@ -163,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])
}
9 changes: 9 additions & 0 deletions src/app/api/chapter/[chapterId]/route.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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();
return deleteChapterResponse.parse(json);
};
13 changes: 13 additions & 0 deletions src/app/api/chapter/[chapterId]/route.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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("CHAPTER_NOT_FOUND"),
message: z.literal("The chapter id could not be found"),
}),
]);
48 changes: 39 additions & 9 deletions src/app/api/chapter/[chapterId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
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
// 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: {
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 +36,30 @@ export const DELETE = withSessionAndRole(["ADMIN"], async ({ params }) => {
)
);

return NextResponse.json("ok");
await prisma.user.updateMany({
where: {
ChapterID: chapterId,
},
data: {
SeniorIDs: {
set: [],
},
position: "",
role: "USER",
},
});

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

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

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

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

return (
<SearchableContainer
elements={chapters}
Expand All @@ -32,7 +36,8 @@ const AdminHomePage = ({ chapters }: AdminHomePageProps) => {
options.push({
name: "Remove Chapter",
onClick: async () => {
return;
const response = await deleteChapter(chapter.id);
router.refresh();
},
color: "#ef6767",
icon: <FontAwesomeIcon icon={faTrashCan} />,
Expand Down
2 changes: 1 addition & 1 deletion src/components/ChapterRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const ChapterRequest = (props: ChapterRequestProps) => {
</div>
) : (
<div className="flex justify-center p-2">
<Spinner height={16} width={16} />
<Spinner height={20} width={16} />
</div>
)}
</div>
Expand Down

0 comments on commit cde0fb2

Please sign in to comment.