Skip to content

Commit

Permalink
fix #107 invalid sql builder
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Aug 22, 2021
1 parent 0c3c8c7 commit e1afb02
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions Model/Customer/SourceProvider/FilterModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,38 @@

use Magento\Framework\Api\Filter;
use Magento\Framework\Data\Collection;
use Magento\Framework\Exception\LocalizedException;
use Opengento\Gdpr\Model\Entity\SourceProvider\ModifierInterface;

final class FilterModifier implements ModifierInterface
{
/**
* @inheritdoc
* @throws LocalizedException
*/
public function apply(Collection $collection, Filter $filter): void
{
if ($collection instanceof Collection\AbstractDb && $filter->getField() === 'created_at') {
$connection = $collection->getConnection();

$visitorSelect = $connection->select()
->from($connection->getTableName('customer_visitor'))
->columns(['customer_id' => 'customer_id', 'last_visit_at' => 'MAX(last_visit_at)'])
->from(
$connection->getTableName('customer_visitor'),
['customer_id' => 'customer_id', 'last_visit_at' => 'MAX(last_visit_at)']
)
->group(['customer_id']);

$collection->getSelect()->joinLeft(
['cv' => $visitorSelect],
'main_table.entity_id=cl.customer_id',
'e.entity_id=cv.customer_id',
null
);
$collection->getSelect()->joinLeft(
['cl' => $connection->getTableName('customer_log')],
'main_table.entity_id=cl.customer_id',
'e.entity_id=cl.customer_id',
null
);
$collection->getSelect()->columns(
[
'last_visit_at' => 'IFNULL(' .
'cv.last_visit_at,'.
'GREATEST(IFNULL(cl.last_login_at, e.created_at), IFNULL(cl.last_logout_at, e.updated_at))' .
')',
]
$collection->getSelect()->where(
$connection->prepareSqlCondition('IFNULL(' .
'cv.last_visit_at,'.
'GREATEST(IFNULL(cl.last_login_at, e.created_at), IFNULL(cl.last_logout_at, e.updated_at))' .
')', [$filter->getConditionType() => $filter->getValue()])
);
$collection->addFieldToFilter('last_visit_at', [$filter->getConditionType() => $filter->getValue()]);
}
}
}

0 comments on commit e1afb02

Please sign in to comment.