CreateAsaasSubaccountJob.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Unit;
  4. use App\Services\Integrations\Asaas\AsaasAccountService;
  5. use Exception;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Illuminate\Support\Facades\Log;
  12. class CreateAsaasSubaccountJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. public int $unitId;
  16. public int $tries = 3;
  17. public function __construct(int $unitId)
  18. {
  19. $this->unitId = $unitId;
  20. }
  21. public function handle(AsaasAccountService $accountService): void
  22. {
  23. $unit = Unit::find($this->unitId);
  24. if (!$unit) {
  25. Log::warning("CreateAsaasSubaccountJob: Unidade {$this->unitId} não encontrada.");
  26. return;
  27. }
  28. try {
  29. $accountService->ensureSubaccount($unit);
  30. Log::info("CreateAsaasSubaccountJob: Subconta criada com sucesso para a Unidade {$unit->id}.");
  31. } catch (Exception $e) {
  32. Log::error("CreateAsaasSubaccountJob: Falha ao criar subconta para Unidade {$unit->id}.", [
  33. 'error' => $e->getMessage()
  34. ]);
  35. throw $e; // Throw to retry if needed
  36. }
  37. }
  38. }