From c5a21c447e76dbb727f953779fa7bd3743355f67 Mon Sep 17 00:00:00 2001
From: Coleman Watts <coleman@civicrm.org>
Date: Sun, 11 Jul 2021 15:43:20 -0400
Subject: [PATCH] SearchKit - Fix reloading a search with an OR group in the
 WHERE clause

Fixes dev/report#71
---
 .../ang/crmSearchAdmin/crmSearchClause.component.js       | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/ext/search_kit/ang/crmSearchAdmin/crmSearchClause.component.js b/ext/search_kit/ang/crmSearchAdmin/crmSearchClause.component.js
index f6409c30ee2..4d2c8072b63 100644
--- a/ext/search_kit/ang/crmSearchAdmin/crmSearchClause.component.js
+++ b/ext/search_kit/ang/crmSearchAdmin/crmSearchClause.component.js
@@ -34,6 +34,7 @@
         _.each(ctrl.clauses, updateOperators);
       };
 
+      // Return a list of operators allowed for the field in a given clause
       this.getOperators = function(clause) {
         var field = ctrl.getField(clause[0]);
         if (!field || !field.operators) {
@@ -48,8 +49,13 @@
         return ctrl.operators[opKey];
       };
 
+      // Ensures a clause is using an operator that is allowed for the field
       function updateOperators(clause) {
-        if (!ctrl.skip && (!clause[1] || !_.includes(_.pluck(ctrl.getOperators(clause), 'key'), clause[1]))) {
+        // Recurse into AND/OR/NOT groups
+        if (ctrl.conjunctions[clause[0]]) {
+          _.each(clause[1], updateOperators);
+        }
+        else if (!ctrl.skip && (!clause[1] || !_.includes(_.pluck(ctrl.getOperators(clause), 'key'), clause[1]))) {
           clause[1] = ctrl.getOperators(clause)[0].key;
           ctrl.changeClauseOperator(clause);
         }
-- 
GitLab