Browse Source

feat: support full expression in conditions builder(#1596)

Ahmad Kholid 1 year ago
parent
commit
8aaabfc22d

+ 1 - 1
src/stores/workflow.js

@@ -47,7 +47,7 @@ const defaultWorkflow = (data = null, options = {}) => {
     settings: {
       publicId: '',
       blockDelay: 0,
-      saveLog: true,
+      saveLog: false,
       debugMode: false,
       restartTimes: 3,
       notification: true,

+ 11 - 1
src/workflowEngine/templating/mustacheReplacer.js

@@ -63,7 +63,14 @@ export function keyParser(key, data) {
 
 function replacer(
   str,
-  { regex, tagLen, modifyPath, data, disableStringify = false }
+  {
+    data,
+    regex,
+    tagLen,
+    modifyPath,
+    checkExistence = false,
+    disableStringify = false,
+  }
 ) {
   const replaceResult = {
     list: {},
@@ -106,6 +113,8 @@ function replacer(
         dataKey = dataKey.slice(1);
       }
 
+      if (checkExistence) return objectPath.has(data[dataKey], path);
+
       result = objectPath.get(data[dataKey], path);
       if (typeof result === 'undefined') result = match;
 
@@ -144,6 +153,7 @@ export default function (str, refData, options = {}) {
         tagLen: 1,
         regex: /\[(.*?)\]/g,
         ...options,
+        checkExistence: false,
       });
       Object.assign(replacedList, list);
 

+ 13 - 7
src/workflowEngine/utils/testConditions.js

@@ -1,9 +1,7 @@
 import cloneDeep from 'lodash.clonedeep';
-import objectPath from 'object-path';
 import { parseJSON } from '@/utils/helper';
 import { conditionBuilder } from '@/utils/shared';
 import renderString from '../templating/renderString';
-import { keyParser } from '../templating/mustacheReplacer';
 
 const isBoolStr = (str) => {
   if (str === 'true') return true;
@@ -56,14 +54,22 @@ export default async function (conditionsArr, workflowData) {
       const isInsideBrackets =
         dataPath.startsWith('{{') && dataPath.endsWith('}}');
 
-      if (isInsideBrackets) {
-        dataPath = dataPath.slice(2, -2).trim();
+      if (!isInsideBrackets) {
+        dataPath = `{{${dataPath}}}`;
       }
 
-      const parsedPath = keyParser(dataPath, workflowData.refData);
-      dataPath = `${parsedPath.dataKey}.${parsedPath.path}`;
+      let dataExists = await renderString(
+        dataPath,
+        workflowData.refData,
+        workflowData.isPopup,
+        {
+          checkExistence: true,
+        }
+      );
+      // It return string for some reason
+      dataExists = Boolean(parseJSON(dataExists.value, false));
 
-      return objectPath.has(workflowData.refData, dataPath);
+      return dataExists;
     }
 
     const copyData = cloneDeep(data);