Skip to content

Commit

Permalink
delete permissions upon deleting chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-j-edwards committed Apr 14, 2024
1 parent 2533716 commit cd1a396
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
5 changes: 4 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions src/app/api/chapter/[chapterId]/route.ts
Original file line number Diff line number Diff line change
@@ -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");
});
9 changes: 0 additions & 9 deletions src/app/api/file/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
20 changes: 17 additions & 3 deletions src/app/api/user-request/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
4 changes: 2 additions & 2 deletions src/components/AdminHomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const AdminHomePage = ({ chapters }: AdminHomePageProps) => {

options.push({
name: "Remove Chapter",
onClick: () => {
console.log("This worked");
onClick: async () => {
return;
},
color: "#ef6767",
icon: <FontAwesomeIcon icon={faTrashCan} />,
Expand Down

0 comments on commit cd1a396

Please sign in to comment.