haixunMaster/lib/services/delete-account.ts

41 lines
1.6 KiB
TypeScript
Raw Normal View History

2026-06-21 12:50:31 +00:00
import { prisma } from "@/lib/db";
import { deleteTopicWithRelations } from "@/lib/services/topic";
export async function deleteAccountWithRelations(accountId: string) {
const topics = await prisma.topic.findMany({
where: { accountId },
select: { id: true },
});
for (const topic of topics) {
await deleteTopicWithRelations(topic.id);
}
await prisma.draft.deleteMany({ where: { accountId } });
const publishedRows = await prisma.published.findMany({
where: { accountId },
select: { id: true },
});
const publishedIds = publishedRows.map((row) => row.id);
if (publishedIds.length > 0) {
const inboundRows = await prisma.inboundReply.findMany({
where: { publishedId: { in: publishedIds } },
select: { id: true },
});
const inboundIds = inboundRows.map((row) => row.id);
if (inboundIds.length > 0) {
await prisma.replyDraft.deleteMany({ where: { inboundReplyId: { in: inboundIds } } });
await prisma.inboundReply.deleteMany({ where: { id: { in: inboundIds } } });
}
await prisma.performanceSnapshot.deleteMany({ where: { publishedId: { in: publishedIds } } });
await prisma.published.deleteMany({ where: { id: { in: publishedIds } } });
}
await prisma.brandProfile.deleteMany({ where: { accountId } });
await prisma.productProfile.deleteMany({ where: { accountId } });
await prisma.scan.deleteMany({ where: { accountId } });
await prisma.backgroundJob.deleteMany({ where: { accountId } });
await prisma.actionLog.deleteMany({ where: { accountId } });
await prisma.account.delete({ where: { id: accountId } });
}