Skip to content

Commit

Permalink
Notifications can be marked as read
Browse files Browse the repository at this point in the history
- Added action to mark multiple notifications as read.
- Tests added
  • Loading branch information
amitavroy committed Dec 26, 2024
1 parent 666ba3d commit e117114
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
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()]);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function execute(Notification $notification): void
$users = User::select('id')->get()->toArray();
$userIds = collect($users)->pluck('id');

$userIds->chunk(100)->each(function (Collection $chunk) use ($notification): void {
$action = app(SendNotificationToUsersAction::class);
$action = app(SendNotificationToUsersAction::class);
$userIds->chunk(100)->each(function (Collection $chunk) use ($notification, $action): void {
$action->execute($notification, $chunk);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
return new class extends Migration {
/**
* Run the migrations.
*/
Expand All @@ -22,7 +21,7 @@ public function up(): void
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->foreignId('notification_id')->constrained()->onDelete('cascade');
$table->timestamp('read_at')->nullable();
$table->timestamp('read_at')->nullable()->default(null);
$table->timestamps();
});
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Feature/Notifications/MarkNotificationAsReadForUserTest.php
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);
});
});

0 comments on commit e117114

Please sign in to comment.