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

Updating the filter criteria as per filter #40

Closed
Closed
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
7 changes: 7 additions & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,12 @@
],
],
],
'fixtures' => [
'mautic.citrix.fixture.test.citrix' => [
'class' => \MauticPlugin\LeuchtfeuerGoToBundle\Tests\DataFixtures\ORM\LoadCitrixData::class,
'tag' => \Doctrine\Bundle\FixturesBundle\DependencyInjection\CompilerPass\FixturesCompilerPass::FIXTURE_TAG,
'optional' => true,
],
]
],
];
17 changes: 17 additions & 0 deletions Entity/GoToProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace MauticPlugin\LeuchtfeuerGoToBundle\Entity;

use Doctrine\ORM\NonUniqueResultException;
use Mautic\CoreBundle\Entity\CommonRepository;
use function Doctrine\ORM\QueryBuilder;

class GoToProductRepository extends CommonRepository
{
Expand Down Expand Up @@ -90,6 +92,21 @@ public function getProductsBetweenSpecificDates($from = null, $to = null)
return $qb->getQuery()->getResult();
}

public function findOneByProductByNameAndDate(string $name, string $date): ?GoToProduct
{

$qb = $this->createQueryBuilder('e');
$qb
->andWhere($qb->expr()->eq('e.name', ':name'))
->andWhere('e.date BETWEEN :from AND :to')
->setParameter('name', $name)
->setParameter('from', new \DateTime($date.':00'))
->setParameter('to', new \DateTime($date.':59'))
->setMaxResults(1);

return $qb->getQuery()->getOneOrNullResult();
}

public function reduceSessionsToWebinar($sessions)
{
$key = '';
Expand Down
27 changes: 21 additions & 6 deletions EventListener/LeadSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,27 @@ public function onListFiltering(LeadListFilteringEvent $event)

if (in_array($currentFilter, $eventFilters, true)) {
$eventNames = $details['filter'];
preg_match('#^([^ ]+ +[^ ]+) +(.*)$#', $eventNames, $matches);
$eventNames = $this->model->getIdByNameAndDate($matches[2], $matches[1]);
if (!$eventNames) {
break;
$isAnyEvent = ('Any Webinar' === $eventNames);
$productId = 0;
if (!$isAnyEvent) {
preg_match('#^([^ ]+ +[^ ]+) +(.*)$#', $eventNames, $matches);
/*
* As the legacy filters only adds H:i and not H:i:s, the second part were ignored hence no product
* id fetched. This is the hack till we correct the filters with product Ids.
* If strlen(23.05.2024 06:19) => 16, then fetch using range.
* If strlen(23.05.2024 06:19:00) => 19, then just use the normal method.
*/
if (strlen($matches[1]) === 19) {
$productId = $this->model->getIdByNameAndDate($matches[2], $matches[1]);
} elseif (strlen($matches[1]) === 16) {
$productId = $this->model->getIdByNameAndDateRange($matches[2], $matches[1]);
}

if (!$productId) {
break;
}
}
$isAnyEvent = in_array('any', $eventNames, true);

$subQueriesSQL = [];

$eventTypes = [GoToEventTypes::REGISTERED, GoToEventTypes::ATTENDED];
Expand All @@ -287,7 +302,7 @@ public function onListFiltering(LeadListFilteringEvent $event)
$query->where(
$q->expr()->andX(
$q->expr()->eq($alias.$k.'.event_type', $q->expr()->literal($eventType)),
$q->expr()->eq($alias.$k.'.citrix_product_id', $eventNames),
$q->expr()->eq($alias.$k.'.citrix_product_id', $productId),
$q->expr()->eq($alias.$k.'.contact_id', 'l.id')
)
);
Expand Down
2 changes: 1 addition & 1 deletion Form/Type/GoToListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
foreach ($products as $key => $product) {
$date = DateTime::createFromFormat('Y-m-d H:i:s.u', $product['date']['date']);
if (false !== $date && STATUS_ACTIVE === $product['status']) {
$active_products[$key] = $date->format('d.m.Y H:i').' '.(null !== $product['recurrence_key'] ? '(...) ' : '').$product['name'];
$active_products[$key] = $date->format('d.m.Y H:i:s').' '.(null !== $product['recurrence_key'] ? '(...) ' : '').$product['name'];
}
}

Expand Down
10 changes: 10 additions & 0 deletions Model/GoToModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,12 +530,22 @@ public function getProducts($product_name, $from = null, $to = null, $reduceSess

public function getIdByNameAndDate($name, $date)
{
/** @var GoToProductRepository $productRepository */
$productRepository = $this->em->getRepository(GoToProduct::class);
$result = $productRepository->findOneBy(['name' => $name, 'date' => $date]);

return $result ? $result->getId() : null;
}

public function getIdByNameAndDateRange($name, $date): ?int
{
/** @var GoToProductRepository $productRepository */
$productRepository = $this->em->getRepository(GoToProduct::class);
$result = $productRepository->findOneByProductByNameAndDate($name, $date);

return $result ? $result->getId() : null;
}

public function getProductById($id)
{
$cpr = $this->em->getRepository(GoToProduct::class);
Expand Down
69 changes: 69 additions & 0 deletions Tests/CreateEntities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace MauticPlugin\LeuchtfeuerGoToBundle\Tests;

use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\LeadList;
use Mautic\PluginBundle\Entity\Integration;
use Mautic\PluginBundle\Entity\Plugin;
use Mautic\PluginBundle\Helper\IntegrationHelper;

trait CreateEntities
{
protected function createSegment($listConfig): LeadList
{
$list = new LeadList();
$list->setName($listConfig['name']);
$list->setPublicName($listConfig['name']);
$list->setAlias($listConfig['alias']);
$list->setIsGlobal($listConfig['public']);
$list->setFilters($listConfig['filters']);

$this->em->persist($list);

return $list;
}

protected function createIntegration(): void
{
$plugin = new Plugin();
$plugin->setName('GoTo Integration by Leuchtfeuer');
$plugin->setDescription('Enables integration with Mautic supported GoTo collaboration products.');
$plugin->setBundle('LeuchtfeuerGoToBundle');
$plugin->setVersion('1.0');
$plugin->setAuthor('Mautic');

$this->em->persist($plugin);

$webinar = new Integration();
$webinar->setIsPublished(true);
$webinar->setApiKeys([
'app_name' => 'Mautic',
'client_id' => 'client_id',
'client_secret' => 'client_secret',
]);
$webinar->setPlugin($plugin);
$webinar->setName('GoTo Integration by Leuchtfeuer');

$this->em->persist($webinar);
$this->em->flush();

/** @var IntegrationHelper $integrationHelper */
$integrationHelper = self::$container->get('mautic.helper.integration');

/** @var Integration $integration */
$integrationObject = $integrationHelper->getIntegrationObject('Gotowebinar');

$integrationObject->encryptAndSetApiKeys([
'app_name' => 'Mautic',
'client_id' => 'client_id',
'client_secret' => 'client_secret',
], $webinar);

$integrationObject->getIntegrationSettings()->setIsPublished(true);

$this->em->flush();
}
}
57 changes: 29 additions & 28 deletions Tests/DataFixtures/ORM/LoadCitrixData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,18 @@

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;
use Mautic\LeadBundle\Entity\Lead;
use MauticPlugin\LeuchtfeuerGoToBundle\Entity\GoToEvent;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use MauticPlugin\LeuchtfeuerGoToBundle\Entity\GoToProduct;

/**
* Class LoadPageData.
*/
class LoadCitrixData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
class LoadCitrixData extends AbstractFixture implements OrderedFixtureInterface
{
/**
* @var ContainerInterface
*/
private $container;

/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null)
public function load(ObjectManager $manager): void
{
$this->container = $container;
}

public function load(ObjectManager $manager)
{
$em = $this->container->get('doctrine')->getManager();
$today = new \DateTime();
$email = 'joe.o\'connor@domain.com';

Expand All @@ -49,23 +34,39 @@ public function load(ObjectManager $manager)
$lead->setEmail($email);
$lead->checkAttributionDate();

$em->persist($lead);
$em->flush();
$manager->persist($lead);
$manager->flush();

$this->setReference('lead-citrix', $lead);

$product = new GoToProduct();
$product->setName('Sample Webinar');
$product->setDate($today->add(new \DateInterval('P1D')));
$product->setDuration(3600);
$product->setProductKey('1234567');
$product->setRecurrenceKey('7654321');
$product->setOrganizerKey('12123434');
$product->setProduct('webinar');
$product->setStatus('active');

$manager->persist($product);
$manager->flush();

$this->setReference('citrix-product', $product);


// create event
$event = new GoToEvent();
$event->setLead($lead);
$event->setContact($lead);
$event->setEventDate($today);
$event->setProduct('webinar');
$event->setEmail($email);
$event->setGoToProduct($product);
$event->setContact($lead);
$event->setEventType('registered');
$event->setEventName('sample-webinar_#0000');
$event->setEventDesc('Sample Webinar');
$event->setJoinUrl('sample-webinar_#0000');
// $event->setEventDesc('Sample Webinar');

$em->persist($event);
$em->flush();
$manager->persist($event);
$manager->flush();

$this->setReference('citrix-event', $event);
}
Expand Down
80 changes: 80 additions & 0 deletions Tests/Functional/GotoSegmentFilterFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace MauticPlugin\LeuchtfeuerGoToBundle\Tests\Functional;

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\UserBundle\DataFixtures\ORM\LoadRoleData;
use Mautic\UserBundle\DataFixtures\ORM\LoadUserData;
use MauticPlugin\LeuchtfeuerGoToBundle\Tests\CreateEntities;
use MauticPlugin\LeuchtfeuerGoToBundle\Tests\DataFixtures\ORM\LoadCitrixData;

class GotoSegmentFilterFunctionalTest extends MauticMysqlTestCase
{
use CreateEntities;

protected function setUp(): void
{
parent::setUp();

$this->fixtures = $this->loadFixtures(
[
LoadRoleData::class,
LoadUserData::class,
LoadCitrixData::class
],
false
)->getReferenceRepository();

$this->createIntegration();
}

protected function beforeBeginTransaction(): void
{
$this->resetAutoincrement(
[
'leads',
'lead_lists',
'users',
]
);
}

public function testGotoSegments(): void
{
// Create Segment with filters
$segment = $this->createSegment([
'name' => 'Goto: Webinar Registration',
'alias' => 'goto-webinar-registration',
'public' => true,
'filters' => [
[
'glue' => 'and',
'type' => 'email',
'object' => 'lead',
'field' => 'webinar-registration',
'operator' => 'including',
'properties' => ['filter' => 'Any Webinar', 'display' => null],
],
],
]);

$this->em->flush();
$this->em->clear();

// Build segments
$command = $this->testSymfonyCommand(
'mautic:segments:update',
[
'-i' => $segment->getId(),
'--env' => 'test',
]
);

$output = $command->getDisplay();
$this->assertEquals(0, $command->getStatusCode());
$this->assertStringContainsString('1 total contact(s) to be added in batches of 300', $output);
$this->assertStringContainsString('1 contact(s) affected', $output);
}
}
Loading
Loading