Skip to content
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
12 changes: 0 additions & 12 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ model User {
organizationId String? @db.ObjectId
organization Organization? @relation(fields: [organizationId], references: [id])
code Code?
eventIds String[] @db.ObjectId
events Event[] @relation(fields: [eventIds], references: [id])
timeSlots TimeSlot[]
volunteerSessions VolunteerSession[]
}
Expand Down Expand Up @@ -82,16 +80,6 @@ model Code {
userId String @unique @db.ObjectId
}

model Event {
id String @id @default(auto()) @map("_id") @db.ObjectId
userIds String[] @db.ObjectId
users User[] @relation(fields: [userIds], references: [id])
eventName String @default("")
dateTime DateTime @default(now())
description String @default("")
maxPeople Int @default(0)
}

model Organization {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String @unique
Expand Down
53 changes: 0 additions & 53 deletions src/app/api/event/route.client.ts

This file was deleted.

142 changes: 0 additions & 142 deletions src/app/api/event/route.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/app/api/organization/route.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ export const getOrganizations = async () => {
const url = `/api/organization`;
return fetchApi(url, "GET");
};

export const deleteOrganization = async (organizationId: string) => {
const url = `/api/organization?id=${organizationId}`;
return fetchApi(url, "DELETE");
};
50 changes: 50 additions & 0 deletions src/app/api/organization/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,53 @@ export const GET = async (request: NextRequest) => {
);
}
};

export const DELETE = async (request: NextRequest) => {
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");

if (!id) {
return NextResponse.json(
{ code: "BAD_REQUEST", message: "Organization ID is required." },
{ status: 400 }
);
}

try {
await prisma.$transaction(async (tx) => {
// Nullify organizationId for Users
await tx.user.updateMany({
where: { organizationId: id },
data: { organizationId: null },
});

// Nullify organizationId for TimeSlots
await tx.timeSlot.updateMany({
where: { organizationId: id },
data: { organizationId: null },
});

// Nullify organizationId for VolunteerSessions
await tx.volunteerSession.updateMany({
where: { organizationId: id },
data: { organizationId: null },
});

// Delete the Organization
await tx.organization.delete({
where: { id },
});
});

return NextResponse.json(
{ code: "SUCCESS", message: "Organization deleted successfully." },
{ status: 200 }
);
} catch (error) {
console.error("Error deleting organization:", error);
return NextResponse.json(
{ code: "ERROR", message: "Failed to delete organization." },
{ status: 500 }
);
}
};
73 changes: 64 additions & 9 deletions src/app/api/user/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,79 @@ export const DELETE = async (request: NextRequest) => {
);
}

// @TODO: If there is time figure out the logic for removing user while preserving previous time slots/sessions
try {
await prisma.code.delete({
where: { userId: id },
});
await prisma.$transaction(async (tx) => {
// Delete TimeSlots where organizationId is null
// await tx.timeSlot.deleteMany({
// where: {
// userId: id,
// organizationId: null,
// },
// });

// Delete VolunteerSessions where organizationId is null
// await tx.volunteerSession.deleteMany({
// where: {
// userId: id,
// organizationId: null,
// },
// });

// For TimeSlots with organizationId, nullify userId
// await tx.timeSlot.updateMany({
// where: {
// userId: id,
// NOT: {
// organizationId: null,
// },
// },
// data: {
// userId: undefined, // nullify userId
// },
// });

// For VolunteerSessions with organizationId, nullify userId
// await tx.volunteerSession.updateMany({
// where: {
// userId: id,
// NOT: {
// organizationId: null,
// },
// },
// data: {
// userId: undefined, // nullify userId
// },
// });

await tx.timeSlot.deleteMany({
where: { userId: id },
});

await prisma.volunteerDetails.delete({
where: { userId: id },
});
await tx.volunteerSession.deleteMany({
where: { userId: id },
});

const deletedUser = await prisma.user.delete({
where: { id },
// Delete related Code
await tx.code.deleteMany({
where: { userId: id },
});

// Delete related VolunteerDetails
await tx.volunteerDetails.deleteMany({
where: { userId: id },
});

// Delete the User
await tx.user.delete({
where: { id },
});
});

return NextResponse.json(
{
code: "SUCCESS",
message: "User deleted successfully",
data: deletedUser,
},
{ status: 200 }
);
Expand Down
Loading