From aa2772c75f49da95b5ac747d73f8e3aa4d6e6bfb Mon Sep 17 00:00:00 2001 From: Abdelrahman Saeed Elhassan Yousif Date: Tue, 22 Oct 2024 10:41:04 +0200 Subject: [PATCH] Adding group created by id --- ...0003_create_filachat_groups_table.php.stub | 1 + src/Services/ChatListService.php | 34 +++++++++++-------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/database/migrations/00003_create_filachat_groups_table.php.stub b/database/migrations/00003_create_filachat_groups_table.php.stub index bde4dff..f21d7cd 100644 --- a/database/migrations/00003_create_filachat_groups_table.php.stub +++ b/database/migrations/00003_create_filachat_groups_table.php.stub @@ -11,6 +11,7 @@ return new class extends Migration Schema::create('filachat_groups', function (Blueprint $table) { $table->id(); $table->text('name'); + $table->unsignedBigInteger('created_by'); $table->softDeletes(); $table->timestamps(); }); diff --git a/src/Services/ChatListService.php b/src/Services/ChatListService.php index 15ec6ca..c25b13e 100644 --- a/src/Services/ChatListService.php +++ b/src/Services/ChatListService.php @@ -179,9 +179,13 @@ public function createConversation(array $data): void try { DB::transaction(function () use ($data) { + $senderableId = auth()->id(); + $senderableType = auth()->user()::class; + if ($data['type'] === 'group') { $group = FilaChatGroup::query()->create([ + 'created_by' => $senderableId, 'name' => $data['group_name'], ]); @@ -194,7 +198,7 @@ public function createConversation(array $data): void $receiverableId = $group->id; $receiverableType = FilaChatGroup::class; $conversation = FilaChatConversation::query()->create([ - 'senderable_id' => auth()->id(), + 'senderable_id' => $senderableId, 'senderable_type' => auth()->user()::class, 'receiverable_id' => $receiverableId, 'receiverable_type' => $receiverableType, @@ -212,31 +216,31 @@ public function createConversation(array $data): void $receiverableId = (int)$matches[1]; } $foundConversation = FilaChatConversation::query() - ->where(function ($query) use ($receiverableId, $receiverableType) { - $query->where(function ($query) { - $query->where('senderable_id', auth()->id()) - ->where('senderable_type', auth()->user()::class); + ->where(function ($query) use ($receiverableId, $receiverableType, $senderableId, $senderableType) { + $query->where(function ($query) use ($senderableId, $senderableType) { + $query->where('senderable_id', $senderableId) + ->where('senderable_type', $senderableType); }) ->orWhere(function ($query) use ($receiverableId, $receiverableType) { $query->where('senderable_id', $receiverableId) ->where('senderable_type', $receiverableType); }); }) - ->where(function ($query) use ($receiverableId, $receiverableType) { + ->where(function ($query) use ($receiverableId, $receiverableType, $senderableId, $senderableType) { $query->where(function ($query) use ($receiverableId, $receiverableType) { $query->where('receiverable_id', $receiverableId) ->where('receiverable_type', $receiverableType); }) - ->orWhere(function ($query) { - $query->where('receiverable_id', auth()->id()) - ->where('receiverable_type', auth()->user()::class); + ->orWhere(function ($query) use ($senderableId, $senderableType) { + $query->where('receiverable_id', $senderableId) + ->where('receiverable_type', $senderableType); }); }) ->first(); if (!$foundConversation) { $conversation = FilaChatConversation::query()->create([ - 'senderable_id' => auth()->id(), - 'senderable_type' => auth()->user()::class, + 'senderable_id' => $senderableId, + 'senderable_type' => $senderableType, 'receiverable_id' => $receiverableId, 'receiverable_type' => $receiverableType, ]); @@ -247,8 +251,8 @@ public function createConversation(array $data): void $message = FilaChatMessage::query()->create([ 'filachat_conversation_id' => $conversation->id, - 'senderable_id' => auth()->id(), - 'senderable_type' => auth()->user()::class, + 'senderable_id' => $senderableId, + 'senderable_type' => $senderableType, 'receiverable_id' => $receiverableId, 'receiverable_type' => $receiverableType, 'message' => $data['message'], @@ -264,7 +268,7 @@ public function createConversation(array $data): void $conversation->id, $message->id, $member->id, - auth()->id(), + $senderableId, )); } } else { @@ -272,7 +276,7 @@ public function createConversation(array $data): void $conversation->id, $message->id, $receiverableId, - auth()->id(), + $senderableId, )); }