diff --git a/src/Models/HubspotCompany.php b/src/Models/HubspotCompany.php index 6d40a54..70cf63d 100644 --- a/src/Models/HubspotCompany.php +++ b/src/Models/HubspotCompany.php @@ -69,6 +69,8 @@ public static function updateOrCreateHubspotCompany($model) $hubspotCompany = static::getCompanyById($model); + // TODO if no hubspot id or if id fetch failed, try searching by name + if (! $hubspotCompany) { return static::createHubspotCompany($model); } @@ -89,16 +91,6 @@ public static function getCompanyById($model) } } - // TODO if no hubspot id or if id fetch failed, try searching by name - // try { - // $hubspotCompany = Hubspot::crm()->companies()->basicApi()->getById($model->email, null, null, null, false, 'email'); - - // // dont save to prevent loop from model event - // $model->hubspot_id = $hubspotCompany['id']; - // } catch (ApiException $e) { - // Log::debug('Hubspot company not found with email', ['email' => $model->email]); - // } - return $hubspotCompany; } @@ -147,8 +139,7 @@ public static function findOrCreateCompany($properties) try { $searchResults = Hubspot::crm()->companies()->searchApi()->doSearch($companySearch); } catch (\Exception $e) { - // TODO debugging - dump($filter, $properties); + // dump($filter, $properties); // dd($e); throw ($e); } diff --git a/src/Models/HubspotContact.php b/src/Models/HubspotContact.php index b82c02d..e5bed68 100644 --- a/src/Models/HubspotContact.php +++ b/src/Models/HubspotContact.php @@ -147,43 +147,6 @@ public function hubspotPropertiesObject(array $map): ContactObject return new ContactObject(['properties' => $this->hubspotProperties($map)]); } - public static function findOrCreateCompany($properties) - { - $filter = new Filter([ - 'value' => $properties['name'], - 'property_name' => 'name', - 'operator' => 'EQ', - ]); - - $filterGroup = new FilterGroup([ - 'filters' => [$filter], - ]); - - $companySearch = new CompanySearch([ - 'filter_groups' => [$filterGroup], - ]); - - $searchResults = Hubspot::crm()->companies()->searchApi()->doSearch($companySearch); - - $companyExists = $searchResults['total']; - - if ($companyExists) { - return $searchResults['results'][0]; - } else { - // TODO create company - dd('todo create company from relation'); - $properties = [ - 'na' => $domain, - ]; - - $companyObject = new CompanyObject([ - 'properties' => $properties, - ]); - - return Hubspot::crm()->companies()->basicApi()->create($companyObject); - } - } - public static function associateCompanyWithContact(string $companyId, string $contactId) { $associationSpec = new AssociationSpec([ @@ -192,12 +155,11 @@ public static function associateCompanyWithContact(string $companyId, string $co ]); try { - $apiResponse = Hubspot::crm()->associations()->v4()->basicApi()->create('contact', $contactId, 'company', $companyId, [$associationSpec]); + return Hubspot::crm()->associations()->v4()->basicApi()->create('contact', $contactId, 'company', $companyId, [$associationSpec]); } catch (AssociationsApiException $e) { // dd($companyId, $contactId); - dd($e); + // dd($e); throw ($e); - echo 'Exception when calling basic_api->create: ', $e->getMessage(); } } }