handlerConditions.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import compareBlockValue from '@/utils/compareBlockValue';
  2. import mustacheReplacer from '@/utils/referenceData/mustacheReplacer';
  3. import testConditions from '@/utils/testConditions';
  4. function checkConditions(data, conditionOptions) {
  5. return new Promise((resolve, reject) => {
  6. let retryCount = 1;
  7. const replacedValue = {};
  8. const testAllConditions = async () => {
  9. try {
  10. for (let index = 0; index < data.conditions.length; index += 1) {
  11. const result = await testConditions(
  12. data.conditions[index].conditions,
  13. conditionOptions
  14. );
  15. Object.assign(replacedValue, result?.replacedValue || {});
  16. if (result.isMatch) {
  17. resolve({ match: true, index, replacedValue });
  18. return;
  19. }
  20. }
  21. if (data.retryConditions && retryCount <= data.retryCount) {
  22. retryCount += 1;
  23. setTimeout(() => {
  24. testAllConditions();
  25. }, data.retryTimeout);
  26. } else {
  27. resolve({ match: false, replacedValue });
  28. }
  29. } catch (error) {
  30. reject(error);
  31. }
  32. };
  33. testAllConditions();
  34. });
  35. }
  36. async function conditions({ data, id }, { prevBlockData, refData }) {
  37. if (data.conditions.length === 0) {
  38. throw new Error('conditions-empty');
  39. }
  40. let resultData = '';
  41. let isConditionMet = false;
  42. let outputId = 'fallback';
  43. const replacedValue = {};
  44. const condition = data.conditions[0];
  45. const prevData = Array.isArray(prevBlockData)
  46. ? prevBlockData[0]
  47. : prevBlockData;
  48. if (condition && condition.conditions) {
  49. const conditionPayload = {
  50. refData,
  51. activeTab: this.activeTab.id,
  52. sendMessage: (payload) =>
  53. this._sendMessageToTab({ ...payload.data, label: 'conditions', id }),
  54. };
  55. const conditionsResult = await checkConditions(data, conditionPayload);
  56. if (conditionsResult.replacedValue) {
  57. Object.assign(replacedValue, conditionsResult.replacedValue);
  58. }
  59. if (conditionsResult.match) {
  60. isConditionMet = true;
  61. outputId = data.conditions[conditionsResult.index].id;
  62. }
  63. } else {
  64. data.conditions.forEach(({ type, value, compareValue, id: itemId }) => {
  65. if (isConditionMet) return;
  66. const firstValue = mustacheReplacer(
  67. compareValue ?? prevData,
  68. refData
  69. ).value;
  70. const secondValue = mustacheReplacer(value, refData).value;
  71. Object.assign(replacedValue, firstValue.list, secondValue.list);
  72. const isMatch = compareBlockValue(
  73. type,
  74. firstValue.value,
  75. secondValue.value
  76. );
  77. if (isMatch) {
  78. outputId = itemId;
  79. resultData = value;
  80. isConditionMet = true;
  81. }
  82. });
  83. }
  84. return {
  85. replacedValue,
  86. data: resultData,
  87. nextBlockId: this.getBlockConnections(id, outputId),
  88. };
  89. }
  90. export default conditions;