Skip to content

Commit

Permalink
DeleteReadNotifications command added
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadmp97 committed Sep 16, 2023
1 parent 57efea5 commit 8ef3e43
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/Console/Commands/DeleteReadNotifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class DeleteReadNotifications extends Command
{
protected $signature = 'app:delete-seen-notifications';

protected $description = 'Delete seen notifications';

public function handle()
{
DB::table('notifications')
->whereNotNull('read_at')
->where('updated_at', '<', now()->subMonths(2))
->delete();

return $this->comment('Done!');
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ protected function schedule(Schedule $schedule): void
$schedule->command('app:close-abandoned-challenges')->hourly();

$schedule->command('app:handle-deactivation-requests')->daily();

$schedule->command('app:delete-seen-notifications')->daily();
}

/**
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/Commands/DeleteReadNotificationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Unit;

use App\Models\Country;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class DeleteReadNotificationsTest extends TestCase
{
use RefreshDatabase;

public function setUp(): void
{
parent::setUp();

Country::create([
'code' => 'GB',
'name' => 'United Kingdom',
]);

$this->signIn();
}

public function test_read_notifications_will_be_deleted_after_months(): void
{
DB::table('notifications')
->insert([
'id' => 'xyz',
'type' => 'xyz',
'notifiable_id' => 1,
'notifiable_type' => 'xyz',
'data' => 'xyz',
'read_at' => now()->subMonths(3),
'created_at' => now()->subMonths(3),
'updated_at' => now()->subMonths(3),
]);

$this->artisan('app:delete-seen-notifications');

$this->assertDatabaseEmpty('notifications');
}
}

0 comments on commit 8ef3e43

Please sign in to comment.