mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 11:48:43 +00:00
feat(collection): implement saved search results as collections
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { prisma } from '@/db'
|
||||
import { setupTestDbHooks } from '@/db-test-hooks'
|
||||
import { collectionService } from './collection'
|
||||
import { projectService } from '../project/project'
|
||||
|
||||
describe('CollectionService', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
async function setupProject() {
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Test User', email: 'test@example.com' },
|
||||
})
|
||||
const team = await prisma.team.create({
|
||||
data: {
|
||||
name: 'Test Team',
|
||||
settings: {
|
||||
enablePublicSignup: true,
|
||||
transcode: { videoStrategy: 'disable', imageStrategy: 'disable' },
|
||||
},
|
||||
sandbox: { create: {} },
|
||||
},
|
||||
})
|
||||
await prisma.teamMember.create({
|
||||
data: {
|
||||
teamId: team.id,
|
||||
userId: user.id,
|
||||
role: 'owner',
|
||||
scope: 'team',
|
||||
},
|
||||
})
|
||||
const project = await projectService.createProject(user, {
|
||||
teamId: team.id,
|
||||
name: 'Test Project',
|
||||
})
|
||||
return { team, project }
|
||||
}
|
||||
|
||||
it('can create, get, update, and delete a collection', async () => {
|
||||
const { project } = await setupProject()
|
||||
|
||||
// Create
|
||||
const collection = await collectionService.createCollection(project.id, {
|
||||
name: 'Test Collection',
|
||||
filter: {
|
||||
operator: 'AND',
|
||||
conditions: [{ field: 'name', operator: 'contains', value: 'test' }],
|
||||
recursively: true,
|
||||
},
|
||||
})
|
||||
expect(collection.name).toBe('Test Collection')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((collection.filter as any).conditions[0].value).toBe('test')
|
||||
|
||||
// Get
|
||||
const fetched = await collectionService.getCollection(collection.id)
|
||||
expect(fetched?.id).toBe(collection.id)
|
||||
|
||||
// Update
|
||||
const updated = await collectionService.updateCollection(collection.id, {
|
||||
name: 'Updated Name',
|
||||
filter: {
|
||||
operator: 'AND',
|
||||
conditions: [{ field: 'rating', operator: 'eq', value: 5 }],
|
||||
recursively: true,
|
||||
},
|
||||
})
|
||||
expect(updated.name).toBe('Updated Name')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((updated.filter as any).conditions[0].field).toBe('rating')
|
||||
|
||||
// Delete
|
||||
await collectionService.deleteCollection(collection.id)
|
||||
const deleted = await collectionService.getCollection(collection.id)
|
||||
expect(deleted).toBeNull()
|
||||
})
|
||||
|
||||
it('can list collections for a project with pagination', async () => {
|
||||
const { project } = await setupProject()
|
||||
|
||||
// Create multiple collections
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await collectionService.createCollection(project.id, {
|
||||
name: `Collection ${i}`,
|
||||
filter: { operator: 'AND', conditions: [], recursively: true },
|
||||
})
|
||||
}
|
||||
|
||||
// List first page
|
||||
const list1 = await collectionService.listCollections(project.id, {
|
||||
first: 2,
|
||||
includeCount: true,
|
||||
})
|
||||
expect(list1.data.length).toBe(2)
|
||||
expect(list1.pageInfo.total).toBe(5)
|
||||
expect(list1.pageInfo.cursor).toBeDefined()
|
||||
|
||||
// List second page
|
||||
const list2 = await collectionService.listCollections(project.id, {
|
||||
first: 2,
|
||||
after: list1.pageInfo.cursor,
|
||||
})
|
||||
expect(list2.data.length).toBe(2)
|
||||
expect(list2.data[0].name).not.toBe(list1.data[0].name)
|
||||
|
||||
// List last page
|
||||
const list3 = await collectionService.listCollections(project.id, {
|
||||
first: 2,
|
||||
after: list2.pageInfo.cursor,
|
||||
})
|
||||
expect(list3.data.length).toBe(1)
|
||||
expect(list3.pageInfo.cursor).toBeUndefined()
|
||||
})
|
||||
|
||||
it('cascades delete when project is deleted', async () => {
|
||||
const { project } = await setupProject()
|
||||
|
||||
await collectionService.createCollection(project.id, {
|
||||
name: 'To be deleted',
|
||||
filter: { operator: 'AND', conditions: [], recursively: true },
|
||||
})
|
||||
|
||||
const countBefore = await prisma.collection.count({
|
||||
where: { projectId: project.id },
|
||||
})
|
||||
expect(countBefore).toBe(1)
|
||||
|
||||
await projectService.deleteProject(project.id)
|
||||
|
||||
const countAfter = await prisma.collection.count({
|
||||
where: { projectId: project.id },
|
||||
})
|
||||
expect(countAfter).toBe(0)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,59 @@
|
||||
import { prisma } from '@/db'
|
||||
import { paginateQuery } from '../pagination'
|
||||
import {
|
||||
CreateCollectionRequest,
|
||||
UpdateCollectionRequest,
|
||||
ListCollectionsRequest,
|
||||
} from '@/dtos/collection'
|
||||
|
||||
export class CollectionService {
|
||||
async createCollection(projectId: string, req: CreateCollectionRequest) {
|
||||
return await prisma.collection.create({
|
||||
data: {
|
||||
name: req.name,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
filter: req.filter as any,
|
||||
projectId,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async updateCollection(collectionId: string, req: UpdateCollectionRequest) {
|
||||
return await prisma.collection.update({
|
||||
where: { id: collectionId },
|
||||
data: {
|
||||
...(req.name && { name: req.name }),
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
...(req.filter && { filter: req.filter as any }),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async deleteCollection(collectionId: string) {
|
||||
return await prisma.collection.delete({
|
||||
where: { id: collectionId },
|
||||
})
|
||||
}
|
||||
|
||||
async getCollection(collectionId: string) {
|
||||
return await prisma.collection.findUnique({
|
||||
where: { id: collectionId },
|
||||
})
|
||||
}
|
||||
|
||||
async listCollections(projectId: string, req: ListCollectionsRequest) {
|
||||
return await paginateQuery(
|
||||
(skip, take) =>
|
||||
prisma.collection.findMany({
|
||||
where: { projectId },
|
||||
orderBy: { id: 'desc' },
|
||||
skip,
|
||||
take,
|
||||
}),
|
||||
() => prisma.collection.count({ where: { projectId } }),
|
||||
req,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export const collectionService = new CollectionService()
|
||||
Reference in New Issue
Block a user