diff --git a/src/HubspotEmailChannel.php b/src/HubspotEmailChannel.php index 0ca65fd..706d780 100644 --- a/src/HubspotEmailChannel.php +++ b/src/HubspotEmailChannel.php @@ -74,6 +74,24 @@ public function send($notifiable, Notification $notification): ?array throw CouldNotSendNotification::serviceRespondedWithAnError($newResp->body()); } $hubspotEmail['associations'] = $newResp['associations']; + + + $url = 'https://api.hubapi.com/crm/v3/objects/contacts/'.$hubspotContactId.'?associations=company'; + $contactResp = $this->callApi($url, 'get'); + + if ($contactResp->status() != 200) { + throw CouldNotSendNotification::serviceRespondedWithAnError($newResp->body()); + } + + if ($hubspotCompanyId = $contactResp['associations']['companies']['results'][0]['id'] ?? null) { + $url = self::HUBSPOT_URL.'/'.$hubspotEmail['id'].'/associations/companies/'.$hubspotCompanyId.'/185'; + $newResp = $this->callApi($url, 'put'); + + if ($newResp->status() != 200) { + throw CouldNotSendNotification::serviceRespondedWithAnError($newResp->body()); + } + $hubspotEmail['associations'] = array_merge($hubspotEmail['associations'], $newResp['associations']); + } } else { throw CouldNotSendNotification::serviceRespondedWithAnError($response->body()); }