developped own webhook controller for mailjet synch

This commit is contained in:
Bachir Soussi Chiadmi 2022-07-04 12:36:40 +02:00
parent ba4a025335
commit 97a9178006
11 changed files with 150 additions and 43 deletions

View File

@ -15,7 +15,7 @@ provider: null
plugin: user_login_block
settings:
id: user_login_block
label: 'Membre access'
label: 'Accès membre'
label_display: visible
provider: user
visibility:

View File

@ -140,6 +140,7 @@ module:
materio_graphql: 0
materio_home: 0
materio_jsonapi: 0
materio_mailjet: 0
materio_samples: 0
materio_sapi: 0
materio_simplenews: 0

View File

@ -1,6 +1,6 @@
uuid: 230f2c4e-c4b4-4179-b9fb-c999cf7a2ddf
langcode: en
status: true
status: false
dependencies: { }
_core:
default_config_hash: 1yiBtHZuy8FsRApYQ9KutX2GXWP6ZmxePqYl2PlyYRo
@ -26,6 +26,8 @@ expression:
-
id: rules_condition
uuid: 1c701012-abc3-4fbd-94ee-6e0e4d56b610
condition_id: rules_data_comparison
negate: false
context_values:
operation: '=='
value: blocked
@ -37,8 +39,6 @@ expression:
value:
rules_tokens: { }
provides_mapping: { }
condition_id: rules_data_comparison
negate: false
actions:
id: rules_action_set
uuid: c2a204bc-1679-415e-b67d-d7a2665f87ca

View File

@ -1,6 +1,6 @@
uuid: 391a5122-6639-4be1-ab72-cecd14167ca1
langcode: en
status: true
status: false
dependencies: { }
_core:
default_config_hash: U_GmlNbp5E1n9UAmEVzmCbLUL5dMoYvsQ47BZrdUT6w
@ -26,6 +26,8 @@ expression:
-
id: rules_condition
uuid: 10af217b-e797-4bcc-9789-d3ae3e333762
condition_id: rules_data_comparison
negate: false
context_values:
operation: '=='
value: bounce
@ -37,8 +39,6 @@ expression:
value:
rules_tokens: { }
provides_mapping: { }
condition_id: rules_data_comparison
negate: false
actions:
id: rules_action_set
uuid: 6f2219f0-df60-4f5d-9ebb-f4dc21e0d2af

View File

@ -1,6 +1,6 @@
uuid: 4396179b-baf3-4613-bdb0-a304d48261c1
langcode: en
status: true
status: false
dependencies: { }
_core:
default_config_hash: DcQy3zLhh7gJFz1DMcClcpvGDtMeA0yio_sVGADE82o
@ -26,6 +26,8 @@ expression:
-
id: rules_condition
uuid: ff98f6a0-0428-460f-808d-cd85da30812e
condition_id: rules_data_comparison
negate: false
context_values:
operation: '=='
value: clicked
@ -37,8 +39,6 @@ expression:
value:
rules_tokens: { }
provides_mapping: { }
condition_id: rules_data_comparison
negate: false
actions:
id: rules_action_set
uuid: 5ef4a8a2-891d-4d6f-aaf5-4fd61fd88784

View File

@ -1,6 +1,6 @@
uuid: 6f3712ca-b451-429d-816f-dd1c4063a9ae
langcode: en
status: true
status: false
dependencies: { }
_core:
default_config_hash: A3m7CCcbVLT6GKSYfmEL4doWJRPs332bdm4HX3Bxz2Q
@ -26,6 +26,8 @@ expression:
-
id: rules_condition
uuid: 41580ec0-e516-4098-b1c7-034ab13db2f4
condition_id: rules_data_comparison
negate: false
context_values:
operation: '=='
value: open
@ -37,8 +39,6 @@ expression:
value:
rules_tokens: { }
provides_mapping: { }
condition_id: rules_data_comparison
negate: false
actions:
id: rules_action_set
uuid: 15a6fd6f-dcc5-4f05-8775-670a337c7823

View File

@ -1,6 +1,6 @@
uuid: 4ee8baa3-ec10-4716-a490-c67888e601ec
langcode: en
status: true
status: false
dependencies: { }
_core:
default_config_hash: TuDPeYYD1x870iE1SLR_YFsuPC3oTRW8BO-coBkLXtc
@ -26,6 +26,8 @@ expression:
-
id: rules_condition
uuid: 1053b6b5-efcb-4339-a618-ee4719cd41c6
condition_id: rules_data_comparison
negate: false
context_values:
operation: '=='
value: spam
@ -37,8 +39,6 @@ expression:
value:
rules_tokens: { }
provides_mapping: { }
condition_id: rules_data_comparison
negate: false
actions:
id: rules_action_set
uuid: 9e5b9e63-86a9-4740-b20d-c79ae00b74a8

View File

@ -1,6 +1,6 @@
uuid: 1308193a-4425-4fc9-95a0-2e7f6c271278
langcode: en
status: true
status: false
dependencies: { }
_core:
default_config_hash: MyY_Lp-SmU46vjiG1JfDXfyjoOc_O-YBiWnBlfDMC_8
@ -46,29 +46,4 @@ expression:
id: rules_action_set
uuid: 5c91a286-ea41-47f7-b6a9-451cb4bb2324
weight: 0
actions:
-
id: rules_action
uuid: 9e184f51-443d-41ec-94c0-2c5ec5bdb1db
weight: -50
action_id: 'rules_entity_create:commerce_log'
context_values: { }
context_mapping: { }
context_processors: { }
provides_mapping:
commerce_log_created: mailjet_event_log_created
-
id: rules_action
uuid: 7eb49497-8830-46d4-975c-6b25b5d6dee6
weight: -49
action_id: rules_simplenews_mailjet_subscriptions_unsubscribe_from_list
context_values:
listID: mailjet_event.event_field
email: mailjet_event.event_field
context_mapping: { }
context_processors:
listID:
rules_tokens: { }
email:
rules_tokens: { }
provides_mapping: { }
actions: { }

View File

@ -0,0 +1,8 @@
name: Materio Mailjet
description: Add support for MailJet Webhooks.
package: Materio
type: module
core_version_requirement: ^8.8 || ^9
dependencies:
- mailjet:mailjet

View File

@ -0,0 +1,7 @@
materio_mailjet.webhook:
path: '/materio_mailjet_webhook'
defaults:
_controller: 'Drupal\materio_mailjet\Controller\MaterioMailjetWebhookCallbackController::callback'
_title: 'Event callback'
requirements:
_access: 'TRUE'

View File

@ -0,0 +1,116 @@
<?php
namespace Drupal\materio_mailjet\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\simplenews_mailjet_subscriptions\Entity\SimplenewsMailjetSubscriptionEntity;
use Drupal\simplenews\Entity\Subscriber;
use Drupal\simplenews\Subscription\SubscriptionManagerInterface;
/**
* Module callback Controller.
*/
class MaterioMailjetWebhookCallbackController extends ControllerBase {
/**
* The subscription manager.
*
* @var \Drupal\simplenews\Subscription\SubscriptionManagerInterface
*/
protected $subscriptionManager;
/**
* Constructs a \Drupal\simplenews\Controller\ConfirmationController object.
*
* @param \Drupal\simplenews\Subscription\SubscriptionManagerInterface $subscription_manager
* The subscription manager service.
*/
public function __construct(SubscriptionManagerInterface $subscription_manager) {
$this->subscriptionManager = $subscription_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('simplenews.subscription_manager')
);
}
/**
* Callback for uninstall the module.
*/
public function callback() {
// No Event sent.
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
print 'No data';
// return false;
}
$post = trim(file_get_contents('php://input'));
$this->getLogger('materio_mailjet')->notice(print_r($post, TRUE));
$event = json_decode($post);
// $event = $json_post[0];
// No information sent with the Event.
if (!is_object($event) && !isset($event)) {
$this->getLogger('materio_mailjet')
->error('No Informations sent with the Event.');
// return false;
}
switch ($event->event) {
case 'unsub':
$this->unsub($event);
break;
};
return [];
}
private function unsub($event) {
// \Drupal::logger('simplenews_mailjet_subscriptions')->info("Unsubscribe webhook triggered");
$MjlistID = $event->mj_list_id;
$email = $event->email;
// find corresponding simplenews list from mailjet listID
$newsletter_id = null;
$entities = SimplenewsMailjetSubscriptionEntity::loadMultiple();
foreach ((array) $entities as $entity) {
$listID = (int)$entity->getID();
if ($listID === $MjlistID) {
$table = $entity->get('mapping_table');
if (is_array($table)) {
foreach ($table as $category) {
$newsletter_id = $category['simplenews_news'];
break;
}
}
break;
}
}
// \Drupal::logger('simplenews_mailjet_subscriptions')->info("Unsubscribe webhook triggered for listID: " . $listID . "and email: " . $email . ".");
// get subscriber entity
// Load entities by their property values.
$subscribers = \Drupal::entityTypeManager()->getStorage('simplenews_subscriber')->loadByProperties(['mail' => $email]);
// unsubscribe subscriber from simplenews list
if (is_array($subscribers) && count($subscribers) && $newsletter_id) {
$subscriber = array_shift($subscribers);
if ($subscriber instanceof Subscriber) {
// $subscriber->unsubscribe($simplenews_news, 'mailjet');
$this->subscriptionManager->unsubscribe($subscriber->getMail(), $newsletter_id, FALSE, 'materio_mailjet');
}
}
}
}