2026-05-25 17:29:45 +08:00
|
|
|
import { zValidator } from '@hono/zod-validator'
|
|
|
|
|
import { Hono } from 'hono'
|
|
|
|
|
import { collectionService } from '@/services/collection/collection'
|
|
|
|
|
import { authzService, Permission } from '@/services/authz/authz'
|
|
|
|
|
import {
|
|
|
|
|
createCollectionRequestSchema,
|
|
|
|
|
updateCollectionRequestSchema,
|
|
|
|
|
listCollectionsRequestSchema,
|
|
|
|
|
CollectionInfo,
|
2026-05-25 20:26:03 +08:00
|
|
|
CollectionFilter,
|
2026-05-25 17:29:45 +08:00
|
|
|
} from '@/dtos/collection'
|
|
|
|
|
import type { Prisma } from '@/generated/prisma/client'
|
|
|
|
|
|
|
|
|
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
|
|
|
|
|
|
|
|
|
function toCollectionInfo(c: Prisma.CollectionGetPayload<Record<string, never>>): CollectionInfo {
|
|
|
|
|
return {
|
|
|
|
|
id: c.id,
|
|
|
|
|
name: c.name,
|
2026-05-25 20:26:03 +08:00
|
|
|
filter: c.filter as unknown as CollectionFilter,
|
2026-05-25 17:29:45 +08:00
|
|
|
projectId: c.projectId,
|
|
|
|
|
createdAt: c.createdAt.toISOString(),
|
|
|
|
|
updatedAt: c.updatedAt.toISOString(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const route = new Hono<{ Variables: { user: User } }>()
|
|
|
|
|
.post(
|
|
|
|
|
'/teams/:teamId/projects/:projectId/collections',
|
|
|
|
|
zValidator('json', createCollectionRequestSchema),
|
|
|
|
|
async (c) => {
|
|
|
|
|
const { teamId, projectId } = c.req.param()
|
|
|
|
|
const req = c.req.valid('json')
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
teamId,
|
|
|
|
|
projectId,
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Edit,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const collection = await collectionService.createCollection(projectId, req)
|
|
|
|
|
return c.json(toCollectionInfo(collection))
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.get(
|
|
|
|
|
'/teams/:teamId/projects/:projectId/collections',
|
|
|
|
|
zValidator('query', listCollectionsRequestSchema),
|
|
|
|
|
async (c) => {
|
|
|
|
|
const { teamId, projectId } = c.req.param()
|
|
|
|
|
const req = c.req.valid('query')
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
teamId,
|
|
|
|
|
projectId,
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Read,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const collections = await collectionService.listCollections(projectId, req)
|
|
|
|
|
return c.json({
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
data: collections.data.map((c: any) => toCollectionInfo(c)),
|
|
|
|
|
pageInfo: collections.pageInfo,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.get('/teams/:teamId/projects/:projectId/collections/:collectionId', async (c) => {
|
|
|
|
|
const { teamId, projectId, collectionId } = c.req.param()
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
teamId,
|
|
|
|
|
projectId,
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Read,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const collection = await collectionService.getCollection(collectionId)
|
|
|
|
|
if (!collection) {
|
|
|
|
|
return c.json({ error: 'Collection not found' }, 404)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.json(toCollectionInfo(collection))
|
|
|
|
|
})
|
|
|
|
|
.patch(
|
|
|
|
|
'/teams/:teamId/projects/:projectId/collections/:collectionId',
|
|
|
|
|
zValidator('json', updateCollectionRequestSchema),
|
|
|
|
|
async (c) => {
|
|
|
|
|
const { teamId, projectId, collectionId } = c.req.param()
|
|
|
|
|
const req = c.req.valid('json')
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
teamId,
|
|
|
|
|
projectId,
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Edit,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const collection = await collectionService.updateCollection(collectionId, req)
|
|
|
|
|
return c.json(toCollectionInfo(collection))
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.delete('/teams/:teamId/projects/:projectId/collections/:collectionId', async (c) => {
|
|
|
|
|
const { teamId, projectId, collectionId } = c.req.param()
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
teamId,
|
|
|
|
|
projectId,
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Edit,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await collectionService.deleteCollection(collectionId)
|
|
|
|
|
return c.json({ success: true })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default route
|