Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| DispatchDealAutomationsJob | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
2 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Sales\Infrastructure\Jobs; |
| 6 | |
| 7 | use Illuminate\Bus\Queueable; |
| 8 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 9 | use Illuminate\Foundation\Bus\Dispatchable; |
| 10 | use Illuminate\Queue\InteractsWithQueue; |
| 11 | use Illuminate\Queue\SerializesModels; |
| 12 | use ImovelZapAi\Sales\Application\Actions\RunDealAutomationsAction; |
| 13 | use ImovelZapAi\Tenancy\Infrastructure\Persistence\Eloquent\TenantModel; |
| 14 | use ImovelZapAi\Tenancy\Support\TenantContext; |
| 15 | |
| 16 | final class DispatchDealAutomationsJob implements ShouldQueue |
| 17 | { |
| 18 | use Dispatchable; |
| 19 | use InteractsWithQueue; |
| 20 | use Queueable; |
| 21 | use SerializesModels; |
| 22 | |
| 23 | public int $tries = 1; |
| 24 | |
| 25 | public function __construct() |
| 26 | { |
| 27 | $this->onQueue((string) config('sales.queue', 'sales')); |
| 28 | } |
| 29 | |
| 30 | public function handle(RunDealAutomationsAction $action): void |
| 31 | { |
| 32 | TenantModel::query() |
| 33 | ->orderBy('id') |
| 34 | ->limit(200) |
| 35 | ->get() |
| 36 | ->each(function (TenantModel $tenant) use ($action): void { |
| 37 | TenantContext::clear(); |
| 38 | TenantContext::set((string) $tenant->id); |
| 39 | try { |
| 40 | $action->execute((string) $tenant->id); |
| 41 | } finally { |
| 42 | TenantContext::clear(); |
| 43 | } |
| 44 | }); |
| 45 | } |
| 46 | } |