Skip to content

Commit

Permalink
Sync Platform Relation wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Sep 9, 2024
1 parent d7511b9 commit 6d9d302
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 43 deletions.
6 changes: 5 additions & 1 deletion packages/sync/src/Http/Controllers/SyncWebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ public function handle(Request $request)
}

$sourcePlatform = Platform::where('domain', $validatedData['platform']['domain'])->firstOrFail();
$targetPlatform = Platform::where('domain', $request->getHost())->firstOrFail();

SyncJob::dispatch(
$validatedData['model_class'],
$validatedData['model'],
$validatedData['event_type'],
$sourcePlatform
$sourcePlatform,
$targetPlatform,
$validatedData['should_delete']
);

return response()->json(['status' => 'success', 'message' => 'Sync job dispatched']);
Expand All @@ -55,6 +58,7 @@ protected function validateRequest(Request $request)
'model_class' => 'required|string',
'platform' => 'required|array',
'platform.domain' => 'required|string',
'should_delete' => 'required|boolean',
]);
}

Expand Down
71 changes: 51 additions & 20 deletions packages/sync/src/Jobs/PrepareSyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Illuminate\Support\Facades\Log;
use Moox\Core\Traits\LogLevel;
use Moox\Sync\Models\Platform;
use Moox\Sync\Models\Sync;
use Moox\Sync\Services\PlatformRelationService;

class PrepareSyncJob implements ShouldQueue
{
Expand All @@ -28,6 +30,10 @@ class PrepareSyncJob implements ShouldQueue

protected $syncConfigurations;

protected $sourcePlatform;

protected $modelData;

public function __construct($identifierField, $identifierValue, $modelClass, $eventType, $platformId, $syncConfigurations)
{
$this->identifierField = $identifierField;
Expand All @@ -36,33 +42,45 @@ public function __construct($identifierField, $identifierValue, $modelClass, $ev
$this->eventType = $eventType;
$this->platformId = $platformId;
$this->syncConfigurations = $syncConfigurations;
$this->sourcePlatform = Platform::findOrFail($platformId);
$this->modelData = $this->findModel()->toArray();
}

public function handle()
public function handle(PlatformRelationService $platformRelationService)
{
$model = $this->findModel();
$sourcePlatform = Platform::findOrFail($this->platformId);
$sync = Sync::where('source_model', $this->modelClass)
->where('source_platform_id', $this->sourcePlatform->id)
->first();

$this->logDebug('Moox Sync: PrepareSyncJob handling', [
'identifier_field' => $this->identifierField,
'identifier_value' => $this->identifierValue,
'model_class' => $this->modelClass,
'event_type' => $this->eventType,
'platform_id' => $this->platformId,
]);
if (! $sync || ! $sync->use_platform_relations) {
// Existing logic for syncing to all platforms
return;
}

$syncData = [
'event_type' => $this->eventType,
'model' => $model ? $this->getFullModelData($model) : [$this->identifierField => $this->identifierValue],
'model_class' => $this->modelClass,
'platform' => $sourcePlatform->toArray(),
];
$modelId = $this->getModelId($this->modelData);
$relatedPlatforms = $platformRelationService->getPlatformsForModel($this->modelClass, $modelId);

$this->logDebug('Moox Sync: Prepared sync data', [
'sync_data' => $syncData,
]);
$allPlatforms = Platform::where('id', '!=', $this->sourcePlatform->id)->get();

$this->invokeWebhooks($syncData);
foreach ($allPlatforms as $platform) {
if ($relatedPlatforms->contains($platform->id)) {
$this->syncToPlatform($platform, false);
} else {
$this->syncToPlatform($platform, true);
}
}
}

protected function syncToPlatform(Platform $platform, bool $shouldDelete)
{
dispatch(new SyncJob(
$this->modelClass,
$this->modelData,
$this->eventType,
$this->sourcePlatform,
$platform,
$shouldDelete
));
}

protected function findModel()
Expand Down Expand Up @@ -154,4 +172,17 @@ protected function invokeWebhooks(array $data)
}
}
}

protected function getModelId($modelData)
{
$identifierFields = config('sync.local_identifier_fields', ['ID', 'uuid', 'ulid', 'id']);

foreach ($identifierFields as $field) {
if (isset($modelData[$field])) {
return $modelData[$field];
}
}

return $modelData[$this->identifierField] ?? null;
}
}
42 changes: 30 additions & 12 deletions packages/sync/src/Jobs/SyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,58 @@ class SyncJob implements ShouldQueue

protected $sourcePlatform;

public function __construct($modelClass, $modelData, $eventType, Platform $sourcePlatform)
protected $targetPlatform;

protected $shouldDelete;

public function __construct($modelClass, $modelData, $eventType, Platform $sourcePlatform, Platform $targetPlatform, bool $shouldDelete)
{
$this->modelClass = $modelClass;
$this->modelData = $modelData;
$this->eventType = $eventType;
$this->sourcePlatform = $sourcePlatform;
$this->targetPlatform = $targetPlatform;
$this->shouldDelete = $shouldDelete;
}

public function handle()
{
try {
$modelId = $this->getModelId();
$this->logInfo('Moox Sync: Syncing model', [
'model_class' => $this->modelClass,
'model_id_field' => $modelId['field'],
'model_id_value' => $modelId['value'],
'event_type' => $this->eventType,
]);

if ($this->modelClass === Platform::class) {
$this->syncPlatform();
if ($this->shouldDelete) {
$this->deleteModel();
} else {
$this->syncModel();
// Existing sync logic
if ($this->modelClass === Platform::class) {
$this->syncPlatform();
} else {
$this->syncModel();
}
}
} catch (\Exception $e) {
Log::error('Moox Sync: Error syncing model', [
'model_class' => $this->modelClass,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
'source_platform' => $this->sourcePlatform->id,
'target_platform' => $this->targetPlatform->id,
'should_delete' => $this->shouldDelete,
]);
throw $e;
}
}

protected function deleteModel()
{
$modelId = $this->getModelId();
$this->logInfo('Moox Sync: Deleting model', [
'model_class' => $this->modelClass,
'model_id_field' => $modelId['field'],
'model_id_value' => $modelId['value'],
]);

DB::table((new $this->modelClass)->getTable())->where($modelId['field'], $modelId['value'])->delete();
}

protected function syncPlatform()
{
$platform = Platform::updateOrCreate(
Expand Down
11 changes: 3 additions & 8 deletions packages/sync/src/Services/PlatformRelationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,12 @@ public function syncPlatformsForModel($model, array $platformIds): void
DB::table('model_platform')->insert($insertData);
}

public function getPlatformsForModel($model)
public function getPlatformsForModel($modelClass, $modelId)
{
$modelType = get_class($model);
$modelId = $model->getKey();

$platformIds = DB::table('model_platform')
->where('model_type', $modelType)
return DB::table('model_platform')
->where('model_type', $modelClass)
->where('model_id', $modelId)
->pluck('platform_id');

return Platform::whereIn('id', $platformIds)->get();
}

public function addPlatformToModel($model, Platform $platform)
Expand Down
5 changes: 3 additions & 2 deletions packages/sync/src/Services/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,12 @@ protected function createOrUpdateModel($modelClass, array $modelData, Platform $

protected function deleteModel($modelClass, array $modelData, Platform $targetPlatform)
{
$model = $modelClass::find($modelData['id']);
$modelId = $this->getModelId($modelData);
$model = $modelClass::where($this->getModelId($modelData), $modelId)->first();

if ($model) {
$model->delete();
$this->logDebug('Model deleted', [
$this->logDebug('Model deleted on target platform', [
'modelClass' => $modelClass,
'modelId' => $modelData['id'],
'targetPlatform' => $targetPlatform->id,
Expand Down

0 comments on commit 6d9d302

Please sign in to comment.