refactor: 修复 modules 三层架构违规
- auth: actionDeleteAccount 改用 service+repo,forgot-password 完整三层实现 - card: serviceCheckCardOwnership 替代直接调用 repository - deck: 移除 service 层的 use server 指令 - dictionary: 数据转换逻辑从 repository 移到 service - ocr: 认证移到 action 层,跨模块调用改用 service - translator: genIPA/genLanguage 改用 service 层
This commit is contained in:
@@ -5,7 +5,6 @@ import { headers } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { ValidateError } from "@/lib/errors";
|
||||
import { createLogger } from "@/lib/logger";
|
||||
import { prisma } from "@/lib/db";
|
||||
import {
|
||||
ActionInputGetUserProfileByUsername,
|
||||
ActionInputSignIn,
|
||||
@@ -20,7 +19,8 @@ import {
|
||||
import {
|
||||
serviceGetUserProfileByUsername,
|
||||
serviceSignIn,
|
||||
serviceSignUp
|
||||
serviceSignUp,
|
||||
serviceDeleteAccount
|
||||
} from "./auth-service";
|
||||
|
||||
// Re-export types for use in components
|
||||
@@ -194,75 +194,11 @@ export async function actionDeleteAccount(): Promise<ActionOutputDeleteAccount>
|
||||
return { success: false, message: "Unauthorized" };
|
||||
}
|
||||
|
||||
const userId = session.user.id;
|
||||
const result = await serviceDeleteAccount({ userId: session.user.id });
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
// Delete in correct order to avoid foreign key constraints
|
||||
// 1. Revlogs (depend on cards)
|
||||
await tx.revlog.deleteMany({
|
||||
where: { card: { note: { userId } } }
|
||||
});
|
||||
|
||||
// 2. Cards (depend on notes and decks)
|
||||
await tx.card.deleteMany({
|
||||
where: { note: { userId } }
|
||||
});
|
||||
|
||||
// 3. Notes (depend on note types)
|
||||
await tx.note.deleteMany({
|
||||
where: { userId }
|
||||
});
|
||||
|
||||
// 4. Note types
|
||||
await tx.noteType.deleteMany({
|
||||
where: { userId }
|
||||
});
|
||||
|
||||
// 5. Deck favorites
|
||||
await tx.deckFavorite.deleteMany({
|
||||
where: { userId }
|
||||
});
|
||||
|
||||
// 6. Decks
|
||||
await tx.deck.deleteMany({
|
||||
where: { userId }
|
||||
});
|
||||
|
||||
// 7. Follows (both as follower and following)
|
||||
await tx.follow.deleteMany({
|
||||
where: {
|
||||
OR: [
|
||||
{ followerId: userId },
|
||||
{ followingId: userId }
|
||||
]
|
||||
}
|
||||
});
|
||||
|
||||
// 8. Dictionary lookups
|
||||
await tx.dictionaryLookUp.deleteMany({
|
||||
where: { userId }
|
||||
});
|
||||
|
||||
// 9. Translation history
|
||||
await tx.translationHistory.deleteMany({
|
||||
where: { userId }
|
||||
});
|
||||
|
||||
// 10. Sessions
|
||||
await tx.session.deleteMany({
|
||||
where: { userId }
|
||||
});
|
||||
|
||||
// 11. Accounts
|
||||
await tx.account.deleteMany({
|
||||
where: { userId }
|
||||
});
|
||||
|
||||
// 12. Finally, delete the user
|
||||
await tx.user.delete({
|
||||
where: { id: userId }
|
||||
});
|
||||
});
|
||||
if (!result.success) {
|
||||
return { success: false, message: "Failed to delete account" };
|
||||
}
|
||||
|
||||
return { success: true, message: "Account deleted successfully" };
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user