96 lines
3.2 KiB
PHP
96 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Actions\ProjectPermissions\ProjectPermissionService;
|
|
use App\Actions\Tickets\CreateTicketWorklogEntry;
|
|
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 ProjectPermissionService $projectPermissionService,
|
|
) {}
|
|
|
|
public function index(Request $request, Project $project): Response
|
|
{
|
|
abort_unless(
|
|
$this->projectPermissionService->userCanViewProject($request->user(), $project),
|
|
403,
|
|
);
|
|
|
|
return Inertia::render('projects/Board', [
|
|
'boardColumns' => $project->boardColumns()
|
|
->with([
|
|
'tickets' => fn ($query) => $query
|
|
->with(['ticketPriority', 'ticketType']),
|
|
])
|
|
->get(),
|
|
'canEditTickets' => $this->projectPermissionService->userHasPermission($request->user(), $project, 'edit'),
|
|
'project' => $project,
|
|
'status' => session('status'),
|
|
]);
|
|
}
|
|
|
|
public function move(
|
|
MoveProjectTicketRequest $request,
|
|
Project $project,
|
|
Ticket $ticket,
|
|
): RedirectResponse {
|
|
abort_unless(
|
|
$this->projectPermissionService->userHasPermission($request->user(), $project, 'edit'),
|
|
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(),
|
|
$request->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}.");
|
|
}
|
|
}
|