Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
RemoveMemberAction
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Organizations\Application\Actions;
6
7use App\Models\User;
8use Illuminate\Validation\ValidationException;
9use ImovelZapAi\Tenancy\Infrastructure\Persistence\Eloquent\TenantModel;
10use RuntimeException;
11
12final class RemoveMemberAction
13{
14    public function execute(string $tenantId, string $memberUserId, string $actorUserId): void
15    {
16        $tenant = TenantModel::query()->findOrFail($tenantId);
17
18        if ((string) $tenant->owner_user_id === $memberUserId) {
19            throw ValidationException::withMessages([
20                'user' => ['O dono da organização não pode ser removido.'],
21            ]);
22        }
23
24        if ($memberUserId === $actorUserId) {
25            throw ValidationException::withMessages([
26                'user' => ['Você não pode remover a si mesmo.'],
27            ]);
28        }
29
30        $user = User::query()
31            ->where('tenant_id', $tenantId)
32            ->whereKey($memberUserId)
33            ->first();
34
35        if ($user === null) {
36            throw new RuntimeException('Membro não encontrado nesta organização.');
37        }
38
39        $user->delete();
40    }
41}