refactor(api): enforce layered architecture by moving db queries to core services

This commit is contained in:
yiling
2026-08-01 16:25:01 +08:00
parent fb66166b49
commit 97069042b2
26 changed files with 256 additions and 363 deletions
+5 -11
View File
@@ -1,6 +1,7 @@
import type { Prisma } from '@shumai/db'
import { auth } from '@shumai/core/src/auth/auth'
import { prisma } from '@shumai/db'
import { userService } from '@shumai/core/src/user/user'
import { teamService } from '@shumai/core/src/team/team'
import { createMiddleware } from 'hono/factory'
type User = Prisma.UserGetPayload<Record<string, never>>
@@ -24,9 +25,7 @@ export const authMiddleware = createMiddleware<{
}
try {
const user = await prisma.user.findUnique({
where: { id: session.user.id },
})
const user = await userService.getUserById(session.user.id)
if (!user) {
return c.json({ error: 'User not found' }, 401)
@@ -38,14 +37,9 @@ export const authMiddleware = createMiddleware<{
const method = c.req.method.toUpperCase()
const path = c.req.path
if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(method) && !path.endsWith('/search')) {
const member = await prisma.teamMember.findFirst({
where: {
userId: user.id,
role: { in: ['owner', 'editor'] },
},
})
const hasWritable = await teamService.hasWritableRoleInAnyTeam(user.id)
if (!member) {
if (!hasWritable) {
return c.json({ error: 'System is in read-only mode' }, 403)
}
}