feat: create PostCategoryFactory for generating post category data feat: create PostFactory for generating post data feat: add retry_requested_at column to services table feat: add vcenter provisioning fields to products table feat: add module_host_id to os table feat: create email_notification_templates table for email templates feat: add scenario and format fields to template table feat: add admin fields to transactions table fix: update ticket table columns and types feat: add uuid to ticket table and backfill existing records fix: make ticket_reply user_id nullable feat: create notifications table for user notifications fix: make ticket uuid nullable feat: create post_categories table for blog categories feat: create posts table for blog posts feat: add invoice email template for PDF generation
165 lines
4.9 KiB
PHP
165 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Ticket;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Events\TicketReplyCreated;
|
|
use App\Http\Resources\Api\Ticket\TicketResource;
|
|
use App\Http\Resources\Api\Ticket\TicketReplyResource;
|
|
use App\Models\Notification;
|
|
use App\Models\Ticket;
|
|
use App\Repositories\Ticket\TicketRepositoryInterface;
|
|
use App\Repositories\TicketReply\TicketReplyRepositoryInterface;
|
|
use Illuminate\Http\Request;
|
|
|
|
class TicketController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected TicketRepositoryInterface $ticketRepository,
|
|
protected TicketReplyRepositoryInterface $ticketReplyRepository,
|
|
) {}
|
|
|
|
/**
|
|
* List all tickets (paginated, filterable by status / customer_id / search).
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$query = Ticket::query()
|
|
->with(['customer'])
|
|
->latest('id');
|
|
|
|
if ($request->filled('status')) {
|
|
$query->where('status', $request->input('status'));
|
|
}
|
|
|
|
if ($request->filled('customer_id')) {
|
|
$query->where('customer_id', $request->input('customer_id'));
|
|
}
|
|
|
|
if ($request->filled('search')) {
|
|
$query->where('subject', 'like', '%' . $request->input('search') . '%');
|
|
}
|
|
|
|
$paginated = $query->paginate($request->input('per_page', 15));
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => [
|
|
'items' => TicketResource::collection($paginated->items()),
|
|
'total' => $paginated->total(),
|
|
'per_page' => $paginated->perPage(),
|
|
'current_page' => $paginated->currentPage(),
|
|
'last_page' => $paginated->lastPage(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show a ticket with its replies and customer info.
|
|
*/
|
|
public function show(string $ticket_num)
|
|
{
|
|
$ticket = Ticket::query()
|
|
->with(['reply.user', 'customer'])
|
|
->where('ticket_num', $ticket_num)
|
|
->first();
|
|
|
|
if (!$ticket) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Ticket không tồn tại.',
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => new TicketResource($ticket),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update ticket status.
|
|
*/
|
|
public function updateStatus(Request $request, string $ticket_num)
|
|
{
|
|
$request->validate([
|
|
'status' => ['required', 'string', 'in:open,pending,resolved,closed'],
|
|
]);
|
|
|
|
$ticket = Ticket::query()->where('ticket_num', $ticket_num)->first();
|
|
|
|
if (!$ticket) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Ticket không tồn tại.',
|
|
], 404);
|
|
}
|
|
|
|
$ticket->update(['status' => $request->input('status')]);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Trạng thái ticket đã được cập nhật.',
|
|
'data' => new TicketResource($ticket->fresh()),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Admin replies to a ticket.
|
|
*/
|
|
public function reply(Request $request, string $ticket_num)
|
|
{
|
|
$request->validate([
|
|
'message' => ['required', 'string', 'max:2000'],
|
|
]);
|
|
|
|
$admin = auth('admin')->user();
|
|
|
|
$ticket = Ticket::query()->where('ticket_num', $ticket_num)->first();
|
|
|
|
if (!$ticket) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Ticket không tồn tại.',
|
|
], 404);
|
|
}
|
|
|
|
$reply = $this->ticketReplyRepository->create([
|
|
'ticket_id' => $ticket->id,
|
|
'user_id' => $admin->id,
|
|
'message' => $request->input('message'),
|
|
'customer_id' => $ticket->customer_id,
|
|
]);
|
|
|
|
Notification::notifyCustomer($ticket->customer, [
|
|
'type' => 'ticket_reply',
|
|
'title' => 'Admin vừa phản hồi ticket của bạn',
|
|
'body' => $ticket->ticket_num . ' - ' . $ticket->subject,
|
|
'link' => '/tickets/' . $ticket->ticket_num,
|
|
'data' => [
|
|
'ticket_num' => $ticket->ticket_num,
|
|
'reply_id' => $reply->id,
|
|
],
|
|
]);
|
|
|
|
// Update ticket status to pending after admin reply
|
|
if ($ticket->status === 'open') {
|
|
$ticket->update(['status' => 'pending']);
|
|
}
|
|
|
|
$reply->load('user');
|
|
|
|
broadcast(new TicketReplyCreated(
|
|
ticketNum: $ticket->ticket_num,
|
|
customerId: $ticket->customer_id,
|
|
replyId: $reply->id,
|
|
));
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Phản hồi đã được gửi.',
|
|
'data' => new TicketReplyResource($reply),
|
|
], 201);
|
|
}
|
|
}
|