From cd1a3961653e4ed37096f627bc95004c75f68eae Mon Sep 17 00:00:00 2001 From: nathan-j-edwards Date: Sun, 14 Apr 2024 14:04:22 -0400 Subject: [PATCH] delete permissions upon deleting chapter --- prisma/schema.prisma | 5 +++- src/app/api/chapter/[chapterId]/route.ts | 35 ++++++++++++++++++++++++ src/app/api/file/route.ts | 9 ------ src/app/api/user-request/route.ts | 20 ++++++++++++-- src/components/AdminHomePage.tsx | 4 +-- 5 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 src/app/api/chapter/[chapterId]/route.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index db81f1e7..ebad137e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -132,10 +132,13 @@ model Chapter { id String @id @default(auto()) @map("_id") @db.ObjectId chapterName String location String - chapterFolder String @default("") dateCreated DateTime @default(now()) students User[] seniors Senior[] + + // Google Drive API related fields + chapterFolder String @default("") + permissions String[] } model Resource { diff --git a/src/app/api/chapter/[chapterId]/route.ts b/src/app/api/chapter/[chapterId]/route.ts new file mode 100644 index 00000000..8053d588 --- /dev/null +++ b/src/app/api/chapter/[chapterId]/route.ts @@ -0,0 +1,35 @@ +import { NextResponse } from "next/server"; +import { withSessionAndRole } from "@server/decorator"; +import { prisma } from "@server/db/client"; +import { driveV3 } from "@server/service"; + +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, + }, + }); + + if (chapter == null) { + // If no ID is found, chapter has been deleted by another admin. + return NextResponse.json("ok"); + } + + await Promise.allSettled( + chapter.permissions.map((permissionId) => + driveV3.permissions.delete({ + fileId: chapter.chapterFolder, + permissionId: permissionId, + }) + ) + ); + + return NextResponse.json("ok"); +}); diff --git a/src/app/api/file/route.ts b/src/app/api/file/route.ts index d3d822f4..37d91361 100644 --- a/src/app/api/file/route.ts +++ b/src/app/api/file/route.ts @@ -83,15 +83,6 @@ export const POST = withSession(async (request) => { // TOOD(nickbar01234) - Handle failure const file = await driveV3.files.create(fileCreateData); const googleFileId = file.data.id; - await driveV3.permissions.create({ - fileId: googleFileId ?? "", - requestBody: { - type: "user", - role: "writer", - emailAddress: user.email, - }, - }); - // If the data is valid, save it to the database via prisma client await prisma.file.create({ data: { diff --git a/src/app/api/user-request/route.ts b/src/app/api/user-request/route.ts index 68c1e990..da00a327 100644 --- a/src/app/api/user-request/route.ts +++ b/src/app/api/user-request/route.ts @@ -197,18 +197,32 @@ export const PATCH = withSession(async ({ req, session }) => { // Define the permission const permission = { type: "user", - role: "reader", // Change role as per your requirement + role: "writer", // Change role as per your requirement emailAddress: userEmail, }; // Share the folder - await driveV3.permissions.create({ + return await driveV3.permissions.create({ fileId: folderId, + sendNotificationEmail: false, requestBody: permission, }); }; // Since we use Google login, they must have an email - await shareFolder(folderId, chapterRequest.user.email ?? ""); + const permission = await shareFolder( + folderId, + chapterRequest.user.email ?? "" + ); + // TODO(nickbar01234) - Handle failure + const permissionId = permission.data.id as string; + await prisma.chapter.update({ + where: { id: chapterRequest.chapterId }, + data: { + permissions: { + push: permissionId, + }, + }, + }); // We update the chapter ID second to allow the user to rejoin in the case that shareFolder fails midway await prisma.user.update({ where: { id: chapterRequest.uid }, diff --git a/src/components/AdminHomePage.tsx b/src/components/AdminHomePage.tsx index a82d14df..42776707 100644 --- a/src/components/AdminHomePage.tsx +++ b/src/components/AdminHomePage.tsx @@ -35,8 +35,8 @@ const AdminHomePage = ({ chapters }: AdminHomePageProps) => { options.push({ name: "Remove Chapter", - onClick: () => { - console.log("This worked"); + onClick: async () => { + return; }, color: "#ef6767", icon: ,