From e3ad1ff86b89539d5f6718cb155cdd59c8a07f37 Mon Sep 17 00:00:00 2001 From: colemanw Date: Thu, 28 Dec 2023 21:19:52 -0600 Subject: [PATCH] SearchKit - Allow aggregate functions to be used without GroupBy Fixes https://lab.civicrm.org/dev/core/-/issues/2415 --- .../crmSearchAdmin/crmSearchAdmin.component.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js b/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js index 48e8f31f7b..79fb64869b 100644 --- a/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js +++ b/ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js @@ -421,7 +421,7 @@ _.each(ctrl.savedSearch.api_params.select, function(col, pos) { var info = searchMeta.parseExpr(col), fieldExpr = (_.findWhere(info.args, {type: 'field'}) || {}).value; - if (ctrl.canAggregate(col)) { + if (ctrl.mustAggregate(col)) { // Ensure all non-grouped columns are aggregated if using GROUP BY if (!info.fn || info.fn.category !== 'aggregate') { var dflFn = searchMeta.getDefaultAggregateFn(info) || 'GROUP_CONCAT', @@ -526,7 +526,16 @@ // Is a column eligible to use an aggregate function? this.canAggregate = function(col) { - // If the query does not use grouping, never + // If the query does not use grouping, it's always allowed + if (!ctrl.savedSearch.api_params.groupBy || !ctrl.savedSearch.api_params.groupBy.length) { + return true; + } + return this.mustAggregate(col); + }; + + // Is a column required to use an aggregate function? + this.mustAggregate = function(col) { + // If the query does not use grouping, it's never required if (!ctrl.savedSearch.api_params.groupBy || !ctrl.savedSearch.api_params.groupBy.length) { return false; } @@ -737,7 +746,7 @@ if (info.field && !info.suffix && !info.fn && info.field.type === 'Field' && (info.field.fk_entity || info.field.name !== info.field.fieldName)) { var idFieldName = info.field.fk_entity ? fieldName : fieldName.substr(0, fieldName.lastIndexOf('.')), idField = searchMeta.parseExpr(idFieldName).args[0].field; - if (!ctrl.canAggregate(idFieldName)) { + if (!ctrl.mustAggregate(idFieldName)) { var joinEntity = searchMeta.getEntity(idField.fk_entity), label = (idField.join ? idField.join.label + ': ' : '') + (idField.input_attrs && idField.input_attrs.label || idField.label); _.each(_.cloneDeep(joinEntity && joinEntity.links), function(link) { -- 2.25.1