use prisma referential actions to cascade deletions
This commit is contained in:
parent
bfd032972b
commit
e969b1b83e
@ -29,38 +29,12 @@ const deleteUserData = Queue<Payload>("api/queue/delete-user-data", async ({ use
|
|||||||
switch (user.memberships[0]!.role) {
|
switch (user.memberships[0]!.role) {
|
||||||
case MembershipRole.OWNER: {
|
case MembershipRole.OWNER: {
|
||||||
const organization = user.memberships[0]!.organization;
|
const organization = user.memberships[0]!.organization;
|
||||||
const where = { organizationId: organization.id };
|
|
||||||
await Promise.all<unknown>([
|
|
||||||
db.notificationSubscription.deleteMany({ where }),
|
|
||||||
db.phoneCall.deleteMany({ where }),
|
|
||||||
db.message.deleteMany({ where }),
|
|
||||||
db.processingPhoneNumber.deleteMany({ where }),
|
|
||||||
]);
|
|
||||||
await db.phoneNumber.deleteMany({ where });
|
|
||||||
|
|
||||||
const orgMembers = organization.memberships
|
|
||||||
.map((membership) => membership.user!)
|
|
||||||
.filter((user) => user !== null);
|
|
||||||
await Promise.all(
|
|
||||||
orgMembers.map((member) =>
|
|
||||||
Promise.all([
|
|
||||||
db.token.deleteMany({ where: { userId: member.id } }),
|
|
||||||
db.session.deleteMany({ where: { userId: member.id } }),
|
|
||||||
db.membership.deleteMany({ where: { userId: member.id } }),
|
|
||||||
db.user.delete({ where: { id: member.id } }),
|
|
||||||
]),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
await db.organization.delete({ where: { id: organization.id } });
|
await db.organization.delete({ where: { id: organization.id } });
|
||||||
|
await db.user.delete({ where: { id: user.id } });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case MembershipRole.USER: {
|
case MembershipRole.USER: {
|
||||||
await Promise.all([
|
await db.user.delete({ where: { id: user.id } });
|
||||||
db.token.deleteMany({ where: { userId: user.id } }),
|
|
||||||
db.session.deleteMany({ where: { userId: user.id } }),
|
|
||||||
db.user.delete({ where: { id: user.id } }),
|
|
||||||
db.membership.deleteMany({ where: { userId: user.id } }),
|
|
||||||
]);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case MembershipRole.ADMIN:
|
case MembershipRole.ADMIN:
|
||||||
|
@ -0,0 +1,77 @@
|
|||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "Membership" DROP CONSTRAINT "Membership_organizationId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "Membership" DROP CONSTRAINT "Membership_userId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "Message" DROP CONSTRAINT "Message_organizationId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "Message" DROP CONSTRAINT "Message_phoneNumberId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "NotificationSubscription" DROP CONSTRAINT "NotificationSubscription_organizationId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "NotificationSubscription" DROP CONSTRAINT "NotificationSubscription_phoneNumberId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "PhoneCall" DROP CONSTRAINT "PhoneCall_organizationId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "PhoneCall" DROP CONSTRAINT "PhoneCall_phoneNumberId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "PhoneNumber" DROP CONSTRAINT "PhoneNumber_organizationId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "ProcessingPhoneNumber" DROP CONSTRAINT "ProcessingPhoneNumber_organizationId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "ProcessingPhoneNumber" DROP CONSTRAINT "ProcessingPhoneNumber_phoneNumberId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "Session" DROP CONSTRAINT "Session_userId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "Token" DROP CONSTRAINT "Token_userId_fkey";
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Membership" ADD CONSTRAINT "Membership_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Membership" ADD CONSTRAINT "Membership_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Session" ADD CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Token" ADD CONSTRAINT "Token_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Message" ADD CONSTRAINT "Message_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Message" ADD CONSTRAINT "Message_phoneNumberId_fkey" FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "PhoneCall" ADD CONSTRAINT "PhoneCall_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "PhoneCall" ADD CONSTRAINT "PhoneCall_phoneNumberId_fkey" FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "PhoneNumber" ADD CONSTRAINT "PhoneNumber_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "ProcessingPhoneNumber" ADD CONSTRAINT "ProcessingPhoneNumber_phoneNumberId_fkey" FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "ProcessingPhoneNumber" ADD CONSTRAINT "ProcessingPhoneNumber_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "NotificationSubscription" ADD CONSTRAINT "NotificationSubscription_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "NotificationSubscription" ADD CONSTRAINT "NotificationSubscription_phoneNumberId_fkey" FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
@ -40,10 +40,10 @@ model Membership {
|
|||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
role MembershipRole
|
role MembershipRole
|
||||||
|
|
||||||
organization Organization @relation(fields: [organizationId], references: [id])
|
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
|
||||||
organizationId String
|
organizationId String
|
||||||
|
|
||||||
user User? @relation(fields: [userId], references: [id])
|
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||||
userId String?
|
userId String?
|
||||||
|
|
||||||
// When the user joins, we will clear out the name and email and set the user.
|
// When the user joins, we will clear out the name and email and set the user.
|
||||||
@ -95,7 +95,7 @@ model Session {
|
|||||||
publicData String?
|
publicData String?
|
||||||
privateData String?
|
privateData String?
|
||||||
|
|
||||||
user User? @relation(fields: [userId], references: [id])
|
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||||
userId String?
|
userId String?
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ model Token {
|
|||||||
expiresAt DateTime @db.Timestamptz
|
expiresAt DateTime @db.Timestamptz
|
||||||
sentTo String
|
sentTo String
|
||||||
|
|
||||||
user User @relation(fields: [userId], references: [id])
|
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||||
userId String
|
userId String
|
||||||
|
|
||||||
@@unique([hashedToken, type])
|
@@unique([hashedToken, type])
|
||||||
@ -127,9 +127,9 @@ model Message {
|
|||||||
direction Direction
|
direction Direction
|
||||||
status MessageStatus
|
status MessageStatus
|
||||||
|
|
||||||
organization Organization @relation(fields: [organizationId], references: [id])
|
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
|
||||||
organizationId String
|
organizationId String
|
||||||
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id])
|
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id], onDelete: Cascade)
|
||||||
phoneNumberId String
|
phoneNumberId String
|
||||||
|
|
||||||
@@unique([organizationId, phoneNumberId, id])
|
@@unique([organizationId, phoneNumberId, id])
|
||||||
@ -167,9 +167,9 @@ model PhoneCall {
|
|||||||
direction Direction
|
direction Direction
|
||||||
duration String
|
duration String
|
||||||
|
|
||||||
organization Organization @relation(fields: [organizationId], references: [id])
|
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
|
||||||
organizationId String
|
organizationId String
|
||||||
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id])
|
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id], onDelete: Cascade)
|
||||||
phoneNumberId String
|
phoneNumberId String
|
||||||
|
|
||||||
@@unique([organizationId, phoneNumberId, id])
|
@@unique([organizationId, phoneNumberId, id])
|
||||||
@ -196,7 +196,7 @@ model PhoneNumber {
|
|||||||
notificationSubscriptions NotificationSubscription[]
|
notificationSubscriptions NotificationSubscription[]
|
||||||
|
|
||||||
processingPhoneNumber ProcessingPhoneNumber?
|
processingPhoneNumber ProcessingPhoneNumber?
|
||||||
organization Organization @relation(fields: [organizationId], references: [id])
|
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
|
||||||
organizationId String
|
organizationId String
|
||||||
|
|
||||||
@@unique([organizationId, id])
|
@@unique([organizationId, id])
|
||||||
@ -206,9 +206,9 @@ model ProcessingPhoneNumber {
|
|||||||
createdAt DateTime @default(now()) @db.Timestamptz
|
createdAt DateTime @default(now()) @db.Timestamptz
|
||||||
hasFetchedMessages Boolean
|
hasFetchedMessages Boolean
|
||||||
hasFetchedCalls Boolean
|
hasFetchedCalls Boolean
|
||||||
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id])
|
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id], onDelete: Cascade)
|
||||||
phoneNumberId String
|
phoneNumberId String
|
||||||
organization Organization @relation(fields: [organizationId], references: [id])
|
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
|
||||||
organizationId String
|
organizationId String
|
||||||
|
|
||||||
@@id([organizationId, phoneNumberId])
|
@@id([organizationId, phoneNumberId])
|
||||||
@ -223,8 +223,8 @@ model NotificationSubscription {
|
|||||||
keys_p256dh String
|
keys_p256dh String
|
||||||
keys_auth String
|
keys_auth String
|
||||||
|
|
||||||
organization Organization @relation(fields: [organizationId], references: [id])
|
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
|
||||||
organizationId String
|
organizationId String
|
||||||
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id])
|
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id], onDelete: Cascade)
|
||||||
phoneNumberId String
|
phoneNumberId String
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user