Skip to content

Commit

Permalink
Sync last mile
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Sep 5, 2024
1 parent 3013dc4 commit 6fd0a0d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions config/sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,14 @@
|
| These are the fields that are used as unique identifiers for
| the models. They are used to identify the models on the
| source platform. They may auto-increment.
| source platform. The array is sorted by priority.
|
*/

'local_identifier_fields' => [
'id',
'ID',
'uuid',
'ulid',
'id',
],
];
4 changes: 2 additions & 2 deletions packages/sync/config/sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,14 @@
|
| These are the fields that are used as unique identifiers for
| the models. They are used to identify the models on the
| source platform. They may auto-increment.
| source platform. The array is sorted by priority.
|
*/

'local_identifier_fields' => [
'id',
'ID',
'uuid',
'ulid',
'id',
],
];
8 changes: 7 additions & 1 deletion packages/sync/src/Handlers/PressSyncHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ protected function getMetaModel(Model $mainRecord): string

protected function getIdField(): string
{
return 'ID';
$idFields = config('sync.local_identifier_fields', ['ID', 'uuid', 'ulid', 'id']);
foreach ($idFields as $field) {
if (isset($this->modelData[$field])) {
return $field;
}
}
throw new \Exception('No suitable ID field found for model');
}

protected function getForeignKeyName(Model $mainRecord): string
Expand Down
14 changes: 13 additions & 1 deletion packages/sync/src/Jobs/PrepareSyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,22 @@ protected function invokeWebhooks(array $data)
foreach ($targetPlatforms as $targetPlatform) {
$webhookUrl = 'https://'.$targetPlatform->domain.'/sync-webhook';

$this->logInfo('Moox Sync: Invoking webhook', [
'platform' => $targetPlatform->name,
'webhook_url' => $webhookUrl,
'data' => $data,
]);

try {
Http::withToken($targetPlatform->api_token)->post($webhookUrl, $data);

$this->logDebug('Moox Sync: Webhook invoked successfully', [
'platform' => $targetPlatform->name,
'webhook_url' => $webhookUrl,
'data' => $data,
]);
} catch (\Exception $e) {
$this->logDebug('Webhook invocation error', [
$this->logDebug('Moox Sync: Webhook invocation error', [
'platform' => $targetPlatform->name,
'error' => $e->getMessage(),
]);
Expand Down
3 changes: 2 additions & 1 deletion packages/sync/src/Jobs/SyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct($modelClass, $modelData, $eventType, Platform $sourc

protected function getModelId()
{
$idFields = ['ulid', 'uuid', 'id', 'ID', 'slug', 'user_login'];
$idFields = config('sync.local_identifier_fields', ['ID', 'uuid', 'ulid', 'id']);
foreach ($idFields as $field) {
if (isset($this->modelData[$field])) {
return [
Expand Down Expand Up @@ -65,6 +65,7 @@ public function handle()
$this->logDebug('Error syncing model', [
'model_class' => $this->modelClass,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
throw $e;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sync/src/SyncServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function registerSyncEloquentListener(): void

if ($syncEloquentListenerConfig['enabled']) {

$this->logDebug('Moox Sync: Registering sync eloquent listener');
$this->logInfo('Moox Sync: Registering sync eloquent listener');

$syncListener = $this->app->make(SyncListener::class);
$syncListener->registerListeners();
Expand Down

0 comments on commit 6fd0a0d

Please sign in to comment.