Commit 01a2f91b22650ec20ba67fa1255074a9b1e71d3e

Authored by Hayk Nazaryan
1 parent 28b1a49dec
Exists in master

delete old messages

Showing 2 changed files with 20 additions and 63 deletions Side-by-side Diff

app/Console/Commands/CreateAdminChat.php
... ... @@ -3,6 +3,7 @@
3 3 namespace App\Console\Commands;
4 4  
5 5 use App\Models\Chat;
  6 +use App\Models\Message;
6 7 use App\Models\User;
7 8 use Illuminate\Console\Command;
8 9  
... ... @@ -29,35 +30,38 @@ class CreateAdminChat extends Command
29 30 */
30 31 public function handle()
31 32 {
32   - $superAdmin=User::superAdmin();
  33 + $superAdmin = User::superAdmin();
33 34  
34   - $users = User::where('id','!=',$superAdmin->id)->get();
35   - $chats = Chat::where('is_admin_chat', 1)
  35 + $users = User::where('id', '!=', $superAdmin->id)->get();
  36 + $chats = Chat::query()
  37 + ->where('is_admin_chat', 1)
  38 + ->orWhere('to_user_id', $superAdmin->id)
36 39 ->get();
37 40  
38 41 foreach ($chats as $chat) {
  42 + Message::query()->where('chat_id_from', $chat->id)->orWhere('chat_id_to', $chat->id)->delete();
39 43 $chat->delete();
40 44 }
41 45  
42 46 $loopCount = $users->count();
43 47  
44   - if ($superAdmin){
45   - foreach ($users as $user){
46   - Chat::firstOrCreate([
47   - 'user_id' => $user->id,
48   - 'to_user_id' => $superAdmin->id,
49   - 'is_admin_chat' => 1,
50   - 'is_removed' => 0,
51   - ]);
  48 + if ($superAdmin) {
  49 + foreach ($users as $user) {
  50 + Chat::firstOrCreate([
  51 + 'user_id' => $user->id,
  52 + 'to_user_id' => $superAdmin->id,
  53 + 'is_admin_chat' => 1,
  54 + 'is_removed' => 0,
  55 + ]);
52 56  
53   - }
  57 + }
54 58  
55   - $this->info("Admin chats created for {$loopCount} users." );
  59 + $this->info("Admin chats created for {$loopCount} users.");
56 60  
57   - return Command::SUCCESS;
58   - }
  61 + return Command::SUCCESS;
  62 + }
59 63  
60   - $this->error("Admin is missing!" );
  64 + $this->error("Admin is missing!");
61 65  
62 66 }
63 67  
app/Console/Commands/DeleteOldMessages.php
... ... @@ -1,47 +0,0 @@
1   -<?php
2   -
3   -namespace App\Console\Commands;
4   -
5   -use App\Models\Chat;
6   -use App\Models\Message;
7   -use App\Models\User;
8   -use Illuminate\Console\Command;
9   -
10   -class DeleteOldMessages extends Command
11   -{
12   - /**
13   - * The name and signature of the console command.
14   - *
15   - * @var string
16   - */
17   - protected $signature = 'app:delete-messages';
18   -
19   - /**
20   - * The console command description.
21   - *
22   - * @var string
23   - */
24   - protected $description = 'Command description';
25   -
26   - /**
27   - * Execute the console command.
28   - *
29   - * @return int
30   - */
31   - public function handle()
32   - {
33   - Message::query()->get()->map(function ($message) {
34   - $chatFrom = Chat::query()->where(['id' => $message->chat_id_from])->first();
35   - if (!$chatFrom) {
36   - $message->delete();
37   - }
38   - $chatTo = Chat::query()->where(['id' => $message->chat_id_to])->first();
39   - if (!$chatTo) {
40   - $message->delete();
41   - }
42   - });
43   -
44   -
45   - return Command::SUCCESS;
46   - }
47   -}