Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| EnsureOrganizationBootstrapAction | |
0.00% |
0 / 25 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Organizations\Application\Actions; |
| 6 | |
| 7 | use ImovelZapAi\Organizations\Domain\Enums\SubscriptionStatus; |
| 8 | use ImovelZapAi\Organizations\Infrastructure\Persistence\Eloquent\OrganizationSettingsModel; |
| 9 | use ImovelZapAi\Organizations\Infrastructure\Persistence\Eloquent\OrganizationSubscriptionModel; |
| 10 | use ImovelZapAi\Organizations\Infrastructure\Persistence\Eloquent\SubscriptionPlanModel; |
| 11 | use ImovelZapAi\Tenancy\Domain\Enums\OrganizationType; |
| 12 | use ImovelZapAi\Tenancy\Infrastructure\Persistence\Eloquent\TenantModel; |
| 13 | |
| 14 | final class EnsureOrganizationBootstrapAction |
| 15 | { |
| 16 | public function __construct( |
| 17 | private readonly SeedSubscriptionPlansAction $seedPlans, |
| 18 | ) {} |
| 19 | |
| 20 | public function execute(string $tenantId, ?OrganizationType $type = null): void |
| 21 | { |
| 22 | $this->seedPlans->execute(); |
| 23 | |
| 24 | $tenant = TenantModel::query()->findOrFail($tenantId); |
| 25 | $resolvedType = $type ?? $tenant->type ?? OrganizationType::Agency; |
| 26 | |
| 27 | OrganizationSettingsModel::query()->firstOrCreate( |
| 28 | ['tenant_id' => $tenantId], |
| 29 | [ |
| 30 | 'trade_name' => $tenant->name, |
| 31 | 'timezone' => config('organizations.default_timezone', 'America/Sao_Paulo'), |
| 32 | ], |
| 33 | ); |
| 34 | |
| 35 | if (OrganizationSubscriptionModel::query()->where('tenant_id', $tenantId)->exists()) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $planCode = $resolvedType === OrganizationType::Solo ? 'solo' : 'start'; |
| 40 | $plan = SubscriptionPlanModel::query()->where('code', $planCode)->where('is_active', true)->first() |
| 41 | ?? SubscriptionPlanModel::query()->where('is_active', true)->orderBy('code')->first(); |
| 42 | |
| 43 | if ($plan === null) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | OrganizationSubscriptionModel::query()->create([ |
| 48 | 'tenant_id' => $tenantId, |
| 49 | 'subscription_plan_id' => $plan->id, |
| 50 | 'status' => SubscriptionStatus::Trialing, |
| 51 | 'trial_ends_at' => now()->addDays((int) config('organizations.trial_days', 14)), |
| 52 | 'current_period_ends_at' => now()->addDays((int) config('organizations.trial_days', 14)), |
| 53 | ]); |
| 54 | } |
| 55 | } |