2026-05-19 16:05:09 +08:00
|
|
|
import { Hono } from 'hono'
|
|
|
|
|
import { zValidator } from '@hono/zod-validator'
|
2026-06-01 23:16:22 +08:00
|
|
|
import { versionStackService } from '@shumai/core/src/versionStack/versionStack'
|
|
|
|
|
import { assetService } from '@shumai/core/src/asset/asset'
|
|
|
|
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
2026-10-27 14:37:26 +08:00
|
|
|
import { createVersionStackOp, removeVersionOp, runOperation } from '@shumai/core/src/operations'
|
2026-06-01 23:08:58 +08:00
|
|
|
import { createVersionStackRequestSchema, changeStackFileVersionRequestSchema } from '@shumai/dtos'
|
2026-06-01 23:16:22 +08:00
|
|
|
import type { Prisma } from '@shumai/db'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
|
|
|
|
|
|
|
|
|
const app = new Hono<{ Variables: { user: User } }>()
|
|
|
|
|
|
|
|
|
|
const route = app
|
|
|
|
|
.post(
|
2026-05-23 10:02:51 +08:00
|
|
|
'/projects/:projectId/version_stacks',
|
2026-05-19 16:05:09 +08:00
|
|
|
zValidator('json', createVersionStackRequestSchema),
|
|
|
|
|
async (c) => {
|
|
|
|
|
const req = c.req.valid('json')
|
2026-10-27 14:37:26 +08:00
|
|
|
// Authorization and the stack's project come from the files, not the path.
|
|
|
|
|
const stack = await runOperation(
|
|
|
|
|
createVersionStackOp,
|
|
|
|
|
{ userId: c.get('user').id, via: 'api' },
|
|
|
|
|
{ fileIds: req.fileIds },
|
|
|
|
|
)
|
|
|
|
|
return c.json(await assetService.getAsset({ assetId: stack.id }))
|
2026-05-19 16:05:09 +08:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.post(
|
2026-05-25 23:51:37 +08:00
|
|
|
'/version_stacks/:stackId/order',
|
2026-05-19 16:05:09 +08:00
|
|
|
zValidator('json', changeStackFileVersionRequestSchema),
|
|
|
|
|
async (c) => {
|
2026-05-23 10:02:51 +08:00
|
|
|
const stackId = c.req.param('stackId')
|
2026-05-19 16:05:09 +08:00
|
|
|
const user = c.get('user')
|
|
|
|
|
const req = c.req.valid('json')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Edit,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: stackId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await versionStackService.changeStackFileVersion({
|
|
|
|
|
stackId,
|
|
|
|
|
fileId: req.fileId,
|
|
|
|
|
beforeId: req.beforeId,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return new Response(null, { status: 200 })
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-05-25 23:51:37 +08:00
|
|
|
.get('/version_stacks/:stackId/versions', async (c) => {
|
2026-05-23 10:02:51 +08:00
|
|
|
const stackId = c.req.param('stackId')
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Read,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: stackId,
|
2026-05-23 10:02:51 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const versions = await assetService.getStackVersions(stackId)
|
|
|
|
|
return c.json(versions)
|
|
|
|
|
})
|
2026-08-15 15:37:17 +08:00
|
|
|
.delete('/version_stacks/:stackId/versions/:versionId', async (c) => {
|
2026-10-27 14:37:26 +08:00
|
|
|
// The stack is the version's parent; the path's stackId is not needed.
|
|
|
|
|
await runOperation(
|
|
|
|
|
removeVersionOp,
|
|
|
|
|
{ userId: c.get('user').id, via: 'api' },
|
|
|
|
|
{ versionId: c.req.param('versionId') },
|
|
|
|
|
)
|
2026-08-15 15:37:17 +08:00
|
|
|
return new Response(null, { status: 200 })
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
export default route
|