Skip to content

Commit c337d9e

Browse files
committed
alpha/develop: Updated the Readme.me
1 parent ddb18ee commit c337d9e

File tree

1 file changed

+126
-11
lines changed

1 file changed

+126
-11
lines changed

README.md

Lines changed: 126 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<a href="https://github.com/wazzac/sync-model-to-crm/blob/main/LICENSE"><img alt="GitHub license" src="https://img.shields.io/github/license/wazzac/sync-model-to-crm"></a>
55
</p>
66

7-
# sync-model-to-crm
7+
# Synchronize a Model to a Remote Crm Object
88

99
A library that will syncronise any defined database table properties (inside the Model) to an external Crm provider, like [HubSpot](https://www.hubspot.com/), [Pipedrive](https://www.pipedrive.com/en) and more.
1010

@@ -15,16 +15,22 @@ The idea around this library is to make it very easy for a developer to define i
1515
After each first time successful sync, the CRM Object primary key will be stored in a mapping table against the local table primary key. This allows for quicker loading times for future changes.
1616

1717
Update your Model with 4 properties that define the rules for 3rd-party CRM synchronization:
18-
- @var string|array|null `$syncModelCrmEnvironment`;
19-
- @var array `$syncModelCrmPropertyMapping`;
18+
- @var string|array|null `$syncModelCrmEnvironment`; *Required
19+
- @var array `$syncModelCrmPropertyMapping`; *Required
2020
- @var array `$syncModelCrmUniqueSearch`;
2121
- @var string `$syncModelCrmRelatedObject`;
22+
- @var array `$syncModelCrmDeleteRules`;
23+
- @var array `$syncModelCrmActiveRules`;
24+
- @var array `$syncModelCrmAssociateRules`;
2225

2326
Looking at the below example:
2427
1. the `User` Model will syncronize to both the `Sandbox` and `Production` **HubSpot** environments _($syncModelCrmEnvironment)_.
2528
2. It will only syncronize the `name` and `email` properties to the HubSpot corresponding `firstname` and `email` fields _($syncModelCrmPropertyMapping)_.
2629
3. When there is no internal mapping yet stored, the CRM record will be uniquely loaded using the `email` property _($syncModelCrmUniqueSearch)_.
2730
4. In order for the script to know which remote CRM object relates to the User model, `contact` _($syncModelCrmRelatedObject)_ have to be defined as the remote item.
31+
5. The _($syncModelCrmDeleteRules)_ property is used to instruct the Crm what action to take when a local record is deleted/removed. For example, when _SoftDeletes_ are enabled locally, the crm will use the `soft_delete` rules to update the crm records or alternatively Archive the record in the crm.
32+
6. The reverse to the above, _($syncModelCrmActiveRules)_ will be used to define the action that will be taken when deleted records are activated again.
33+
7. Finally, the non-required _($syncModelCrmAssociateRules)_ property is used to define the relationship (associations) between objects. e.g. `user` to `entity`.
2834

2935
```PHP
3036
class User extends Authenticatable
@@ -48,6 +54,7 @@ class User extends Authenticatable
4854

4955
/**
5056
* Mapping array for local and CRM properties
57+
* This will be the primary property used to cycle through the crm providers
5158
*
5259
* @var array
5360
*/
@@ -72,10 +79,65 @@ class User extends Authenticatable
7279
/**
7380
* The CRM object to sync this model to.
7481
* This is the CRM object type (e.g. contact, company, deal, etc.)
82+
* If this is null or not provided, the `object_table_mapping` key will be used from the config file.
7583
*
7684
* @var string
7785
*/
7886
public $syncModelCrmRelatedObject = 'contact';
87+
88+
/**
89+
* The Crm Delete rules to follow.
90+
* i.e. if Soft-delete is applicable, what should the CRM record be updated to?
91+
* if Hard-delete is used, the record will be deleted/archived in the CRM.
92+
*
93+
* @var array
94+
*/
95+
public $syncModelCrmDeleteRules = [
96+
'hard_delete' => [
97+
'hubspot' => false,
98+
],
99+
'soft_delete' => [
100+
'hubspot' => [
101+
'lifecyclestage' => 'other',
102+
'hs_lead_status' => 'DELETED',
103+
],
104+
]
105+
];
106+
107+
/**
108+
* The Crm Active/Restore rules to follow.
109+
* These will be the rules to follow for any new entries that are not soft-deleted.
110+
*/
111+
public $syncModelCrmActiveRules = [
112+
'hubspot' => [
113+
'lifecyclestage' => 'customer',
114+
'hs_lead_status' => 'OPEN',
115+
],
116+
];
117+
118+
/**
119+
* The Crm Associations to sync.
120+
* This is used to associate the model with other CRM objects.
121+
*
122+
* @var array
123+
*/
124+
public $syncModelCrmAssociateRules = [
125+
[
126+
'assocMethod' => 'entity', // App\Models\Entity::class
127+
'provider' => [
128+
'hubspot' => [
129+
[
130+
'association_category' => HubSpotController::ASSOCIATION_CATEGORY__HUBSPOT_DEFINED,
131+
'association_type_id' => HubSpotController::ASSOCIATION_TYPE_ID__CONTACT_TO_COMPANY_PRIMARY,
132+
],
133+
[
134+
'association_category' => HubSpotController::ASSOCIATION_CATEGORY__HUBSPOT_DEFINED,
135+
'association_type_id' => HubSpotController::ASSOCIATION_TYPE_ID__CONTACT_TO_COMPANY,
136+
],
137+
],
138+
],
139+
],
140+
];
79141
}
80142
```
81143

@@ -87,15 +149,68 @@ Executing `(new CrmController())->setModel($user)->execute();`:
87149
1. Directly in a controller action.
88150
2. Via a Observer. e.g. inside a UserObserver to trigger after a save() event. (see below)
89151
```PHP
90-
/**
91-
* Handle the User "saved" event.
92-
*
93-
*/
94-
public function saved(User $user): void
152+
class UserObserver implements ShouldHandleEventsAfterCommit
95153
{
96-
echo ('saved...');
97-
(new CrmController())->setModel($user)->execute();
98-
echo ('synced...');
154+
/**
155+
* Handle the User "created" event.
156+
*/
157+
public function created(User $user): void
158+
{
159+
echo ('create...');
160+
(new CrmController())->setModel($user)->execute(CrmController::EXEC_ACTION_CREATE);
161+
echo ('created...');
162+
}
163+
164+
/**
165+
* Handle the User "updated" event.
166+
*/
167+
public function updated(User $user): void
168+
{
169+
echo ('update...');
170+
(new CrmController())
171+
->setModel($user)
172+
->execute(CrmController::EXEC_ACTION_UPDATE, true);
173+
echo ('updated...');
174+
}
175+
176+
/**
177+
* Handle the User "deleted" event.
178+
* Run when a user is soft-deleted.
179+
*/
180+
public function deleted(User $user)
181+
{
182+
echo ('delete...');
183+
(new CrmController())->setModel($user)->execute(CrmController::EXEC_ACTION_DELETE);
184+
echo ('deleted...');
185+
}
186+
187+
/**
188+
* Handle the User "restored" event.
189+
* Soft-delete has been reversed.
190+
*/
191+
public function restored(User $user): void
192+
{
193+
echo ('restore...');
194+
(new CrmController())->setModel($user)->execute(CrmController::EXEC_ACTION_RESTORE);
195+
echo ('restored...');
196+
}
197+
198+
/**
199+
* Handle the User "force deleted" event.
200+
*/
201+
public function forceDeleted(User $user): void
202+
{
203+
echo ('forceDeleted...');
204+
}
205+
206+
/**
207+
* Handle the User "saved" event.
208+
*
209+
*/
210+
public function saved(User $user): void
211+
{
212+
echo ('saving...');
213+
}
99214
}
100215
```
101216
3. Inside an event job. This is a good method to separate the logic from the save event and put the sync in a job queue to be processed shortly after the record has been saved.

0 commit comments

Comments
 (0)