Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| GetSettingsOverviewQuery | |
0.00% |
0 / 28 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Organizations\Application\Queries; |
| 6 | |
| 7 | use ImovelZapAi\Billing\Infrastructure\Persistence\Eloquent\BillingPaymentEventModel; |
| 8 | use ImovelZapAi\Organizations\Application\Actions\EnsureOrganizationBootstrapAction; |
| 9 | use ImovelZapAi\Organizations\Infrastructure\Persistence\Eloquent\OrganizationSettingsModel; |
| 10 | use ImovelZapAi\Organizations\Infrastructure\Persistence\Eloquent\OrganizationSubscriptionModel; |
| 11 | use ImovelZapAi\Tenancy\Infrastructure\Persistence\Eloquent\TenantModel; |
| 12 | |
| 13 | final class GetSettingsOverviewQuery |
| 14 | { |
| 15 | public function __construct( |
| 16 | private readonly EnsureOrganizationBootstrapAction $bootstrap, |
| 17 | ) {} |
| 18 | |
| 19 | /** |
| 20 | * @return array{ |
| 21 | * tenant: TenantModel, |
| 22 | * settings: OrganizationSettingsModel, |
| 23 | * subscription: ?OrganizationSubscriptionModel, |
| 24 | * payments_count: int, |
| 25 | * profile_completeness: int |
| 26 | * } |
| 27 | */ |
| 28 | public function execute(string $tenantId): array |
| 29 | { |
| 30 | $this->bootstrap->execute($tenantId); |
| 31 | |
| 32 | $tenant = TenantModel::query()->findOrFail($tenantId); |
| 33 | /** @var OrganizationSettingsModel $settings */ |
| 34 | $settings = OrganizationSettingsModel::query()->where('tenant_id', $tenantId)->firstOrFail(); |
| 35 | $subscription = OrganizationSubscriptionModel::query() |
| 36 | ->with('plan') |
| 37 | ->where('tenant_id', $tenantId) |
| 38 | ->first(); |
| 39 | |
| 40 | $filled = 0; |
| 41 | $fields = [ |
| 42 | $settings->trade_name, |
| 43 | $settings->document, |
| 44 | $settings->creci, |
| 45 | $settings->email, |
| 46 | $settings->phone, |
| 47 | $settings->city, |
| 48 | $settings->state, |
| 49 | ]; |
| 50 | foreach ($fields as $value) { |
| 51 | if (filled($value)) { |
| 52 | $filled++; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return [ |
| 57 | 'tenant' => $tenant, |
| 58 | 'settings' => $settings, |
| 59 | 'subscription' => $subscription, |
| 60 | 'payments_count' => BillingPaymentEventModel::query()->where('tenant_id', $tenantId)->count(), |
| 61 | 'profile_completeness' => (int) round(($filled / max(1, count($fields))) * 100), |
| 62 | ]; |
| 63 | } |
| 64 | } |