Skip to content

Commit

Permalink
Flexible id support
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Sep 4, 2024
1 parent 914e1fc commit 59326f4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 42 deletions.
18 changes: 18 additions & 0 deletions config/sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,22 @@
'name',
'title',
],

/*
|--------------------------------------------------------------------------
| Local Identifier Fields
|--------------------------------------------------------------------------
|
| 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.
|
*/

'local_identifier_fields' => [
'id',
'ID',
'uuid',
'ulid',
],
];
18 changes: 18 additions & 0 deletions packages/sync/config/sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,22 @@
'name',
'title',
],

/*
|--------------------------------------------------------------------------
| Local Identifier Fields
|--------------------------------------------------------------------------
|
| 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.
|
*/

'local_identifier_fields' => [
'id',
'ID',
'uuid',
'ulid',
],
];
76 changes: 34 additions & 42 deletions packages/sync/src/Jobs/PrepareSyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Moox\Core\Traits\LogLevel;
use Moox\Sync\Models\Platform;

class PrepareSyncJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, LogLevel, Queueable, SerializesModels;

protected $modelId;
protected $localIdentifier;

protected $modelClass;

protected $eventType;

protected $platformId;

public function __construct($modelId, $modelClass, $eventType, $platformId)
public function __construct($localIdentifier, $modelClass, $eventType, $platformId)
{
$this->modelId = $modelId;
$this->localIdentifier = $localIdentifier;
$this->modelClass = $modelClass;
$this->eventType = $eventType;
$this->platformId = $platformId;
Expand All @@ -34,62 +33,55 @@ public function __construct($modelId, $modelClass, $eventType, $platformId)
public function handle()
{
$this->logDebug('PrepareSyncJob started', [
'model_id' => $this->modelId,
'local_identifier' => $this->localIdentifier,
'model_class' => $this->modelClass,
'event_type' => $this->eventType,
'platform_id' => $this->platformId,
]);

try {
$model = $this->modelClass::findOrFail($this->modelId);
$model = $this->findModel();
} catch (ModelNotFoundException $e) {
$this->logDebug('Model not found, possibly deleted', [
'model_id' => $this->modelId,
'model_class' => $this->modelClass,
]);

if ($this->eventType === 'deleted') {
// If it's a delete event, we can proceed with sync
$model = new $this->modelClass;
$model->ID = $this->modelId;
} else {
// For other events, we can't proceed without the model
return;
}
}

try {
$platform = Platform::findOrFail($this->platformId);
} catch (ModelNotFoundException $e) {
$this->logDebug('Platform not found', ['platform_id' => $this->platformId]);
$this->handleModelNotFound();

return;
}

$modelData = $model->toArray();
// ... rest of the handle method ...
}

protected function findModel()
{
$localIdentifierFields = config('sync.local_identifier_fields', ['id']);
$query = $this->modelClass::query();

if ($model instanceof \Moox\Press\Models\WpUser && $this->eventType !== 'deleted') {
$userMeta = $model->getAllMetaAttributes();
$this->logDebug('User meta data retrieved in deferred job', ['user_meta' => $userMeta]);
$modelData = array_merge($modelData, $userMeta);
foreach ($localIdentifierFields as $field) {
$query->orWhere($field, $this->localIdentifier);
}

$syncData = [
'event_type' => $this->eventType,
'model' => $modelData,
'model_class' => $this->modelClass,
'platform' => $platform->toArray(),
];
return $query->firstOrFail();
}

$this->logDebug('Sync data prepared in deferred job', ['sync_data' => $syncData]);
protected function handleModelNotFound()
{
$this->logDebug('Model not found, possibly deleted', [
'local_identifier' => $this->localIdentifier,
'model_class' => $this->modelClass,
]);

$this->invokeWebhooks($syncData);
if ($this->eventType === 'deleted') {
// If it's a delete event, we can proceed with sync
$model = new $this->modelClass;
$model->{$this->getFirstLocalIdentifierField()} = $this->localIdentifier;
// 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 invokeWebhooks(array $data)
protected function getFirstLocalIdentifierField()
{
// Implement the webhook invocation logic here
// This might involve sending HTTP requests to other platforms
// Make sure to log the process and handle any errors
return config('sync.local_identifier_fields.0', 'id');
}
}
1 change: 1 addition & 0 deletions packages/sync/src/Listener/SyncListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected function registerModelListeners($modelClass)
$this->logDebug('Moox Sync: Registering listeners for model', ['model' => $modelClass]);

Event::listen("eloquent.created: {$modelClass}", function ($model) use ($modelClass) {
// Logs model id = null from here on
$this->logDebug('Moox Sync: Created event triggered', ['model' => $modelClass, 'id' => $model->id]);
$this->handleModelEvent($model, 'created');
});
Expand Down

0 comments on commit 59326f4

Please sign in to comment.