Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions app/Domain/Tickets/Services/Tickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Leantime\Domain\Projects\Services\Projects as ProjectService;
use Leantime\Domain\Setting\Repositories\Setting as SettingRepository;
use Leantime\Domain\Sprints\Services\Sprints as SprintService;
use Leantime\Domain\Tickets\Models\TicketHistoryModel;
use Leantime\Domain\Tickets\Models\Tickets as TicketModel;
use Leantime\Domain\Tickets\Repositories\TicketHistory;
use Leantime\Domain\Tickets\Repositories\Tickets as TicketRepository;
Expand Down Expand Up @@ -2044,6 +2045,37 @@ private function logPatchChanges($ticketId, $oldValues, $newParams, $oldTicket)
}
}

/**
* Log a kanban status change to the ticket activity history.
*/
private function logStatusChangeActivity(
int $ticketId,
int $oldStatus,
int $newStatus,
int $projectId
): void {
try {
$ticketHistoryModel = app()->make(TicketHistoryModel::class);
$statusLabels = $this->getStatusLabels($projectId);
$currentUserName = session('userdata.name') ?? 'Unknown User';

$oldStatusText = $statusLabels[$oldStatus]['name'] ?? (string) $oldStatus;
$newStatusText = $statusLabels[$newStatus]['name'] ?? (string) $newStatus;

$ticketHistoryModel->addStatusChange(
$ticketId,
$oldStatus,
$newStatus,
$oldStatusText,
$newStatusText,
$currentUserName,
'status-select'
);
} catch (\Exception $e) {
Log::error($e);
}
}

/**
* moveTicket - Moves a ticket from one project to another. Milestone children will be moved as well
*
Expand Down Expand Up @@ -2201,6 +2233,20 @@ public function updateTicketSorting($params)
*/
public function updateTicketStatusAndSorting($params, $handler = null): bool
{
$handlerTicketId = null;
$oldStatus = null;
$projectId = null;

if (is_string($handler) && preg_match('/ticket_(\d+)/', $handler, $matches)) {
$handlerTicketId = (int) $matches[1];
$ticketBeforeMove = $this->getTicket($handlerTicketId);

if ($ticketBeforeMove) {
$oldStatus = (int) $ticketBeforeMove->status;
$projectId = (int) $ticketBeforeMove->projectId;
}
}

foreach ($params as $status => $ticketList) {
if (is_numeric($status) && ! empty($ticketList)) {
$parsedTicketList = [];
Expand All @@ -2224,6 +2270,18 @@ public function updateTicketStatusAndSorting($params, $handler = null): bool
}
}

if ($handlerTicketId !== null && $oldStatus !== null && $projectId !== null) {
$ticketAfterMove = $this->getTicket($handlerTicketId);

if ($ticketAfterMove) {
$newStatus = (int) $ticketAfterMove->status;

if ($oldStatus !== $newStatus) {
$this->logStatusChangeActivity($handlerTicketId, $oldStatus, $newStatus, $projectId);
}
}
}

self::dispatchEvent('ticket_updated');

return true;
Expand Down