Skip to content

Commit

Permalink
Update PrepareSyncJob.php
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Sep 9, 2024
1 parent 9b12be2 commit b076db3
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions packages/sync/src/Jobs/PrepareSyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,48 @@ public function handle(PlatformRelationService $platformRelationService)

protected function sendToWebhook(Platform $platform, bool $shouldDelete)
{
// Implement the logic to send data to the target platform's webhook
// This should include the model data, shouldDelete flag, and any other necessary information
$webhookPath = config('sync.sync_webhook_url', '/sync-webhook');
$syncToken = config('sync.sync_token');
$webhookUrl = 'https://'.$platform->domain.$webhookPath;

$data = [
'event_type' => $this->eventType,
'model' => $this->modelData,
'model_class' => $this->modelClass,
'platform' => [
'domain' => $this->sourcePlatform->domain,
],
'should_delete' => $shouldDelete,
];

$payload = json_encode($data);
$signature = hash_hmac('sha256', $payload, $platform->api_token.$syncToken);

$this->logDebug('Moox Sync: Preparing to invoke webhook', [
'platform' => $platform->name,
'webhook_url' => $webhookUrl,
'full_data' => $data,
]);

try {
$response = Http::withHeaders([
'X-Platform-Token' => $platform->api_token ?? $syncToken,
'X-Webhook-Signature' => $signature,
])->post($webhookUrl, $data);

$this->logDebug('Moox Sync: Webhook invoked', [
'platform' => $platform->name,
'webhook_url' => $webhookUrl,
'response_status' => $response->status(),
'response_body' => $response->body(),
]);
} catch (\Exception $e) {
$this->logDebug('Moox Sync: Webhook invocation error', [
'platform' => $platform->name,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
}
}

protected function syncToPlatform(Platform $platform, bool $shouldDelete)
Expand Down

0 comments on commit b076db3

Please sign in to comment.