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.
+
{{ $ticket->subject }}
- +{{ $ticket->sender_name }}
+