Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
33 / 33 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| DefaultPipelineSeeder | |
100.00% |
33 / 33 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| ensureForTenant | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
3 | |||
| stageByKey | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Sales\Application\Services; |
| 6 | |
| 7 | use ImovelZapAi\Sales\Domain\Enums\DealStageKey; |
| 8 | use ImovelZapAi\Sales\Infrastructure\Persistence\Eloquent\SalesDealStageModel; |
| 9 | use ImovelZapAi\Sales\Infrastructure\Persistence\Eloquent\SalesPipelineModel; |
| 10 | |
| 11 | final class DefaultPipelineSeeder |
| 12 | { |
| 13 | public function ensureForTenant(string $tenantId): SalesPipelineModel |
| 14 | { |
| 15 | $pipeline = SalesPipelineModel::query() |
| 16 | ->where('tenant_id', $tenantId) |
| 17 | ->where('is_default', true) |
| 18 | ->first(); |
| 19 | |
| 20 | if ($pipeline !== null) { |
| 21 | return $pipeline->load('stages'); |
| 22 | } |
| 23 | |
| 24 | $pipeline = SalesPipelineModel::query()->create([ |
| 25 | 'tenant_id' => $tenantId, |
| 26 | 'name' => 'Pipeline padrĂ£o', |
| 27 | 'is_default' => true, |
| 28 | ]); |
| 29 | |
| 30 | foreach (DealStageKey::defaultPipelineDefinition() as $stage) { |
| 31 | SalesDealStageModel::query()->create([ |
| 32 | 'tenant_id' => $tenantId, |
| 33 | 'pipeline_id' => $pipeline->id, |
| 34 | 'key' => $stage['key'], |
| 35 | 'name' => $stage['name'], |
| 36 | 'position' => $stage['position'], |
| 37 | 'is_won' => $stage['is_won'], |
| 38 | 'is_lost' => $stage['is_lost'], |
| 39 | 'color' => $stage['color'], |
| 40 | ]); |
| 41 | } |
| 42 | |
| 43 | return $pipeline->load('stages'); |
| 44 | } |
| 45 | |
| 46 | public function stageByKey(string $tenantId, string $key, ?string $pipelineId = null): SalesDealStageModel |
| 47 | { |
| 48 | $pipeline = $pipelineId !== null |
| 49 | ? SalesPipelineModel::query()->where('tenant_id', $tenantId)->whereKey($pipelineId)->firstOrFail() |
| 50 | : $this->ensureForTenant($tenantId); |
| 51 | |
| 52 | $stage = SalesDealStageModel::query() |
| 53 | ->where('pipeline_id', $pipeline->id) |
| 54 | ->where('key', $key) |
| 55 | ->first(); |
| 56 | |
| 57 | if ($stage === null) { |
| 58 | throw \ImovelZapAi\Sales\Domain\Exceptions\DealException::stageNotFound($key); |
| 59 | } |
| 60 | |
| 61 | return $stage; |
| 62 | } |
| 63 | } |