Implement ticket details and worklog editing
This commit is contained in:
@@ -4,10 +4,15 @@ namespace App\Actions\Tickets;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Ticket;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CreateProjectTicket
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array{
|
||||
* acceptance_criteria: string|null,
|
||||
@@ -17,14 +22,14 @@ class CreateProjectTicket
|
||||
* title: string
|
||||
* } $attributes
|
||||
*/
|
||||
public function handle(Project $project, array $attributes): Ticket
|
||||
public function handle(Project $project, array $attributes, ?User $actor = null): Ticket
|
||||
{
|
||||
return DB::transaction(function () use ($attributes, $project): Ticket {
|
||||
return DB::transaction(function () use ($actor, $attributes, $project): Ticket {
|
||||
$nextSequenceNumber = (int) Ticket::query()
|
||||
->where('project_id', $project->id)
|
||||
->max('sequence_number') + 1;
|
||||
|
||||
return Ticket::query()->create([
|
||||
$ticket = Ticket::query()->create([
|
||||
'acceptance_criteria' => $attributes['acceptance_criteria'],
|
||||
'board_column_id' => null,
|
||||
'description' => $attributes['description'],
|
||||
@@ -35,6 +40,23 @@ class CreateProjectTicket
|
||||
'ticket_type_id' => $attributes['ticket_type_id'],
|
||||
'title' => $attributes['title'],
|
||||
]);
|
||||
|
||||
if ($actor !== null) {
|
||||
$this->createTicketWorklogEntry->handle(
|
||||
$ticket,
|
||||
$actor,
|
||||
'created',
|
||||
'Ticket was created in Backlog.',
|
||||
[
|
||||
'from' => null,
|
||||
'to' => [
|
||||
'label' => 'Backlog',
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
return $ticket;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,306 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Tickets;
|
||||
|
||||
use App\Models\Ticket;
|
||||
use App\Models\TicketFieldDefinition;
|
||||
use App\Models\TicketFieldValue;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class UpdateProjectTicket
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array{
|
||||
* acceptance_criteria: string|null,
|
||||
* description: string|null,
|
||||
* ticket_priority_id: int,
|
||||
* ticket_type_id: int,
|
||||
* title: string
|
||||
* } $ticketAttributes
|
||||
* @param array<int, array{
|
||||
* ticket_field_definition_id: int,
|
||||
* value_number?: float|int|string|null,
|
||||
* value_user_id?: int|string|null
|
||||
* }> $fieldValueAttributes
|
||||
*/
|
||||
public function handle(
|
||||
Ticket $ticket,
|
||||
User $actor,
|
||||
array $ticketAttributes,
|
||||
array $fieldValueAttributes,
|
||||
): bool {
|
||||
return DB::transaction(function () use ($actor, $fieldValueAttributes, $ticket, $ticketAttributes): bool {
|
||||
$ticket->loadMissing([
|
||||
'fieldValues.ticketFieldDefinition',
|
||||
'fieldValues.userValue',
|
||||
'project',
|
||||
'ticketPriority',
|
||||
'ticketType',
|
||||
]);
|
||||
|
||||
/** @var Collection<int, TicketFieldDefinition> $fieldDefinitions */
|
||||
$fieldDefinitions = $ticket->project->ticketFieldDefinitions()
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$ticketTypeOptions = $ticket->project->ticketTypes()
|
||||
->get()
|
||||
->keyBy('id');
|
||||
$ticketPriorityOptions = $ticket->project->ticketPriorities()
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
/** @var Collection<int, TicketFieldValue> $existingFieldValues */
|
||||
$existingFieldValues = $ticket->fieldValues
|
||||
->keyBy('ticket_field_definition_id');
|
||||
|
||||
$changes = [];
|
||||
$summaryLabels = [];
|
||||
|
||||
$this->trackScalarChange(
|
||||
$changes,
|
||||
$summaryLabels,
|
||||
'title',
|
||||
'title',
|
||||
$ticket->title,
|
||||
$ticketAttributes['title'],
|
||||
);
|
||||
$this->trackScalarChange(
|
||||
$changes,
|
||||
$summaryLabels,
|
||||
'description',
|
||||
'description',
|
||||
$ticket->description,
|
||||
$ticketAttributes['description'],
|
||||
);
|
||||
$this->trackScalarChange(
|
||||
$changes,
|
||||
$summaryLabels,
|
||||
'acceptance_criteria',
|
||||
'acceptance criteria',
|
||||
$ticket->acceptance_criteria,
|
||||
$ticketAttributes['acceptance_criteria'],
|
||||
);
|
||||
|
||||
if ($ticket->ticket_type_id !== $ticketAttributes['ticket_type_id']) {
|
||||
$changes['ticket_type'] = [
|
||||
'from' => $ticket->ticketType?->name,
|
||||
'to' => $ticketTypeOptions->get($ticketAttributes['ticket_type_id'])?->name,
|
||||
];
|
||||
$summaryLabels[] = 'type';
|
||||
}
|
||||
|
||||
if ($ticket->ticket_priority_id !== $ticketAttributes['ticket_priority_id']) {
|
||||
$changes['ticket_priority'] = [
|
||||
'from' => $ticket->ticketPriority?->name,
|
||||
'to' => $ticketPriorityOptions->get($ticketAttributes['ticket_priority_id'])?->name,
|
||||
];
|
||||
$summaryLabels[] = 'priority';
|
||||
}
|
||||
|
||||
$ticket->fill($ticketAttributes);
|
||||
|
||||
if ($ticket->isDirty()) {
|
||||
$ticket->save();
|
||||
}
|
||||
|
||||
foreach ($fieldValueAttributes as $fieldValueAttribute) {
|
||||
$fieldDefinition = $fieldDefinitions->get((int) $fieldValueAttribute['ticket_field_definition_id']);
|
||||
|
||||
if ($fieldDefinition === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$existingFieldValue = $existingFieldValues->get($fieldDefinition->id);
|
||||
|
||||
if ($fieldDefinition->field_type === 'user') {
|
||||
$this->syncUserFieldValue(
|
||||
$changes,
|
||||
$existingFieldValue,
|
||||
$fieldDefinition,
|
||||
$fieldValueAttribute,
|
||||
$summaryLabels,
|
||||
$ticket,
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->syncNumberFieldValue(
|
||||
$changes,
|
||||
$existingFieldValue,
|
||||
$fieldDefinition,
|
||||
$fieldValueAttribute,
|
||||
$summaryLabels,
|
||||
$ticket,
|
||||
);
|
||||
}
|
||||
|
||||
if ($summaryLabels === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->createTicketWorklogEntry->handle(
|
||||
$ticket->fresh(),
|
||||
$actor,
|
||||
'updated',
|
||||
$this->buildSummary($summaryLabels),
|
||||
$changes,
|
||||
);
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $changes
|
||||
* @param array<int, string> $summaryLabels
|
||||
*/
|
||||
private function trackScalarChange(
|
||||
array &$changes,
|
||||
array &$summaryLabels,
|
||||
string $changeKey,
|
||||
string $summaryLabel,
|
||||
?string $currentValue,
|
||||
?string $newValue,
|
||||
): void {
|
||||
if ($currentValue === $newValue) {
|
||||
return;
|
||||
}
|
||||
|
||||
$changes[$changeKey] = [
|
||||
'from' => $currentValue,
|
||||
'to' => $newValue,
|
||||
];
|
||||
$summaryLabels[] = $summaryLabel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $changes
|
||||
* @param array{value_user_id?: int|string|null} $fieldValueAttribute
|
||||
* @param array<int, string> $summaryLabels
|
||||
*/
|
||||
private function syncUserFieldValue(
|
||||
array &$changes,
|
||||
?TicketFieldValue $existingFieldValue,
|
||||
TicketFieldDefinition $fieldDefinition,
|
||||
array $fieldValueAttribute,
|
||||
array &$summaryLabels,
|
||||
Ticket $ticket,
|
||||
): void {
|
||||
$newUserId = $fieldValueAttribute['value_user_id'] ?? null;
|
||||
$newUserId = $newUserId === null || $newUserId === '' ? null : (int) $newUserId;
|
||||
|
||||
if ($existingFieldValue?->value_user_id === $newUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$oldUserName = $existingFieldValue?->userValue?->name;
|
||||
$newUserName = $newUserId === null ? null : User::query()->find($newUserId)?->name;
|
||||
|
||||
$changes['field_values'][$fieldDefinition->id] = [
|
||||
'field' => $fieldDefinition->label,
|
||||
'from' => $oldUserName,
|
||||
'to' => $newUserName,
|
||||
];
|
||||
$summaryLabels[] = "{$fieldDefinition->label} field";
|
||||
|
||||
if ($newUserId === null) {
|
||||
$existingFieldValue?->delete();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$ticket->fieldValues()->updateOrCreate(
|
||||
[
|
||||
'ticket_field_definition_id' => $fieldDefinition->id,
|
||||
],
|
||||
[
|
||||
'value_number' => null,
|
||||
'value_user_id' => $newUserId,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $changes
|
||||
* @param array{value_number?: float|int|string|null} $fieldValueAttribute
|
||||
* @param array<int, string> $summaryLabels
|
||||
*/
|
||||
private function syncNumberFieldValue(
|
||||
array &$changes,
|
||||
?TicketFieldValue $existingFieldValue,
|
||||
TicketFieldDefinition $fieldDefinition,
|
||||
array $fieldValueAttribute,
|
||||
array &$summaryLabels,
|
||||
Ticket $ticket,
|
||||
): void {
|
||||
$newNumber = $fieldValueAttribute['value_number'] ?? null;
|
||||
$newNumber = $newNumber === null || $newNumber === '' ? null : (float) $newNumber;
|
||||
$oldNumber = $existingFieldValue?->value_number;
|
||||
|
||||
if ($oldNumber === $newNumber) {
|
||||
return;
|
||||
}
|
||||
|
||||
$changes['field_values'][$fieldDefinition->id] = [
|
||||
'field' => $fieldDefinition->label,
|
||||
'from' => $this->formatNumber($oldNumber),
|
||||
'to' => $this->formatNumber($newNumber),
|
||||
];
|
||||
$summaryLabels[] = "{$fieldDefinition->label} field";
|
||||
|
||||
if ($newNumber === null) {
|
||||
$existingFieldValue?->delete();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$ticket->fieldValues()->updateOrCreate(
|
||||
[
|
||||
'ticket_field_definition_id' => $fieldDefinition->id,
|
||||
],
|
||||
[
|
||||
'value_number' => $newNumber,
|
||||
'value_user_id' => null,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $summaryLabels
|
||||
*/
|
||||
private function buildSummary(array $summaryLabels): string
|
||||
{
|
||||
$uniqueLabels = collect($summaryLabels)
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
if ($uniqueLabels->count() === 1) {
|
||||
return 'Updated '.$uniqueLabels->first().'.';
|
||||
}
|
||||
|
||||
if ($uniqueLabels->count() === 2) {
|
||||
return 'Updated '.$uniqueLabels->first().' and '.$uniqueLabels->last().'.';
|
||||
}
|
||||
|
||||
$lastLabel = $uniqueLabels->pop();
|
||||
|
||||
return 'Updated '.$uniqueLabels->implode(', ').', and '.$lastLabel.'.';
|
||||
}
|
||||
|
||||
private function formatNumber(?float $value): ?string
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return rtrim(rtrim(number_format($value, 2, '.', ''), '0'), '.');
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ class ProjectBacklogController extends Controller
|
||||
403,
|
||||
);
|
||||
|
||||
$ticket = $this->createProjectTicket->handle($project, $request->validated());
|
||||
$ticket = $this->createProjectTicket->handle($project, $request->validated(), $request->user());
|
||||
|
||||
return to_route('projects.backlog.index', $project)
|
||||
->with('status', "{$ticket->ticket_key} was added to the backlog.");
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\ProjectPermissions\ProjectPermissionService;
|
||||
use App\Actions\Tickets\UpdateProjectTicket;
|
||||
use App\Http\Requests\UpdateProjectTicketRequest;
|
||||
use App\Models\Project;
|
||||
use App\Models\Ticket;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class ProjectTicketController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ProjectPermissionService $projectPermissionService,
|
||||
private readonly UpdateProjectTicket $updateProjectTicket,
|
||||
) {}
|
||||
|
||||
public function show(Request $request, Project $project, Ticket $ticket): Response
|
||||
{
|
||||
abort_unless(
|
||||
$this->projectPermissionService->userCanViewProject($request->user(), $project),
|
||||
403,
|
||||
);
|
||||
|
||||
return Inertia::render('projects/Ticket', [
|
||||
'canEditTicket' => $this->projectPermissionService->userHasPermission($request->user(), $project, 'edit'),
|
||||
'project' => $project,
|
||||
'status' => session('status'),
|
||||
'ticket' => $ticket->load([
|
||||
'fieldValues.ticketFieldDefinition',
|
||||
'fieldValues.userValue',
|
||||
'ticketPriority',
|
||||
'ticketType',
|
||||
'worklogEntries.actor',
|
||||
]),
|
||||
'ticketFieldDefinitions' => $project->ticketFieldDefinitions()->get(),
|
||||
'ticketPriorities' => $project->ticketPriorities()->get(),
|
||||
'ticketTypes' => $project->ticketTypes()->get(),
|
||||
'users' => User::query()
|
||||
->orderBy('name')
|
||||
->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(
|
||||
UpdateProjectTicketRequest $request,
|
||||
Project $project,
|
||||
Ticket $ticket,
|
||||
): RedirectResponse {
|
||||
abort_unless(
|
||||
$this->projectPermissionService->userHasPermission($request->user(), $project, 'edit'),
|
||||
403,
|
||||
);
|
||||
|
||||
$wasUpdated = $this->updateProjectTicket->handle(
|
||||
$ticket,
|
||||
$request->user(),
|
||||
$request->ticketAttributes(),
|
||||
$request->fieldValues(),
|
||||
);
|
||||
|
||||
$statusMessage = $wasUpdated
|
||||
? "{$ticket->ticket_key} was updated."
|
||||
: 'No ticket changes were detected.';
|
||||
|
||||
return to_route('projects.tickets.show', [$project, $ticket])
|
||||
->with('status', $statusMessage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Project;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateProjectTicketRequest 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
|
||||
{
|
||||
/** @var Project $project */
|
||||
$project = $this->route('project');
|
||||
|
||||
return [
|
||||
'acceptance_criteria' => ['nullable', 'string'],
|
||||
'description' => ['nullable', 'string'],
|
||||
'field_values' => ['array'],
|
||||
'field_values.*' => ['array'],
|
||||
'field_values.*.ticket_field_definition_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists('ticket_field_definitions', 'id')->where('project_id', $project->id),
|
||||
],
|
||||
'field_values.*.value_number' => ['nullable', 'numeric'],
|
||||
'field_values.*.value_user_id' => ['nullable', 'integer', Rule::exists('users', 'id')],
|
||||
'ticket_priority_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists('ticket_priorities', 'id')->where('project_id', $project->id),
|
||||
],
|
||||
'ticket_type_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists('ticket_types', 'id')->where('project_id', $project->id),
|
||||
],
|
||||
'title' => ['required', 'string', 'max:255'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* acceptance_criteria: string|null,
|
||||
* description: string|null,
|
||||
* ticket_priority_id: int,
|
||||
* ticket_type_id: int,
|
||||
* title: string
|
||||
* }
|
||||
*/
|
||||
public function ticketAttributes(): array
|
||||
{
|
||||
/** @var array{
|
||||
* acceptance_criteria: string|null,
|
||||
* description: string|null,
|
||||
* ticket_priority_id: int,
|
||||
* ticket_type_id: int,
|
||||
* title: string
|
||||
* } $attributes
|
||||
*/
|
||||
$attributes = Arr::only($this->validated(), [
|
||||
'acceptance_criteria',
|
||||
'description',
|
||||
'ticket_priority_id',
|
||||
'ticket_type_id',
|
||||
'title',
|
||||
]);
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{
|
||||
* ticket_field_definition_id: int,
|
||||
* value_number?: float|int|string|null,
|
||||
* value_user_id?: int|string|null
|
||||
* }>
|
||||
*/
|
||||
public function fieldValues(): array
|
||||
{
|
||||
/** @var array<int, array{
|
||||
* ticket_field_definition_id: int,
|
||||
* value_number?: float|int|string|null,
|
||||
* value_user_id?: int|string|null
|
||||
* }> $fieldValues
|
||||
*/
|
||||
$fieldValues = $this->validated('field_values', []);
|
||||
|
||||
return $fieldValues;
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,12 @@ class Ticket extends Model
|
||||
return $this->belongsTo(BoardColumn::class);
|
||||
}
|
||||
|
||||
public function fieldValues(): HasMany
|
||||
{
|
||||
return $this->hasMany(TicketFieldValue::class)
|
||||
->orderBy('ticket_field_definition_id');
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
|
||||
@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable(['field_type', 'label', 'position', 'project_id'])]
|
||||
class TicketFieldDefinition extends Model
|
||||
@@ -26,4 +27,10 @@ class TicketFieldDefinition extends Model
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
|
||||
public function ticketFieldValues(): HasMany
|
||||
{
|
||||
return $this->hasMany(TicketFieldValue::class)
|
||||
->orderBy('ticket_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\TicketFieldValueFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable([
|
||||
'ticket_field_definition_id',
|
||||
'ticket_id',
|
||||
'value_number',
|
||||
'value_user_id',
|
||||
])]
|
||||
class TicketFieldValue extends Model
|
||||
{
|
||||
/** @use HasFactory<TicketFieldValueFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'value_number' => 'float',
|
||||
];
|
||||
}
|
||||
|
||||
public function ticket(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Ticket::class);
|
||||
}
|
||||
|
||||
public function ticketFieldDefinition(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(TicketFieldDefinition::class);
|
||||
}
|
||||
|
||||
public function userValue(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'value_user_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user