Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAUT-11467 : fix date issue if empty/not empty filter is used in segment filter #349

Merged
merged 4 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions Helper/QueryFilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function addCustomFieldValueExpressionFromSegmentFilter(
$expression = $this->getCustomValueValueExpression(
$segmentQueryBuilder,
$tableAlias,
$filter->getOperator(),
$filter,
$valueParameter,
$filterAlreadyNegated
);
Expand Down Expand Up @@ -162,10 +162,11 @@ private function addOperatorExpression(
private function getCustomValueValueExpression(
SegmentQueryBuilder $customQuery,
string $tableAlias,
string $operator,
ContactSegmentFilter $filter,
string $valueParameter,
bool $alreadyNegated = false
) {
$operator = $filter->getOperator();
if ($alreadyNegated) {
switch ($operator) {
case 'empty':
Expand All @@ -181,15 +182,22 @@ private function getCustomValueValueExpression(
case 'empty':
$expression = $customQuery->expr()->orX(
$customQuery->expr()->isNull($tableAlias.'_value.value'),
$customQuery->expr()->eq($tableAlias.'_value.value', $customQuery->expr()->literal(''))
);

if ($filter->doesColumnSupportEmptyValue()) {
$expression->add(
$customQuery->expr()->eq($tableAlias.'_value.value', $customQuery->expr()->literal(''))
);
}
break;
case 'notEmpty':
$expression = $customQuery->expr()->andX(
$customQuery->expr()->isNotNull($tableAlias.'_value.value'),
$customQuery->expr()->neq($tableAlias.'_value.value', $customQuery->expr()->literal(''))
);
if ($filter->doesColumnSupportEmptyValue()) {
$expression->add(
$customQuery->expr()->neq($tableAlias.'_value.value', $customQuery->expr()->literal(''))
);
}

break;
case 'notIn':
Expand Down Expand Up @@ -337,7 +345,9 @@ public function createMergeFilterQuery(
$segmentFilterFieldType = $filter['type'] ?: $this->queryFilterFactory
->getCustomFieldTypeById($segmentFilterFieldId);
$dataTable = $this->queryFilterFactory->getTableNameFromType($segmentFilterFieldType);
$segmentMergedFilter = $filter['filter'];
$segmentFilterFieldOperator = (string) $filter['operator'];

$alias = $customItemXrefContactAlias.'_'.$segmentFilterFieldId.'_'.$filter['type'];
$aliasValue = $alias.'_value';
$isCmoFilter = $filter['cmo_filter'] ?? false;
Expand Down Expand Up @@ -366,7 +376,7 @@ public function createMergeFilterQuery(
$qb,
$cinAlias,
$alias,
$segmentFilterFieldOperator,
$segmentMergedFilter,
$valueParameter
),
$segmentFilterFieldOperator,
Expand Down Expand Up @@ -419,9 +429,10 @@ private function getMergeExpression(
SegmentQueryBuilder $qb,
string $cinAlias,
string $alias,
string $segmentFilterFieldOperator,
ContactSegmentFilter $filter,
string $valueParameter
) {
$segmentFilterFieldOperator = $filter->getOperator();
if ($isCmoFilter) {
$expression = $this->getCustomObjectNameExpression(
$qb,
Expand All @@ -433,7 +444,7 @@ private function getMergeExpression(
$expression = $this->getCustomValueValueExpression(
$qb,
$alias,
$segmentFilterFieldOperator,
$filter,
$valueParameter
);
}
Expand Down
28 changes: 28 additions & 0 deletions Tests/Functional/Helper/QueryFilterHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,34 @@ public function testGetCustomValueValueExpression(): void
],
]
);

$this->assertMatchWhere(
'test_value.value IS NULL',
[
'glue' => 'and',
'field' => 'cmf_'.$this->getFixtureById('custom_object_product')->getId(),
'object' => 'custom_object',
'type' => 'datetime',
'operator' => 'empty',
'properties' => [
'filter' => [],
],
]
);

$this->assertMatchWhere(
'test_value.value IS NOT NULL',
[
'glue' => 'and',
'field' => 'cmf_'.$this->getFixtureById('custom_object_product')->getId(),
'object' => 'custom_object',
'type' => 'datetime',
'operator' => '!empty',
'properties' => [
'filter' => [],
],
]
);
}

protected function assertMatchWhere(string $expectedWhere, array $filter): void
Expand Down
Loading