Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| SyncNuvemshopOrdersJob | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Jobs; |
| 4 | |
| 5 | use App\Enums\SyncStatus; |
| 6 | use App\Models\StoreIntegration; |
| 7 | use App\Models\SyncLog; |
| 8 | use App\Services\Nuvemshop\NuvemshopOrderService; |
| 9 | use Carbon\Carbon; |
| 10 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 11 | use Illuminate\Foundation\Queue\Queueable; |
| 12 | use Throwable; |
| 13 | |
| 14 | class SyncNuvemshopOrdersJob implements ShouldQueue |
| 15 | { |
| 16 | use Queueable; |
| 17 | |
| 18 | public int $tries = 3; |
| 19 | |
| 20 | public function __construct( |
| 21 | public int $integrationId, |
| 22 | public ?string $from = null, |
| 23 | public ?string $to = null, |
| 24 | ) { |
| 25 | } |
| 26 | |
| 27 | public function handle(NuvemshopOrderService $service): void |
| 28 | { |
| 29 | $integration = StoreIntegration::findOrFail($this->integrationId); |
| 30 | $log = SyncLog::create([ |
| 31 | 'store_integration_id' => $integration->id, |
| 32 | 'type' => 'orders_hourly', |
| 33 | 'status' => SyncStatus::Running->value, |
| 34 | 'started_at' => now(), |
| 35 | ]); |
| 36 | |
| 37 | try { |
| 38 | $result = $service->syncOrders( |
| 39 | $integration, |
| 40 | $this->from ? Carbon::parse($this->from) : now()->subHour(), |
| 41 | $this->to ? Carbon::parse($this->to) : now(), |
| 42 | ); |
| 43 | |
| 44 | $integration->update(['last_synced_at' => now()]); |
| 45 | $log->update([ |
| 46 | 'status' => SyncStatus::Success->value, |
| 47 | 'finished_at' => now(), |
| 48 | 'records_processed' => $result['processed'] ?? 0, |
| 49 | 'records_created' => $result['created'] ?? 0, |
| 50 | 'records_updated' => $result['updated'] ?? 0, |
| 51 | 'metadata' => $result, |
| 52 | ]); |
| 53 | } catch (Throwable $exception) { |
| 54 | $log->update([ |
| 55 | 'status' => SyncStatus::Failed->value, |
| 56 | 'finished_at' => now(), |
| 57 | 'error_message' => $exception->getMessage(), |
| 58 | ]); |
| 59 | throw $exception; |
| 60 | } |
| 61 | } |
| 62 | } |