ソースを参照

feat(student): add daily scheduled command to sync student status based on contract dates

alvesantos 1 週間 前
コミット
effb3ace9f
2 ファイル変更49 行追加0 行削除
  1. 47 0
      app/Console/Commands/UpdateStudentStatus.php
  2. 2 0
      bootstrap/app.php

+ 47 - 0
app/Console/Commands/UpdateStudentStatus.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\Student;
+use Illuminate\Console\Command;
+
+class UpdateStudentStatus extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'students:update-status';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Updates students status based on their contracts validity';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle(): void
+    {
+        $this->info('Starting student status update...');
+
+        // Using chunk to avoid memory exhaustion on large databases
+        $updatedCount = 0;
+        
+        Student::with('contracts')->chunkById(200, function ($students) use (&$updatedCount) {
+            foreach ($students as $student) {
+                $oldStatus = $student->status;
+                $student->updateStatus();
+                
+                if ($oldStatus !== $student->status) {
+                    $updatedCount++;
+                }
+            }
+        });
+
+        $this->info("Student status update finished. {$updatedCount} students updated.");
+    }
+}

+ 2 - 0
bootstrap/app.php

@@ -35,6 +35,8 @@
         $schedule->call(new DeleteExpiredTokens)->everyMinute();
         // Gera automaticamente o TBR do mês fechado, todo dia 1 às 03:00.
         $schedule->command('tbr:generate-monthly')->monthlyOn(1, '03:00');
+        // Atualiza os status dos alunos diariamente
+        $schedule->command('students:update-status')->dailyAt('00:00');
     })
     ->withExceptions(function (Exceptions $exceptions) {
         //