diff --git a/app/Http/Controllers/AuditoriaController.php b/app/Http/Controllers/AuditoriaController.php index c5c2815c31b40b99dc3757af97f2646230a1130d..74f6145f0fbb9b2fab84b46c608af60494dc270d 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/app/Http/Controllers/ContactoController.php b/app/Http/Controllers/ContactoController.php index e3e6aadc2f0773916ee0dc56a0da75e6cd41f04e..9f895bccb07e78e869f2a69a18a6ca6f5f512287 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'); } diff --git a/database/factories/AuditoriaFactory.php b/database/factories/AuditoriaFactory.php new file mode 100644 index 0000000000000000000000000000000000000000..6e0891036831d43ba3ae64805909d5325a085a08 --- /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 0000000000000000000000000000000000000000..6b298afcf57b5a6e3b704b513cd5714b6a5b1264 --- /dev/null +++ b/database/seeders/AuditoriaSeeder.php @@ -0,0 +1,43 @@ + 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 0000000000000000000000000000000000000000..062207c117dbeddc7c3d9a038ad66a62c3d3a9f7 --- /dev/null +++ b/resources/views/adminGen/auditoria/index.blade.php @@ -0,0 +1,106 @@ + + + + + +
+ + +
+ + + + + + + + + +
\ No newline at end of file diff --git a/routes/web.php b/routes/web.php index d02aacbdd30ffe4660aff03ac288b063e1688eb7..1cab2b30491eddd008d396691a4813b747cc198e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@ 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', @@ -135,5 +136,12 @@ 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', + config('jetstream.auth_session'), + 'verified', + CheckBanned::class)->name('auditoria.')->group(function(){ + Route::get('/administracion/auditoria', [AuditoriaController::class, 'index'])->name('get')->middleware(CheckRoles::class . ':admingen,admin'); + });