mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 14:08:45 +00:00
fix(backend,ui): fix version stack overcounting and ShareManager navigation issues
This commit is contained in:
@@ -318,12 +318,16 @@ export class AssetService {
|
||||
},
|
||||
})
|
||||
|
||||
await tx.asset.update({
|
||||
where: { id: oldParentId },
|
||||
data: { fileCount: { decrement: 1 } },
|
||||
})
|
||||
await this.updateAncestorsSize(tx, oldParentId, -sourceAsset.sizeByte)
|
||||
await this.updateAncestorsSize(tx, stackParentId, sourceAsset.sizeByte)
|
||||
const isUploading = sourceAsset.status === 'uploading'
|
||||
|
||||
if (!isUploading) {
|
||||
await tx.asset.update({
|
||||
where: { id: oldParentId },
|
||||
data: { fileCount: { decrement: 1 } },
|
||||
})
|
||||
await this.updateAncestorsSize(tx, oldParentId, -sourceAsset.sizeByte)
|
||||
await this.updateAncestorsSize(tx, stackParentId, sourceAsset.sizeByte)
|
||||
}
|
||||
|
||||
const firstIndex = generateKeyBetween(null, null)
|
||||
const secondIndex = generateKeyBetween(firstIndex, null)
|
||||
@@ -341,8 +345,8 @@ export class AssetService {
|
||||
await tx.asset.update({
|
||||
where: { id: stack.id },
|
||||
data: {
|
||||
fileCount: 2,
|
||||
sizeByte: destFile.sizeByte + sourceAsset.sizeByte,
|
||||
fileCount: isUploading ? 1 : 2,
|
||||
sizeByte: isUploading ? destFile.sizeByte : destFile.sizeByte + sourceAsset.sizeByte,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { describe, expect, it, vi, beforeEach } from 'vitest'
|
||||
import { prisma } from '@/db'
|
||||
import { setupTestDbHooks } from '@/db-test-hooks'
|
||||
import { uploadService } from './upload'
|
||||
import { s3Service } from '@/services/s3/s3'
|
||||
import { AssetStatus, AssetType, WorkflowTaskType } from '@/generated/prisma/client'
|
||||
|
||||
vi.mock('@/services/s3/s3', () => ({
|
||||
@@ -179,4 +180,109 @@ describe('UploadService', () => {
|
||||
})
|
||||
expect(workflowTask).toBeNull()
|
||||
})
|
||||
|
||||
it('should create a version stack when parentId is a file', async () => {
|
||||
// Create an existing file
|
||||
const fileA = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fileA.txt',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
parentId,
|
||||
status: AssetStatus.processed,
|
||||
sizeByte: 1000,
|
||||
},
|
||||
})
|
||||
|
||||
const req = {
|
||||
parentId: fileA.id,
|
||||
files: [
|
||||
{
|
||||
name: 'fileA_v2.txt',
|
||||
id: 'v2',
|
||||
size: 2000,
|
||||
type: 'file' as const,
|
||||
mediaType: 'text/plain',
|
||||
children: [],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const resp = await uploadService.createUploadTask(userId, req)
|
||||
expect(resp.taskId).toBeDefined()
|
||||
|
||||
// Should have created a version stack
|
||||
const stack = await prisma.asset.findFirst({
|
||||
where: { type: AssetType.version_stack, parentId },
|
||||
})
|
||||
expect(stack).toBeDefined()
|
||||
expect(stack?.fileCount).toBe(1) // Only fileA is "uploaded", the new one is still "uploading"
|
||||
expect(stack?.sizeByte).toBe(1000)
|
||||
|
||||
// The new asset should be inside the stack
|
||||
const newAsset = await prisma.asset.findFirst({
|
||||
where: { taskId: resp.taskId, parentId: stack!.id },
|
||||
})
|
||||
expect(newAsset).toBeDefined()
|
||||
expect(newAsset?.status).toBe(AssetStatus.uploading)
|
||||
})
|
||||
|
||||
it('should correctly update counts when confirming a version in a stack', async () => {
|
||||
// Create a stack with one existing file
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
projectId,
|
||||
parentId,
|
||||
status: AssetStatus.uploaded,
|
||||
fileCount: 1,
|
||||
sizeByte: 1000,
|
||||
},
|
||||
})
|
||||
const fileV1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'v1.txt',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
parentId: stack.id,
|
||||
status: AssetStatus.processed,
|
||||
sizeByte: 1000,
|
||||
},
|
||||
})
|
||||
|
||||
// Create an uploading asset in that stack
|
||||
const task = await prisma.task.create({
|
||||
data: { creatorId: userId, total: 1, uploaded: 0, type: 'upload' },
|
||||
})
|
||||
const fileV2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'v2.txt',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
parentId: stack.id,
|
||||
status: AssetStatus.uploading,
|
||||
key: 'v2-key',
|
||||
sizeByte: 2000,
|
||||
},
|
||||
})
|
||||
|
||||
vi.spyOn(s3Service, 'getObjectSize').mockResolvedValue(2000)
|
||||
|
||||
// Manually set parent folder size to match initial stack size for realistic aggregation
|
||||
await prisma.asset.update({
|
||||
where: { id: parentId },
|
||||
data: { sizeByte: 1000 },
|
||||
})
|
||||
|
||||
await uploadService.confirmFileUpload(userId, task.id, { fileId: fileV2.id })
|
||||
|
||||
const updatedStack = await prisma.asset.findUnique({ where: { id: stack.id } })
|
||||
expect(updatedStack?.fileCount).toBe(2)
|
||||
expect(updatedStack?.sizeByte).toBe(3000)
|
||||
|
||||
// Verify parent folder size (initially 1000 from stack)
|
||||
const parentFolder = await prisma.asset.findUnique({ where: { id: parentId } })
|
||||
expect(parentFolder?.sizeByte).toBe(3000)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
ContextMenuSub,
|
||||
ContextMenuSubContent,
|
||||
ContextMenuSubTrigger,
|
||||
ContextMenuPortal,
|
||||
} from '@/ui/components/ui/context-menu'
|
||||
import {
|
||||
Download,
|
||||
@@ -177,16 +178,18 @@ export function FileBrowserContextMenu({
|
||||
<Link className="mr-2 h-4 w-4" />
|
||||
<span>Add to Share Links</span>
|
||||
</ContextMenuSubTrigger>
|
||||
<ContextMenuSubContent className="w-48">
|
||||
{shareLinks.map((share) => (
|
||||
<ContextMenuItem
|
||||
key={share.id}
|
||||
onSelect={() => onAddToShareLink?.(share.id, itemsToModify)}
|
||||
>
|
||||
<span className="truncate">{share.name}</span>
|
||||
</ContextMenuItem>
|
||||
))}
|
||||
</ContextMenuSubContent>
|
||||
<ContextMenuPortal>
|
||||
<ContextMenuSubContent className="w-48">
|
||||
{shareLinks.map((share) => (
|
||||
<ContextMenuItem
|
||||
key={share.id}
|
||||
onSelect={() => onAddToShareLink?.(share.id, itemsToModify)}
|
||||
>
|
||||
<span className="truncate">{share.name}</span>
|
||||
</ContextMenuItem>
|
||||
))}
|
||||
</ContextMenuSubContent>
|
||||
</ContextMenuPortal>
|
||||
</ContextMenuSub>
|
||||
)}
|
||||
</ContextMenuContent>
|
||||
@@ -241,16 +244,18 @@ export function FileBrowserContextMenu({
|
||||
<Link className="mr-2 h-4 w-4" />
|
||||
<span>Add to Share Links</span>
|
||||
</ContextMenuSubTrigger>
|
||||
<ContextMenuSubContent className="w-48">
|
||||
{shareLinks.map((share) => (
|
||||
<ContextMenuItem
|
||||
key={share.id}
|
||||
onSelect={() => onAddToShareLink?.(share.id, itemsToModify)}
|
||||
>
|
||||
<span className="truncate">{share.name}</span>
|
||||
</ContextMenuItem>
|
||||
))}
|
||||
</ContextMenuSubContent>
|
||||
<ContextMenuPortal>
|
||||
<ContextMenuSubContent className="w-48">
|
||||
{shareLinks.map((share) => (
|
||||
<ContextMenuItem
|
||||
key={share.id}
|
||||
onSelect={() => onAddToShareLink?.(share.id, itemsToModify)}
|
||||
>
|
||||
<span className="truncate">{share.name}</span>
|
||||
</ContextMenuItem>
|
||||
))}
|
||||
</ContextMenuSubContent>
|
||||
</ContextMenuPortal>
|
||||
</ContextMenuSub>
|
||||
)}
|
||||
</ContextMenuContent>
|
||||
|
||||
@@ -56,10 +56,12 @@ export default function ShareManager({
|
||||
const [ancestorFolders, setAncestorFolders] = useState<AncestorFolder[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
if (shareRootId && !currentFolderId) {
|
||||
if (shareRootId) {
|
||||
setCurrentFolderId(shareRootId)
|
||||
setAncestorFolders([])
|
||||
setSelectedIds(new Set())
|
||||
}
|
||||
}, [shareRootId, currentFolderId])
|
||||
}, [shareRootId, shareId])
|
||||
|
||||
const setProjectState = useTopNavStore((s) => s.setProjectState)
|
||||
const clearProjectState = useTopNavStore((s) => s.clearProjectState)
|
||||
@@ -157,7 +159,7 @@ export default function ShareManager({
|
||||
: folders.find((f) => f.id === currentFolderId)?.name || shareLink.name,
|
||||
type: 'folder',
|
||||
},
|
||||
isRootFolder: currentFolderId === shareRootId,
|
||||
isRootFolder: false, // Ensure share link name or subfolder is always shown in TopNav
|
||||
shareId,
|
||||
onFolderClick: handleBreadcrumbClick,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user