Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| InvoicesReclassifyHyphenSplitterSkips | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
110 | |
0.00% |
0 / 1 |
| handle | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
110 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Models\TblInvoiceReminderSkips; |
| 6 | use Illuminate\Console\Command; |
| 7 | use Illuminate\Support\Facades\Log; |
| 8 | |
| 9 | /** |
| 10 | * FIRE-1562 — one-off historical cleanup. Antes del fix, el splitter de |
| 11 | * emails de FacturasService rompía dominios con guion (avc@avc-administraciones.es, |
| 12 | * foo@grupo-fire.es, laura@ayto-meco.es) y clasificaba la factura como |
| 13 | * `no_valid_email`, aunque el email real era perfectamente válido. |
| 14 | * |
| 15 | * Este comando recorre las filas ya escritas con `skip_reason = 'no_valid_email'`, |
| 16 | * re-ejecuta la validación con el splitter arreglado, y para las que resulta |
| 17 | * que al menos un email SÍ era válido reclasifica el `skip_reason` a |
| 18 | * `historical_splitter_bug`. Al no estar ese nuevo valor en el whitelist |
| 19 | * `$filterReasons` del panel `payment_reminders_kpi`, esas filas dejan de |
| 20 | * aparecer en el bucket "no enviadas" — sin borrar datos, la traza queda. |
| 21 | * |
| 22 | * Se ejecuta una única vez post-deploy. Idempotente: correr dos veces no |
| 23 | * causa efecto (las filas ya reclasificadas no volverán a aparecer con |
| 24 | * `skip_reason = 'no_valid_email'`). |
| 25 | */ |
| 26 | class InvoicesReclassifyHyphenSplitterSkips extends Command |
| 27 | { |
| 28 | protected $signature = 'invoices:reclassify-hyphen-splitter-skips {--dry-run}'; |
| 29 | |
| 30 | protected $description = 'FIRE-1562: reclassify historical no_valid_email skips whose raw_email is valid under the fixed splitter'; |
| 31 | |
| 32 | public function handle(): int |
| 33 | { |
| 34 | $dryRun = (bool) $this->option('dry-run'); |
| 35 | |
| 36 | $rows = TblInvoiceReminderSkips::where('skip_reason', 'no_valid_email')->get(); |
| 37 | |
| 38 | $reclassified = 0; |
| 39 | $genuine = 0; |
| 40 | $noRawEmail = 0; |
| 41 | |
| 42 | foreach ($rows as $row) { |
| 43 | $raw = data_get($row->context, 'raw_email'); |
| 44 | if (! is_string($raw) || $raw === '') { |
| 45 | $noRawEmail++; |
| 46 | |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | $parts = preg_split('/\s*[,;]\s*|\s+/', $raw, -1, PREG_SPLIT_NO_EMPTY) ?: []; |
| 51 | $hasValid = false; |
| 52 | foreach ($parts as $email) { |
| 53 | if (filter_var($email, FILTER_VALIDATE_EMAIL)) { |
| 54 | $hasValid = true; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (! $hasValid) { |
| 60 | $genuine++; |
| 61 | |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | if (! $dryRun) { |
| 66 | $row->skip_reason = 'historical_splitter_bug'; |
| 67 | $row->save(); |
| 68 | } |
| 69 | $reclassified++; |
| 70 | } |
| 71 | |
| 72 | $summary = sprintf( |
| 73 | '%s | scanned=%d, reclassified=%d, genuine_no_valid=%d, no_raw_email=%d', |
| 74 | $dryRun ? 'DRY-RUN' : 'APPLIED', |
| 75 | $rows->count(), |
| 76 | $reclassified, |
| 77 | $genuine, |
| 78 | $noRawEmail, |
| 79 | ); |
| 80 | |
| 81 | $this->info($summary); |
| 82 | Log::channel('g3w_invoices')->info('invoices:reclassify-hyphen-splitter-skips '.$summary); |
| 83 | |
| 84 | return self::SUCCESS; |
| 85 | } |
| 86 | } |