Skip to content

Commit

Permalink
feat(SFT-1624): added additional error catching for tags in mailchimp…
Browse files Browse the repository at this point in the history
… integrations (#1663)
  • Loading branch information
seandelaney authored Nov 29, 2024
1 parent de84fd1 commit f6fc1a0
Showing 1 changed file with 63 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,20 @@ protected function manageTags(Client $client, string $listId, string $email, arr

$emailHash = md5(strtolower($email));

$response = $client->get(
$this->getEndpoint('/lists/'.$listId.'/members/'.$emailHash.'/tags'),
[
'query' => [
'count' => 999,
try {
$response = $client->get(
$this->getEndpoint('/lists/'.$listId.'/members/'.$emailHash.'/tags'),
[
'query' => [
'count' => 999,
],
],
],
);
);

$this->triggerAfterResponseEvent(self::CATEGORY_TAG, $response);
} catch (RequestException $exception) {
throw $exception;
}

$json = json_decode((string) $response->getBody());

Expand Down Expand Up @@ -265,15 +271,21 @@ private function fetchTags(Client $client, string $listId): array
return $this->existingTags;
}

$response = $client->get(
$this->getEndpoint('/lists/'.$listId.'/segments'),
[
'query' => [
'fields' => 'segments.id,segments.name',
'count' => 999,
try {
$response = $client->get(
$this->getEndpoint('/lists/'.$listId.'/segments'),
[
'query' => [
'fields' => 'segments.id,segments.name',
'count' => 999,
],
],
],
);
);

$this->triggerAfterResponseEvent(self::CATEGORY_TAG, $response);
} catch (RequestException $exception) {
throw $exception;
}

$json = json_decode((string) $response->getBody());

Expand All @@ -293,15 +305,21 @@ private function getOrCreateTag(Client $client, string $listId, string $tagName)
return array_search($tagNameLowerCase, $existingTags, true);
}

$response = $client->post(
$this->getEndpoint('/lists/'.$listId.'/segments'),
[
'json' => [
'name' => $tagName,
'static_segment' => [],
try {
$response = $client->post(
$this->getEndpoint('/lists/'.$listId.'/segments'),
[
'json' => [
'name' => $tagName,
'static_segment' => [],
],
],
],
);
);

$this->triggerAfterResponseEvent(self::CATEGORY_TAG, $response);
} catch (RequestException $exception) {
throw $exception;
}

$json = json_decode((string) $response->getBody());

Expand All @@ -313,21 +331,35 @@ private function addTagsForMember(Client $client, string $listId, string $email,
foreach ($tags as $tag) {
$tagId = $this->getOrCreateTag($client, $listId, $tag);

$client->post(
$this->getEndpoint('/lists/'.$listId.'/segments/'.$tagId.'/members'),
[
'json' => [
'email_address' => $email,
try {
$response = $client->post(
$this->getEndpoint('/lists/'.$listId.'/segments/'.$tagId.'/members'),
[
'json' => [
'email_address' => $email,
],
],
],
);
);

$this->triggerAfterResponseEvent(self::CATEGORY_TAG, $response);
} catch (RequestException $exception) {
throw $exception;
}

$json = json_decode((string) $response->getBody());
}
}

private function deleteTagsForMember(Client $client, string $listId, string $emailHash, array $tagsToDelete): void
{
foreach ($tagsToDelete as $tagId => $tagName) {
$client->delete($this->getEndpoint('/lists/'.$listId.'/segments/'.$tagId.'/members/'.$emailHash));
try {
$response = $client->delete($this->getEndpoint('/lists/'.$listId.'/segments/'.$tagId.'/members/'.$emailHash));

$this->triggerAfterResponseEvent(self::CATEGORY_TAG, $response);
} catch (RequestException $exception) {
throw $exception;
}
}
}

Expand Down

0 comments on commit f6fc1a0

Please sign in to comment.