mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 17:19:02 +00:00
feat(operations): add create_project operation across agent, mcp, and cli (#529)
* feat(operations): add create_project operation across agent, mcp, and cli * fix(api): remove legacy team creation endpoint * fix(api): preserve ProjectInfo response on POST /teams/:teamId/projects
This commit is contained in:
@@ -32,6 +32,7 @@ export { listCommentsOp } from './comment/list-comments'
|
||||
export { resolveCommentOp } from './comment/resolve-comment'
|
||||
export { listMetadataFieldsOp } from './metadata/list-metadata-fields'
|
||||
export { updateAssetMetadataOp } from './metadata/update-asset-metadata'
|
||||
export { createProjectOp } from './project/create-project'
|
||||
export { listProjectsOp } from './project/list-projects'
|
||||
export { completeUploadOp } from './upload/complete-upload'
|
||||
export { createFileOp } from './upload/create-file'
|
||||
|
||||
@@ -6,6 +6,7 @@ import { projectService } from '@shumai/core/src/project/project'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import {
|
||||
createFolderOp,
|
||||
createProjectOp,
|
||||
deleteAssetOp,
|
||||
listAssetsOp,
|
||||
listProjectsOp,
|
||||
@@ -262,6 +263,97 @@ describe('asset and project Operations', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('create_project', () => {
|
||||
it('creates a project with explicit teamId and logs audit with via and agentId', async () => {
|
||||
const agentUser = await prisma.user.create({
|
||||
data: { name: 'Ops Agent', email: 'ops-agent@test.com', password: 'pw', type: 'agent' },
|
||||
})
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: agentUser.id,
|
||||
type: 'chat',
|
||||
teamId,
|
||||
config: { provider: 'openai', model: 'gpt-4o' },
|
||||
},
|
||||
})
|
||||
|
||||
const created = await runOperation(
|
||||
createProjectOp,
|
||||
{ userId: owner.id, via: 'agent', agentId: agent.id },
|
||||
{ name: ' Agent Project ', teamId },
|
||||
)
|
||||
|
||||
expect(created).toEqual({
|
||||
id: expect.any(String),
|
||||
name: 'Agent Project',
|
||||
teamId,
|
||||
rootFolderId: expect.any(String),
|
||||
})
|
||||
|
||||
const row = await prisma.project.findUniqueOrThrow({ where: { id: created.id } })
|
||||
expect(row.rootFolderId).toBe(created.rootFolderId)
|
||||
expect(row.shareRootId).toEqual(expect.any(String))
|
||||
|
||||
const [audit] = await auditRows(AuditAction.project_create)
|
||||
expect(audit).toMatchObject({
|
||||
teamId,
|
||||
projectId: created.id,
|
||||
itemId: created.id,
|
||||
userId: owner.id,
|
||||
agentId: agent.id,
|
||||
via: 'agent',
|
||||
})
|
||||
})
|
||||
|
||||
it('resolves teamId from ctx.teamId or sole team membership when omitted', async () => {
|
||||
const fromCtx = await runOperation(
|
||||
createProjectOp,
|
||||
{ userId: owner.id, via: 'agent', teamId },
|
||||
{ name: 'From Context' },
|
||||
)
|
||||
expect(fromCtx.teamId).toBe(teamId)
|
||||
|
||||
const fromSoleMembership = await runOperation(createProjectOp, as(owner, 'cli'), {
|
||||
name: 'From Sole Team',
|
||||
})
|
||||
expect(fromSoleMembership.teamId).toBe(teamId)
|
||||
})
|
||||
|
||||
it('rejects omitted teamId when the user belongs to multiple teams', async () => {
|
||||
const secondTeam = await prisma.team.create({ data: { name: 'Second Team' } })
|
||||
await prisma.teamMember.create({
|
||||
data: { teamId: secondTeam.id, userId: owner.id, role: 'owner' },
|
||||
})
|
||||
|
||||
await expect(
|
||||
runOperation(createProjectOp, as(owner, 'mcp'), { name: 'Ambiguous' }),
|
||||
).rejects.toMatchObject({ status: 400 })
|
||||
expect(await prisma.project.count({ where: { name: 'Ambiguous' } })).toBe(0)
|
||||
expect(await auditRows(AuditAction.project_create)).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('rejects reviewers, project-scoped members, and non-members with no side effects', async () => {
|
||||
const projectScoped = await prisma.user.create({
|
||||
data: { name: 'Scoped', email: 'ops-scoped@test.com', password: 'pw' },
|
||||
})
|
||||
await prisma.teamMember.create({
|
||||
data: { teamId, userId: projectScoped.id, role: 'editor', scope: 'project' },
|
||||
})
|
||||
|
||||
for (const user of [reviewer, projectScoped, outsider]) {
|
||||
await expect(
|
||||
runOperation(createProjectOp, as(user), { name: 'Blocked', teamId }),
|
||||
).rejects.toMatchObject({ status: 403 })
|
||||
}
|
||||
await expect(
|
||||
runOperation(createProjectOp, as(outsider), { name: 'NoTeam' }),
|
||||
).rejects.toMatchObject({ status: 403 })
|
||||
|
||||
expect(await prisma.project.count({ where: { name: { in: ['Blocked', 'NoTeam'] } } })).toBe(0)
|
||||
expect(await auditRows(AuditAction.project_create)).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('list_projects', () => {
|
||||
it("lists the caller's projects with their root folder", async () => {
|
||||
const result = await runOperation(listProjectsOp, as(reviewer), {})
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { createProject } from '@shumai/operations'
|
||||
import { AuditAction } from '@shumai/dtos'
|
||||
import { Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { projectService } from '@shumai/core/src/project/project'
|
||||
import { teamService } from '@shumai/core/src/team/team'
|
||||
import type { OperationContext } from '../context'
|
||||
import { asBadRequest, badRequest } from '../errors'
|
||||
import { implementOperation } from '../implement'
|
||||
|
||||
async function resolveTeamId(
|
||||
inputTeamId: string | undefined,
|
||||
ctx: OperationContext,
|
||||
): Promise<string> {
|
||||
if (inputTeamId) return inputTeamId
|
||||
if (ctx.teamId) return ctx.teamId
|
||||
|
||||
const teams = await teamService.getUserTeams({
|
||||
userId: ctx.userId,
|
||||
pagination: { first: 2 },
|
||||
})
|
||||
if (teams.data.length === 1) return teams.data[0].id
|
||||
if (teams.data.length === 0) {
|
||||
throw new HTTPException(403, { message: 'User is not a member of any team' })
|
||||
}
|
||||
throw badRequest('Multiple teams found for user; specify teamId')
|
||||
}
|
||||
|
||||
export const createProjectOp = implementOperation(createProject, {
|
||||
authz: async (input, ctx) => {
|
||||
const teamId = await resolveTeamId(input.teamId, ctx)
|
||||
return [{ permission: Permission.Edit, type: ResourceType.Team, id: teamId }]
|
||||
},
|
||||
prepare: async (input, ctx) => ({
|
||||
teamId: await resolveTeamId(input.teamId, ctx),
|
||||
}),
|
||||
execute: async (input, ctx, { teamId }) => {
|
||||
const project = await asBadRequest(() =>
|
||||
projectService.createProject(
|
||||
{ id: ctx.userId },
|
||||
{
|
||||
teamId,
|
||||
name: input.name,
|
||||
coverImageKey: input.coverImageKey,
|
||||
enableNotification: input.enableNotification ?? true,
|
||||
},
|
||||
),
|
||||
)
|
||||
return {
|
||||
id: project.id,
|
||||
name: project.name,
|
||||
teamId: project.teamId,
|
||||
rootFolderId: project.rootFolder ?? null,
|
||||
}
|
||||
},
|
||||
audit: (_input, output) => [
|
||||
{
|
||||
action: AuditAction.project_create,
|
||||
itemId: output.id,
|
||||
teamId: output.teamId,
|
||||
projectId: output.id,
|
||||
},
|
||||
],
|
||||
})
|
||||
@@ -17,6 +17,7 @@ import { resolveCommentOp } from './comment/resolve-comment'
|
||||
import type { Operation } from './implement'
|
||||
import { listMetadataFieldsOp } from './metadata/list-metadata-fields'
|
||||
import { updateAssetMetadataOp } from './metadata/update-asset-metadata'
|
||||
import { createProjectOp } from './project/create-project'
|
||||
import { listProjectsOp } from './project/list-projects'
|
||||
import { completeUploadOp } from './upload/complete-upload'
|
||||
import { createFileOp } from './upload/create-file'
|
||||
@@ -43,6 +44,7 @@ function defineRegistry<const TOps extends readonly Operation[]>(
|
||||
|
||||
/** Every implemented Operation. Each definition in `@shumai/operations` must appear exactly once. */
|
||||
export const operations = defineRegistry([
|
||||
createProjectOp,
|
||||
listProjectsOp,
|
||||
listAssetsOp,
|
||||
searchAssetsOp,
|
||||
|
||||
Reference in New Issue
Block a user