diff --git a/app/Console/Commands/CreateAdminChat.php b/app/Console/Commands/CreateAdminChat.php index 18a116c..62db1fb 100644 --- a/app/Console/Commands/CreateAdminChat.php +++ b/app/Console/Commands/CreateAdminChat.php @@ -3,6 +3,7 @@ namespace App\Console\Commands; use App\Models\Chat; +use App\Models\Message; use App\Models\User; use Illuminate\Console\Command; @@ -29,35 +30,38 @@ class CreateAdminChat extends Command */ public function handle() { - $superAdmin=User::superAdmin(); + $superAdmin = User::superAdmin(); - $users = User::where('id','!=',$superAdmin->id)->get(); - $chats = Chat::where('is_admin_chat', 1) + $users = User::where('id', '!=', $superAdmin->id)->get(); + $chats = Chat::query() + ->where('is_admin_chat', 1) + ->orWhere('to_user_id', $superAdmin->id) ->get(); foreach ($chats as $chat) { + Message::query()->where('chat_id_from', $chat->id)->orWhere('chat_id_to', $chat->id)->delete(); $chat->delete(); } $loopCount = $users->count(); - if ($superAdmin){ - foreach ($users as $user){ - Chat::firstOrCreate([ - 'user_id' => $user->id, - 'to_user_id' => $superAdmin->id, - 'is_admin_chat' => 1, - 'is_removed' => 0, - ]); + if ($superAdmin) { + foreach ($users as $user) { + Chat::firstOrCreate([ + 'user_id' => $user->id, + 'to_user_id' => $superAdmin->id, + 'is_admin_chat' => 1, + 'is_removed' => 0, + ]); - } + } - $this->info("Admin chats created for {$loopCount} users." ); + $this->info("Admin chats created for {$loopCount} users."); - return Command::SUCCESS; - } + return Command::SUCCESS; + } - $this->error("Admin is missing!" ); + $this->error("Admin is missing!"); } diff --git a/app/Console/Commands/DeleteOldMessages.php b/app/Console/Commands/DeleteOldMessages.php deleted file mode 100644 index 6866d16..0000000 --- a/app/Console/Commands/DeleteOldMessages.php +++ /dev/null @@ -1,47 +0,0 @@ -get()->map(function ($message) { - $chatFrom = Chat::query()->where(['id' => $message->chat_id_from])->first(); - if (!$chatFrom) { - $message->delete(); - } - $chatTo = Chat::query()->where(['id' => $message->chat_id_to])->first(); - if (!$chatTo) { - $message->delete(); - } - }); - - - return Command::SUCCESS; - } -}