| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Notifications; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Agent; |
| 6 |
use FluentSupport\App\Models\Ticket; |
| 7 |
|
| 8 |
class RecipientResolver |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @param array $agentIds |
| 12 |
* @param int|null $excludePersonId |
| 13 |
* @return \FluentSupport\Framework\Support\Collection |
| 14 |
*/ |
| 15 |
public function resolveMentionedAgents(array $agentIds, $excludePersonId = null) |
| 16 |
{ |
| 17 |
$agentIds = array_values(array_unique(array_filter(array_map('intval', $agentIds)))); |
| 18 |
|
| 19 |
if (!$agentIds) { |
| 20 |
return Agent::whereIn('id', [0])->get(); |
| 21 |
} |
| 22 |
|
| 23 |
$query = Agent::whereIn('id', $agentIds); |
| 24 |
|
| 25 |
if ($excludePersonId) { |
| 26 |
$query->where('id', '!=', (int) $excludePersonId); |
| 27 |
} |
| 28 |
|
| 29 |
return $query->get(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Resolve a ticket's assigned agent, optionally excluding the actor. |
| 34 |
* |
| 35 |
* @param \FluentSupport\App\Models\Ticket $ticket |
| 36 |
* @param int|null $excludePersonId |
| 37 |
* @return \FluentSupport\App\Models\Agent|null |
| 38 |
*/ |
| 39 |
public function resolveAssignedAgent(Ticket $ticket, $excludePersonId = null) |
| 40 |
{ |
| 41 |
if (!$ticket->agent_id) { |
| 42 |
return null; |
| 43 |
} |
| 44 |
|
| 45 |
$query = Agent::where('id', $ticket->agent_id); |
| 46 |
|
| 47 |
if ($excludePersonId) { |
| 48 |
$query->where('id', '!=', $excludePersonId); |
| 49 |
} |
| 50 |
|
| 51 |
return $query->first(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Normalize recipient person IDs for notification fanout. |
| 56 |
* |
| 57 |
* @param iterable $recipients |
| 58 |
* @param int|null $excludePersonId |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public function extractRecipientPersonIds($recipients, $excludePersonId = null) |
| 62 |
{ |
| 63 |
$ids = []; |
| 64 |
|
| 65 |
foreach ($recipients as $recipient) { |
| 66 |
if (!$recipient || empty($recipient->id)) { |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
$recipientId = (int) $recipient->id; |
| 71 |
|
| 72 |
if ($excludePersonId && $recipientId === (int) $excludePersonId) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
$ids[] = $recipientId; |
| 77 |
} |
| 78 |
|
| 79 |
return array_values(array_unique($ids)); |
| 80 |
} |
| 81 |
} |
| 82 |
|