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

Cascading Deletion for Organization-Related Tables on User Removal #228

Merged
merged 1 commit into from
Dec 4, 2023
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
29 changes: 29 additions & 0 deletions prisma/migrations/20231204162528_cascade_delete_user/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- DropForeignKey
ALTER TABLE "ActivityLog" DROP CONSTRAINT "ActivityLog_performedById_fkey";

-- DropForeignKey
ALTER TABLE "LastReadMessage" DROP CONSTRAINT "LastReadMessage_userId_fkey";

-- DropForeignKey
ALTER TABLE "MembershipRequest" DROP CONSTRAINT "MembershipRequest_userId_fkey";

-- DropForeignKey
ALTER TABLE "Messages" DROP CONSTRAINT "Messages_userId_fkey";

-- DropForeignKey
ALTER TABLE "UserOrganizationRole" DROP CONSTRAINT "UserOrganizationRole_userId_fkey";

-- AddForeignKey
ALTER TABLE "UserOrganizationRole" ADD CONSTRAINT "UserOrganizationRole_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Messages" ADD CONSTRAINT "Messages_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "LastReadMessage" ADD CONSTRAINT "LastReadMessage_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MembershipRequest" ADD CONSTRAINT "MembershipRequest_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ActivityLog" ADD CONSTRAINT "ActivityLog_performedById_fkey" FOREIGN KEY ("performedById") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
10 changes: 5 additions & 5 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ model UserOrganizationRole {
userId String
organizationId String
role Role
user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)

@@id([userId, organizationId])
Expand All @@ -293,7 +293,7 @@ model Messages {
content String
createdAt DateTime @default(now())
userId String // Reference to the User model
user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
organizationId String // Reference to the Organization model
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
lastReadByUsers LastReadMessage[]
Expand All @@ -306,7 +306,7 @@ model LastReadMessage {
organizationId String

lastMessage Messages @relation(fields: [lastMessageId], references: [id])
user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)

@@unique([userId, organizationId])
Expand All @@ -331,7 +331,7 @@ model MembershipRequest {
id Int @id @default(autoincrement())
userId String
organizationId String
user User @relation(fields: [userId], references: [id], name: "MembershipRequestsForUser")
user User @relation(fields: [userId], references: [id], name: "MembershipRequestsForUser", onDelete: Cascade)
organization Organization @relation(fields: [organizationId], references: [id], name: "MembershipRequestsForOrganization", onDelete: Cascade)
}

Expand All @@ -340,7 +340,7 @@ model ActivityLog {
action String
createdAt DateTime @default(now())
performedById String
performedBy User @relation(fields: [performedById], references: [id])
performedBy User @relation(fields: [performedById], references: [id], onDelete: Cascade)
organizationId String? // Make this optional
organization Organization? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ const OrganizationInviteModal = ({ organizationId }: Iprops) => {
});

const { closeModal } = useModalStore((state) => state);
const { refetch: refecthOrgUsers } = api.org.getOrgUsers.useQuery({
organizationId,
});
const { data: allUsers } = api.admin.getUsers.useQuery({ isAdmin: false });
// const { refetch: refetchOrganization } = api.org.getAllOrg.useQuery();

const { mutate: addUser } = api.org.addUser.useMutation();

Expand Down Expand Up @@ -90,6 +92,7 @@ const OrganizationInviteModal = ({ organizationId }: Iprops) => {
{
onSuccess: () => {
toast.success("User added successfully");
refecthOrgUsers();
closeModal();
},
},
Expand Down
4 changes: 4 additions & 0 deletions src/components/organization/editUserModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const EditOrganizationUserModal = ({ user, organizationId }: Iprops) => {
const [input, setInput] = useState({ name: "" });
const { closeModal } = useModalStore((state) => state);

const { refetch: refecthOrgUsers } = api.org.getOrgUsers.useQuery({
organizationId,
});
const { refetch: refecthOrg } = api.org.getOrgById.useQuery({
organizationId,
});
Expand All @@ -39,6 +42,7 @@ const EditOrganizationUserModal = ({ user, organizationId }: Iprops) => {
},
onSuccess: () => {
refecthOrg();
refecthOrgUsers();
closeModal();
toast.success(t("users.users.userOptionModal.toast.deleteUserSuccess"));
},
Expand Down