101 lines
3.2 KiB
PHP
101 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Actions\Permissions\PermissionService;
|
|
use App\Actions\Tickets\CreateTicketWorklogEntry;
|
|
use App\Enums\Permission;
|
|
use App\Http\Requests\MoveProjectTicketRequest;
|
|
use App\Models\Project;
|
|
use App\Models\Ticket;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class ProjectBoardController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
|
|
private readonly PermissionService $permissionService,
|
|
) {}
|
|
|
|
public function index(Request $request, Project $project): Response
|
|
{
|
|
$user = $request->user();
|
|
|
|
abort_unless(
|
|
$this->permissionService->allows($user, Permission::ProjectRead, $project),
|
|
403,
|
|
);
|
|
|
|
return Inertia::render('projects/Board', [
|
|
'boardColumns' => $project->boardColumns()
|
|
->with([
|
|
'tickets' => fn ($query) => $query
|
|
->with(['ticketPriority', 'ticketType']),
|
|
])
|
|
->get(),
|
|
'canEditTickets' => $this->permissionService->allows($user, Permission::ProjectWrite, $project),
|
|
'project' => $project,
|
|
'status' => session('status'),
|
|
]);
|
|
}
|
|
|
|
public function move(
|
|
MoveProjectTicketRequest $request,
|
|
Project $project,
|
|
Ticket $ticket,
|
|
): RedirectResponse {
|
|
$user = $request->user();
|
|
|
|
abort_unless(
|
|
$this->permissionService->allows($user, Permission::ProjectWrite, $project),
|
|
403,
|
|
);
|
|
|
|
$validated = $request->validated();
|
|
$boardColumnId = Arr::get($validated, 'board_column_id');
|
|
$redirectRoute = Arr::get($validated, 'redirect_to') === 'backlog'
|
|
? 'projects.backlog.index'
|
|
: 'projects.board.index';
|
|
|
|
$destinationColumn = $project->boardColumns()
|
|
->find($boardColumnId);
|
|
|
|
$originBoardColumnId = $ticket->board_column_id;
|
|
$originLabel = $ticket->boardColumn?->name ?? 'Backlog';
|
|
$destinationLabel = $destinationColumn?->name ?? 'Backlog';
|
|
|
|
if ($ticket->board_column_id === $boardColumnId) {
|
|
return to_route($redirectRoute, $project)
|
|
->with('status', "{$ticket->ticket_key} already belongs to {$destinationLabel}.");
|
|
}
|
|
|
|
$ticket->update([
|
|
'board_column_id' => $destinationColumn?->id,
|
|
]);
|
|
|
|
$this->createTicketWorklogEntry->handle(
|
|
$ticket->fresh(),
|
|
$user,
|
|
'status_changed',
|
|
"Moved from {$originLabel} to {$destinationLabel}.",
|
|
[
|
|
'from' => [
|
|
'board_column_id' => $originBoardColumnId,
|
|
'label' => $originLabel,
|
|
],
|
|
'to' => [
|
|
'board_column_id' => $destinationColumn?->id,
|
|
'label' => $destinationLabel,
|
|
],
|
|
],
|
|
);
|
|
|
|
return to_route($redirectRoute, $project)
|
|
->with('status', "{$ticket->ticket_key} moved to {$destinationLabel}.");
|
|
}
|
|
}
|