187 lines
6.5 KiB
PHP
Executable File
187 lines
6.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\API\TicketRequest;
|
|
use App\Http\Requests\API\MessageRequest;
|
|
use App\Http\Resources\MessageResource;
|
|
use App\Http\Resources\TicketResource;
|
|
use App\Models\Account;
|
|
use App\Models\Business;
|
|
use App\Models\Client;
|
|
use App\Models\Company;
|
|
use App\Models\Message;
|
|
use App\Models\Status;
|
|
use App\Models\Ticket;
|
|
use App\Models\User;
|
|
use App\Notifications\NewTicket;
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
class TicketController extends Controller
|
|
{
|
|
|
|
public function chat(Request $request)
|
|
{
|
|
$ticket = Ticket::with(['account.profile','category'])->find($request->ticket_id);
|
|
//todo send email to admin
|
|
return view('admin.messages',[
|
|
'ticket' => $ticket
|
|
]);
|
|
}
|
|
|
|
public function getTicketMessages(Request $request)
|
|
{
|
|
if(Ticket::find($request->ticket_id))
|
|
{
|
|
$messages = Message::where('ticket_id', $request->ticket_id)
|
|
->orderBy('id', 'asc')
|
|
->paginate($request->per_page ?? 10);
|
|
|
|
return MessageResource::collection($messages);
|
|
}
|
|
|
|
return response()->json([
|
|
"message" => trans('app.ticket.not_found', ['id' => $request->ticket_id]),
|
|
"errors" => [
|
|
"ticket_id" => [trans('app.ticket.not_found', ['id' => $request->ticket_id])
|
|
]
|
|
]
|
|
], 404);
|
|
}
|
|
|
|
public function postMessage(MessageRequest $request)
|
|
{
|
|
try{
|
|
$message = new Message($request->only(['ticket_id', 'content']));
|
|
$message['is_client'] = true;
|
|
$message['client_id'] = $request->user()->id;
|
|
$message->save();
|
|
$ticket = Ticket::find($request->ticket_id);
|
|
if($ticket->last_sender == 'client'){
|
|
$users = User::with('permissions')->whereHas("permissions", function($q) {
|
|
$q->whereIn("name", ["tickets"]);
|
|
})->get();
|
|
Notification::send($users, new NewTicket());
|
|
}
|
|
$ticket['last_sender'] = 'client';
|
|
$ticket->save();
|
|
|
|
//todo send notification email to admin
|
|
return MessageResource::make($message);
|
|
}
|
|
catch(\Exception $e){
|
|
return $e->getMessage();
|
|
}
|
|
}
|
|
|
|
public function postMessageAdmin(MessageRequest $request)
|
|
{
|
|
try{
|
|
$message = new Message($request->only(['ticket_id', 'content', 'admin_id', 'status_id']));
|
|
$message['is_client'] = false;
|
|
$message->save();
|
|
$ticket = Ticket::find($request->ticket_id);
|
|
if($ticket->last_sender == 'admin'){
|
|
$user = Client::find($ticket->client_id);
|
|
$user->notify(new NewTicket());
|
|
}
|
|
$ticket['last_sender'] = 'admin';
|
|
$ticket->save();
|
|
return MessageResource::make($message);
|
|
}
|
|
catch(\Exception $e){
|
|
return $e->getMessage();
|
|
}
|
|
}
|
|
|
|
public function getTickets(Request $request)
|
|
{
|
|
$client = $request->user();
|
|
$account = Account::find($client->account_id);
|
|
$tickets = Ticket::with('status')->where('client_id', $account->id)
|
|
->orderBy('created_at', 'desc')->paginate($request->per_page ?? 10);
|
|
|
|
return TicketResource::collection($tickets);
|
|
}
|
|
|
|
public function postTicket(TicketRequest $request)
|
|
{
|
|
$ticket = new Ticket($request->only('content', 'title','category_id'));
|
|
$client = $request->user();
|
|
$ticket['client_id'] = $client->id;
|
|
$ticket['account_id'] = $client->account_id;
|
|
$status = Status::where('name', 'Open')->firstOrFail();
|
|
$ticket['status_id'] = $status->id;
|
|
$ticket['last_sender'] = 'client';
|
|
$ticket->save();
|
|
$users = User::with('permissions')->whereHas("permissions", function($q) {
|
|
$q->whereIn("name", ["tickets"]);
|
|
})->get();
|
|
Notification::send($users, new NewTicket());
|
|
return TicketResource::make($ticket);
|
|
}
|
|
|
|
|
|
public function createAppTicket(Request $request){
|
|
$ticket = new Ticket($request->only('content', 'title','category_id', 'client_id', 'account_id', 'application_id'));
|
|
$ticket['status_id'] = 1;
|
|
$ticket->save();
|
|
$not_suspended_clients = Client::where('is_suspended', 0)->where('account_id', $request->account_id)->get();
|
|
Notification::send($not_suspended_clients, new NewTicket());
|
|
return redirect(route('chat',['ticket_id' => $ticket->id]));
|
|
}
|
|
|
|
public function accountForTicketsAjax(Request $request){
|
|
$search_term = $request->input('q');
|
|
|
|
if ($search_term)
|
|
{
|
|
$accounts = Account::whereHasMorph('profile',
|
|
[Business::class, Company::class],
|
|
function (Builder $query) use ($search_term) {
|
|
$query->where('name', 'like', '%' . $search_term . '%');
|
|
})->paginate(10);
|
|
$accounts_array = Account::whereHasMorph('profile',
|
|
[Business::class, Company::class],
|
|
function (Builder $query) use ($search_term) {
|
|
$query->where('name', 'like', '%' . $search_term . '%');
|
|
})->paginate(10)->toArray();
|
|
$array = [];
|
|
foreach($accounts as $account){
|
|
if($account->type == 'company'){
|
|
$array_item['name'] = $account->profile->name;
|
|
}
|
|
else{
|
|
$array_item['name'] = $account->profile->name . ' ' .$account->profile->surname;
|
|
}
|
|
$array_item['id'] = $account['id'];
|
|
array_push($array, $array_item);
|
|
}
|
|
$accounts_array['data'] = $array;
|
|
}
|
|
else
|
|
{
|
|
$accounts = Account::paginate(10);
|
|
$accounts_array = Account::paginate(10)->toArray();
|
|
$array = [];
|
|
foreach($accounts as $account){
|
|
if($account->type == 'company'){
|
|
$array_item['name'] = $account->profile->name;
|
|
}
|
|
else{
|
|
$array_item['name'] = $account->profile->name . ' ' . $account->profile->surname;
|
|
}
|
|
$array_item['id'] = $account['id'];
|
|
array_push($array, $array_item);
|
|
}
|
|
$accounts_array['data'] = $array;
|
|
}
|
|
|
|
return $accounts_array;
|
|
}
|
|
|
|
}
|