From 41b8d703188a246abbd9cdb0e7348f0971f6e351 Mon Sep 17 00:00:00 2001 From: Alf Drollinger Date: Thu, 5 Sep 2024 00:53:53 +0200 Subject: [PATCH] wip --- .../sync/src/Handlers/PressSyncHandler.php | 2 +- packages/sync/src/Jobs/PrepareSyncJob.php | 31 +++++++------------ packages/sync/src/Listener/SyncListener.php | 12 +++++-- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/packages/sync/src/Handlers/PressSyncHandler.php b/packages/sync/src/Handlers/PressSyncHandler.php index 570e5bd68..352014df2 100644 --- a/packages/sync/src/Handlers/PressSyncHandler.php +++ b/packages/sync/src/Handlers/PressSyncHandler.php @@ -214,7 +214,7 @@ protected function syncMetaData(Model $mainRecord) } else { // Special handling for capabilities if ($key === 'jku8u_capabilities' && ! is_string($value)) { - $value = maybe_serialize($value); + $value = serialize($value); } $metaModel::updateOrCreate( diff --git a/packages/sync/src/Jobs/PrepareSyncJob.php b/packages/sync/src/Jobs/PrepareSyncJob.php index b8ef1f6b5..9a05c65c3 100644 --- a/packages/sync/src/Jobs/PrepareSyncJob.php +++ b/packages/sync/src/Jobs/PrepareSyncJob.php @@ -14,7 +14,9 @@ class PrepareSyncJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, LogLevel, Queueable, SerializesModels; - protected $localIdentifier; + protected $identifierField; + + protected $identifierValue; protected $modelClass; @@ -22,9 +24,10 @@ class PrepareSyncJob implements ShouldQueue protected $platformId; - public function __construct($localIdentifier, $modelClass, $eventType, $platformId) + public function __construct($identifierField, $identifierValue, $modelClass, $eventType, $platformId) { - $this->localIdentifier = $localIdentifier; + $this->identifierField = $identifierField; + $this->identifierValue = $identifierValue; $this->modelClass = $modelClass; $this->eventType = $eventType; $this->platformId = $platformId; @@ -33,7 +36,8 @@ public function __construct($localIdentifier, $modelClass, $eventType, $platform public function handle() { $this->logDebug('PrepareSyncJob started', [ - 'local_identifier' => $this->localIdentifier, + 'identifier_field' => $this->identifierField, + 'identifier_value' => $this->identifierValue, 'model_class' => $this->modelClass, 'event_type' => $this->eventType, 'platform_id' => $this->platformId, @@ -52,36 +56,25 @@ public function handle() protected function findModel() { - $localIdentifierFields = config('sync.local_identifier_fields', ['id']); - $query = $this->modelClass::query(); - - foreach ($localIdentifierFields as $field) { - $query->orWhere($field, $this->localIdentifier); - } - - return $query->firstOrFail(); + return $this->modelClass::where($this->identifierField, $this->identifierValue)->firstOrFail(); } protected function handleModelNotFound() { $this->logDebug('Model not found, possibly deleted', [ - 'local_identifier' => $this->localIdentifier, + 'identifier_field' => $this->identifierField, + 'identifier_value' => $this->identifierValue, 'model_class' => $this->modelClass, ]); if ($this->eventType === 'deleted') { // If it's a delete event, we can proceed with sync $model = new $this->modelClass; - $model->{$this->getFirstLocalIdentifierField()} = $this->localIdentifier; + $model->{$this->identifierField} = $this->identifierValue; // Proceed with sync logic for deleted model } else { // For other events, we can't proceed without the model $this->logDebug('Cannot proceed with sync for non-existent model'); } } - - protected function getFirstLocalIdentifierField() - { - return config('sync.local_identifier_fields.0', 'id'); - } } diff --git a/packages/sync/src/Listener/SyncListener.php b/packages/sync/src/Listener/SyncListener.php index 67e4d4e8e..d3e77fc4a 100644 --- a/packages/sync/src/Listener/SyncListener.php +++ b/packages/sync/src/Listener/SyncListener.php @@ -93,6 +93,12 @@ protected function handleModelEvent($model, $eventType) $localIdentifier = $this->getLocalIdentifier($model); + if (! $localIdentifier) { + $this->logDebug('Moox Sync: Model event ignored - no local identifier found', ['model' => get_class($model), 'event' => $eventType]); + + return; + } + $this->logDebug('Dispatching PrepareSyncJob', [ 'model' => get_class($model), 'local_identifier' => $localIdentifier, @@ -100,17 +106,17 @@ protected function handleModelEvent($model, $eventType) 'platform' => $this->currentPlatform->id, ]); - PrepareSyncJob::dispatch($localIdentifier, get_class($model), $eventType, $this->currentPlatform->id) + PrepareSyncJob::dispatch($localIdentifier['field'], $localIdentifier['value'], get_class($model), $eventType, $this->currentPlatform->id) ->delay(now()->addSeconds(5)); } protected function getLocalIdentifier($model) { - $localIdentifierFields = config('sync.local_identifier_fields', ['id']); + $localIdentifierFields = config('sync.local_identifier_fields', ['id', 'ID', 'uuid', 'ulid']); foreach ($localIdentifierFields as $field) { if (isset($model->$field)) { - return $model->$field; + return ['field' => $field, 'value' => $model->$field]; } }