feat(agent): add teamId foreign key to agent table and refactor service queries

This commit is contained in:
yiling
2026-05-21 20:41:37 +08:00
parent 6e2ff8a003
commit 2c106bbb77
16 changed files with 498 additions and 71 deletions
+4
View File
@@ -63,6 +63,7 @@ describe('AgentService', () => {
const agent = await db.agent.create({
data: {
id: user.id,
teamId: team.id,
type: 'chat',
providerId: provider.id,
modelId: model.id,
@@ -290,6 +291,7 @@ describe('AgentService', () => {
type: 'agent',
agent: {
create: {
teamId: team.id,
type: 'autofill',
enabled: true,
providerId: provider.id,
@@ -378,6 +380,7 @@ describe('AgentService', () => {
type: 'agent',
agent: {
create: {
teamId: team.id,
type: 'chat',
enabled: true,
providerId: provider.id,
@@ -456,6 +459,7 @@ describe('AgentService', () => {
const agent = await db.agent.create({
data: {
id: botUser.id,
teamId: team.id,
type: 'chat',
enabled: true,
providerId: provider.id,
+14 -36
View File
@@ -35,11 +35,7 @@ export class AgentService {
const existing = await this.prismaClient.agent.findFirst({
where: {
type,
user: {
teamMembers: {
some: { teamId },
},
},
teamId,
},
})
if (existing) {
@@ -85,6 +81,7 @@ export class AgentService {
const agent = await tx.agent.create({
data: {
id: user.id,
teamId,
type,
enabled: enabled ?? true,
providerId: providerId || null,
@@ -139,22 +136,15 @@ export class AgentService {
if (!agent) throw new Error('agent not found')
if (type === 'embedding' || type === 'transcription' || type === 'autofill') {
const teamId = agent.user.teamMembers[0]?.teamId
if (teamId) {
const existing = await this.prismaClient.agent.findFirst({
where: {
type,
id: { not: agentId },
user: {
teamMembers: {
some: { teamId },
},
},
},
})
if (existing) {
throw new Error(`Agent of type ${type} already exists for this team`)
}
const existing = await this.prismaClient.agent.findFirst({
where: {
type,
id: { not: agentId },
teamId: agent.teamId,
},
})
if (existing) {
throw new Error(`Agent of type ${type} already exists for this team`)
}
}
@@ -232,11 +222,7 @@ export class AgentService {
async listAgents(params: ListAgentsParams) {
return this.prismaClient.agent.findMany({
where: {
user: {
teamMembers: {
some: { teamId: params.teamId },
},
},
teamId: params.teamId,
},
orderBy: {
id: 'desc',
@@ -271,11 +257,7 @@ export class AgentService {
where: {
type: 'chat',
enabled: true,
user: {
teamMembers: {
some: { teamId },
},
},
teamId,
},
})
if (!agent) {
@@ -426,11 +408,7 @@ export class AgentService {
where: {
type: 'autofill',
enabled: true,
user: {
teamMembers: {
some: { teamId },
},
},
teamId,
},
})
if (!agent) {