Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ListOrganizationMembersQuery | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
6 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Organizations\Application\Queries; |
| 6 | |
| 7 | use App\Models\User; |
| 8 | use Illuminate\Support\Collection; |
| 9 | use ImovelZapAi\Organizations\Domain\Enums\InvitationStatus; |
| 10 | use ImovelZapAi\Organizations\Infrastructure\Persistence\Eloquent\OrganizationInvitationModel; |
| 11 | use ImovelZapAi\Tenancy\Infrastructure\Persistence\Eloquent\TenantModel; |
| 12 | use Spatie\Permission\PermissionRegistrar; |
| 13 | |
| 14 | final class ListOrganizationMembersQuery |
| 15 | { |
| 16 | public function __construct( |
| 17 | private readonly PermissionRegistrar $permissionRegistrar, |
| 18 | ) {} |
| 19 | |
| 20 | /** |
| 21 | * @return array{ |
| 22 | * tenant: TenantModel, |
| 23 | * members: Collection<int, array{user: User, roles: list<string>, is_owner: bool}>, |
| 24 | * invitations: Collection<int, OrganizationInvitationModel> |
| 25 | * } |
| 26 | */ |
| 27 | public function execute(string $tenantId): array |
| 28 | { |
| 29 | $tenant = TenantModel::query()->findOrFail($tenantId); |
| 30 | $this->permissionRegistrar->setPermissionsTeamId($tenantId); |
| 31 | |
| 32 | $members = User::query() |
| 33 | ->where('tenant_id', $tenantId) |
| 34 | ->orderBy('name') |
| 35 | ->get() |
| 36 | ->map(fn (User $user): array => [ |
| 37 | 'user' => $user, |
| 38 | 'roles' => $user->getRoleNames()->map(fn ($r) => (string) $r)->values()->all(), |
| 39 | 'is_owner' => (string) $tenant->owner_user_id === (string) $user->id, |
| 40 | ]); |
| 41 | |
| 42 | $invitations = OrganizationInvitationModel::query() |
| 43 | ->where('tenant_id', $tenantId) |
| 44 | ->where('status', InvitationStatus::Pending) |
| 45 | ->orderByDesc('created_at') |
| 46 | ->get(); |
| 47 | |
| 48 | return [ |
| 49 | 'tenant' => $tenant, |
| 50 | 'members' => $members, |
| 51 | 'invitations' => $invitations, |
| 52 | ]; |
| 53 | } |
| 54 | } |