-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added action to mark multiple notifications as read. - Tests added
- Loading branch information
Showing
4 changed files
with
63 additions
and
5 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
app/Domain/Notification/Actions/MarkNotificationsAsReadForUserAction.php
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,21 @@ | ||
<?php | ||
|
||
namespace App\Domain\Notification\Actions; | ||
|
||
use App\Domain\Notification\Models\Notification; | ||
use App\Models\User; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class MarkNotificationsAsReadForUserAction | ||
{ | ||
public function execute(Collection $notificationIds, User $user): void | ||
{ | ||
$notificationIds->each(function (int $notificationId) use ($user): void { | ||
DB::table('user_notification')->where([ | ||
'user_id' => $user->id, | ||
'notification_id' => $notificationId | ||
])->update(['read_at' => now()]); | ||
}); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
tests/Feature/Notifications/MarkNotificationAsReadForUserTest.php
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,38 @@ | ||
<?php | ||
|
||
use App\Domain\Notification\Actions\MarkNotificationsAsReadForUserAction; | ||
use App\Domain\Notification\Actions\SendNotificationToAllAction; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
||
uses(RefreshDatabase::class); | ||
|
||
describe('Test that notifications are marked as read for a user', function () { | ||
it('marks notifications as read when notification ids are sent', function () { | ||
$user = \App\Models\User::factory()->create(); | ||
$notifications = \App\Domain\Notification\Models\Notification::factory()->count(3)->create(); | ||
|
||
// sending 3 notifications to all users | ||
$sendNotification = app(SendNotificationToAllAction::class); | ||
$notifications->each(function (\App\Domain\Notification\Models\Notification $notification) use ($sendNotification) { | ||
$sendNotification->execute($notification); | ||
}); | ||
|
||
$notificationIds = $notifications->pluck('id'); | ||
|
||
// mark all those notifications as read | ||
app(MarkNotificationsAsReadForUserAction::class) | ||
->execute($notificationIds, $user); | ||
|
||
// create a new notification which will not be read and sending to all | ||
$notification = \App\Domain\Notification\Models\Notification::factory()->create(); | ||
$sendNotification->execute($notification); | ||
|
||
$readCount = \Illuminate\Support\Facades\DB::table('user_notification') | ||
->where('read_at', '!=', null)->count(); | ||
|
||
$unreadCount = \Illuminate\Support\Facades\DB::table('user_notification') | ||
->where('read_at', null)->count(); | ||
|
||
expect($readCount)->toBe(3)->and($unreadCount)->toBe(1); | ||
}); | ||
}); |