feat: Phase 3.1 - FormRequests for Task, Issue, Inspection
- Create TaskStoreRequest, TaskUpdateRequest with authorize() + rules() - Create IssueStoreRequest, IssueUpdateRequest with authorize() + rules() - Create InspectionStoreRequest, InspectionUpdateRequest with authorize() + rules() + withValidator() - Update TaskForm to use FormRequests via app() - Update IssueForm (kept inline validation for complex logic) - Add InspectionStore/Update requests for ProjectMap inspection methods Tests: 101 passing (319 assertions)
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\InspectionTemplate;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class InspectionStoreRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user()->can('create inspections');
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'selectedTemplateId' => 'required|exists:inspection_templates,id',
|
||||
'inspectionPhotos.*' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:10240',
|
||||
'inspectionFormData' => 'nullable|array',
|
||||
'inspectionResult' => 'nullable|in:pass,fail',
|
||||
'inspectionNotes' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
|
||||
public function withValidator($validator)
|
||||
{
|
||||
$validator->after(function ($validator) {
|
||||
$template = InspectionTemplate::find($this->input('selectedTemplateId'));
|
||||
if ($template) {
|
||||
foreach ($template->fields as $field) {
|
||||
if (($field['required'] ?? false) && empty($this->input("inspectionFormData.{$field['name']}"))) {
|
||||
$validator->errors()->add(
|
||||
"inspectionFormData.{$field['name']}",
|
||||
"El campo {$field['label']} es obligatorio."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Inspection;
|
||||
use App\Models\InspectionTemplate;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class InspectionUpdateRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
$inspection = $this->route('inspection') ?? Inspection::find($this->route('inspection'));
|
||||
if ($inspection) {
|
||||
return $this->user()->can('edit inspections', $inspection->project) && $this->user()->can('edit', $inspection);
|
||||
}
|
||||
return $this->user()->can('edit inspections');
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'selectedTemplateId' => 'required|exists:inspection_templates,id',
|
||||
'editInspectionPhotos.*' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:10240',
|
||||
'editInspectionPhotosToDelete' => 'array',
|
||||
'editInspectionPhotosToDelete.*' => 'exists:media,id',
|
||||
'editInspectionFormData' => 'nullable|array',
|
||||
'editInspectionResult' => 'nullable|in:pass,fail',
|
||||
'editInspectionNotes' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
|
||||
public function withValidator($validator)
|
||||
{
|
||||
$validator->after(function ($validator) {
|
||||
$template = InspectionTemplate::find($this->input('selectedTemplateId'));
|
||||
if ($template) {
|
||||
foreach ($template->fields as $field) {
|
||||
if (($field['required'] ?? false) && empty($this->input("editInspectionFormData.{$field['name']}"))) {
|
||||
$validator->errors()->add(
|
||||
"editInspectionFormData.{$field['name']}",
|
||||
"El campo {$field['label']} es obligatorio."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Issue;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class IssueStoreRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
$project = $this->route('project') ?? Project::find($this->input('project_id'));
|
||||
if ($project) {
|
||||
return $this->user()->can('create issues', $project);
|
||||
}
|
||||
return $this->user()->can('create issues');
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'status' => ['required', Rule::in(Issue::STATUSES)],
|
||||
'priority' => ['required', Rule::in(Issue::PRIORITIES)],
|
||||
'type' => ['required', Rule::in(Issue::TYPES)],
|
||||
'assignedTo' => 'nullable|exists:users,id',
|
||||
'resolutionNotes' => 'nullable|string',
|
||||
'featureId' => 'nullable|exists:features,id',
|
||||
'inspectionId' => 'nullable|exists:inspections,id',
|
||||
'project_id' => 'required|exists:projects,id',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Issue;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class IssueUpdateRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
$issue = $this->route('issue') ?? Issue::find($this->route('issue'));
|
||||
if ($issue) {
|
||||
return $this->user()->can('edit issues', $issue->project) && $this->user()->can('edit', $issue);
|
||||
}
|
||||
return $this->user()->can('edit issues');
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'sometimes|required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'status' => ['sometimes', 'required', Rule::in(Issue::STATUSES)],
|
||||
'priority' => ['sometimes', 'required', Rule::in(Issue::PRIORITIES)],
|
||||
'type' => ['sometimes', 'required', Rule::in(Issue::TYPES)],
|
||||
'assigned_to' => 'nullable|exists:users,id',
|
||||
'resolution_notes' => 'nullable|string',
|
||||
'feature_id' => 'nullable|exists:features,id',
|
||||
'inspection_id' => 'nullable|exists:inspections,id',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Project;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class TaskStoreRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
$project = $this->route('project') ?? $this->input('project_id');
|
||||
if ($project instanceof Project) {
|
||||
return $this->user()->can('create tasks', $project);
|
||||
}
|
||||
return $this->user()->can('create tasks');
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'phase_id' => 'nullable|exists:phases,id',
|
||||
'status' => ['required', Rule::in(['pending', 'in_progress', 'completed', 'cancelled'])],
|
||||
'priority' => ['required', Rule::in(['low', 'medium', 'high', 'critical'])],
|
||||
'assigned_to' => 'nullable|exists:users,id',
|
||||
'due_date' => 'nullable|date',
|
||||
'start_date' => 'nullable|date|before_or_equal:due_date',
|
||||
'estimated_hours' => 'nullable|integer|min:0|max:10000',
|
||||
'actual_hours' => 'nullable|integer|min:0|max:10000',
|
||||
'order' => 'nullable|integer|min:0',
|
||||
'parent_task_id' => 'nullable|exists:tasks,id',
|
||||
'project_id' => 'required|exists:projects,id',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Task;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class TaskUpdateRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
$task = $this->route('task') ?? Task::find($this->route('task'));
|
||||
if ($task) {
|
||||
return $this->user()->can('update', $task);
|
||||
}
|
||||
return $this->user()->can('update tasks');
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'sometimes|required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'phase_id' => 'nullable|exists:phases,id',
|
||||
'status' => ['sometimes', 'required', Rule::in(['pending', 'in_progress', 'completed', 'cancelled'])],
|
||||
'priority' => ['sometimes', 'required', Rule::in(['low', 'medium', 'high', 'critical'])],
|
||||
'assigned_to' => 'nullable|exists:users,id',
|
||||
'due_date' => 'nullable|date',
|
||||
'start_date' => 'nullable|date|before_or_equal:due_date',
|
||||
'estimated_hours' => 'nullable|integer|min:0|max:10000',
|
||||
'actual_hours' => 'nullable|integer|min:0|max:10000',
|
||||
'order' => 'nullable|integer|min:0',
|
||||
'parent_task_id' => 'nullable|exists:tasks,id',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user