Skip to content

Commit

Permalink
Fixed create / update of simple emails
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck Allimant committed Nov 21, 2023
1 parent 225a455 commit 8622fc0
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions Api/BrevoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ public function checkIfContactExist($email)
return $this->contactApi->getContactInfoWithHttpInfo($email);
}

public function buildCreateOrUpdateData(string $email): array
{
$contactAttribute = [];

if (null !== $customer = CustomerQuery::create()->findOneByEmail($email)) {
$contactAttribute = $this->getCustomerAttribute($customer->getId());
}

$data['email'] = $email;
if (! empty($contactAttribute)) {
$data['attributes'] = $contactAttribute;
}
$data['listIds'] = [$this->newsletterId];

return $data;
}

public function createContact(string $email)
{
$contactAttribute = [];
Expand All @@ -89,22 +106,16 @@ public function createContact(string $email)
$contactAttribute = $this->getCustomerAttribute($customer->getId());
}

$createContact = new CreateContact();
$createContact['email'] = $email;
$createContact['attributes'] = $contactAttribute;
$createContact['listIds'] = [$this->newsletterId];
$createContact = new CreateContact($this->buildCreateOrUpdateData($email));

$this->contactApi->createContactWithHttpInfo($createContact);

return $this->contactApi->getContactInfoWithHttpInfo($email);
}

public function updateContact($identifier, Customer $customer)
{
$contactAttribute = $this->getCustomerAttribute($customer->getId());
$createContact = new UpdateContact();
$createContact['email'] = $customer->getEmail();
$createContact['attributes'] = $contactAttribute;
$createContact['listIds'] = [$this->newsletterId];
$createContact = new UpdateContact($this->buildCreateOrUpdateData($customer->getEmail()));
$this->contactApi->updateContactWithHttpInfo($identifier, $createContact);

return $this->contactApi->getContactInfoWithHttpInfo($customer->getEmail());
Expand Down

0 comments on commit 8622fc0

Please sign in to comment.