BackfillPaidOrderStatusesJob.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Jobs;
  3. use App\Services\OrderPaymentStatusService;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Queue\Queueable;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Log;
  8. class BackfillPaidOrderStatusesJob implements ShouldQueue
  9. {
  10. use Queueable;
  11. public int $timeout = 1200;
  12. public int $tries = 1;
  13. public function __construct(
  14. private readonly ?int $year = null,
  15. private readonly int $chunkSize = 500,
  16. ) {
  17. }
  18. public function handle(OrderPaymentStatusService $paymentStatusService): void
  19. {
  20. $checked = 0;
  21. $markedPaid = 0;
  22. $paymentStatusService
  23. ->paidBackfillCandidatesQuery($this->year)
  24. ->select('orders.id')
  25. ->chunkById($this->chunkSize, function (Collection $orders) use ($paymentStatusService, &$checked, &$markedPaid): void {
  26. foreach ($orders as $order) {
  27. $checked++;
  28. if ($paymentStatusService->markPaidIfAllMafsHavePaymentData((int) $order->id)) {
  29. $markedPaid++;
  30. }
  31. }
  32. }, 'orders.id', 'id');
  33. Log::info('Paid order statuses backfill finished.', [
  34. 'year' => $this->year,
  35. 'checked' => $checked,
  36. 'marked_paid' => $markedPaid,
  37. ]);
  38. }
  39. }