Skip to content

Commit b2cb019

Browse files
committed
fix(core): évaluation des variables dynamiques dans les conditions pour les boutons de réponse en fin de message (après calcul de la valeur finale de chaque variable)
Sinon les conditions étaient évaluées avec les valeurs des variables obtenues en début de message
1 parent f2ca14a commit b2cb019

3 files changed

Lines changed: 10 additions & 43 deletions

File tree

app/js/core/interactions/helpers/choiceOptions.mjs

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
shouldBeRandomized,
44
randomizeArrayWithFixedElements,
55
} from "../../../utils/arrays.mjs";
6-
import { evaluateExpression } from "../../../markdown/custom/variablesDynamic/evaluateExpression.mjs";
76
import { processDirectiveSelect } from "../../../markdown/custom/directives/select.mjs";
87

98
export function responseToSelectedChoiceOption(chatbot, choiceOptionLink) {
@@ -36,40 +35,6 @@ export function processMessageWithChoiceOptions(
3635
choiceOptions,
3736
) {
3837
// Si on a du contenu dynamique et qu'on utilise <!-- if @VARIABLE==VALEUR … --> on filtre d'abord les options si elles dépendent d'une variable
39-
let dynamicVariables = chatbot.dynamicVariables;
40-
if (yaml && yaml.dynamicContent) {
41-
if (choiceOptions) {
42-
choiceOptions = choiceOptions.filter((option) => {
43-
let condition = option.condition;
44-
if (!condition) {
45-
return true;
46-
} else {
47-
// Remplace les variables personnalisées dans la condition
48-
condition = condition.replace(
49-
/@([^\s()&|!=<>]+)/g,
50-
function (match, varName) {
51-
return (
52-
'tryConvertStringToNumber(dynamicVariables["' +
53-
varName.trim() +
54-
'"])'
55-
);
56-
},
57-
);
58-
// Gestion des valeurs si elles ne sont pas mises entre guillemets + gestion du cas undefined
59-
condition = condition
60-
.replaceAll(
61-
/(==|!=|<=|>=|<|>) ?(.*?) ?(\)|&|\||$)/g,
62-
function (match, comparisonSignLeft, value, comparisonSignRight) {
63-
return `${comparisonSignLeft}"${value}" ${comparisonSignRight}`;
64-
},
65-
)
66-
.replaceAll('""', '"')
67-
.replace('"undefined"', "undefined");
68-
return evaluateExpression(condition, dynamicVariables);
69-
}
70-
});
71-
}
72-
}
7338

7439
// S'il y a la directive !Select: x on sélectionne aléatoirement seulement x options dans l'ensemble des options disponibles
7540
[response, choiceOptions] = processDirectiveSelect(response, choiceOptions);
@@ -85,15 +50,17 @@ export function processMessageWithChoiceOptions(
8550
const choiceOptionsLength = choiceOptions.length;
8651
for (let i = 0; i < choiceOptionsLength; i++) {
8752
const choiceOption = choiceOptions[i];
88-
const choiceOptionText = choiceOption.text;
89-
const choiceOptionLink = choiceOption.link;
53+
const hasConditions =
54+
yaml && yaml.dynamicContent && choiceOption.condition;
9055
messageOptions =
9156
messageOptions +
57+
(hasConditions ? "\n`if " + choiceOption.condition + "`" : "") +
9258
'<li><a href="#' +
93-
choiceOptionLink +
59+
choiceOption.link +
9460
'">' +
95-
choiceOptionText +
96-
"</a></li>\n";
61+
choiceOption.text +
62+
"</a></li>\n" +
63+
(hasConditions ? "\n`endif`" : "");
9764
}
9865
messageOptions = messageOptions + "</ul>";
9966
response = response + messageOptions;

app/script.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/script.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)