mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 12:24:39 +00:00
refactor(core): fix fileCount and createAssetsRecursively transaction
This commit is contained in:
@@ -559,6 +559,63 @@ describe('AssetService', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('fileCount verification', () => {
|
||||
it('createAsset should increment parent fileCount', async () => {
|
||||
const { user, assets, project } = await setupBasicAssets()
|
||||
|
||||
const parent = assets.folderA
|
||||
const initialCount = parent.fileCount // should be 2 from setup (fileA1, fileA2)
|
||||
|
||||
// Create a new folder inside folderA
|
||||
await assetService.createAsset({
|
||||
name: 'newFolder',
|
||||
type: 'folder',
|
||||
parentId: parent.id,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
const updatedParent = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||
expect(updatedParent?.fileCount).toBe(initialCount + 1)
|
||||
|
||||
// Create a new file inside folderA
|
||||
await assetService.createAsset({
|
||||
name: 'newFile',
|
||||
type: 'file',
|
||||
parentId: parent.id,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
const updatedParent2 = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||
expect(updatedParent2?.fileCount).toBe(initialCount + 2)
|
||||
})
|
||||
|
||||
it('reparentAssets should update fileCount when moving folders', async () => {
|
||||
const { user, assets } = await setupBasicAssets()
|
||||
|
||||
const sourceParent = assets.root
|
||||
const targetParent = assets.folderB
|
||||
const folderToMove = assets.folderA
|
||||
|
||||
// folderA is a child of root.
|
||||
// root children: folderA, folderB, fileRoot1. count=3.
|
||||
// folderB children: stackB, fileB2. count=2.
|
||||
|
||||
await assetService.reparentAssets({
|
||||
assetIds: [folderToMove.id],
|
||||
newParentId: targetParent.id,
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
const updatedSourceParent = await prisma.asset.findUnique({ where: { id: sourceParent.id } })
|
||||
expect(updatedSourceParent?.fileCount).toBe(2)
|
||||
|
||||
const updatedTargetParent = await prisma.asset.findUnique({ where: { id: targetParent.id } })
|
||||
expect(updatedTargetParent?.fileCount).toBe(3)
|
||||
})
|
||||
})
|
||||
|
||||
it('handles reparenting - Version Stack: dissolve with 1 remaining', async () => {
|
||||
const { user, assets } = await setupBasicAssets()
|
||||
await assetService.reparentAssets({
|
||||
|
||||
@@ -172,8 +172,8 @@ export class AssetService {
|
||||
}
|
||||
}
|
||||
|
||||
async reparentAssets(req: ReparentAssetsRequest): Promise<void> {
|
||||
await this.prismaClient.$transaction(async (tx) => {
|
||||
async reparentAssets(req: ReparentAssetsRequest, tx?: Prisma.TransactionClient): Promise<void> {
|
||||
const runInTx = async (tx: Prisma.TransactionClient) => {
|
||||
const newParent = await tx.asset.findUnique({
|
||||
where: { id: req.newParentId },
|
||||
include: { project: { include: { team: true } } },
|
||||
@@ -269,7 +269,13 @@ export class AssetService {
|
||||
if (oldParent) {
|
||||
await this.dissolveStackIfEmpty(tx, oldParent)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (tx) {
|
||||
await runInTx(tx)
|
||||
} else {
|
||||
await this.prismaClient.$transaction(runInTx)
|
||||
}
|
||||
}
|
||||
|
||||
async copyAssets(req: CopyAssetsRequest): Promise<void> {
|
||||
@@ -722,30 +728,41 @@ export class AssetService {
|
||||
if (req.contentType) data.mediaType = req.contentType
|
||||
if (req.creatorId) data.creator = { connect: { id: req.creatorId } }
|
||||
|
||||
const asset = await this.prismaClient.asset.create({
|
||||
data,
|
||||
include: {
|
||||
creator: true,
|
||||
metadataValues: true,
|
||||
storageKey: true,
|
||||
target: {
|
||||
include: {
|
||||
creator: true,
|
||||
metadataValues: true,
|
||||
storageKey: true,
|
||||
children: {
|
||||
include: { creator: true, metadataValues: true, storageKey: true },
|
||||
take: 3,
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
const asset = await this.prismaClient.$transaction(async (tx) => {
|
||||
const createdAsset = await tx.asset.create({
|
||||
data,
|
||||
include: {
|
||||
creator: true,
|
||||
metadataValues: true,
|
||||
storageKey: true,
|
||||
target: {
|
||||
include: {
|
||||
creator: true,
|
||||
metadataValues: true,
|
||||
storageKey: true,
|
||||
children: {
|
||||
include: { creator: true, metadataValues: true, storageKey: true },
|
||||
take: 3,
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
},
|
||||
},
|
||||
},
|
||||
children: {
|
||||
include: { creator: true, metadataValues: true, storageKey: true },
|
||||
take: 3,
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
},
|
||||
},
|
||||
children: {
|
||||
include: { creator: true, metadataValues: true, storageKey: true },
|
||||
take: 3,
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (parent) {
|
||||
await tx.asset.update({
|
||||
where: { id: parent.id },
|
||||
data: { fileCount: { increment: 1 } },
|
||||
})
|
||||
}
|
||||
|
||||
return createdAsset
|
||||
})
|
||||
|
||||
const infos = await this.toAssetInfos([asset])
|
||||
|
||||
Reference in New Issue
Block a user