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

♻️ Update relational model #1472

Merged
merged 5 commits into from
Dec 28, 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
46 changes: 0 additions & 46 deletions apps/web/src/app/api/house-keeping/[task]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,47 +88,6 @@ async function removeDeletedPolls(req: Request) {

const deletedPollIds = deletedPolls.map((poll) => poll.id);

const { count: deletedWatcherCount } = await prisma.watcher.deleteMany({
where: {
pollId: {
in: deletedPollIds,
},
},
});

const { count: deletedVoteCount } = await prisma.vote.deleteMany({
where: {
pollId: {
in: deletedPollIds,
},
},
});

const { count: deletedParticipantCount } =
await prisma.participant.deleteMany({
where: {
pollId: {
in: deletedPollIds,
},
},
});

const { count: deletedOptionCount } = await prisma.option.deleteMany({
where: {
pollId: {
in: deletedPollIds,
},
},
});

const { count: deletedCommentCount } = await prisma.comment.deleteMany({
where: {
pollId: {
in: deletedPollIds,
},
},
});

const { count: deletedPollCount } = await prisma.poll.deleteMany({
where: {
id: {
Expand All @@ -141,11 +100,6 @@ async function removeDeletedPolls(req: Request) {
success: true,
summary: {
deleted: {
votes: deletedVoteCount,
options: deletedOptionCount,
participants: deletedParticipantCount,
comments: deletedCommentCount,
watchers: deletedWatcherCount,
polls: deletedPollCount,
},
},
Expand Down
9 changes: 0 additions & 9 deletions apps/web/src/app/api/stripe/webhook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@ export async function POST(request: NextRequest) {
await stripe.invoices.voidInvoice(invoice.id);
}

// remove the subscription from the user
await prisma.user.update({
where: {
subscriptionId: subscription.id,
},
data: {
subscriptionId: null,
},
});
// delete the subscription from the database
await prisma.subscription.delete({
where: {
Expand Down
23 changes: 7 additions & 16 deletions apps/web/src/trpc/routers/polls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,14 @@ export const polls = router({
const pollId = await getPollIdFromAdminUrlId(input.urlId);

if (input.optionsToDelete && input.optionsToDelete.length > 0) {
await prisma.$transaction([
prisma.option.deleteMany({
where: {
pollId,
id: {
in: input.optionsToDelete,
},
},
}),
prisma.vote.deleteMany({
where: {
optionId: {
in: input.optionsToDelete,
},
await prisma.option.deleteMany({
where: {
pollId,
id: {
in: input.optionsToDelete,
},
}),
]);
},
});
}

if (input.optionsToAdd && input.optionsToAdd.length > 0) {
Expand Down
48 changes: 9 additions & 39 deletions apps/web/src/trpc/routers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,47 +53,17 @@ export const user = router({
});
}),
delete: privateProcedure.mutation(async ({ ctx }) => {
await prisma.$transaction(async (tx) => {
const polls = await tx.poll.findMany({
select: { id: true },
where: {
userId: ctx.user.id,
},
if (ctx.user.isGuest) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Guest users cannot be deleted",
});
const pollIds = polls.map((poll) => poll.id);
}

await tx.vote.deleteMany({
where: { pollId: { in: pollIds } },
});
await tx.comment.deleteMany({
where: { OR: [{ pollId: { in: pollIds } }, { userId: ctx.user.id }] },
});
await tx.option.deleteMany({
where: { pollId: { in: pollIds } },
});
await tx.participant.deleteMany({
where: { OR: [{ pollId: { in: pollIds } }, { userId: ctx.user.id }] },
});
await tx.watcher.deleteMany({
where: { OR: [{ pollId: { in: pollIds } }, { userId: ctx.user.id }] },
});
await tx.event.deleteMany({
where: { userId: ctx.user.id },
});
await tx.poll.deleteMany({
where: { userId: ctx.user.id },
});
await tx.account.deleteMany({
where: { userId: ctx.user.id },
});
await tx.userPaymentData.deleteMany({
where: { userId: ctx.user.id },
});
await tx.user.delete({
where: {
id: ctx.user.id,
},
});
await prisma.user.delete({
where: {
id: ctx.user.id,
},
});
}),
subscription: possiblyPublicProcedure.query(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
-- Clean up polls
DELETE FROM polls
WHERE user_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM users u WHERE u.id = polls.user_id);

-- Clean up participants
DELETE FROM participants pa
WHERE NOT EXISTS (
SELECT 1 FROM polls p
WHERE p.id = pa.poll_id
);

DELETE FROM participants
WHERE user_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM users u WHERE u.id = participants.user_id);

-- Clean up options
DELETE FROM options o
WHERE NOT EXISTS (
SELECT 1 FROM polls p
WHERE p.id = o.poll_id
);

-- Clean up votes
DELETE FROM votes v
WHERE NOT EXISTS (
SELECT 1 FROM polls p
WHERE p.id = v.poll_id
);

DELETE FROM votes v
WHERE NOT EXISTS (
SELECT 1 FROM participants p
WHERE p.poll_id = v.poll_id
AND p.id = v.participant_id
);

DELETE FROM votes v
WHERE NOT EXISTS (
SELECT 1 FROM options o
WHERE o.poll_id = v.poll_id
AND o.id = v.option_id
);

-- Clean up comments
DELETE FROM comments c
WHERE NOT EXISTS (
SELECT 1 FROM polls p
WHERE p.id = c.poll_id
);

DELETE FROM comments
WHERE user_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM users u WHERE u.id = comments.user_id);

-- Clean up watchers
DELETE FROM watchers w
WHERE NOT EXISTS (
SELECT 1 FROM polls p
WHERE p.id = w.poll_id
);

DELETE FROM watchers
WHERE user_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM users u WHERE u.id = watchers.user_id);

-- Clean up events
DELETE FROM events
WHERE user_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM users u WHERE u.id = events.user_id);

-- Handle subscription updates
UPDATE users
SET subscription_id = NULL
WHERE subscription_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM subscriptions s WHERE s.id = users.subscription_id);

-- DropIndex
DROP INDEX "accounts_user_id_idx";

-- DropIndex
DROP INDEX "comments_user_id_idx";

-- DropIndex
DROP INDEX "polls_guest_id_idx";

-- DropIndex
DROP INDEX "polls_user_id_idx";

-- DropIndex
DROP INDEX "watchers_user_id_idx";

-- CreateIndex
CREATE INDEX "polls_guest_id_idx" ON "polls"("guest_id");

-- AddForeignKey
ALTER TABLE "accounts" ADD CONSTRAINT "accounts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "users" ADD CONSTRAINT "users_subscription_id_fkey" FOREIGN KEY ("subscription_id") REFERENCES "subscriptions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "polls" ADD CONSTRAINT "polls_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "polls" ADD CONSTRAINT "polls_event_id_fkey" FOREIGN KEY ("event_id") REFERENCES "events"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "events" ADD CONSTRAINT "events_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watchers" ADD CONSTRAINT "watchers_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watchers" ADD CONSTRAINT "watchers_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "participants" ADD CONSTRAINT "participants_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "participants" ADD CONSTRAINT "participants_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "options" ADD CONSTRAINT "options_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "votes" ADD CONSTRAINT "votes_participant_id_fkey" FOREIGN KEY ("participant_id") REFERENCES "participants"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "votes" ADD CONSTRAINT "votes_option_id_fkey" FOREIGN KEY ("option_id") REFERENCES "options"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "votes" ADD CONSTRAINT "votes_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "comments" ADD CONSTRAINT "comments_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "comments" ADD CONSTRAINT "comments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Loading
Loading