Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Sep 4, 2024
1 parent 872bd56 commit 41b8d70
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/sync/src/Handlers/PressSyncHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
31 changes: 12 additions & 19 deletions packages/sync/src/Jobs/PrepareSyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ class PrepareSyncJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, LogLevel, Queueable, SerializesModels;

protected $localIdentifier;
protected $identifierField;

protected $identifierValue;

protected $modelClass;

protected $eventType;

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;
Expand All @@ -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,
Expand All @@ -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');
}
}
12 changes: 9 additions & 3 deletions packages/sync/src/Listener/SyncListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,30 @@ 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,
'event' => $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];
}
}

Expand Down

0 comments on commit 41b8d70

Please sign in to comment.