Skip to content

Commit

Permalink
Update PressSyncHandler.php
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Sep 4, 2024
1 parent 2e0fc24 commit 6b69556
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/sync/src/Handlers/PressSyncHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ protected function syncMainRecord()
$mainTableData = $this->getMainTableData();
$idField = $this->getIdField();

// Sanitize date fields
$mainTableData = $this->sanitizeDateFields($mainTableData);
// Ensure user_registered is set to current time if it's missing or invalid
if ($this->modelClass === \Moox\Press\Models\WpUser::class) {
$mainTableData['user_registered'] = $this->sanitizeDate($mainTableData['user_registered'] ?? null);
}

$this->logDebug('Syncing main record', [
'model_class' => $this->modelClass,
Expand All @@ -59,16 +61,27 @@ protected function syncMainRecord()
'main_table_data' => $mainTableData,
]);

// Handle WpUser specific logic
if ($this->modelClass === \Moox\Press\Models\WpUser::class) {
return $this->syncWpUser($mainTableData, $idField);
}
try {
$model = $this->modelClass::updateOrCreate(
[$idField => $this->modelData[$idField]],
$mainTableData
);

// For other models, use the default logic
return $this->modelClass::updateOrCreate(
[$idField => $this->modelData[$idField]],
$mainTableData
);
$this->logDebug('Main record synced successfully', [
'model_class' => $this->modelClass,
'id' => $model->getKey(),
'attributes' => $model->getAttributes(),
]);

return $model;
} catch (\Exception $e) {
$this->logDebug('Error syncing main record', [
'model_class' => $this->modelClass,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
throw $e;
}
}

protected function syncWpUser(array $mainTableData, string $idField)
Expand Down

0 comments on commit 6b69556

Please sign in to comment.