Suggests another Typebot fetchSessions solution instead of upsert

This commit is contained in:
Júlio Paulillo 2024-09-04 18:13:52 -03:00
parent 586b703872
commit 78abb796e5
2 changed files with 13 additions and 16 deletions

View File

@ -633,18 +633,8 @@ export class TypebotController extends ChatbotController implements ChatbotContr
}); });
if (!findBot) { if (!findBot) {
findBot = await this.botRepository.upsert({ findBot = await this.botRepository.create({
where: { data: {
url_typebot_instanceId: {
url: url,
typebot: typebot,
instanceId: instanceData.id,
},
},
update: {
enabled: true,
},
create: {
enabled: true, enabled: true,
url: url, url: url,
typebot: typebot, typebot: typebot,
@ -657,7 +647,12 @@ export class TypebotController extends ChatbotController implements ChatbotContr
stopBotFromMe: stopBotFromMe, stopBotFromMe: stopBotFromMe,
keepOpen: keepOpen, keepOpen: keepOpen,
instanceId: instanceData.id, instanceId: instanceData.id,
}, }
});
} else {
findBot = await this.botRepository.update({
where: { id: findBot.id },
data: { enabled: true }
}); });
} }
@ -682,7 +677,7 @@ export class TypebotController extends ChatbotController implements ChatbotContr
stopBotFromMe: stopBotFromMe, stopBotFromMe: stopBotFromMe,
keepOpen: keepOpen, keepOpen: keepOpen,
prefilledVariables: prefilledVariables, prefilledVariables: prefilledVariables,
typebotId: findBot.id, botId: findBot.id
}); });
if (response.sessionId) { if (response.sessionId) {
@ -889,7 +884,7 @@ export class TypebotController extends ChatbotController implements ChatbotContr
throw new Error('Typebot not found'); throw new Error('Typebot not found');
} }
return await this.sessionRepository.findMany({ const sessions = await this.sessionRepository.findMany({
where: { where: {
instanceId: instanceId, instanceId: instanceId,
remoteJid, remoteJid,
@ -897,6 +892,8 @@ export class TypebotController extends ChatbotController implements ChatbotContr
type: 'typebot', type: 'typebot',
}, },
}); });
return sessions;
} catch (error) { } catch (error) {
this.logger.error(error); this.logger.error(error);
throw new Error('Error fetching sessions'); throw new Error('Error fetching sessions');

View File

@ -113,7 +113,7 @@ export class TypebotRouter extends RouterBroker {
request: req, request: req,
schema: instanceSchema, schema: instanceSchema,
ClassRef: InstanceDto, ClassRef: InstanceDto,
execute: (instance) => typebotController.fetchSessions(instance, req.params.typebotId), execute: (instance) => typebotController.fetchSessions(instance, req.params.typebotId, req.query.remoteJid as string | undefined),
}); });
res.status(HttpStatus.OK).json(response); res.status(HttpStatus.OK).json(response);