71 lines
2.1 KiB
PHP
71 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Dashboard\Ticketing\Http\Controllers;
|
|
|
|
use Dashboard\Ticketing\Models\Ticket;
|
|
use Dashboard\Ticketing\Models\TicketMessage;
|
|
use Dashboard\Ticketing\Models\TicketingAgentAccess;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class TicketMessageController extends Controller
|
|
{
|
|
private function isAgent(int $groupId): bool
|
|
{
|
|
return TicketingAgentAccess::where('user_id', Auth::id())
|
|
->where('group_id', $groupId)
|
|
->exists();
|
|
}
|
|
|
|
public function store(Request $request, Ticket $ticket)
|
|
{
|
|
$user = Auth::user();
|
|
$isAgent = $this->isAgent($ticket->group_id);
|
|
$isSubmitter = $ticket->submitter_id === $user->id;
|
|
|
|
if (!$isAgent && !$isSubmitter) {
|
|
abort(403);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'body' => 'required|string',
|
|
'is_internal' => 'boolean',
|
|
]);
|
|
|
|
// Only agents can post internal notes
|
|
if ($validated['is_internal'] ?? false) {
|
|
if (!$isAgent) {
|
|
abort(403, 'Only agents can post internal notes.');
|
|
}
|
|
}
|
|
|
|
$message = TicketMessage::create([
|
|
'ticket_id' => $ticket->id,
|
|
'user_id' => $user->id,
|
|
'body' => $validated['body'],
|
|
'is_internal' => $validated['is_internal'] ?? false,
|
|
'source' => 'web',
|
|
]);
|
|
|
|
// Submitter reply reopens resolved/closed tickets
|
|
if ($isSubmitter && in_array($ticket->status, ['resolved', 'closed'])) {
|
|
$ticket->update(['status' => 'open']);
|
|
}
|
|
|
|
// Notify submitter if agent replied (not internal)
|
|
if ($isAgent && !($validated['is_internal'] ?? false)) {
|
|
$this->notifySubmitter($ticket, $message);
|
|
}
|
|
|
|
return back()->with('success', 'Message sent.');
|
|
}
|
|
|
|
private function notifySubmitter(Ticket $ticket, TicketMessage $message): void
|
|
{
|
|
// Placeholder for email notification
|
|
// In production: Mail::to($submitterEmail)->send(new TicketReplyMail($ticket, $message));
|
|
}
|
|
}
|