Blame view

app/Console/Commands/CreateAdminChat.php 1.55 KB
09a2fc9d8   Hayk Nazaryan   messages and subs...
1
2
3
4
5
  <?php
  
  namespace App\Console\Commands;
  
  use App\Models\Chat;
01a2f91b2   Hayk Nazaryan   delete old messages
6
  use App\Models\Message;
09a2fc9d8   Hayk Nazaryan   messages and subs...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  use App\Models\User;
  use Illuminate\Console\Command;
  
  class CreateAdminChat extends Command
  {
      /**
       * The name and signature of the console command.
       *
       * @var string
       */
      protected $signature = 'admin:chat';
  
      /**
       * The console command description.
       *
       * @var string
       */
      protected $description = 'Command description';
  
      /**
       * Execute the console command.
       *
       * @return int
       */
      public function handle()
      {
01a2f91b2   Hayk Nazaryan   delete old messages
33
          $superAdmin = User::superAdmin();
09a2fc9d8   Hayk Nazaryan   messages and subs...
34

01a2f91b2   Hayk Nazaryan   delete old messages
35
36
37
38
          $users = User::where('id', '!=', $superAdmin->id)->get();
          $chats = Chat::query()
              ->where('is_admin_chat', 1)
              ->orWhere('to_user_id', $superAdmin->id)
7f3406fc6   Hayk Nazaryan   fix subscribe and...
39
              ->orWhere('user_id', $superAdmin->id)
09a2fc9d8   Hayk Nazaryan   messages and subs...
40
41
42
              ->get();
  
          foreach ($chats as $chat) {
01a2f91b2   Hayk Nazaryan   delete old messages
43
              Message::query()->where('chat_id_from', $chat->id)->orWhere('chat_id_to', $chat->id)->delete();
09a2fc9d8   Hayk Nazaryan   messages and subs...
44
45
46
47
              $chat->delete();
          }
  
          $loopCount = $users->count();
01a2f91b2   Hayk Nazaryan   delete old messages
48
49
50
51
52
53
54
55
          if ($superAdmin) {
              foreach ($users as $user) {
                  Chat::firstOrCreate([
                      'user_id' => $user->id,
                      'to_user_id' => $superAdmin->id,
                      'is_admin_chat' => 1,
                      'is_removed' => 0,
                  ]);
09a2fc9d8   Hayk Nazaryan   messages and subs...
56

01a2f91b2   Hayk Nazaryan   delete old messages
57
              }
09a2fc9d8   Hayk Nazaryan   messages and subs...
58

01a2f91b2   Hayk Nazaryan   delete old messages
59
              $this->info("Admin chats created for {$loopCount} users.");
09a2fc9d8   Hayk Nazaryan   messages and subs...
60

01a2f91b2   Hayk Nazaryan   delete old messages
61
62
              return Command::SUCCESS;
          }
09a2fc9d8   Hayk Nazaryan   messages and subs...
63

01a2f91b2   Hayk Nazaryan   delete old messages
64
          $this->error("Admin is missing!");
09a2fc9d8   Hayk Nazaryan   messages and subs...
65
66
67
68
  
      }
  
  }