feat: migrate Canvas and Groups API to Prisma ORM
This commit is contained in:
@@ -1,34 +1,41 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { canvases, userGroups } from '~/server/utils/db'
|
||||
import { prisma } from '~/server/utils/prisma'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const user = requireAuth(event)
|
||||
|
||||
// Get all canvases the user can access
|
||||
const userGroupIds = Array.from(userGroups.values())
|
||||
.filter(membership => membership.userId === user.id)
|
||||
.map(membership => membership.groupId)
|
||||
const userGroups = await prisma.groupMember.findMany({
|
||||
where: { userId: user.id },
|
||||
select: { groupId: true }
|
||||
})
|
||||
const groupIds = userGroups.map(m => m.groupId)
|
||||
|
||||
const accessibleCanvases = Array.from(canvases.values()).filter(canvas => {
|
||||
// User can see their own canvases
|
||||
if (canvas.ownerId === user.id) return true
|
||||
// User can see public canvases
|
||||
if (canvas.isPublic) return true
|
||||
// User can see group canvases they're a member of
|
||||
if (canvas.groupId && userGroupIds.includes(canvas.groupId)) return true
|
||||
return false
|
||||
const canvasList = await prisma.canvas.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{ ownerId: user.id },
|
||||
{ isPublic: true },
|
||||
{ groupId: { in: groupIds } }
|
||||
]
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
ownerId: true,
|
||||
groupId: true,
|
||||
isPublic: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
nodes: true
|
||||
},
|
||||
orderBy: { updatedAt: 'desc' }
|
||||
})
|
||||
|
||||
const canvasList = accessibleCanvases.map(canvas => ({
|
||||
id: canvas.id,
|
||||
name: canvas.name,
|
||||
ownerId: canvas.ownerId,
|
||||
createdAt: canvas.createdAt,
|
||||
updatedAt: canvas.updatedAt,
|
||||
isPublic: canvas.isPublic,
|
||||
groupId: canvas.groupId,
|
||||
nodeCount: canvas.nodes.length
|
||||
}))
|
||||
|
||||
return { canvases: canvasList }
|
||||
return {
|
||||
canvases: canvasList.map(c => ({
|
||||
...c,
|
||||
nodeCount: Array.isArray(c.nodes) ? (c.nodes as any[]).length : 0,
|
||||
nodes: undefined
|
||||
}))
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user