Fix: Corrected openai trigger validation

Corrected an issue in the validation of openai triggers by changing the 'findFirst' method to 'findMany' and implementing a loop to find the correct trigger match. This change improves the accuracy of trigger validation and ensures that all possible matches are considered.

Impact: This fix ensures that the correct trigger is identified, improving the overall functionality of the openai integration.

Affected files:
- openai.service.ts
This commit is contained in:
Davidson Gomes 2024-07-29 17:29:55 -03:00
parent c03e0cc954
commit 7bfb4f93bb

View File

@ -863,7 +863,7 @@ export class OpenaiService {
if (findTriggerRegex) return findTriggerRegex;
// Check for startsWith match
const findTriggerStartsWith = await this.prismaRepository.openaiBot.findFirst({
const findStartsWith = await this.prismaRepository.openaiBot.findMany({
where: {
enabled: true,
triggerType: 'keyword',
@ -875,10 +875,19 @@ export class OpenaiService {
},
});
let findTriggerStartsWith = null;
for (const startsWith of findStartsWith) {
if (content.startsWith(startsWith.triggerValue)) {
findTriggerStartsWith = startsWith;
break;
}
}
if (findTriggerStartsWith) return findTriggerStartsWith;
// Check for endsWith match
const findTriggerEndsWith = await this.prismaRepository.openaiBot.findFirst({
const findEndsWith = await this.prismaRepository.openaiBot.findMany({
where: {
enabled: true,
triggerType: 'keyword',
@ -890,10 +899,19 @@ export class OpenaiService {
},
});
let findTriggerEndsWith = null;
for (const endsWith of findEndsWith) {
if (content.endsWith(endsWith.triggerValue)) {
findTriggerEndsWith = endsWith;
break;
}
}
if (findTriggerEndsWith) return findTriggerEndsWith;
// Check for contains match
const findTriggerContains = await this.prismaRepository.openaiBot.findFirst({
const findContains = await this.prismaRepository.openaiBot.findMany({
where: {
enabled: true,
triggerType: 'keyword',
@ -905,6 +923,15 @@ export class OpenaiService {
},
});
let findTriggerContains = null;
for (const contains of findContains) {
if (content.includes(contains.triggerValue)) {
findTriggerContains = contains;
break;
}
}
if (findTriggerContains) return findTriggerContains;
const fallback = await this.prismaRepository.openaiSetting.findFirst({