Skip to content

Commit 64fc7a0

Browse files
committed
refactor: improve session handling and validation in N8n integration
This commit enhances the N8n integration by refining session management and validation logic. Key changes include: - Added error handling for session creation failures in the BaseChatbotService. - Removed unused methods and properties in N8nService and N8nDto to streamline the codebase. - Updated N8n schema to enforce required fields and improve validation checks. - Simplified message processing logic to utilize base class methods, enhancing maintainability. These improvements contribute to a more robust and efficient N8n integration.
1 parent 39aaf29 commit 64fc7a0

File tree

5 files changed

+111
-474
lines changed

5 files changed

+111
-474
lines changed

src/api/integrations/chatbot/base-chatbot.service.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -353,20 +353,25 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
353353
? pushName
354354
: null;
355355

356-
session = (
357-
await this.createNewSession(
358-
{
359-
instanceName: instance.instanceName,
360-
instanceId: instance.instanceId,
361-
},
362-
{
363-
remoteJid,
364-
pushName: pushNameValue,
365-
botId: (bot as any).id,
366-
},
367-
this.getBotType(),
368-
)
369-
)?.session;
356+
const sessionResult = await this.createNewSession(
357+
{
358+
instanceName: instance.instanceName,
359+
instanceId: instance.instanceId,
360+
},
361+
{
362+
remoteJid,
363+
pushName: pushNameValue,
364+
botId: (bot as any).id,
365+
},
366+
this.getBotType(),
367+
);
368+
369+
if (!sessionResult || !sessionResult.session) {
370+
this.logger.error('Failed to create new session');
371+
return;
372+
}
373+
374+
session = sessionResult.session;
370375
}
371376

372377
// Update session status to opened

src/api/integrations/chatbot/n8n/controllers/n8n.controller.ts

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -110,58 +110,6 @@ export class N8nController extends BaseChatbotController<N8nModel, N8nDto> {
110110
return super.createBot(instance, data);
111111
}
112112

113-
public async findBot(instance: InstanceDto) {
114-
if (!this.integrationEnabled) throw new BadRequestException('N8n is disabled');
115-
116-
const instanceId = await this.prismaRepository.instance
117-
.findFirst({
118-
where: {
119-
name: instance.instanceName,
120-
},
121-
})
122-
.then((instance) => instance.id);
123-
124-
const bots = await this.botRepository.findMany({
125-
where: {
126-
instanceId: instanceId,
127-
},
128-
});
129-
130-
if (!bots.length) {
131-
return null;
132-
}
133-
134-
return bots;
135-
}
136-
137-
public async fetchBot(instance: InstanceDto, botId: string) {
138-
if (!this.integrationEnabled) throw new BadRequestException('N8n is disabled');
139-
140-
const instanceId = await this.prismaRepository.instance
141-
.findFirst({
142-
where: {
143-
name: instance.instanceName,
144-
},
145-
})
146-
.then((instance) => instance.id);
147-
148-
const bot = await this.botRepository.findFirst({
149-
where: {
150-
id: botId,
151-
},
152-
});
153-
154-
if (!bot) {
155-
throw new Error('N8n not found');
156-
}
157-
158-
if (bot.instanceId !== instanceId) {
159-
throw new Error('N8n not found');
160-
}
161-
162-
return bot;
163-
}
164-
165113
// Process N8n-specific bot logic
166114
protected async processBot(
167115
instance: any,
@@ -173,6 +121,7 @@ export class N8nController extends BaseChatbotController<N8nModel, N8nDto> {
173121
pushName?: string,
174122
msg?: any,
175123
) {
176-
this.n8nService.process(instance, remoteJid, bot, session, settings, content, pushName, msg);
124+
// Use the base class pattern instead of calling n8nService.process directly
125+
await this.n8nService.process(instance, remoteJid, bot, session, settings, content, pushName, msg);
177126
}
178127
}

src/api/integrations/chatbot/n8n/dto/n8n.dto.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1-
import { TriggerOperator, TriggerType } from '@prisma/client';
2-
31
import { BaseChatbotDto, BaseChatbotSettingDto } from '../../base-chatbot.dto';
42

53
export class N8nDto extends BaseChatbotDto {
64
// N8n specific fields
75
webhookUrl?: string;
86
basicAuthUser?: string;
97
basicAuthPass?: string;
10-
11-
// Advanced bot properties (copied from DifyDto style)
12-
triggerType: TriggerType;
13-
triggerOperator?: TriggerOperator;
14-
triggerValue?: string;
15-
expire?: number;
16-
keywordFinish?: string;
17-
delayMessage?: number;
18-
unknownMessage?: string;
19-
listeningFromMe?: boolean;
20-
stopBotFromMe?: boolean;
21-
keepOpen?: boolean;
22-
debounceTime?: number;
23-
ignoreJids?: string[];
24-
splitMessages?: boolean;
25-
timePerChar?: number;
268
}
279

2810
export class N8nSettingDto extends BaseChatbotSettingDto {
29-
// N8n specific fields
11+
// N8n has no specific fields
3012
}
3113

3214
export class N8nMessageDto {

0 commit comments

Comments
 (0)