fix(dingtalk): exclude removed instances from routing + clear binding on remove

Two bugs fixed:
1. findByDingTalkUserId now filters status != 'removed' so a re-bound new instance
   is not shadowed by an old removed one with the same DingTalk user ID.
2. When an agent is deleted (removed), its dingtalkUserId is cleared so the
   DingTalk ID is freed for reuse by the next binding.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-03-08 14:49:00 -07:00
parent a8b5571aea
commit 50401660ef
2 changed files with 4 additions and 2 deletions

View File

@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm'; import { DataSource, Not, Repository } from 'typeorm';
import { AgentInstance } from '../../domain/entities/agent-instance.entity'; import { AgentInstance } from '../../domain/entities/agent-instance.entity';
@Injectable() @Injectable()
@ -27,7 +27,8 @@ export class AgentInstanceRepository {
} }
findByDingTalkUserId(dingTalkUserId: string): Promise<AgentInstance | null> { findByDingTalkUserId(dingTalkUserId: string): Promise<AgentInstance | null> {
return this.repo.findOne({ where: { dingTalkUserId } as any }); // Exclude removed instances so a rebound new instance is routed correctly
return this.repo.findOne({ where: { dingTalkUserId, status: Not('removed') } as any });
} }
save(instance: AgentInstance): Promise<AgentInstance> { save(instance: AgentInstance): Promise<AgentInstance> {

View File

@ -146,6 +146,7 @@ export class AgentInstanceController {
if (!inst) throw new NotFoundException(`Instance ${id} not found`); if (!inst) throw new NotFoundException(`Instance ${id} not found`);
await this.deployService.removeInstance(inst); await this.deployService.removeInstance(inst);
inst.status = 'removed'; inst.status = 'removed';
inst.dingTalkUserId = null as any; // Clear DingTalk binding so the ID can be reused by a new instance
await this.instanceRepo.save(inst); await this.instanceRepo.save(inst);
return { message: 'Instance removed', id }; return { message: 'Instance removed', id };
} }