2026-06-16 18:05:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Livewire;
|
|
|
|
|
|
|
|
|
|
use Livewire\Component;
|
|
|
|
|
use Livewire\Attributes\Layout;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use App\Models\Project;
|
|
|
|
|
use App\Models\Issue;
|
|
|
|
|
use App\Notifications\IssueReportedNotification;
|
|
|
|
|
|
|
|
|
|
#[Layout('layouts.app')]
|
|
|
|
|
class IssueManager extends Component
|
|
|
|
|
{
|
|
|
|
|
public Project $project;
|
|
|
|
|
|
2026-06-17 14:16:14 +02:00
|
|
|
public $issues = [];
|
|
|
|
|
public $projectUsers = [];
|
|
|
|
|
|
|
|
|
|
// Form / modal state
|
|
|
|
|
public $showForm = false;
|
|
|
|
|
public $editingIssue = null; // issue id when editing, null when creating
|
2026-06-16 18:05:53 +02:00
|
|
|
|
2026-06-17 14:16:14 +02:00
|
|
|
// Form fields
|
2026-06-16 18:05:53 +02:00
|
|
|
public $title = '';
|
|
|
|
|
public $description = '';
|
|
|
|
|
public $status = 'open';
|
|
|
|
|
public $priority = 'medium';
|
2026-06-17 14:16:14 +02:00
|
|
|
public $assignedTo = '';
|
|
|
|
|
public $resolutionNotes = '';
|
|
|
|
|
|
|
|
|
|
// Optional context (e.g. when reporting from a map feature)
|
2026-06-16 18:05:53 +02:00
|
|
|
public $featureId = null;
|
|
|
|
|
public $inspectionId = null;
|
|
|
|
|
|
|
|
|
|
public function mount(Project $project)
|
|
|
|
|
{
|
|
|
|
|
$this->project = $project;
|
2026-06-17 14:16:14 +02:00
|
|
|
$this->loadProjectUsers();
|
2026-06-16 18:05:53 +02:00
|
|
|
$this->loadIssues();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function loadIssues()
|
|
|
|
|
{
|
|
|
|
|
$this->issues = Issue::where('project_id', $this->project->id)
|
|
|
|
|
->with(['feature', 'reporter', 'assignee'])
|
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
|
->get();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 14:16:14 +02:00
|
|
|
public function loadProjectUsers()
|
2026-06-16 18:05:53 +02:00
|
|
|
{
|
2026-06-17 14:16:14 +02:00
|
|
|
$this->projectUsers = $this->project->users()->orderBy('name')->get();
|
2026-06-16 18:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-17 14:16:14 +02:00
|
|
|
protected function rules(): array
|
2026-06-16 18:05:53 +02:00
|
|
|
{
|
2026-06-17 14:16:14 +02:00
|
|
|
return [
|
|
|
|
|
'title' => 'required|string|max:255',
|
|
|
|
|
'description' => 'nullable|string',
|
|
|
|
|
'status' => 'required|in:' . implode(',', Issue::STATUSES),
|
|
|
|
|
'priority' => 'required|in:' . implode(',', Issue::PRIORITIES),
|
|
|
|
|
'assignedTo' => 'nullable|exists:users,id',
|
|
|
|
|
'resolutionNotes' => 'nullable|string',
|
|
|
|
|
];
|
2026-06-16 18:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-17 14:16:14 +02:00
|
|
|
public function openForm($issueId = null)
|
|
|
|
|
{
|
|
|
|
|
$this->resetForm();
|
|
|
|
|
|
|
|
|
|
if ($issueId) {
|
|
|
|
|
$issue = Issue::where('project_id', $this->project->id)->findOrFail($issueId);
|
|
|
|
|
$this->editingIssue = $issue->id;
|
|
|
|
|
$this->title = $issue->title;
|
|
|
|
|
$this->description = $issue->description ?? '';
|
|
|
|
|
$this->status = $issue->status;
|
|
|
|
|
$this->priority = $issue->priority;
|
|
|
|
|
$this->assignedTo = $issue->assigned_to ?? '';
|
|
|
|
|
$this->resolutionNotes = $issue->resolution_notes ?? '';
|
|
|
|
|
$this->featureId = $issue->feature_id;
|
|
|
|
|
$this->inspectionId = $issue->inspection_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->showForm = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function closeForm()
|
|
|
|
|
{
|
|
|
|
|
$this->showForm = false;
|
|
|
|
|
$this->resetForm();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function resetForm(): void
|
2026-06-16 18:05:53 +02:00
|
|
|
{
|
2026-06-17 14:16:14 +02:00
|
|
|
$this->reset([
|
|
|
|
|
'title', 'description', 'assignedTo', 'resolutionNotes',
|
|
|
|
|
'featureId', 'inspectionId', 'editingIssue',
|
2026-06-16 18:05:53 +02:00
|
|
|
]);
|
2026-06-17 14:16:14 +02:00
|
|
|
$this->status = 'open';
|
|
|
|
|
$this->priority = 'medium';
|
|
|
|
|
$this->resetErrorBag();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function save()
|
|
|
|
|
{
|
|
|
|
|
$this->validate();
|
|
|
|
|
|
|
|
|
|
$payload = [
|
|
|
|
|
'title' => $this->title,
|
|
|
|
|
'description' => $this->description,
|
|
|
|
|
'status' => $this->status,
|
|
|
|
|
'priority' => $this->priority,
|
|
|
|
|
'feature_id' => $this->featureId,
|
|
|
|
|
'inspection_id' => $this->inspectionId,
|
|
|
|
|
'assigned_to' => $this->assignedTo ?: null,
|
|
|
|
|
'resolution_notes' => $this->resolutionNotes ?: null,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Keep resolved_at in sync with the status
|
|
|
|
|
if (in_array($this->status, ['resolved', 'closed'])) {
|
|
|
|
|
$payload['resolved_at'] = now();
|
|
|
|
|
} else {
|
|
|
|
|
$payload['resolved_at'] = null;
|
|
|
|
|
}
|
2026-06-16 18:05:53 +02:00
|
|
|
|
2026-06-17 14:16:14 +02:00
|
|
|
if ($this->editingIssue) {
|
|
|
|
|
$issue = Issue::where('project_id', $this->project->id)->findOrFail($this->editingIssue);
|
|
|
|
|
// Don't overwrite an existing resolved date if it was already resolved
|
|
|
|
|
if ($issue->resolved_at && in_array($this->status, ['resolved', 'closed'])) {
|
|
|
|
|
unset($payload['resolved_at']);
|
|
|
|
|
}
|
|
|
|
|
$issue->update($payload);
|
2026-06-16 18:05:53 +02:00
|
|
|
} else {
|
2026-06-17 14:16:14 +02:00
|
|
|
$issue = Issue::create(array_merge($payload, [
|
|
|
|
|
'project_id' => $this->project->id,
|
|
|
|
|
'reported_by' => Auth::id(),
|
|
|
|
|
]));
|
2026-06-16 18:05:53 +02:00
|
|
|
|
|
|
|
|
if ($issue->wasRecentlyCreated) {
|
|
|
|
|
$issue->load(['feature', 'assignee']);
|
|
|
|
|
$creator = $this->project->creator;
|
|
|
|
|
if ($creator && $creator->id !== Auth::id()) {
|
|
|
|
|
$creator->notify(new IssueReportedNotification($issue));
|
|
|
|
|
}
|
|
|
|
|
if ($issue->assignee && $issue->assignee->id !== Auth::id()) {
|
|
|
|
|
$issue->assignee->notify(new IssueReportedNotification($issue));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 14:16:14 +02:00
|
|
|
$this->closeForm();
|
2026-06-16 18:05:53 +02:00
|
|
|
$this->loadIssues();
|
|
|
|
|
$this->dispatch('notify', 'Issue guardado correctamente');
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 14:16:14 +02:00
|
|
|
public function resolve($issueId)
|
2026-06-16 18:05:53 +02:00
|
|
|
{
|
|
|
|
|
$issue = Issue::where('project_id', $this->project->id)->findOrFail($issueId);
|
2026-06-17 14:16:14 +02:00
|
|
|
$issue->update([
|
|
|
|
|
'status' => 'resolved',
|
|
|
|
|
'resolved_at' => $issue->resolved_at ?? now(),
|
|
|
|
|
]);
|
2026-06-16 18:05:53 +02:00
|
|
|
$this->loadIssues();
|
2026-06-17 14:16:14 +02:00
|
|
|
$this->dispatch('notify', 'Issue marcado como resuelto');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function close($issueId)
|
|
|
|
|
{
|
|
|
|
|
$issue = Issue::where('project_id', $this->project->id)->findOrFail($issueId);
|
|
|
|
|
$issue->update([
|
|
|
|
|
'status' => 'closed',
|
|
|
|
|
'resolved_at' => $issue->resolved_at ?? now(),
|
|
|
|
|
]);
|
|
|
|
|
$this->loadIssues();
|
|
|
|
|
$this->dispatch('notify', 'Issue cerrado');
|
2026-06-16 18:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-17 14:16:14 +02:00
|
|
|
public function delete($issueId)
|
2026-06-16 18:05:53 +02:00
|
|
|
{
|
2026-06-17 14:16:14 +02:00
|
|
|
$issue = Issue::where('project_id', $this->project->id)->findOrFail($issueId);
|
|
|
|
|
$issue->delete();
|
|
|
|
|
$this->loadIssues();
|
|
|
|
|
$this->dispatch('notify', 'Issue eliminado');
|
2026-06-16 18:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
2026-06-17 14:16:14 +02:00
|
|
|
return view('livewire.issues.issue-manager');
|
2026-06-16 18:05:53 +02:00
|
|
|
}
|
|
|
|
|
}
|