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::with('admin')->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 == 'admin'){ $users = User::with(['permissions' => function($q) { $q->whereIn("name", ["tickets"]); }])->get(); Notification::send($users, new TicketMessage()); } $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 == 'client'){ $account = Account::with('clients')->find($ticket->client_id); Notification::send($account->clients()->where('is_suspended', 0)->get(), new TicketMessage()); } $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')); $category = Category::find($ticket->catgeory_id); $client = $request->user(); $ticket['client_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" => function($q) use ($category){ $q->whereIn("name", ["tickets"])->whereIn("name", ["ticket-category-" . $category->name]); }])->get(); Notification::send($users, new TicketPosted()); return TicketResource::make($ticket); } public function createAppTicket(Request $request){ $ticket = new Ticket($request->only('content', 'title','category_id', 'account_id', 'application_id')); $ticket['status_id'] = 1; $ticket['client_id'] = $request->account_id; $ticket['last_sender'] = 'admin'; $ticket->save(); $not_suspended_clients = Client::where('is_suspended', 0)->where('account_id', $request->account_id)->get(); Notification::send($not_suspended_clients, new TicketPosted()); return redirect(route('chat',['ticket_id' => $ticket->id])); } public function createBrokerAppTicket(Request $request){ $ticket = new Ticket($request->only('content', 'title','category_id', 'account_id', 'broker_application_id')); $ticket['status_id'] = 1; $ticket['client_id'] = $request->account_id; $ticket['last_sender'] = 'admin'; $ticket->save(); $not_suspended_clients = Client::where('is_suspended', 0)->where('account_id', $request->account_id)->get(); Notification::send($not_suspended_clients, new TicketPosted()); 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; } }