mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 16:28:42 +00:00
feat(collection): implement saved search results as collections
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
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,
|
||||
} from '@/dtos/collection'
|
||||
import type { Prisma } from '@/generated/prisma/client'
|
||||
import type { SearchFilter } from '@/dtos/search'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
function toCollectionInfo(c: Prisma.CollectionGetPayload<Record<string, never>>): CollectionInfo {
|
||||
return {
|
||||
id: c.id,
|
||||
name: c.name,
|
||||
filter: c.filter as unknown as SearchFilter,
|
||||
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
|
||||
Reference in New Issue
Block a user