Skip to content

Commit

Permalink
Adds routine cleanup job #24
Browse files Browse the repository at this point in the history
  • Loading branch information
cdterry87 committed Aug 10, 2024
1 parent 2403265 commit 3393ad6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 deletions.
34 changes: 34 additions & 0 deletions app/Jobs/RoutineCleanupJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Jobs;

use App\Models\Bill;
use App\Models\User;
use Database\Seeders\DemoUserSeeder;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;

class RoutineCleanupJob implements ShouldQueue
{
use Queueable;

public function __invoke()
{
// Get all demo user ids
$demoUsers = User::query()
->select('id')
->where('demo', true)
->get()
->pluck('id')
->toArray();

// Delete demo user's bills
Bill::query()
->whereIn('user_id', $demoUsers)
->delete();

// Run DemoUserSeeder to repopulate data
$seeder = new DemoUserSeeder();
$seeder->run();
}
}
9 changes: 7 additions & 2 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?php

use App\Jobs\RoutineCleanupJob;
use Illuminate\Foundation\Application;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
web: __DIR__ . '/../routes/web.php',
commands: __DIR__ . '/../routes/console.php',
health: '/up',
)
->withSchedule(function (Schedule $schedule) {
$schedule->call(new RoutineCleanupJob)->daily();
})
->withMiddleware(function (Middleware $middleware) {
//
})
Expand Down
40 changes: 25 additions & 15 deletions database/seeders/DemoUserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,32 @@ class DemoUserSeeder extends Seeder
*/
public function run(): void
{
// Delete any existing demo user
User::where('demo', true)->delete();
// Set demo user's email
$demoUserEmail = 'demo@example.com';

// Create demo user
$demoUser = User::factory()->create([
'name' => 'Demo User',
'email' => 'demo@example.com',
'income' => '100000',
'frequency' => '1',
'password' => Hash::make('password1'),
'demo' => true
]);
// Check if there is already a demo user
$demoUser = User::where('email', $demoUserEmail)->first();

// Create bills for demo user
Bill::factory(10)->create([
'user_id' => $demoUser->id
]);
// Create demo user if one doesn't already exist
if (!$demoUser) {
$demoUser = User::factory()->create([
'name' => 'Demo User',
'email' => $demoUserEmail,
'income' => '100000',
'frequency' => '1',
'password' => Hash::make('password1'),
'demo' => true
]);
}

// Check if demo user has any bills
$demoUserBills = Bill::where('user_id', $demoUser->id)->count();

// If demo user doesn't have any bills, add some
if (!$demoUserBills) {
Bill::factory(10)->create([
'user_id' => $demoUser->id
]);
}
}
}
7 changes: 0 additions & 7 deletions routes/console.php
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
<?php

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;

Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote')->hourly();

0 comments on commit 3393ad6

Please sign in to comment.