mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 10:48:42 +00:00
feat(version-stack): add manage versions dialog and version removal (#311)
* feat(version-stack): add manage versions dialog and version removal * fix(webui): remove ellipsis and add pointer cursor to manage versions button * fix(version-stack): handle symlink deduplication and name sync for share links * refactor(version-stack): extract shared symlink dedup helper The 'repoint first symlink per share folder, delete duplicates with fileCount decrement' logic was triplicated across createVersionStack, reparentAssets, and reparentFileToFile with subtle variations. Extract it into dedupeSymlinksToTarget() in packages/core/src/asset/symlink.ts and call it from all three sites (behavior unchanged), with a dedicated test suite covering the edge cases.
This commit is contained in:
@@ -2,6 +2,7 @@ import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { VersionStackService } from './versionStack'
|
||||
import { assetService } from '../asset/asset'
|
||||
import { AssetType } from '@shumai/db'
|
||||
|
||||
describe('VersionStackService', () => {
|
||||
@@ -207,4 +208,299 @@ describe('VersionStackService', () => {
|
||||
expect(children.map((c) => c.id).includes(file3.id)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('removeVersionFromStack', () => {
|
||||
it('removes a version from a 3-version stack and turns it into a standalone file adjacent to the stack', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const proj = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
||||
})
|
||||
const root = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'root',
|
||||
type: AssetType.folder,
|
||||
status: 'uploaded',
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
const parent = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Parent Folder',
|
||||
type: AssetType.folder,
|
||||
projectId: proj.id,
|
||||
parentId: root.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 3,
|
||||
},
|
||||
})
|
||||
|
||||
const file1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file1.txt',
|
||||
type: AssetType.file,
|
||||
parentId: parent.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
sortIndex: 'a0',
|
||||
},
|
||||
})
|
||||
const file2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file2.txt',
|
||||
type: AssetType.file,
|
||||
parentId: parent.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 200,
|
||||
},
|
||||
})
|
||||
const file3 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file3.txt',
|
||||
type: AssetType.file,
|
||||
parentId: parent.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 300,
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await service.createVersionStack({
|
||||
fileIds: [file1.id, file2.id, file3.id],
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
// Stack should have 3 versions
|
||||
expect(stack.fileCount).toBe(3)
|
||||
expect(Number(stack.sizeByte)).toBe(600)
|
||||
|
||||
// Parent fileCount should be 1 (the stack)
|
||||
const parentBefore = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||
expect(parentBefore?.fileCount).toBe(1)
|
||||
|
||||
// Remove file2 from stack
|
||||
await service.removeVersionFromStack({
|
||||
stackId: stack.id,
|
||||
fileId: file2.id,
|
||||
})
|
||||
|
||||
// File2 should now be in parent folder
|
||||
const updatedFile2 = await prisma.asset.findUnique({ where: { id: file2.id } })
|
||||
expect(updatedFile2?.parentId).toBe(parent.id)
|
||||
expect(updatedFile2?.sortIndex).toBeDefined()
|
||||
expect(updatedFile2!.sortIndex! > stack.sortIndex!).toBe(true)
|
||||
|
||||
// Parent fileCount should be 2 (stack + file2)
|
||||
const updatedParent = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||
expect(updatedParent?.fileCount).toBe(2)
|
||||
|
||||
// Stack should still exist with 2 versions (file1 and file3)
|
||||
const updatedStack = await prisma.asset.findUnique({ where: { id: stack.id } })
|
||||
expect(updatedStack).toBeDefined()
|
||||
expect(updatedStack?.fileCount).toBe(2)
|
||||
expect(Number(updatedStack?.sizeByte)).toBe(400)
|
||||
|
||||
const remainingChildren = await prisma.asset.findMany({
|
||||
where: { parentId: stack.id },
|
||||
})
|
||||
expect(remainingChildren).toHaveLength(2)
|
||||
expect(remainingChildren.map((c) => c.id).sort()).toEqual([file1.id, file3.id].sort())
|
||||
})
|
||||
|
||||
it('dissolves the version stack when removing down to 1 remaining version', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const proj = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
||||
})
|
||||
const root = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'root',
|
||||
type: AssetType.folder,
|
||||
status: 'uploaded',
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
const parent = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Parent Folder',
|
||||
type: AssetType.folder,
|
||||
projectId: proj.id,
|
||||
parentId: root.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 2,
|
||||
},
|
||||
})
|
||||
|
||||
const file1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file1.txt',
|
||||
type: AssetType.file,
|
||||
parentId: parent.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
sortIndex: 'a0',
|
||||
},
|
||||
})
|
||||
const file2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file2.txt',
|
||||
type: AssetType.file,
|
||||
parentId: parent.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 200,
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await service.createVersionStack({
|
||||
fileIds: [file1.id, file2.id],
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
// Remove file2 from stack
|
||||
await service.removeVersionFromStack({
|
||||
stackId: stack.id,
|
||||
fileId: file2.id,
|
||||
})
|
||||
|
||||
// Stack asset should be deleted
|
||||
const deletedStack = await prisma.asset.findUnique({ where: { id: stack.id } })
|
||||
expect(deletedStack).toBeNull()
|
||||
|
||||
// File1 should now be in parent folder and inherited stack's sortIndex
|
||||
const updatedFile1 = await prisma.asset.findUnique({ where: { id: file1.id } })
|
||||
expect(updatedFile1?.parentId).toBe(parent.id)
|
||||
expect(updatedFile1?.sortIndex).toBe('a0')
|
||||
|
||||
// File2 should also be in parent folder
|
||||
const updatedFile2 = await prisma.asset.findUnique({ where: { id: file2.id } })
|
||||
expect(updatedFile2?.parentId).toBe(parent.id)
|
||||
|
||||
// Parent fileCount should be 2
|
||||
const updatedParent = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||
expect(updatedParent?.fileCount).toBe(2)
|
||||
})
|
||||
|
||||
it('updates share link symlinks when normal asset becomes version stack and when stack dissolves back to normal asset', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const proj = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
||||
})
|
||||
const root = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'root',
|
||||
type: AssetType.folder,
|
||||
status: 'uploaded',
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
const shareRoot = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'share-root',
|
||||
type: AssetType.share_root,
|
||||
status: 'uploaded',
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
fileCount: 0,
|
||||
},
|
||||
})
|
||||
const shareLink = await prisma.shareLink.create({
|
||||
data: {
|
||||
name: 'My Share',
|
||||
projectId: proj.id,
|
||||
rootFolderId: shareRoot.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
|
||||
const file1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file1.txt',
|
||||
type: AssetType.file,
|
||||
parentId: root.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
},
|
||||
})
|
||||
const file2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file2.txt',
|
||||
type: AssetType.file,
|
||||
parentId: root.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 200,
|
||||
},
|
||||
})
|
||||
|
||||
// Add file1 to share link (create symlink)
|
||||
const symlink = await prisma.asset.create({
|
||||
data: {
|
||||
name: file1.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareLink.rootFolderId,
|
||||
targetId: file1.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// 1. Group file1 and file2 into a version stack
|
||||
const stack = await service.createVersionStack({
|
||||
fileIds: [file1.id, file2.id],
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
// Symlink in share link should now point to stack.id
|
||||
const symlinkAfterStack = await prisma.asset.findUnique({ where: { id: symlink.id } })
|
||||
expect(symlinkAfterStack?.targetId).toBe(stack.id)
|
||||
|
||||
// 2. Remove file2 from stack -> stack dissolves -> only file1 remains
|
||||
await service.removeVersionFromStack({
|
||||
stackId: stack.id,
|
||||
fileId: file2.id,
|
||||
})
|
||||
|
||||
// Symlink in share link should now point directly to file1.id and have file1's name
|
||||
const symlinkAfterDissolve = await prisma.asset.findUnique({ where: { id: symlink.id } })
|
||||
expect(symlinkAfterDissolve?.targetId).toBe(file1.id)
|
||||
expect(symlinkAfterDissolve?.name).toBe(file1.name)
|
||||
|
||||
// Listing share link files returns file1 with correct name
|
||||
const shareFiles = await assetService.listChildren({
|
||||
assetId: shareLink.rootFolderId,
|
||||
assetType: AssetType.file,
|
||||
})
|
||||
expect(shareFiles.data).toHaveLength(1)
|
||||
expect(shareFiles.data[0].name).toBe(file1.name)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { type Asset, AssetType } from '@shumai/db'
|
||||
import { ChangeStackFileVersionParams, CreateVersionStackParams } from '@shumai/dtos'
|
||||
import {
|
||||
ChangeStackFileVersionParams,
|
||||
CreateVersionStackParams,
|
||||
RemoveStackVersionParams,
|
||||
} from '@shumai/dtos'
|
||||
import { dedupeSymlinksToTarget } from '../asset/symlink'
|
||||
|
||||
function generateSortIndex(previous?: string | null): string {
|
||||
if (!previous) return generateKeyBetween(null, null)
|
||||
@@ -95,6 +100,9 @@ export class VersionStackService {
|
||||
},
|
||||
})
|
||||
|
||||
// Update existing symlinks pointing to any of the stacked files
|
||||
await dedupeSymlinksToTarget(tx, { targetIds: fileIds, newTargetId: stack.id })
|
||||
|
||||
return stack
|
||||
})
|
||||
}
|
||||
@@ -127,7 +135,7 @@ export class VersionStackService {
|
||||
|
||||
if (beforeId === '-1') {
|
||||
const lastFile = await tx.asset.findFirst({
|
||||
where: { parentId: stackId },
|
||||
where: { parentId: stackId, id: { not: fileId } },
|
||||
orderBy: { sortIndex: 'desc' },
|
||||
})
|
||||
newSortIndex = generateKeyBetween(lastFile?.sortIndex || null, null)
|
||||
@@ -145,6 +153,7 @@ export class VersionStackService {
|
||||
const prevFile = await tx.asset.findFirst({
|
||||
where: {
|
||||
parentId: stackId,
|
||||
id: { not: fileId },
|
||||
sortIndex: {
|
||||
lt: beforeFile.sortIndex!,
|
||||
},
|
||||
@@ -168,6 +177,123 @@ export class VersionStackService {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async removeVersionFromStack(params: RemoveStackVersionParams): Promise<void> {
|
||||
const { stackId, fileId } = params
|
||||
|
||||
await this.prismaClient.$transaction(async (tx) => {
|
||||
const stack = await tx.asset.findUnique({
|
||||
where: { id: stackId },
|
||||
})
|
||||
if (!stack) {
|
||||
throw new Error('stack not found')
|
||||
}
|
||||
if (stack.type !== AssetType.version_stack) {
|
||||
throw new Error('asset is not a version stack')
|
||||
}
|
||||
if (!stack.parentId) {
|
||||
throw new Error('stack has no parent')
|
||||
}
|
||||
|
||||
const fileToRemove = await tx.asset.findUnique({
|
||||
where: { id: fileId },
|
||||
})
|
||||
if (!fileToRemove) {
|
||||
throw new Error('file not found')
|
||||
}
|
||||
if (fileToRemove.parentId !== stackId) {
|
||||
throw new Error('file is not in the stack')
|
||||
}
|
||||
|
||||
// Find next sibling in parent folder after the stack to insert the removed file adjacent to the stack
|
||||
const nextSibling = await tx.asset.findFirst({
|
||||
where: {
|
||||
parentId: stack.parentId,
|
||||
id: { not: stack.id },
|
||||
sortIndex: {
|
||||
gt: stack.sortIndex || '',
|
||||
},
|
||||
},
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
})
|
||||
|
||||
let newSortIndex: string
|
||||
try {
|
||||
newSortIndex = generateKeyBetween(stack.sortIndex || null, nextSibling?.sortIndex || null)
|
||||
} catch {
|
||||
newSortIndex = generateKeyBetween(null, null)
|
||||
}
|
||||
|
||||
await tx.asset.update({
|
||||
where: { id: fileId },
|
||||
data: {
|
||||
parentId: stack.parentId,
|
||||
sortIndex: newSortIndex,
|
||||
},
|
||||
})
|
||||
|
||||
// Increment parent folder fileCount for the newly standalone file
|
||||
await tx.asset.update({
|
||||
where: { id: stack.parentId },
|
||||
data: {
|
||||
fileCount: { increment: 1 },
|
||||
},
|
||||
})
|
||||
|
||||
// Remaining files in stack
|
||||
const remainingFiles = await tx.asset.findMany({
|
||||
where: { parentId: stackId, isDeleted: false, id: { not: fileId } },
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
})
|
||||
|
||||
if (remainingFiles.length > 1) {
|
||||
let totalSize = 0
|
||||
for (const f of remainingFiles) {
|
||||
totalSize += Number(f.sizeByte)
|
||||
}
|
||||
await tx.asset.update({
|
||||
where: { id: stack.id },
|
||||
data: {
|
||||
fileCount: remainingFiles.length,
|
||||
sizeByte: totalSize,
|
||||
},
|
||||
})
|
||||
} else if (remainingFiles.length === 1) {
|
||||
const lastChild = remainingFiles[0]
|
||||
|
||||
// Update symlinks pointing to stack.id to point to the single remaining child
|
||||
await tx.asset.updateMany({
|
||||
where: { targetId: stack.id, type: AssetType.symlink },
|
||||
data: { targetId: lastChild.id, name: lastChild.name },
|
||||
})
|
||||
|
||||
// Reparent the single remaining child to stack.parentId, keeping stack's sortIndex
|
||||
await tx.asset.update({
|
||||
where: { id: lastChild.id },
|
||||
data: {
|
||||
parentId: stack.parentId,
|
||||
sortIndex: stack.sortIndex,
|
||||
},
|
||||
})
|
||||
|
||||
// Delete the stack asset (parent folder file count is unchanged: -1 stack, +1 remaining child)
|
||||
await tx.asset.delete({
|
||||
where: { id: stack.id },
|
||||
})
|
||||
} else {
|
||||
await tx.asset.deleteMany({
|
||||
where: { targetId: stack.id, type: AssetType.symlink },
|
||||
})
|
||||
await tx.asset.delete({
|
||||
where: { id: stack.id },
|
||||
})
|
||||
await tx.asset.update({
|
||||
where: { id: stack.parentId },
|
||||
data: { fileCount: { decrement: 1 } },
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const versionStackService = new VersionStackService()
|
||||
|
||||
Reference in New Issue
Block a user