diff --git a/modules/helpdesk/lib/Module/Helpdesk/Service/TicketCommunicationService.php b/modules/helpdesk/lib/Module/Helpdesk/Service/TicketCommunicationService.php index 34c4fc0..0f6ea49 100644 --- a/modules/helpdesk/lib/Module/Helpdesk/Service/TicketCommunicationService.php +++ b/modules/helpdesk/lib/Module/Helpdesk/Service/TicketCommunicationService.php @@ -933,7 +933,8 @@ class TicketCommunicationService return []; } - $result = []; + // Collect all unique support actor codes + $codes = []; foreach ([...$odataEntries, ...$soapEntries] as $entry) { $actorRole = trim((string) ($entry['actor_role'] ?? '')); if ($actorRole !== 'support') { @@ -941,11 +942,45 @@ class TicketCommunicationService } $actor = strtoupper(trim((string) ($entry['actor'] ?? ''))); if ($actor !== '' && !$this->isContactActorId($actor) && mb_strlen($actor) <= 10) { - $result[$actor] = $supportUserName; + $codes[$actor] = true; } } - return $result; + if ($codes === []) { + return []; + } + + // If only one support code exists, it must be the current user + if (count($codes) === 1) { + return [array_keys($codes)[0] => $supportUserName]; + } + + // Multiple codes (ticket was reassigned): find the code that appears + // in the most recent entry — that's most likely the current assignee. + // Only map that code; leave older codes as abbreviations. + $latestCode = null; + $latestTs = ''; + foreach ([...$odataEntries, ...$soapEntries] as $entry) { + $actorRole = trim((string) ($entry['actor_role'] ?? '')); + if ($actorRole !== 'support') { + continue; + } + $actor = strtoupper(trim((string) ($entry['actor'] ?? ''))); + if (!isset($codes[$actor])) { + continue; + } + $ts = trim((string) ($entry['timestamp_iso'] ?? '')); + if ($ts > $latestTs) { + $latestTs = $ts; + $latestCode = $actor; + } + } + + if ($latestCode !== null) { + return [$latestCode => $supportUserName]; + } + + return []; } /** @@ -965,11 +1000,11 @@ class TicketCommunicationService return []; } - // Map support actor codes to names using ticket assignment data. - // For each entry with a support role, look up the ticket's Support_User_Name - // and map the actor code to that name. This handles tickets with multiple - // support codes (e.g. after reassignment) robustly. - $result = []; + // Group support actor codes per ticket with their latest timestamp. + // For tickets with a single code, map it directly. For tickets with + // multiple codes (reassigned), only map the most recent one. + /** @var array> $codesByTicket ticket_no → [CODE → latest_ts] */ + $codesByTicket = []; foreach ($entries as $entry) { $actorRole = trim((string) ($entry['actor_role'] ?? '')); if ($actorRole !== 'support') { @@ -983,9 +1018,35 @@ class TicketCommunicationService if ($ticketNo === '') { continue; } + $ts = trim((string) ($entry['timestamp_iso'] ?? '')); + if (!isset($codesByTicket[$ticketNo][$actor]) || $ts > $codesByTicket[$ticketNo][$actor]) { + $codesByTicket[$ticketNo][$actor] = $ts; + } + } + + $result = []; + foreach ($codesByTicket as $ticketNo => $codes) { $supportUserName = $supportUserNamesByTicket[$ticketNo] ?? ''; - if ($supportUserName !== '' && !isset($result[$actor])) { - $result[$actor] = $supportUserName; + if ($supportUserName === '') { + continue; + } + + if (count($codes) === 1) { + $result[array_keys($codes)[0]] = $supportUserName; + continue; + } + + // Multiple codes: map only the most recent one + $latestCode = null; + $latestTs = ''; + foreach ($codes as $code => $ts) { + if ($ts > $latestTs) { + $latestTs = $ts; + $latestCode = $code; + } + } + if ($latestCode !== null && !isset($result[$latestCode])) { + $result[$latestCode] = $supportUserName; } }