-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DeleteReadNotifications command added
- Loading branch information
1 parent
57efea5
commit 8ef3e43
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |