Skip to content

Commit

Permalink
Sync test -> org
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Sep 2, 2024
1 parent 6412e01 commit 6a5566f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function handle(Request $request)

$sync = Sync::findOrFail($validatedData['sync']['id']);

$this->logDebug('Webhook recieved for sync', ['sync' => $sync->id]);
$this->logdebug('Moox Sync: Webhook recieved for sync', ['sync' => $sync->id]);

SyncJob::dispatch($sync, $validatedData['model'], $validatedData['event_type']);

Expand Down
16 changes: 8 additions & 8 deletions packages/sync/src/Listener/SyncListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()

if ($platform) {
$this->currentPlatformId = $platform->id;
$this->logDebug('Platform found for domain: '.$domain);
$this->logdebug('Moox Sync: Platform found for domain: '.$domain);
} else {
$this->logDebug("Platform not found for domain: {$domain}");
$this->currentPlatformId = null;
Expand Down Expand Up @@ -57,20 +57,20 @@ protected function registerModelListeners(Sync $sync)
{
$modelClass = $sync->source_model;

$this->logDebug('Listen to Events for '.$modelClass);
$this->logdebug('Moox Sync: Listen to Events for '.$modelClass);

Event::listen("eloquent.created: {$modelClass}", function ($model) use ($sync) {
$this->logDebug('Event created for '.$model->id);
$this->logdebug('Moox Sync: Event created for '.$model->id);
$this->handleEvent($model, 'created', $sync);
});

Event::listen("eloquent.updated: {$modelClass}", function ($model) use ($sync) {
$this->logDebug('Event updated for '.$model->title);
$this->logdebug('Moox Sync: Event updated for '.$model->title);
$this->handleEvent($model, 'updated', $sync);
});

Event::listen("eloquent.deleted: {$modelClass}", function ($model) use ($sync) {
$this->logDebug('Event deleted for '.$model->id);
$this->logdebug('Moox Sync: Event deleted for '.$model->id);
$this->handleEvent($model, 'deleted', $sync);
});
}
Expand All @@ -84,7 +84,7 @@ protected function handleEvent($model, $eventType, Sync $sync)
'sync' => $sync->toArray(),
];

$this->logDebug('Invoke Webhook for '.$this->currentPlatformId);
$this->logdebug('Moox Sync: Invoke Webhook for '.$this->currentPlatformId);

$this->invokeWebhook($sync, $syncData);
}
Expand All @@ -93,13 +93,13 @@ protected function invokeWebhook(Sync $sync, array $data)
{
$webhookUrl = 'https://'.$sync->targetPlatform->domain.'/sync-webhook';

$this->logDebug('Push to Webhook:', ['url' => $webhookUrl, 'data' => $data]);
$this->logdebug('Moox Sync: Push to Webhook:', ['url' => $webhookUrl, 'data' => $data]);

try {
$response = Http::asJson()->post($webhookUrl, $data);

if ($response->successful()) {
$this->logDebug('Webhook invoked successfully.', ['url' => $webhookUrl, 'response' => $response->body()]);
$this->logdebug('Moox Sync: Webhook invoked successfully.', ['url' => $webhookUrl, 'response' => $response->body()]);
} elseif ($response->clientError()) {
Log::warning('Client error occurred when invoking webhook.', [
'url' => $webhookUrl,
Expand Down
6 changes: 4 additions & 2 deletions packages/sync/src/Resources/PlatformResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ public static function form(Form $form): Form
'lg' => 12,
])
->reactive()
->afterStateUpdated(function ($state, callable $set) {
if (! $state) {
->afterStateUpdated(function ($state, callable $set, $livewire) {
if (!$state) {
$set('order', null);
$livewire->record->order = null;
$livewire->record->save();
}
}),

Expand Down
29 changes: 27 additions & 2 deletions packages/sync/src/SyncServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use Moox\Sync\Commands\InstallCommand;
use Moox\Sync\Http\Middleware\PlatformTokenAuthMiddleware;
use Moox\Sync\Listener\SyncListener;
use Moox\Sync\Jobs\SyncBackupJob;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Illuminate\Support\Facades\Config;
use Illuminate\Console\Scheduling\Schedule;

class SyncServiceProvider extends PackageServiceProvider
{
Expand All @@ -33,7 +36,29 @@ public function boot()
PlatformTokenAuthMiddleware::class
);

$syncListener = new SyncListener;
$syncListener->registerListeners();
$this->registerSyncBackupJob();
$this->registerSyncEloquentListener();
}

protected function registerSyncBackupJob()
{
$syncBackupJobConfig = Config::get('sync.sync_backup_job');

if ($syncBackupJobConfig['enabled']) {
$this->app->booted(function () use ($syncBackupJobConfig) {
$schedule = $this->app->make(Schedule::class);
$schedule->job(new SyncBackupJob())->{$syncBackupJobConfig['frequency']}();
});
}
}

protected function registerSyncEloquentListener()
{
$syncEloquentListenerConfig = Config::get('sync.sync_eloquent_listener');

if ($syncEloquentListenerConfig['enabled']) {
$syncListener = new SyncListener;
$syncListener->registerListeners();
}
}
}

0 comments on commit 6a5566f

Please sign in to comment.