diff --git a/README.md b/README.md index eef188e..4c2982b 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,38 @@ `php artisan install PASSWORD USER_NAME EMAIL_ADDRESS` +## Deleting Visitors By Its Age + +You can remove visitors that have a certain age (in seconds) after their addition to the system. You can do that by running the artisan `deleteVisitorByAge` command. Run `php artisan help deleteVisitorByAge` for more info. + +To schedule `deleteVisitorByAge` command, you need to define it in `app/Console/Kernel.php` file's schedule method. For example: +``` +protected function schedule(Schedule $schedule) +{ + $schedule->command('deleteVisitorByAge --days=3') + ->daily() + ->runInBackground(); +} +``` + +This will schedule the deletion of visitors that are 3 days old every day at midnight. + +To running the actual Laravel Scheduler, add a single cron entry to your server to run `schedule:run` command every minute. + +``` +* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 +``` + +Refer [this article](https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/) on how to add Cron entries in Linux. + +Refer [Laravel documentation](https://laravel.com/docs/9.x/scheduling#scheduling-artisan-commands) for more info in task scheduling. + +### Logs + +All logs of `deleteVisitorByAge` command are stored in `storage/logs/deleteVisitorByAgeCommand.log` file. + ## Copyright -Copyright (c) 2022 Muhammad Hanis Irfan Bin Mohd Zaid a.k.a. Hanis Irfan. +Copyright (c) 2022 Muhammad Hanis Irfan Bin Mohd Zaid. Licensed under the MIT license. diff --git a/app/Console/Commands/DeleteVisitorByAge.php b/app/Console/Commands/DeleteVisitorByAge.php new file mode 100644 index 0000000..b6f7972 --- /dev/null +++ b/app/Console/Commands/DeleteVisitorByAge.php @@ -0,0 +1,106 @@ +option('seconds'))) { + $optionSeconds = $this->option('seconds'); + } else { + $optionSeconds = 0; + } + + if (!empty($this->option('minutes'))) { + $optionMinutes = $this->option('minutes'); + $optionMinutes = $optionMinutes * 60; + } else { + $optionMinutes = 0; + } + + if (!empty($this->option('hours'))) { + $optionHours = $this->option('hours'); + $optionHours = ($optionHours * 60) * 60; + } else { + $optionHours = 0; + } + + if (!empty($this->option('days'))) { + $optionDays = $this->option('days'); + $optionDays = (($optionDays * 24) * 60) * 60; + } else { + $optionDays = 0; + } + + if (!empty($this->option('weeks'))) { + $optionWeeks = $this->option('weeks'); + $optionWeeks = ((($optionWeeks * 7) * 24) * 60) * 60; + } else { + $optionWeeks = 0; + } + + $totalAgeInSeconds = $optionSeconds + $optionMinutes + $optionHours + $optionDays + $optionWeeks; + + $this->info('Total age limit in seconds: ' . $totalAgeInSeconds); + Log::channel('deleteVisitorByAgeCommand')->info('Total age limit in seconds: ' . $totalAgeInSeconds); + + if (Visitor::first()) { + + $visitors = Visitor::select('uuid', 'created_at')->get(); + + $now = Carbon::now(); + foreach ($visitors as $visitor) { + $visitorAgeInSeconds = $visitor->created_at->diffInSeconds($now); + $this->info('-----------------------------------------------------------'); + $this->info('Visitor UUID: ' . $visitor->uuid); + $this->info('Visitor Created At: ' . $visitor->created_at); + $this->info('Visitor Age In Seconds: ' . $visitorAgeInSeconds); + + // Deleting visitor if the age (in seconds) is more than the limit defined when running the command. + if ($visitorAgeInSeconds > $totalAgeInSeconds) { + + Visitor::where('uuid', $visitor->uuid)->delete(); + $this->info('Deleted visitor with UUID: ' . $visitor->uuid); + Log::channel('deleteVisitorByAgeCommand')->info('Deleted visitor with UUID: ' . $visitor->uuid); + } + $this->info('-----------------------------------------------------------'); + } + } else { + $this->info('No visitors found.'); + Log::channel('deleteVisitorByAgeCommand')->info('No visitors found.'); + } + + return Command::SUCCESS; + } +} diff --git a/app/Console/Commands/Install.php b/app/Console/Commands/Install.php index 6960767..0824525 100644 --- a/app/Console/Commands/Install.php +++ b/app/Console/Commands/Install.php @@ -21,7 +21,7 @@ class Install extends Command * * @var string */ - protected $description = 'formqr: Use composer compile-project to compile the system assets. Use this command to perform other actions (DB migration, creating admin user etc).'; + protected $description = 'visitorqr: Use composer compile-project to compile the system assets. Use this command to perform other actions (DB migration, creating admin user etc).'; /** * Create a new command instance. diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index d8bc1d2..708125b 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,7 +15,7 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // $schedule->command('inspire')->hourly(); + } /** diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 6960b77..665f993 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -25,7 +25,7 @@ public function __construct() public function index(Request $request) { if ($request->isMethod('get')) { - $pagination = 5; + $pagination = 7; $visitors = Visitor::orderBy('created_at', 'DESC')->paginate($pagination)->withQueryString(); diff --git a/app/Http/Controllers/VisitorController.php b/app/Http/Controllers/VisitorController.php index 268e54b..bdf440d 100644 --- a/app/Http/Controllers/VisitorController.php +++ b/app/Http/Controllers/VisitorController.php @@ -79,4 +79,25 @@ public function create(Request $request) return view('visitors.add'); } } + + + public function delete(Request $request) { + if ($request->isMethod('post')) { + + $validated = $request->validate([ + 'delete-visitor-uuid' => ['required', 'uuid'], + ]); + + $visitorUuid = $request->input('delete-visitor-uuid'); + Visitor::destroy($visitorUuid); + + $request->session()->flash('deleteVisitorSuccess', 'Visitor successfully deleted!'); + $request->session()->flash('uuid', $visitorUuid); + + return back(); + } else { + return redirect()->route('home'); + } + } + } diff --git a/app/View/Components/Visitors/Delete.php b/app/View/Components/Visitors/Delete.php new file mode 100644 index 0000000..9247ef5 --- /dev/null +++ b/app/View/Components/Visitors/Delete.php @@ -0,0 +1,54 @@ +visitorUuid = $visitorUuid; + $this->visitorName = Visitor::select('name')->where('uuid', $visitorUuid)->first()->name; + $this->visitorDateTime = Visitor::select('visit_datetime')->where('uuid', $visitorUuid)->first()->date_time; + } + + /** + * Get the view / contents that represent the component. + * + * @return \Illuminate\Contracts\View\View|\Closure|string + */ + public function render() + { + return view('components.visitors.delete'); + } +} diff --git a/config/logging.php b/config/logging.php index 5aa1dbb..93d7107 100644 --- a/config/logging.php +++ b/config/logging.php @@ -63,6 +63,12 @@ 'level' => env('LOG_LEVEL', 'debug'), ], + 'deleteVisitorByAgeCommand' => [ + 'driver' => 'single', + 'path' => storage_path('logs/deleteVisitorByAgeCommand.log'), + 'level' => env('LOG_LEVEL', 'debug'), + ], + 'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), diff --git a/resources/views/components/visitors/delete.blade.php b/resources/views/components/visitors/delete.blade.php new file mode 100644 index 0000000..7dc162f --- /dev/null +++ b/resources/views/components/visitors/delete.blade.php @@ -0,0 +1,34 @@ +
+ + + +
diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 7cf0bab..29b8c74 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -6,8 +6,17 @@
{{ __('Visitor List') }}
-
+ + @if(session()->has('deleteVisitorSuccess')) + +
+

{{ session('deleteVisitorSuccess') }}

+

{{ __('UUID') }}: {{ session('uuid') }}

+
+
+ @endif +
@if ($visitors->count() > 0) @@ -43,16 +52,16 @@ @endforeach @else -

No visitors added.

+

{{ __('No visitors found.') }}

@endif
- + - +
{{ $visitors->links() }} diff --git a/routes/web.php b/routes/web.php index 97d4cd3..b58e295 100644 --- a/routes/web.php +++ b/routes/web.php @@ -27,5 +27,7 @@ Route::get('/visitors/view/{uuid}', [VisitorController::class, 'index'])->name('visitors.view'); Route::get('/visitors/add', [VisitorController::class, 'create'])->name('visitors.add'); Route::post('/visitors/add', [VisitorController::class, 'create']); +Route::get('/visitors/delete', [VisitorController::class, 'delete'])->name('visitors.delete'); +Route::post('/visitors/delete', [VisitorController::class, 'delete']); Route::get('/scanner', [ScannerController::class, 'index'])->name('scanner');