126 lines
4.2 KiB
PHP
126 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Ticket;
|
|
use App\Models\TicketMessage;
|
|
use App\Traits\BaseApiResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class TicketApiController extends Controller
|
|
{
|
|
use BaseApiResponse;
|
|
|
|
public function index()
|
|
{
|
|
$tickets = Ticket::where('user_id', auth()->id())
|
|
->orderBy('updated_at', 'desc')
|
|
->get()
|
|
->map(function ($ticket) {
|
|
return [
|
|
'id' => $ticket->id,
|
|
'subject' => $ticket->subject,
|
|
'status' => $ticket->status,
|
|
'priority' => $ticket->priority,
|
|
'created_at' => $ticket->created_at,
|
|
'updated_at' => $ticket->updated_at,
|
|
];
|
|
});
|
|
|
|
return $this->success($tickets, 'موفق', 'لیست تیکتها با موفقیت دریافت شد');
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$ticket = Ticket::with(['messages' => function ($query) {
|
|
$query->orderBy('created_at', 'asc');
|
|
}])->where('user_id', auth()->id())->find($id);
|
|
|
|
if (!$ticket) {
|
|
return $this->failed([], 'یافت نشد', 'تیکت مورد نظر یافت نشد.', 404);
|
|
}
|
|
|
|
$formattedTicket = [
|
|
'id' => $ticket->id,
|
|
'subject' => $ticket->subject,
|
|
'status' => $ticket->status,
|
|
'priority' => $ticket->priority,
|
|
'created_at' => $ticket->created_at,
|
|
'messages' => $ticket->messages->map(function ($msg) {
|
|
return [
|
|
'id' => $msg->id,
|
|
// If user_id matches the logged in user, it's their message. Otherwise, it's admin's reply.
|
|
'is_me' => $msg->user_id === auth()->id(),
|
|
'message' => $msg->message,
|
|
'attachment_url' => $msg->attachment_path ? asset('storage/' . $msg->attachment_path) : null,
|
|
'created_at' => $msg->created_at,
|
|
];
|
|
}),
|
|
];
|
|
|
|
return $this->success($formattedTicket, 'موفق', 'جزئیات تیکت دریافت شد');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'subject' => 'required|string|max:255',
|
|
'priority' => 'nullable|in:low,medium,high',
|
|
'message' => 'required|string',
|
|
'attachment' => 'nullable|file|mimes:jpg,jpeg,png,pdf|max:5120', // Max 5MB
|
|
]);
|
|
|
|
$ticket = Ticket::create([
|
|
'user_id' => auth()->id(),
|
|
'subject' => $validated['subject'],
|
|
'status' => 'open',
|
|
'priority' => $validated['priority'] ?? 'medium',
|
|
]);
|
|
|
|
$attachmentPath = null;
|
|
if ($request->hasFile('attachment')) {
|
|
$attachmentPath = $request->file('attachment')->store('tickets', 'public');
|
|
}
|
|
|
|
TicketMessage::create([
|
|
'ticket_id' => $ticket->id,
|
|
'user_id' => auth()->id(),
|
|
'message' => $validated['message'],
|
|
'attachment_path' => $attachmentPath,
|
|
]);
|
|
|
|
return $this->success(['ticket_id' => $ticket->id], 'موفق', 'تیکت شما با موفقیت ثبت شد');
|
|
}
|
|
|
|
public function reply(Request $request, $id)
|
|
{
|
|
$validated = $request->validate([
|
|
'message' => 'required|string',
|
|
'attachment' => 'nullable|file|mimes:jpg,jpeg,png,pdf|max:5120',
|
|
]);
|
|
|
|
$ticket = Ticket::where('user_id', auth()->id())->find($id);
|
|
|
|
if (!$ticket) {
|
|
return $this->failed([], 'خطا', 'تیکت یافت نشد', 404);
|
|
}
|
|
|
|
$attachmentPath = null;
|
|
if ($request->hasFile('attachment')) {
|
|
$attachmentPath = $request->file('attachment')->store('tickets', 'public');
|
|
}
|
|
|
|
TicketMessage::create([
|
|
'ticket_id' => $ticket->id,
|
|
'user_id' => auth()->id(),
|
|
'message' => $validated['message'],
|
|
'attachment_path' => $attachmentPath,
|
|
]);
|
|
|
|
$ticket->update(['status' => 'user_reply']);
|
|
|
|
return $this->success([], 'موفق', 'پاسخ شما با موفقیت ثبت شد');
|
|
}
|
|
}
|