Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UpdateMemberRoleAction | |
0.00% |
0 / 13 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Organizations\Application\Actions; |
| 6 | |
| 7 | use App\Models\User; |
| 8 | use Illuminate\Validation\ValidationException; |
| 9 | use ImovelZapAi\Authentication\Domain\Enums\SystemRole; |
| 10 | use ImovelZapAi\Tenancy\Infrastructure\Persistence\Eloquent\TenantModel; |
| 11 | use Spatie\Permission\PermissionRegistrar; |
| 12 | |
| 13 | final class UpdateMemberRoleAction |
| 14 | { |
| 15 | public function __construct( |
| 16 | private readonly PermissionRegistrar $permissionRegistrar, |
| 17 | ) {} |
| 18 | |
| 19 | public function execute(string $tenantId, string $memberUserId, SystemRole $role): User |
| 20 | { |
| 21 | $tenant = TenantModel::query()->findOrFail($tenantId); |
| 22 | |
| 23 | $user = User::query() |
| 24 | ->where('tenant_id', $tenantId) |
| 25 | ->whereKey($memberUserId) |
| 26 | ->firstOrFail(); |
| 27 | |
| 28 | if ((string) $tenant->owner_user_id === $memberUserId && $role !== SystemRole::Admin) { |
| 29 | throw ValidationException::withMessages([ |
| 30 | 'role' => ['O dono da organização deve permanecer como Admin.'], |
| 31 | ]); |
| 32 | } |
| 33 | |
| 34 | $this->permissionRegistrar->setPermissionsTeamId($tenantId); |
| 35 | $user->syncRoles([$role->value]); |
| 36 | |
| 37 | return $user->fresh() ?? $user; |
| 38 | } |
| 39 | } |