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

Add contact segment membership webhook #10511

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 35 additions & 8 deletions app/bundles/LeadBundle/EventListener/WebhookSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Mautic\LeadBundle\Event\CompanyEvent;
use Mautic\LeadBundle\Event\LeadChangeCompanyEvent;
use Mautic\LeadBundle\Event\LeadEvent;
use Mautic\LeadBundle\Event\ListChangeEvent;
use Mautic\LeadBundle\Event\PointsChangeEvent;
use Mautic\LeadBundle\LeadEvents;
use Mautic\WebhookBundle\Event\WebhookBuilderEvent;
Expand All @@ -31,14 +32,16 @@ public function __construct(WebhookModel $webhookModel)
public static function getSubscribedEvents()
{
return [
WebhookEvents::WEBHOOK_ON_BUILD => ['onWebhookBuild', 0],
LeadEvents::LEAD_POST_SAVE => ['onLeadNewUpdate', 0],
LeadEvents::LEAD_POINTS_CHANGE => ['onLeadPointChange', 0],
LeadEvents::LEAD_POST_DELETE => ['onLeadDelete', 0],
LeadEvents::CHANNEL_SUBSCRIPTION_CHANGED => ['onChannelSubscriptionChange', 0],
LeadEvents::LEAD_COMPANY_CHANGE => ['onLeadCompanyChange', 0],
LeadEvents::COMPANY_POST_SAVE => ['onCompanySave', 0],
LeadEvents::COMPANY_POST_DELETE => ['onCompanyDelete', 0],
WebhookEvents::WEBHOOK_ON_BUILD => ['onWebhookBuild', 0],
LeadEvents::LEAD_POST_SAVE => ['onLeadNewUpdate', 0],
LeadEvents::LEAD_POINTS_CHANGE => ['onLeadPointChange', 0],
LeadEvents::LEAD_POST_DELETE => ['onLeadDelete', 0],
LeadEvents::CHANNEL_SUBSCRIPTION_CHANGED => ['onChannelSubscriptionChange', 0],
LeadEvents::LEAD_COMPANY_CHANGE => ['onLeadCompanyChange', 0],
LeadEvents::COMPANY_POST_SAVE => ['onCompanySave', 0],
LeadEvents::COMPANY_POST_DELETE => ['onCompanyDelete', 0],
LeadEvents::LEAD_LIST_CHANGE => ['onSegmentChange', 0],
LeadEvents::LEAD_LIST_BATCH_CHANGE => ['onSegmentChange', 0],
];
}

Expand Down Expand Up @@ -118,6 +121,15 @@ public function onWebhookBuild(WebhookBuilderEvent $event)
'description' => 'mautic.lead.webhook.event.company.deleted_desc',
]
);

// add checkbox to the webhook contact segment membership changed
$event->addEvent(
LeadEvents::LEAD_LIST_CHANGE,
[
'label' => 'mautic.lead.webhook.event.lead.segment.change',
'description' => 'mautic.lead.webhook.event.lead.segment.change.desc',
]
);
}

public function onLeadNewUpdate(LeadEvent $event)
Expand Down Expand Up @@ -249,4 +261,19 @@ public function onCompanyDelete(CompanyEvent $event)
]
);
}

public function onSegmentChange(ListChangeEvent $changeEvent)
{
$contacts = null !== $changeEvent->getLeads() ? $changeEvent->getLeads() : [$changeEvent->getLead()];
foreach ($contacts as $contact) {
$this->webhookModel->queueWebhooksByType(
LeadEvents::LEAD_LIST_CHANGE,
[
'contact' => $contact,
'segment' => $changeEvent->getList(),
'action' => $changeEvent->wasAdded() ? 'added' : 'removed',
]
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use Mautic\LeadBundle\Entity\Company;
use Mautic\LeadBundle\Entity\DoNotContact;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\LeadList;
use Mautic\LeadBundle\Event\ChannelSubscriptionChange;
use Mautic\LeadBundle\Event\CompanyEvent;
use Mautic\LeadBundle\Event\LeadChangeCompanyEvent;
use Mautic\LeadBundle\Event\LeadEvent;
use Mautic\LeadBundle\Event\ListChangeEvent;
use Mautic\LeadBundle\EventListener\WebhookSubscriber;
use Mautic\LeadBundle\LeadEvents;
use Mautic\WebhookBundle\Model\WebhookModel;
Expand Down Expand Up @@ -185,4 +187,28 @@ public function testOnCompanySaveAndDelete()
$dispatcher->dispatch(LeadEvents::COMPANY_POST_SAVE, $event);
$dispatcher->dispatch(LeadEvents::COMPANY_POST_DELETE, $event);
}

public function testOnSegmentChange()
{
$mockModel = $this->createMock(WebhookModel::class);

$mockModel->expects($this->once())
->method('queueWebhooksByType')
->with(
$this->callback(
function ($type) {
return LeadEvents::LEAD_LIST_CHANGE === $type;
}
)
);

$webhookSubscriber = new WebhookSubscriber($mockModel);

$this->dispatcher->addSubscriber($webhookSubscriber);

$lead = new Lead();
$segment = new LeadList();
$event = new ListChangeEvent($lead, $segment, true);
$this->dispatcher->dispatch(LeadEvents::LEAD_LIST_CHANGE, $event);
}
}
2 changes: 2 additions & 0 deletions app/bundles/LeadBundle/Translations/en_US/messages.ini
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,8 @@ mautic.lead.webhook.event.company.new_or_update="Company Create/Update Event"
mautic.lead.webhook.event.company.new_or_update_desc="Triggered when a company is created/updated"
mautic.lead.webhook.event.company.deleted="Company Deleted Event"
mautic.lead.webhook.event.company.deleted_desc="Triggered when a company is deleted"
mautic.lead.webhook.event.lead.segment.change="Contact Segment Membership Change Event"
mautic.lead.webhook.event.lead.segment.change.desc="Triggered when a contact segment membership is changed"
mautic.lead.campaign.event.field="Contact Field"
mautic.lead.campaign.event.field_descr="Select the contact field which holds the value you want to compare."
mautic.lead.campaign.event.device_type="Device type"
Expand Down