From f13e2a194595f486391046b9858a66c186f7ffdb Mon Sep 17 00:00:00 2001 From: abdul-wahab12345 Date: Tue, 9 Jul 2024 10:58:20 +0000 Subject: [PATCH] chat function --- .gitignore | 1 + app/Helper/helpers.php | 71 ++++++ app/Http/Controllers/Chat/ChatController.php | 112 +++++++++ app/Http/Controllers/InboxController.php | 2 +- app/Models/ChatGroup.php | 19 ++ app/Models/Message.php | 20 ++ ...4_07_09_090116_create_chat_group_table.php | 34 +++ ...2024_07_09_090129_create_message_table.php | 32 +++ error_log | 6 + mailgun.kundesone.no/.htaccess | 8 + resources/views/all-tickets.blade.php | 233 +++++++++++++++++- resources/views/components/header.blade.php | 11 +- resources/views/components/sidebar.blade.php | 17 +- resources/views/inbox.blade.php | 2 +- resources/views/index.blade.php | 4 +- resources/views/layouts/master.blade.php | 66 ++++- resources/views/profile.blade.php | 2 +- resources/views/show-ticket.blade.php | 10 +- resources/views/waiting.blade.php | 4 +- routes/api.php | 7 + 20 files changed, 630 insertions(+), 31 deletions(-) create mode 100644 app/Http/Controllers/Chat/ChatController.php create mode 100644 app/Models/ChatGroup.php create mode 100644 app/Models/Message.php create mode 100644 database/migrations/2024_07_09_090116_create_chat_group_table.php create mode 100644 database/migrations/2024_07_09_090129_create_message_table.php create mode 100644 mailgun.kundesone.no/.htaccess diff --git a/.gitignore b/.gitignore index 7fe978f..88e63b7 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ yarn-error.log /.fleet /.idea /.vscode +/chat.kundesone.no diff --git a/app/Helper/helpers.php b/app/Helper/helpers.php index 0872d9e..3f9a533 100644 --- a/app/Helper/helpers.php +++ b/app/Helper/helpers.php @@ -8,6 +8,15 @@ use Illuminate\Support\Facades\Session; use Mailgun\Mailgun; use App\Models\Company; +use App\Models\ChatGroup; +use App\Models\Message; +use App\Models\CompanyUser; + +function get_company_users($company_id){ + + return CompanyUser::where('company_id', $company_id)->with('user')->get(); + +} function get_company($key,$value){ @@ -206,3 +215,65 @@ function createResponse($ticket_id, $message, $user_id = 0) return $response; } } + + +/** + * Create a new chat group. + * + * @param int $companyId The ID of the company associated with the chat. + * @param int $userId The ID of the user creating the chat group. + * @param string $customerId Identifier for the customer involved in the chat. + * @param string $name Name or title of the chat group. + * @param string $email Email address associated with the chat group. + * @param string $subject Subject or initial topic of the chat group. + * @param string $status Current status of the chat group (e.g., 'active', 'closed'). + * @return ChatGroup The newly created chat group object. + */ + function createChatGroup($companyId, $userId, $customerId, $name, $email, $subject, $status) +{ + return ChatGroup::create([ + 'company_id' => $companyId, + 'user_id' => $userId, + 'customer_id' => $customerId, + 'name' => $name, + 'email' => $email, + 'subject' => $subject, + 'status' => $status + ]); +} + + +/** + * Store a new message in a specified chat group. + * + * @param int $chatId The ID of the chat group. + * @param string $from Sender identifier. + * @param string $to Receiver identifier. + * @param string $message Content of the message. + * @param string $type Type of the message. + * @return Message The newly created message object. + */ + function storeMessage($chatId, $from, $to, $message, $type) +{ + return Message::create([ + 'chat_id' => $chatId, + 'from' => $from, + 'to' => $to, + 'message' => $message, + 'type' => $type + ]); +} + + +/** + * Retrieve all messages from a specified chat group. + * + * @param int $chatId The ID of the chat group. + * @return \Illuminate\Database\Eloquent\Collection A collection of messages. + */ + function getMessagesByChatId($chatId) +{ + return Message::where('chat_id', $chatId)->orderBy('created_at', 'asc')->get(); +} + + diff --git a/app/Http/Controllers/Chat/ChatController.php b/app/Http/Controllers/Chat/ChatController.php new file mode 100644 index 0000000..0e936a2 --- /dev/null +++ b/app/Http/Controllers/Chat/ChatController.php @@ -0,0 +1,112 @@ +all(), [ + 'company_id' => 'required|integer|exists:companies,id', + //'user_id' => 'required|integer|exists:users,id', + 'customer_id' => 'required|string|max:255', + 'name' => 'required|string|max:255', + 'email' => 'required|email|max:255', + 'subject' => 'required|string|max:1000', + //'status' => 'required|string|max:255' + ]); + + if ($validator->fails()) { + return response()->json(['errors' => $validator->errors()], 422); + } + + $company_id = $request->company_id; + + $user_id = $this->select_user($company_id); + + if($user_id){ + + $data = [ + 'company_id' => $company_id, + 'user_id' => $user_id, + 'customer_id' => $request->customer_id, + 'name' => $request->name, + 'email' => $request->email, + 'subject' => $request->subject, + 'status' => 'open', + + ]; + + $chatGroup = ChatGroup::create($data); + + return response()->json(['chat_id' => $chatGroup->id], 200); + + }else{ + return response()->json(['message' => 'user not found'], 400); + } + } + + + + public function sendMessage(Request $request) + { + $validator = Validator::make($request->all(), [ + 'chat_id' => 'required|integer|exists:chat_group,id', + 'from' => 'required|string|max:255', + //'to' => 'required|string|max:255', + 'message' => 'required|string', + 'type' => 'required|string|max:255' + ]); + + if ($validator->fails()) { + return response()->json(['errors' => $validator->errors()], 422); + } + + $data = [ + 'chat_id' => $request->chat_id, + + 'from' => $request->from, + 'to' => $request->from == 'user'?'company':'user', + 'message' => $request->message, + 'type' => $request->type, + + + ]; + + + $message = Message::create($data); + + return response()->json(['message' => 'Message sent successfully', 'data' => $message], 200); + } + + public function select_user($company_id){ + $companyUsers = get_company_users($company_id); + + $selected = false; + + foreach($companyUsers as $user){ + $access = json_decode($user->access); + + if(in_array('chat',$access)){ + $selected = $user->user_id; + } + } + + return $selected; + + } +} diff --git a/app/Http/Controllers/InboxController.php b/app/Http/Controllers/InboxController.php index f038b00..cd541a5 100644 --- a/app/Http/Controllers/InboxController.php +++ b/app/Http/Controllers/InboxController.php @@ -348,7 +348,7 @@ public function fetchActionBox($ticketId) { $selectedCompany = getSelectedCompany(); $ticket = Ticket::where('id', $ticketId)->with('comments')->first(); - $companyUsers = CompanyUser::where('company_id', $selectedCompany)->with('user')->get(); + $companyUsers = get_company_users($selectedCompany);//CompanyUser::where('company_id', $selectedCompany)->with('user')->get(); return view('partials.action-box', compact('ticket','companyUsers'))->render(); } diff --git a/app/Models/ChatGroup.php b/app/Models/ChatGroup.php new file mode 100644 index 0000000..ba82e03 --- /dev/null +++ b/app/Models/ChatGroup.php @@ -0,0 +1,19 @@ +hasMany(Message::class, 'chat_id'); + } +} diff --git a/app/Models/Message.php b/app/Models/Message.php new file mode 100644 index 0000000..81418e0 --- /dev/null +++ b/app/Models/Message.php @@ -0,0 +1,20 @@ +belongsTo(ChatGroup::class, 'chat_id'); + } +} diff --git a/database/migrations/2024_07_09_090116_create_chat_group_table.php b/database/migrations/2024_07_09_090116_create_chat_group_table.php new file mode 100644 index 0000000..a4f62ba --- /dev/null +++ b/database/migrations/2024_07_09_090116_create_chat_group_table.php @@ -0,0 +1,34 @@ +id(); + $table->foreignId('company_id')->constrained('companies')->onDelete('cascade'); + $table->foreignId('user_id')->constrained('users'); + $table->string('customer_id'); + $table->string('name'); + $table->string('email'); + $table->text('subject'); + $table->string('status'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('chat_group'); + } +}; diff --git a/database/migrations/2024_07_09_090129_create_message_table.php b/database/migrations/2024_07_09_090129_create_message_table.php new file mode 100644 index 0000000..a409c74 --- /dev/null +++ b/database/migrations/2024_07_09_090129_create_message_table.php @@ -0,0 +1,32 @@ +id(); + $table->foreignId('chat_id')->constrained('chat_group')->onDelete('cascade'); + $table->string('from'); + $table->string('to'); + $table->text('message'); + $table->string('type'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('message'); + } +}; diff --git a/error_log b/error_log index 9b46dc6..66c58b3 100644 --- a/error_log +++ b/error_log @@ -39,3 +39,9 @@ [30-Jun-2024 04:00:06 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 [30-Jun-2024 04:30:35 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 [01-Jul-2024 18:34:48 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 +[09-Jul-2024 09:01:01 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 +[09-Jul-2024 09:01:29 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 +[09-Jul-2024 09:14:30 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 +[09-Jul-2024 09:14:45 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 +[09-Jul-2024 09:14:56 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 +[09-Jul-2024 09:21:16 UTC] PHP Warning: Module "fileinfo" is already loaded in Unknown on line 0 diff --git a/mailgun.kundesone.no/.htaccess b/mailgun.kundesone.no/.htaccess new file mode 100644 index 0000000..997a9b8 --- /dev/null +++ b/mailgun.kundesone.no/.htaccess @@ -0,0 +1,8 @@ + + +# php -- BEGIN cPanel-generated handler, do not edit +# Set the “ea-php81” package as the default “PHP” programming language. + + AddHandler application/x-httpd-ea-php81 .php .php8 .phtml + +# php -- END cPanel-generated handler, do not edit diff --git a/resources/views/all-tickets.blade.php b/resources/views/all-tickets.blade.php index 7a4183d..07d979a 100644 --- a/resources/views/all-tickets.blade.php +++ b/resources/views/all-tickets.blade.php @@ -5,14 +5,168 @@ @section('content') + +
@@ -29,7 +183,23 @@
- + +
+
+
+ +
+ + + + + + + + + +
+
    @@ -55,8 +225,8 @@ class="chat-status-icon rounded-circle text-center align-content-center position
-

{{ $ticket->subject }}

-

- {{ $ticket->content }}

+

{{ $ticket->sender_name }}

+

- {{\Illuminate\Support\Str::limit($ticket->subject, 90)}}

{{\Carbon\Carbon::parse($ticket->created_at)->format('h:i A')}}

@@ -70,5 +240,52 @@ class="chat-status-icon rounded-circle text-center align-content-center position
+ + + + + + +@endsection -@endsection \ No newline at end of file diff --git a/resources/views/components/header.blade.php b/resources/views/components/header.blade.php index 566b10f..af60e19 100644 --- a/resources/views/components/header.blade.php +++ b/resources/views/components/header.blade.php @@ -97,4 +97,13 @@ class="ui secondary basic button shadow-none setting-btn text-white align-conten - \ No newline at end of file + + + + diff --git a/resources/views/components/sidebar.blade.php b/resources/views/components/sidebar.blade.php index bb5cda5..7ce5c35 100644 --- a/resources/views/components/sidebar.blade.php +++ b/resources/views/components/sidebar.blade.php @@ -9,6 +9,13 @@ .side-bar-links ul li .bg-light-color .color-light{ color: #383f33 !important; } + .sidebar_icon{ + font-size: 20px !important; + color: white !important; + } + .bg-light-color .sidebar_icon{ + color: #383F33 !important; +} @@ -263,6 +267,52 @@ + + + + + \ No newline at end of file diff --git a/resources/views/profile.blade.php b/resources/views/profile.blade.php index 9a90c32..b6e4b41 100644 --- a/resources/views/profile.blade.php +++ b/resources/views/profile.blade.php @@ -31,7 +31,7 @@ -