Implement ticket types priorities and custom fields
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\ProjectPermissions\ProjectPermissionService;
|
||||
use App\Actions\ProjectTicketMetadata\InitializeProjectTicketMetadata;
|
||||
use App\Http\Requests\StoreProjectRequest;
|
||||
use App\Http\Requests\SyncProjectPermissionsRequest;
|
||||
use App\Models\Project;
|
||||
@@ -16,6 +17,7 @@ use Inertia\Response;
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly InitializeProjectTicketMetadata $initializeProjectTicketMetadata,
|
||||
private readonly ProjectPermissionService $projectPermissionService,
|
||||
) {}
|
||||
|
||||
@@ -50,6 +52,7 @@ class ProjectController extends Controller
|
||||
public function store(StoreProjectRequest $request): RedirectResponse
|
||||
{
|
||||
$project = Project::query()->create($request->validated());
|
||||
$this->initializeProjectTicketMetadata->handle($project);
|
||||
$this->projectPermissionService->grantAllProjectPermissions($project, $request->user());
|
||||
|
||||
return to_route('projects.show', $project);
|
||||
|
||||
@@ -0,0 +1,291 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\ProjectPermissions\ProjectPermissionService;
|
||||
use App\Actions\ProjectTicketMetadata\InitializeProjectTicketMetadata;
|
||||
use App\Http\Requests\StoreProjectNamedSettingRequest;
|
||||
use App\Http\Requests\StoreTicketFieldDefinitionRequest;
|
||||
use App\Http\Requests\UpdateProjectNamedSettingRequest;
|
||||
use App\Http\Requests\UpdateTicketFieldDefinitionRequest;
|
||||
use App\Models\Project;
|
||||
use App\Models\TicketFieldDefinition;
|
||||
use App\Models\TicketPriority;
|
||||
use App\Models\TicketType;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class ProjectTicketMetadataSettingsController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly InitializeProjectTicketMetadata $initializeProjectTicketMetadata,
|
||||
private readonly ProjectPermissionService $projectPermissionService,
|
||||
) {}
|
||||
|
||||
public function show(Request $request, Project $project): Response
|
||||
{
|
||||
$this->abortUnlessCanManage($request, $project);
|
||||
$this->initializeProjectTicketMetadata->handle($project);
|
||||
|
||||
return Inertia::render('projects/settings/Metadata', [
|
||||
'fieldTypeOptions' => collect(TicketFieldDefinition::FIELD_TYPES)
|
||||
->map(fn (string $fieldType): array => [
|
||||
'label' => ucfirst($fieldType),
|
||||
'value' => $fieldType,
|
||||
])
|
||||
->all(),
|
||||
'project' => $project,
|
||||
'status' => session('status'),
|
||||
'ticketFieldDefinitions' => $project->ticketFieldDefinitions()->get(),
|
||||
'ticketPriorities' => $project->ticketPriorities()->get(),
|
||||
'ticketTypes' => $project->ticketTypes()->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function storeType(StoreProjectNamedSettingRequest $request, Project $project): RedirectResponse
|
||||
{
|
||||
$this->abortUnlessCanManage($request, $project);
|
||||
$this->initializeProjectTicketMetadata->handle($project);
|
||||
|
||||
$ticketType = DB::transaction(function () use ($project, $request): TicketType {
|
||||
$ticketType = $project->ticketTypes()->create([
|
||||
'is_builtin' => false,
|
||||
'name' => $request->validated('name'),
|
||||
'position' => $project->ticketTypes()->count() + 1,
|
||||
'system_key' => null,
|
||||
]);
|
||||
|
||||
$this->reorderModels(
|
||||
$project->ticketTypes()->whereKeyNot($ticketType->id)->get()->values(),
|
||||
$ticketType,
|
||||
$request->position() ?? $project->ticketTypes()->count(),
|
||||
);
|
||||
|
||||
return $ticketType;
|
||||
});
|
||||
|
||||
return to_route('projects.settings.metadata', $project)
|
||||
->with('status', "{$ticketType->name} type was added.");
|
||||
}
|
||||
|
||||
public function updateType(
|
||||
UpdateProjectNamedSettingRequest $request,
|
||||
Project $project,
|
||||
TicketType $ticketType,
|
||||
): RedirectResponse {
|
||||
$this->abortUnlessCanManage($request, $project);
|
||||
$this->initializeProjectTicketMetadata->handle($project);
|
||||
|
||||
DB::transaction(function () use ($project, $request, $ticketType): void {
|
||||
$ticketType->update([
|
||||
'name' => $request->validated('name'),
|
||||
]);
|
||||
|
||||
$this->reorderModels(
|
||||
$project->ticketTypes()->whereKeyNot($ticketType->id)->get()->values(),
|
||||
$ticketType,
|
||||
$request->position(),
|
||||
);
|
||||
});
|
||||
|
||||
return to_route('projects.settings.metadata', $project)
|
||||
->with('status', "{$ticketType->fresh()->name} type was updated.");
|
||||
}
|
||||
|
||||
public function destroyType(Request $request, Project $project, TicketType $ticketType): RedirectResponse
|
||||
{
|
||||
$this->abortUnlessCanManage($request, $project);
|
||||
|
||||
abort_if($ticketType->is_builtin, 403);
|
||||
|
||||
$deletedName = $ticketType->name;
|
||||
|
||||
DB::transaction(function () use ($project, $ticketType): void {
|
||||
$ticketType->delete();
|
||||
|
||||
$this->persistOrderedModels($project->ticketTypes()->get()->values());
|
||||
});
|
||||
|
||||
return to_route('projects.settings.metadata', $project)
|
||||
->with('status', "{$deletedName} type was removed.");
|
||||
}
|
||||
|
||||
public function storePriority(StoreProjectNamedSettingRequest $request, Project $project): RedirectResponse
|
||||
{
|
||||
$this->abortUnlessCanManage($request, $project);
|
||||
|
||||
$ticketPriority = DB::transaction(function () use ($project, $request): TicketPriority {
|
||||
$ticketPriority = $project->ticketPriorities()->create([
|
||||
'name' => $request->validated('name'),
|
||||
'position' => $project->ticketPriorities()->count() + 1,
|
||||
]);
|
||||
|
||||
$this->reorderModels(
|
||||
$project->ticketPriorities()->whereKeyNot($ticketPriority->id)->get()->values(),
|
||||
$ticketPriority,
|
||||
$request->position() ?? $project->ticketPriorities()->count(),
|
||||
);
|
||||
|
||||
return $ticketPriority;
|
||||
});
|
||||
|
||||
return to_route('projects.settings.metadata', $project)
|
||||
->with('status', "{$ticketPriority->name} priority was added.");
|
||||
}
|
||||
|
||||
public function updatePriority(
|
||||
UpdateProjectNamedSettingRequest $request,
|
||||
Project $project,
|
||||
TicketPriority $ticketPriority,
|
||||
): RedirectResponse {
|
||||
$this->abortUnlessCanManage($request, $project);
|
||||
|
||||
DB::transaction(function () use ($project, $request, $ticketPriority): void {
|
||||
$ticketPriority->update([
|
||||
'name' => $request->validated('name'),
|
||||
]);
|
||||
|
||||
$this->reorderModels(
|
||||
$project->ticketPriorities()->whereKeyNot($ticketPriority->id)->get()->values(),
|
||||
$ticketPriority,
|
||||
$request->position(),
|
||||
);
|
||||
});
|
||||
|
||||
return to_route('projects.settings.metadata', $project)
|
||||
->with('status', "{$ticketPriority->fresh()->name} priority was updated.");
|
||||
}
|
||||
|
||||
public function destroyPriority(
|
||||
Request $request,
|
||||
Project $project,
|
||||
TicketPriority $ticketPriority,
|
||||
): RedirectResponse {
|
||||
$this->abortUnlessCanManage($request, $project);
|
||||
|
||||
$deletedName = $ticketPriority->name;
|
||||
|
||||
DB::transaction(function () use ($project, $ticketPriority): void {
|
||||
$ticketPriority->delete();
|
||||
|
||||
$this->persistOrderedModels($project->ticketPriorities()->get()->values());
|
||||
});
|
||||
|
||||
return to_route('projects.settings.metadata', $project)
|
||||
->with('status', "{$deletedName} priority was removed.");
|
||||
}
|
||||
|
||||
public function storeFieldDefinition(
|
||||
StoreTicketFieldDefinitionRequest $request,
|
||||
Project $project,
|
||||
): RedirectResponse {
|
||||
$this->abortUnlessCanManage($request, $project);
|
||||
|
||||
$ticketFieldDefinition = DB::transaction(function () use ($project, $request): TicketFieldDefinition {
|
||||
$ticketFieldDefinition = $project->ticketFieldDefinitions()->create([
|
||||
'field_type' => $request->validated('field_type'),
|
||||
'label' => $request->validated('label'),
|
||||
'position' => $project->ticketFieldDefinitions()->count() + 1,
|
||||
]);
|
||||
|
||||
$this->reorderModels(
|
||||
$project->ticketFieldDefinitions()->whereKeyNot($ticketFieldDefinition->id)->get()->values(),
|
||||
$ticketFieldDefinition,
|
||||
$request->position() ?? $project->ticketFieldDefinitions()->count(),
|
||||
);
|
||||
|
||||
return $ticketFieldDefinition;
|
||||
});
|
||||
|
||||
return to_route('projects.settings.metadata', $project)
|
||||
->with('status', "{$ticketFieldDefinition->label} field was added.");
|
||||
}
|
||||
|
||||
public function updateFieldDefinition(
|
||||
UpdateTicketFieldDefinitionRequest $request,
|
||||
Project $project,
|
||||
TicketFieldDefinition $ticketFieldDefinition,
|
||||
): RedirectResponse {
|
||||
$this->abortUnlessCanManage($request, $project);
|
||||
|
||||
DB::transaction(function () use ($project, $request, $ticketFieldDefinition): void {
|
||||
$ticketFieldDefinition->update([
|
||||
'field_type' => $request->validated('field_type'),
|
||||
'label' => $request->validated('label'),
|
||||
]);
|
||||
|
||||
$this->reorderModels(
|
||||
$project->ticketFieldDefinitions()->whereKeyNot($ticketFieldDefinition->id)->get()->values(),
|
||||
$ticketFieldDefinition,
|
||||
$request->position(),
|
||||
);
|
||||
});
|
||||
|
||||
return to_route('projects.settings.metadata', $project)
|
||||
->with('status', "{$ticketFieldDefinition->fresh()->label} field was updated.");
|
||||
}
|
||||
|
||||
public function destroyFieldDefinition(
|
||||
Request $request,
|
||||
Project $project,
|
||||
TicketFieldDefinition $ticketFieldDefinition,
|
||||
): RedirectResponse {
|
||||
$this->abortUnlessCanManage($request, $project);
|
||||
|
||||
$deletedLabel = $ticketFieldDefinition->label;
|
||||
|
||||
DB::transaction(function () use ($project, $ticketFieldDefinition): void {
|
||||
$ticketFieldDefinition->delete();
|
||||
|
||||
$this->persistOrderedModels($project->ticketFieldDefinitions()->get()->values());
|
||||
});
|
||||
|
||||
return to_route('projects.settings.metadata', $project)
|
||||
->with('status', "{$deletedLabel} field was removed.");
|
||||
}
|
||||
|
||||
private function abortUnlessCanManage(Request $request, Project $project): void
|
||||
{
|
||||
abort_unless(
|
||||
$this->projectPermissionService->userCanManageProjectAccess($request->user(), $project),
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, Model> $models
|
||||
*/
|
||||
private function persistOrderedModels(Collection $models): void
|
||||
{
|
||||
$temporaryOffset = ((int) $models->max('position')) + $models->count() + 1;
|
||||
|
||||
$models->each(function (Model $model, int $index) use ($temporaryOffset): void {
|
||||
$model->forceFill([
|
||||
'position' => $temporaryOffset + $index,
|
||||
])->saveQuietly();
|
||||
});
|
||||
|
||||
$models->each(function (Model $model, int $index): void {
|
||||
$model->forceFill([
|
||||
'position' => $index + 1,
|
||||
])->saveQuietly();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, Model> $existingModels
|
||||
*/
|
||||
private function reorderModels(Collection $existingModels, Model $movingModel, int $desiredPosition): void
|
||||
{
|
||||
$insertAt = max(0, min($desiredPosition - 1, $existingModels->count()));
|
||||
|
||||
$existingModels->splice($insertAt, 0, [$movingModel]);
|
||||
|
||||
$this->persistOrderedModels($existingModels->values());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreProjectNamedSettingRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'position' => ['nullable', 'integer', 'min:1'],
|
||||
];
|
||||
}
|
||||
|
||||
public function position(): ?int
|
||||
{
|
||||
$position = $this->validated('position');
|
||||
|
||||
if ($position === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (int) $position;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\TicketFieldDefinition;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreTicketFieldDefinitionRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'field_type' => ['required', 'string', Rule::in(TicketFieldDefinition::FIELD_TYPES)],
|
||||
'label' => ['required', 'string', 'max:255'],
|
||||
'position' => ['nullable', 'integer', 'min:1'],
|
||||
];
|
||||
}
|
||||
|
||||
public function position(): ?int
|
||||
{
|
||||
$position = $this->validated('position');
|
||||
|
||||
if ($position === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (int) $position;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateProjectNamedSettingRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'position' => ['required', 'integer', 'min:1'],
|
||||
];
|
||||
}
|
||||
|
||||
public function position(): int
|
||||
{
|
||||
return (int) $this->validated('position');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\TicketFieldDefinition;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateTicketFieldDefinitionRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'field_type' => ['required', 'string', Rule::in(TicketFieldDefinition::FIELD_TYPES)],
|
||||
'label' => ['required', 'string', 'max:255'],
|
||||
'position' => ['required', 'integer', 'min:1'],
|
||||
];
|
||||
}
|
||||
|
||||
public function position(): int
|
||||
{
|
||||
return (int) $this->validated('position');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user