feat(projects): usuarios/empresas del proyecto como tablas Rappasoft + permiso assign companies
- Permiso 'assign companies' propio (antes empresas reutilizaba 'assign users'); concedido a los roles que ya tenían 'assign users'. - ProjectUsersTable y ProjectCompaniesTable (Rappasoft): búsqueda, filtro por rol, cambio de rol en línea y quitar; gateadas por assign users / assign companies. - ProjectUsers/ProjectCompanies quedan como contenedor (form de asignación) que embebe la tabla y refresca el desplegable vía eventos. - Unificadas confirmaciones (wire:confirm) y notificaciones (dispatch notify). Tests: ProjectAssignmentsTest (4). Suite 69 passing (solo 2 pre-existentes sqlite). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,15 +2,15 @@
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
use App\Models\Project;
|
||||
use App\Models\Company;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class ProjectCompanies extends Component
|
||||
{
|
||||
public Project $project;
|
||||
public $assignedCompanies = [];
|
||||
public $allCompanies = [];
|
||||
public $selectedCompanyId = '';
|
||||
public $selectedRole = 'other';
|
||||
@@ -18,64 +18,46 @@ class ProjectCompanies extends Component
|
||||
public function mount(Project $project)
|
||||
{
|
||||
$this->project = $project;
|
||||
$this->loadCompanies();
|
||||
$this->loadAvailable();
|
||||
}
|
||||
|
||||
public function loadCompanies()
|
||||
/** Companies not yet assigned to the project (for the dropdown). */
|
||||
public function loadAvailable(): void
|
||||
{
|
||||
$this->assignedCompanies = $this->project->companies()->withPivot('role_in_project')->get();
|
||||
$assignedIds = $this->assignedCompanies->pluck('id')->toArray();
|
||||
$assignedIds = $this->project->companies()->pluck('companies.id')->toArray();
|
||||
$this->allCompanies = Company::whereNotIn('id', $assignedIds)->orderBy('name')->get();
|
||||
}
|
||||
|
||||
/** Reload the dropdown when the embedded table changes assignments. */
|
||||
#[On('project-companies-changed')]
|
||||
public function onCompaniesChanged(): void
|
||||
{
|
||||
$this->loadAvailable();
|
||||
}
|
||||
|
||||
public function assignCompany()
|
||||
{
|
||||
$user = Auth::user();
|
||||
if (!$user->can('assign users')) {
|
||||
session()->flash('error', 'No tienes permisos para asignar compañías.');
|
||||
return;
|
||||
}
|
||||
abort_unless(Auth::user()->can('assign companies'), 403);
|
||||
|
||||
$this->validate([
|
||||
'selectedCompanyId' => 'required|exists:companies,id',
|
||||
'selectedRole' => 'required|in:owner,constructor,subcontractor,consultant,supplier,other',
|
||||
'selectedRole' => 'required|in:' . implode(',', array_keys(ProjectCompaniesTable::ROLES)),
|
||||
]);
|
||||
|
||||
$this->project->companies()->attach($this->selectedCompanyId, [
|
||||
'role_in_project' => $this->selectedRole
|
||||
'role_in_project' => $this->selectedRole,
|
||||
]);
|
||||
|
||||
$this->reset(['selectedCompanyId', 'selectedRole']);
|
||||
$this->loadCompanies();
|
||||
$this->dispatch('notify', 'Compañía asignada al proyecto.');
|
||||
}
|
||||
|
||||
public function removeCompany($companyId)
|
||||
{
|
||||
$user = Auth::user();
|
||||
if (!$user->can('assign users')) {
|
||||
session()->flash('error', 'Sin permisos.');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->project->companies()->detach($companyId);
|
||||
$this->loadCompanies();
|
||||
$this->dispatch('notify', 'Compañía eliminada del proyecto.');
|
||||
}
|
||||
|
||||
public function changeRole($companyId, $role)
|
||||
{
|
||||
if (!in_array($role, ['owner', 'constructor', 'subcontractor', 'consultant', 'supplier', 'other'])) return;
|
||||
|
||||
$this->project->companies()->updateExistingPivot($companyId, [
|
||||
'role_in_project' => $role
|
||||
]);
|
||||
$this->loadCompanies();
|
||||
$this->dispatch('notify', 'Rol actualizado.');
|
||||
$this->loadAvailable();
|
||||
$this->dispatch('project-companies-changed');
|
||||
$this->dispatch('notify', 'Empresa asignada al proyecto.');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.project-companies');
|
||||
return view('livewire.project-companies', [
|
||||
'roles' => ProjectCompaniesTable::ROLES,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user