From 6b62a74dd940c173bcfafecc3841d04eea18f362 Mon Sep 17 00:00:00 2001 From: Joaquim d'Souza Date: Wed, 13 May 2026 17:22:23 +0200 Subject: [PATCH] fix: check person exists in CRMs before trying to tag them --- .../src/Services/ActionNetworkService.php | 37 +++++++++++++++++++ .../src/Services/MailchimpService.php | 10 +++++ 2 files changed, 47 insertions(+) diff --git a/packages/join-block/src/Services/ActionNetworkService.php b/packages/join-block/src/Services/ActionNetworkService.php index ab3e6b46..ce4e8b96 100644 --- a/packages/join-block/src/Services/ActionNetworkService.php +++ b/packages/join-block/src/Services/ActionNetworkService.php @@ -93,6 +93,8 @@ public static function signup($data) ]; } + $joinBlockLog->info("Action Network payload for {$data['email']}: " . json_encode($anData)); + try { $client = new Client(); $client->request( @@ -113,8 +115,36 @@ public static function signup($data) } } + public static function personExists($email) + { + $client = new Client(); + + $response = $client->request( + "GET", + "https://actionnetwork.org/api/v2/people/", + [ + "headers" => [ + "OSDI-API-Token" => Settings::get("ACTION_NETWORK_API_KEY") + ], + "query" => [ + "filter" => "email_address eq '" . $email . "'" + ] + ] + ); + + $data = json_decode($response->getBody()->getContents(), true); + return !empty($data["_embedded"]["osdi:people"]); + } + public static function addTag($email, $tag) { + global $joinBlockLog; + + if (!self::personExists($email)) { + $joinBlockLog->warning("Skipping Action Network addTag('$tag') for $email: person does not exist"); + return; + } + $client = new Client(); $data = [ @@ -143,6 +173,13 @@ public static function addTag($email, $tag) public static function removeTag($email, $tag) { + global $joinBlockLog; + + if (!self::personExists($email)) { + $joinBlockLog->warning("Skipping Action Network removeTag('$tag') for $email: person does not exist"); + return; + } + $client = new Client(); $data = [ diff --git a/packages/join-block/src/Services/MailchimpService.php b/packages/join-block/src/Services/MailchimpService.php index ec50b85c..5f96e6a3 100644 --- a/packages/join-block/src/Services/MailchimpService.php +++ b/packages/join-block/src/Services/MailchimpService.php @@ -252,6 +252,11 @@ public static function addTag($email, $tag) { global $joinBlockLog; + if (!self::memberExists($email)) { + $joinBlockLog->warning("Skipping Mailchimp addTag('$tag') for $email: member does not exist"); + return; + } + $mailchimp = self::getClient(); $mailchimp_audience_id = Settings::get("MAILCHIMP_AUDIENCE_ID"); @@ -274,6 +279,11 @@ public static function removeTag($email, $tag) { global $joinBlockLog; + if (!self::memberExists($email)) { + $joinBlockLog->warning("Skipping Mailchimp removeTag('$tag') for $email: member does not exist"); + return; + } + $mailchimp = self::getClient(); $mailchimp_audience_id = Settings::get("MAILCHIMP_AUDIENCE_ID");