Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
scott grayson authored and scott grayson committed Dec 20, 2024
1 parent 6dafdb5 commit 044a74e
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/Models/HubspotContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ trait HubspotContact
{
// public array $hubspotMap = [];

// public array $hubspotCompanyMap = [];

public static function bootHubspotContact(): void
{
static::creating(fn (Model $model) => static::updateOrCreateHubspotContact($model));
Expand All @@ -28,7 +30,7 @@ public static function bootHubspotContact(): void
public static function createHubspotContact($model)
{
try {
$hubspotContact = Hubspot::crm()->contacts()->basicApi()->create($model->hubspotPropertiesObject());
$hubspotContact = Hubspot::crm()->contacts()->basicApi()->create($model->hubspotPropertiesObject($model->hubspotMap));

$model->hubspot_id = $hubspotContact['id'];
} catch (ApiException $e) {
Expand All @@ -38,8 +40,7 @@ public static function createHubspotContact($model)
return;
}

$domain = preg_replace('/[^@]+@/i', '', $model->email);
$hubspotCompany = static::findOrCreateCompanyByDomain($domain);
$hubspotCompany = static::findOrCreateCompany($model->hubspotPropertiesObject($model->hubspotCompanyMap));

static::associateCompanyWithContact($hubspotCompany['id'], $hubspotContact['id']);

Expand All @@ -53,10 +54,16 @@ public static function updateHubspotContact($model)
}

try {
return Hubspot::crm()->contacts()->basicApi()->update($model->hubspot_id, $model->hubspotPropertiesObject());
Hubspot::crm()->contacts()->basicApi()->update($model->hubspot_id, $model->hubspotPropertiesObject($model->hubspotMap));
} catch (ApiException $e) {
Log::error('Hubspot contact update failed', ['email' => $model->email]);
}

$hubspotCompany = static::findOrCreateCompany($model->hubspotPropertiesObject($model->hubspotCompanyMap));

static::associateCompanyWithContact($hubspotCompany['id'], $hubspotContact['id']);

return $hubspotContact;
}

/*
Expand Down Expand Up @@ -98,11 +105,11 @@ public static function updateOrCreateHubspotContact($model)
/**
* get properties to be synced with hubspot
*/
public function hubspotProperties(): array
public function hubspotProperties(array $map): array
{
$properties = [];

foreach ($this->hubspotMap as $key => $value) {
foreach ($map as $key => $value) {
if (strpos($value, '.')) {
$properties[$key] = data_get($this, $value);
} else {
Expand All @@ -116,19 +123,16 @@ public function hubspotProperties(): array
/**
* get properties to be synced with hubspot
*/
public function hubspotPropertiesObject(): ContactObject
public function hubspotPropertiesObject(array $map): ContactObject
{
return new ContactObject(['properties' => $this->hubspotProperties()]);
return new ContactObject(['properties' => $this->hubspotProperties($map)]);
}

/**
* TODO untested
*/
public static function findOrCreateCompanyByDomain(string $domain)
public static function findOrCreateCompany($properties)
{
$filter = new Filter([
'value' => $domain,
'property_name' => 'domain',
'value' => $properties['name'],
'property_name' => 'name',
'operator' => 'EQ',
]);

Expand All @@ -148,7 +152,7 @@ public static function findOrCreateCompanyByDomain(string $domain)
return $searchResults['results'][0];
} else {
$properties = [
'domain' => $domain,
'na' => $domain,
];

$companyObject = new CompanyObject([
Expand Down

0 comments on commit 044a74e

Please sign in to comment.