From a51a09c6cc3156c61d46f26df6562c3fdc033075 Mon Sep 17 00:00:00 2001 From: Alfonso Rafael Solis Rangel Date: Wed, 25 Sep 2024 08:56:50 -0600 Subject: [PATCH 1/6] Primer ruta de auditorias --- routes/web.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/routes/web.php b/routes/web.php index d02aacb..c2a6ee4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@ name('update')->middleware(CheckRoles::class . ':admingen,admin,capturista' ); Route::delete('/listas/{id}', [ListaController::class, 'destroy'])->name('destroy')->middleware(CheckRoles::class . ':admingen,admin,capturista'); }); + +Route::middleware('auth:sanctum', + config('jetstream.auth_sesion'), + 'verified', + CheckBanned::class)->name('auditoria.')->group(function(){ + Route::get('/auditoriad', [AuditoriaController::class, 'index'])->name('get')->middleware(CheckRoles::class . ':admingen,admin'); + }); -- GitLab From 31c0a1449a8e4e7bd7e5be65070c6d28c423ac7a Mon Sep 17 00:00:00 2001 From: Alfonso Rafael Solis Rangel Date: Wed, 25 Sep 2024 08:58:07 -0600 Subject: [PATCH 2/6] Los capturistas no tienen permisos de eliminar --- routes/web.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/web.php b/routes/web.php index c2a6ee4..f28c47f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -108,7 +108,7 @@ ->middleware(CheckRoles::class . ':admingen,admin'); Route::get('/instituciones/{id}', [InstitucionesController::class, 'show']) ->name('show') - ->middleware(CheckRoles::class . ':admingen,admin,capturista'); + ->middleware(CheckRoles::class . ':admingen,admin'); }); Route::middleware('auth:sanctum', @@ -136,7 +136,7 @@ route::get('/listas/{id}/ver', [ListaController::class, 'show'])->name('show')->middleware(CheckRoles::class . ':admingen,admin,capturista'); Route::get('/listas/{id}/editar', [ListaController::class, 'edit'])->name('edit')->middleware(CheckRoles::class . ':admingen,admin,capturista' ); Route::put('/listas/{id}/editar', [ListaController::class, 'update'])->name('update')->middleware(CheckRoles::class . ':admingen,admin,capturista' ); - Route::delete('/listas/{id}', [ListaController::class, 'destroy'])->name('destroy')->middleware(CheckRoles::class . ':admingen,admin,capturista'); + Route::delete('/listas/{id}', [ListaController::class, 'destroy'])->name('destroy')->middleware(CheckRoles::class . ':admingen,admin'); }); Route::middleware('auth:sanctum', -- GitLab From 220c40cbe7ad38d6bdbb192bba928760f4a9badd Mon Sep 17 00:00:00 2001 From: Alfonso Rafael Solis Rangel Date: Thu, 26 Sep 2024 15:02:36 -0600 Subject: [PATCH 3/6] Funcion de auditoria mejorada, almacenando los datos que fueron cambiados --- app/Http/Controllers/ContactoController.php | 81 ++++++++++++--------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/app/Http/Controllers/ContactoController.php b/app/Http/Controllers/ContactoController.php index e3e6aad..9f895bc 100644 --- a/app/Http/Controllers/ContactoController.php +++ b/app/Http/Controllers/ContactoController.php @@ -227,8 +227,8 @@ public function update(Request $request, string $id) } // Guardar auditoría de teléfonos - $telefonosAnteriores = $contacto->telefonos->toArray(); - $telefonos = collect($request->input('prefijo'))->map(function($prefijo, $index) use ($request) { + $telefonosAnteriores = collect($contacto->telefonos); + $telefonosNuevos = collect($request->input('prefijo'))->map(function($prefijo, $index) use ($request) { return [ 'prefijo' => $prefijo, 'lada' => $request->input('lada')[$index], @@ -239,15 +239,22 @@ public function update(Request $request, string $id) 'id_radio' => $request->input('id_radio')[$index], 'observaciones' => $request->input('observaciones')[$index], ]; - })->toArray(); - - foreach ($telefonosAnteriores as $index => $telefonoAnterior) { - foreach ($telefonoAnterior as $key => $value) { - if (isset($telefonos[$index][$key]) && $value != $telefonos[$index][$key]) { + }); + + // Unificar comparación entre teléfonos anteriores y nuevos + $telefonosNuevos->each(function ($telefonoNuevo, $index) use ($telefonosAnteriores, &$changes, $contacto, $user) { + // Asegurarse de que $telefonosAnteriores sea accesible y no usar get(), ya que es un array simple + $telefonoAnterior = $telefonosAnteriores[$index] ?? []; // Si no hay un teléfono anterior en ese índice, usa un array vacío + + foreach ($telefonoNuevo as $key => $nuevoValor) { + $valorAnterior = $telefonoAnterior[$key] ?? null; // Usa null si no existe el valor anterior + + // Solo registrar cambios si hay una diferencia + if (strval($valorAnterior) !== $nuevoValor) { $changes[] = [ 'campo_editado' => "telefono_{$index}_{$key}", - 'valor_anterior' => $value, - 'nuevo_valor' => $telefonos[$index][$key], + 'valor_anterior' => $valorAnterior, + 'nuevo_valor' => $nuevoValor, 'contacto_id' => $contacto->id, 'user_id' => $user->id, 'created_at' => now(), @@ -255,24 +262,28 @@ public function update(Request $request, string $id) ]; } } - } + }); // Guardar auditoría de redes sociales - $redesAnteriores = $contacto->redes->toArray(); - $redes = collect($request->input('red_social'))->map(function($url, $index) use ($request) { + $redesAnteriores = collect($contacto->redes); + $redesNuevos = collect($request->input('red_social'))->map(function($url, $index) use ($request) { return [ 'red_social' => $url, 'tipo_red_social' => $request->input('tipo_red_social')[$index], ]; - })->toArray(); - - foreach ($redesAnteriores as $index => $redAnterior) { - foreach ($redAnterior as $key => $value) { - if (isset($redes[$index][$key]) && $value != $redes[$index][$key]) { + }); + + $redesNuevos->each(function ($redNuevo, $index) use ($redesAnteriores, &$changes, $contacto, $user){ + $redAnterior = $redesAnteriores->get($index, []); + + foreach ($redNuevo as $key => $nuevoValor){ + $valorAnterior = $redAnterior[$key] ?? null; + + if(strval($valorAnterior) !== $nuevoValor){ $changes[] = [ 'campo_editado' => "red_{$index}_{$key}", - 'valor_anterior' => $value, - 'nuevo_valor' => $redes[$index][$key], + 'valor_anterior' => $valorAnterior, + 'nuevo_valor' => $nuevoValor, 'contacto_id' => $contacto->id, 'user_id' => $user->id, 'created_at' => now(), @@ -280,24 +291,28 @@ public function update(Request $request, string $id) ]; } } - } + }); // Guardar auditoría de correos - $correosAnteriores = $contacto->correos->toArray(); - $correos = collect($request->input('correo_electronico'))->map(function($email, $index) use ($request) { + $correosAnteriores = collect($contacto->correos); + $correosNuevos = collect($request->input('correo_electronico'))->map(function($email, $index) use ($request) { return [ 'correo_electronico' => $email, 'tipo_correo_electronico' => $request->input('tipo_correo_electronico')[$index], ]; - })->toArray(); - - foreach ($correosAnteriores as $index => $correoAnterior) { - foreach ($correoAnterior as $key => $value) { - if (isset($correos[$index][$key]) && $value != $correos[$index][$key]) { + }); + + $correosNuevos->each(function ($correoNuevo, $index) use ($correosAnteriores, &$changes, $contacto, $user){ + $redAnterior = $correosAnteriores->get($index, []); + + foreach ($correoNuevo as $key => $nuevoValor){ + $valorAnterior = $redAnterior[$key] ?? null; + + if(strval($valorAnterior) !== $nuevoValor){ $changes[] = [ 'campo_editado' => "correo_{$index}_{$key}", - 'valor_anterior' => $value, - 'nuevo_valor' => $correos[$index][$key], + 'valor_anterior' => $valorAnterior, + 'nuevo_valor' => $nuevoValor, 'contacto_id' => $contacto->id, 'user_id' => $user->id, 'created_at' => now(), @@ -305,7 +320,7 @@ public function update(Request $request, string $id) ]; } } - } + }); if (!empty($changes)) { Auditoria::insert($changes); @@ -349,15 +364,15 @@ public function update(Request $request, string $id) // Guardar teléfonos $contacto->telefonos()->delete(); - $contacto->telefonos()->createMany($telefonos); + $contacto->telefonos()->createMany($telefonosNuevos->toArray()); // Guardar redes sociales $contacto->redes()->delete(); - $contacto->redes()->createMany($redes); + $contacto->redes()->createMany($redesNuevos->toArray()); // Guardar correos electrónicos $contacto->correos()->delete(); - $contacto->correos()->createMany($correos); + $contacto->correos()->createMany($correosNuevos->toArray()); return redirect()->route('contacto.get')->with('success', 'Contacto actualizado correctamente'); } -- GitLab From fd2636482a111b1e83fc1d3027ff0c074379eefb Mon Sep 17 00:00:00 2001 From: Alfonso Rafael Solis Rangel Date: Thu, 26 Sep 2024 15:03:01 -0600 Subject: [PATCH 4/6] Ruta de auditoria --- routes/web.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/web.php b/routes/web.php index f28c47f..1cab2b3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -140,8 +140,8 @@ }); Route::middleware('auth:sanctum', - config('jetstream.auth_sesion'), + config('jetstream.auth_session'), 'verified', CheckBanned::class)->name('auditoria.')->group(function(){ - Route::get('/auditoriad', [AuditoriaController::class, 'index'])->name('get')->middleware(CheckRoles::class . ':admingen,admin'); + Route::get('/administracion/auditoria', [AuditoriaController::class, 'index'])->name('get')->middleware(CheckRoles::class . ':admingen,admin'); }); -- GitLab From f3c7820279790fe1b2428a82b88abee3378f5678 Mon Sep 17 00:00:00 2001 From: Alfonso Rafael Solis Rangel Date: Fri, 27 Sep 2024 09:23:34 -0600 Subject: [PATCH 5/6] Seeder para probar la auditoria con 70 mil registros --- database/factories/AuditoriaFactory.php | 32 +++++++++++++++++++++++++ database/seeders/AuditoriaSeeder.php | 18 ++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 database/factories/AuditoriaFactory.php create mode 100644 database/seeders/AuditoriaSeeder.php diff --git a/database/factories/AuditoriaFactory.php b/database/factories/AuditoriaFactory.php new file mode 100644 index 0000000..6e08910 --- /dev/null +++ b/database/factories/AuditoriaFactory.php @@ -0,0 +1,32 @@ + + */ +class AuditoriaFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + protected $model = Auditoria::class; + + public function definition(): array + { + return [ + 'campo_editado' => $this->faker->word(), + 'valor_anterior' => $this->faker->sentence(), + 'nuevo_valor' => $this->faker->sentence(), + 'contacto_id' => Contacto::inRandomOrder()->first()->id ?? null, + 'user_id' => User::inRandomOrder()->first()->id ?? null, + ]; + } +} diff --git a/database/seeders/AuditoriaSeeder.php b/database/seeders/AuditoriaSeeder.php new file mode 100644 index 0000000..dfe9e25 --- /dev/null +++ b/database/seeders/AuditoriaSeeder.php @@ -0,0 +1,18 @@ +count(10000)->create(); + } +} -- GitLab From b1e4fa164f6a00140dc8469953e1b21f899107d2 Mon Sep 17 00:00:00 2001 From: Alfonso Rafael Solis Rangel Date: Wed, 2 Oct 2024 09:45:22 -0600 Subject: [PATCH 6/6] Auditoria terminado --- app/Http/Controllers/AuditoriaController.php | 3 +- database/seeders/AuditoriaSeeder.php | 27 ++++- .../views/adminGen/auditoria/index.blade.php | 106 ++++++++++++++++++ 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 resources/views/adminGen/auditoria/index.blade.php diff --git a/app/Http/Controllers/AuditoriaController.php b/app/Http/Controllers/AuditoriaController.php index c5c2815..74f6145 100644 --- a/app/Http/Controllers/AuditoriaController.php +++ b/app/Http/Controllers/AuditoriaController.php @@ -12,7 +12,8 @@ class AuditoriaController extends Controller */ public function index() { - // + $datos = Auditoria::with('contacto', 'user')->orderBy('id', 'desc')->get(); + return view('adminGen.auditoria.index', compact('datos')); } /** diff --git a/database/seeders/AuditoriaSeeder.php b/database/seeders/AuditoriaSeeder.php index dfe9e25..6b298af 100644 --- a/database/seeders/AuditoriaSeeder.php +++ b/database/seeders/AuditoriaSeeder.php @@ -13,6 +13,31 @@ class AuditoriaSeeder extends Seeder */ public function run(): void { - Auditoria::factory()->count(10000)->create(); + $batchSize = 1000; // Número de registros por lote + $totalRecords = 70000; + $data = []; + + for ($i = 0; $i < $totalRecords; $i++) { + $data[] = [ + 'campo_editado' => fake()->word(), + 'valor_anterior' => fake()->sentence(), + 'nuevo_valor' => fake()->sentence(), + 'contacto_id' => \App\Models\Contacto::inRandomOrder()->first()->id ?? null, + 'user_id' => \App\Models\User::inRandomOrder()->first()->id ?? null, + 'created_at' => now(), + 'updated_at' => now(), + ]; + + // Cuando se llega al tamaño del lote, insertamos y limpiamos el array + if (count($data) === $batchSize) { + \DB::table('auditorias')->insert($data); + $data = []; + } + } + + // Inserta los registros restantes si hay + if (!empty($data)) { + \DB::table('auditorias')->insert($data); + } } } diff --git a/resources/views/adminGen/auditoria/index.blade.php b/resources/views/adminGen/auditoria/index.blade.php new file mode 100644 index 0000000..062207c --- /dev/null +++ b/resources/views/adminGen/auditoria/index.blade.php @@ -0,0 +1,106 @@ + + + + + +
+ + +
+ + + + + + + + + +
\ No newline at end of file -- GitLab