Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete permissions upon deleting chapter #160

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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